Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1 | /* |
| 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 Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 13 | * Copyright (C) 2004 Nadia Yvette Chambers |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 16 | #include <linux/stop_machine.h> |
| 17 | #include <linux/clocksource.h> |
| 18 | #include <linux/kallsyms.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 20 | #include <linux/suspend.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 21 | #include <linux/debugfs.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 22 | #include <linux/hardirq.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 23 | #include <linux/kthread.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 25 | #include <linux/bsearch.h> |
Paul Gortmaker | 56d82e0 | 2011-05-26 17:53:52 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 27 | #include <linux/ftrace.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 28 | #include <linux/sysctl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 30 | #include <linux/ctype.h> |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 31 | #include <linux/sort.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 32 | #include <linux/list.h> |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 33 | #include <linux/hash.h> |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 34 | #include <linux/rcupdate.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 35 | |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 36 | #include <trace/events/sched.h> |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 37 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 38 | #include <asm/setup.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 39 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 40 | #include "trace_output.h" |
Steven Rostedt | bac429f | 2009-03-20 12:50:56 -0400 | [diff] [blame] | 41 | #include "trace_stat.h" |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 42 | |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 43 | #define FTRACE_WARN_ON(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 44 | ({ \ |
| 45 | int ___r = cond; \ |
| 46 | if (WARN_ON(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 47 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 48 | ___r; \ |
| 49 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 50 | |
| 51 | #define FTRACE_WARN_ON_ONCE(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 52 | ({ \ |
| 53 | int ___r = cond; \ |
| 54 | if (WARN_ON_ONCE(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 55 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 56 | ___r; \ |
| 57 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 58 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 59 | /* hash bits for specific function selection */ |
| 60 | #define FTRACE_HASH_BITS 7 |
| 61 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 62 | #define FTRACE_HASH_DEFAULT_BITS 10 |
| 63 | #define FTRACE_HASH_MAX_BITS 12 |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 64 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 65 | #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 66 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 67 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 68 | #define INIT_REGEX_LOCK(opsname) \ |
| 69 | .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock), |
| 70 | #else |
| 71 | #define INIT_REGEX_LOCK(opsname) |
| 72 | #endif |
| 73 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 74 | static struct ftrace_ops ftrace_list_end __read_mostly = { |
| 75 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | 395b97a | 2013-03-27 09:31:28 -0400 | [diff] [blame] | 76 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 77 | }; |
| 78 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 79 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 80 | int ftrace_enabled __read_mostly; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 81 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 82 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 83 | /* Quick disabling of function tracer. */ |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 84 | int function_trace_stop __read_mostly; |
| 85 | |
| 86 | /* Current function tracing op */ |
| 87 | struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 88 | /* What to set function_trace_op to */ |
| 89 | static struct ftrace_ops *set_function_trace_op; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 90 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 91 | /* List for set_ftrace_pid's pids. */ |
| 92 | LIST_HEAD(ftrace_pids); |
| 93 | struct ftrace_pid { |
| 94 | struct list_head list; |
| 95 | struct pid *pid; |
| 96 | }; |
| 97 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 98 | /* |
| 99 | * ftrace_disabled is set when an anomaly is discovered. |
| 100 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 101 | */ |
| 102 | static int ftrace_disabled __read_mostly; |
| 103 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 104 | static DEFINE_MUTEX(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 105 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 106 | static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 107 | static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 108 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 109 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 110 | static struct ftrace_ops global_ops; |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 111 | static struct ftrace_ops control_ops; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 112 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 113 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 114 | static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 115 | struct ftrace_ops *op, struct pt_regs *regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 116 | #else |
| 117 | /* See comment below, where ftrace_ops_list_func is defined */ |
| 118 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip); |
| 119 | #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops) |
| 120 | #endif |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 121 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 122 | /* |
| 123 | * Traverse the ftrace_global_list, invoking all entries. The reason that we |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 124 | * can use rcu_dereference_raw_notrace() is that elements removed from this list |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 125 | * are simply leaked, so there is no need to interact with a grace-period |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 126 | * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 127 | * concurrent insertions into the ftrace_global_list. |
| 128 | * |
| 129 | * Silly Alpha and silly pointer-speculation compiler optimizations! |
| 130 | */ |
| 131 | #define do_for_each_ftrace_op(op, list) \ |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 132 | op = rcu_dereference_raw_notrace(list); \ |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 133 | do |
| 134 | |
| 135 | /* |
| 136 | * Optimized for just a single item in the list (as that is the normal case). |
| 137 | */ |
| 138 | #define while_for_each_ftrace_op(op) \ |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 139 | while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \ |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 140 | unlikely((op) != &ftrace_list_end)) |
| 141 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 142 | static inline void ftrace_ops_init(struct ftrace_ops *ops) |
| 143 | { |
| 144 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 145 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { |
| 146 | mutex_init(&ops->regex_lock); |
| 147 | ops->flags |= FTRACE_OPS_FL_INITIALIZED; |
| 148 | } |
| 149 | #endif |
| 150 | } |
| 151 | |
Steven Rostedt | ea701f1 | 2012-07-20 13:08:05 -0400 | [diff] [blame] | 152 | /** |
| 153 | * ftrace_nr_registered_ops - return number of ops registered |
| 154 | * |
| 155 | * Returns the number of ftrace_ops registered and tracing functions |
| 156 | */ |
| 157 | int ftrace_nr_registered_ops(void) |
| 158 | { |
| 159 | struct ftrace_ops *ops; |
| 160 | int cnt = 0; |
| 161 | |
| 162 | mutex_lock(&ftrace_lock); |
| 163 | |
| 164 | for (ops = ftrace_ops_list; |
| 165 | ops != &ftrace_list_end; ops = ops->next) |
| 166 | cnt++; |
| 167 | |
| 168 | mutex_unlock(&ftrace_lock); |
| 169 | |
| 170 | return cnt; |
| 171 | } |
| 172 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 173 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 174 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 175 | { |
Steven Rostedt | 0ef8cde | 2008-12-03 15:36:58 -0500 | [diff] [blame] | 176 | if (!test_tsk_trace_trace(current)) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 177 | return; |
| 178 | |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 179 | ftrace_pid_function(ip, parent_ip, op, regs); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void set_ftrace_pid_function(ftrace_func_t func) |
| 183 | { |
| 184 | /* do not set ftrace_pid_function to itself! */ |
| 185 | if (func != ftrace_pid_func) |
| 186 | ftrace_pid_function = func; |
| 187 | } |
| 188 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 189 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 190 | * clear_ftrace_function - reset the ftrace function |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 191 | * |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 192 | * This NULLs the ftrace function and in essence stops |
| 193 | * tracing. There may be lag |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 194 | */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 195 | void clear_ftrace_function(void) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 196 | { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 197 | ftrace_trace_function = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 198 | ftrace_pid_function = ftrace_stub; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 199 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 200 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 201 | static void control_ops_disable_all(struct ftrace_ops *ops) |
| 202 | { |
| 203 | int cpu; |
| 204 | |
| 205 | for_each_possible_cpu(cpu) |
| 206 | *per_cpu_ptr(ops->disabled, cpu) = 1; |
| 207 | } |
| 208 | |
| 209 | static int control_ops_alloc(struct ftrace_ops *ops) |
| 210 | { |
| 211 | int __percpu *disabled; |
| 212 | |
| 213 | disabled = alloc_percpu(int); |
| 214 | if (!disabled) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | ops->disabled = disabled; |
| 218 | control_ops_disable_all(ops); |
| 219 | return 0; |
| 220 | } |
| 221 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 222 | static void ftrace_sync(struct work_struct *work) |
| 223 | { |
| 224 | /* |
| 225 | * This function is just a stub to implement a hard force |
| 226 | * of synchronize_sched(). This requires synchronizing |
| 227 | * tasks even in userspace and idle. |
| 228 | * |
| 229 | * Yes, function tracing is rude. |
| 230 | */ |
| 231 | } |
| 232 | |
| 233 | static void ftrace_sync_ipi(void *data) |
| 234 | { |
| 235 | /* Probably not needed, but do it anyway */ |
| 236 | smp_rmb(); |
| 237 | } |
| 238 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 239 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 240 | static void update_function_graph_func(void); |
| 241 | #else |
| 242 | static inline void update_function_graph_func(void) { } |
| 243 | #endif |
| 244 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 245 | static void update_ftrace_function(void) |
| 246 | { |
| 247 | ftrace_func_t func; |
| 248 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 249 | /* |
| 250 | * If we are at the end of the list and this ops is |
Steven Rostedt | 4740974 | 2012-07-20 11:04:44 -0400 | [diff] [blame] | 251 | * recursion safe and not dynamic and the arch supports passing ops, |
| 252 | * then have the mcount trampoline call the function directly. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 253 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 254 | if (ftrace_ops_list == &ftrace_list_end || |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 255 | (ftrace_ops_list->next == &ftrace_list_end && |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 256 | !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) && |
Steven Rostedt | 4740974 | 2012-07-20 11:04:44 -0400 | [diff] [blame] | 257 | (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) && |
Steven Rostedt | ccf3672 | 2012-06-05 09:44:25 -0400 | [diff] [blame] | 258 | !FTRACE_FORCE_LIST_FUNC)) { |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 259 | /* Set the ftrace_ops that the arch callback uses */ |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 260 | set_function_trace_op = ftrace_ops_list; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 261 | func = ftrace_ops_list->func; |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 262 | } else { |
| 263 | /* Just use the default ftrace_ops */ |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 264 | set_function_trace_op = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 265 | func = ftrace_ops_list_func; |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 266 | } |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 267 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 268 | /* If there's no change, then do nothing more here */ |
| 269 | if (ftrace_trace_function == func) |
| 270 | return; |
| 271 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 272 | update_function_graph_func(); |
| 273 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 274 | /* |
| 275 | * If we are using the list function, it doesn't care |
| 276 | * about the function_trace_ops. |
| 277 | */ |
| 278 | if (func == ftrace_ops_list_func) { |
| 279 | ftrace_trace_function = func; |
| 280 | /* |
| 281 | * Don't even bother setting function_trace_ops, |
| 282 | * it would be racy to do so anyway. |
| 283 | */ |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | #ifndef CONFIG_DYNAMIC_FTRACE |
| 288 | /* |
| 289 | * For static tracing, we need to be a bit more careful. |
| 290 | * The function change takes affect immediately. Thus, |
| 291 | * we need to coorditate the setting of the function_trace_ops |
| 292 | * with the setting of the ftrace_trace_function. |
| 293 | * |
| 294 | * Set the function to the list ops, which will call the |
| 295 | * function we want, albeit indirectly, but it handles the |
| 296 | * ftrace_ops and doesn't depend on function_trace_op. |
| 297 | */ |
| 298 | ftrace_trace_function = ftrace_ops_list_func; |
| 299 | /* |
| 300 | * Make sure all CPUs see this. Yes this is slow, but static |
| 301 | * tracing is slow and nasty to have enabled. |
| 302 | */ |
| 303 | schedule_on_each_cpu(ftrace_sync); |
| 304 | /* Now all cpus are using the list ops. */ |
| 305 | function_trace_op = set_function_trace_op; |
| 306 | /* Make sure the function_trace_op is visible on all CPUs */ |
| 307 | smp_wmb(); |
| 308 | /* Nasty way to force a rmb on all cpus */ |
| 309 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
| 310 | /* OK, we are all set to update the ftrace_trace_function now! */ |
| 311 | #endif /* !CONFIG_DYNAMIC_FTRACE */ |
| 312 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 313 | ftrace_trace_function = func; |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 316 | static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 317 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 318 | ops->next = *list; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 319 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 320 | * We are entering ops into the list but another |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 321 | * CPU might be walking that list. We need to make sure |
| 322 | * the ops->next pointer is valid before another CPU sees |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 323 | * the ops pointer included into the list. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 324 | */ |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 325 | rcu_assign_pointer(*list, ops); |
| 326 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 327 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 328 | static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
| 329 | { |
| 330 | struct ftrace_ops **p; |
| 331 | |
| 332 | /* |
| 333 | * If we are removing the last function, then simply point |
| 334 | * to the ftrace_stub. |
| 335 | */ |
| 336 | if (*list == ops && ops->next == &ftrace_list_end) { |
| 337 | *list = &ftrace_list_end; |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
| 342 | if (*p == ops) |
| 343 | break; |
| 344 | |
| 345 | if (*p != ops) |
| 346 | return -1; |
| 347 | |
| 348 | *p = (*p)->next; |
| 349 | return 0; |
| 350 | } |
| 351 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 352 | static void add_ftrace_list_ops(struct ftrace_ops **list, |
| 353 | struct ftrace_ops *main_ops, |
| 354 | struct ftrace_ops *ops) |
| 355 | { |
| 356 | int first = *list == &ftrace_list_end; |
| 357 | add_ftrace_ops(list, ops); |
| 358 | if (first) |
| 359 | add_ftrace_ops(&ftrace_ops_list, main_ops); |
| 360 | } |
| 361 | |
| 362 | static int remove_ftrace_list_ops(struct ftrace_ops **list, |
| 363 | struct ftrace_ops *main_ops, |
| 364 | struct ftrace_ops *ops) |
| 365 | { |
| 366 | int ret = remove_ftrace_ops(list, ops); |
| 367 | if (!ret && *list == &ftrace_list_end) |
| 368 | ret = remove_ftrace_ops(&ftrace_ops_list, main_ops); |
| 369 | return ret; |
| 370 | } |
| 371 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 372 | static int __register_ftrace_function(struct ftrace_ops *ops) |
| 373 | { |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 374 | if (ops->flags & FTRACE_OPS_FL_DELETED) |
| 375 | return -EINVAL; |
| 376 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 377 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 378 | return -EBUSY; |
| 379 | |
Masami Hiramatsu | 06aeaae | 2012-09-28 17:15:17 +0900 | [diff] [blame] | 380 | #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 381 | /* |
| 382 | * If the ftrace_ops specifies SAVE_REGS, then it only can be used |
| 383 | * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. |
| 384 | * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. |
| 385 | */ |
| 386 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && |
| 387 | !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) |
| 391 | ops->flags |= FTRACE_OPS_FL_SAVE_REGS; |
| 392 | #endif |
| 393 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 394 | if (!core_kernel_data((unsigned long)ops)) |
| 395 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; |
| 396 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 397 | if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 398 | if (control_ops_alloc(ops)) |
| 399 | return -ENOMEM; |
| 400 | add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 401 | } else |
| 402 | add_ftrace_ops(&ftrace_ops_list, ops); |
| 403 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 404 | if (ftrace_enabled) |
| 405 | update_ftrace_function(); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 406 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 410 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 411 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 412 | int ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 413 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 414 | if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) |
| 415 | return -EBUSY; |
| 416 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 417 | if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 418 | ret = remove_ftrace_list_ops(&ftrace_control_list, |
| 419 | &control_ops, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 420 | } else |
| 421 | ret = remove_ftrace_ops(&ftrace_ops_list, ops); |
| 422 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 423 | if (ret < 0) |
| 424 | return ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 425 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 426 | if (ftrace_enabled) |
| 427 | update_ftrace_function(); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 428 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 429 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 432 | static void ftrace_update_pid_func(void) |
| 433 | { |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 434 | /* Only do something if we are tracing something */ |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 435 | if (ftrace_trace_function == ftrace_stub) |
KOSAKI Motohiro | 10dd3eb | 2009-03-06 15:29:04 +0900 | [diff] [blame] | 436 | return; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 437 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 438 | update_ftrace_function(); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 439 | } |
| 440 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 441 | #ifdef CONFIG_FUNCTION_PROFILER |
| 442 | struct ftrace_profile { |
| 443 | struct hlist_node node; |
| 444 | unsigned long ip; |
| 445 | unsigned long counter; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 446 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 447 | unsigned long long time; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 448 | unsigned long long time_squared; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 449 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | struct ftrace_profile_page { |
| 453 | struct ftrace_profile_page *next; |
| 454 | unsigned long index; |
| 455 | struct ftrace_profile records[]; |
| 456 | }; |
| 457 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 458 | struct ftrace_profile_stat { |
| 459 | atomic_t disabled; |
| 460 | struct hlist_head *hash; |
| 461 | struct ftrace_profile_page *pages; |
| 462 | struct ftrace_profile_page *start; |
| 463 | struct tracer_stat stat; |
| 464 | }; |
| 465 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 466 | #define PROFILE_RECORDS_SIZE \ |
| 467 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) |
| 468 | |
| 469 | #define PROFILES_PER_PAGE \ |
| 470 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) |
| 471 | |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 472 | static int ftrace_profile_enabled __read_mostly; |
| 473 | |
| 474 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 475 | static DEFINE_MUTEX(ftrace_profile_lock); |
| 476 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 477 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 478 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 479 | #define FTRACE_PROFILE_HASH_BITS 10 |
| 480 | #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 481 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 482 | static void * |
| 483 | function_stat_next(void *v, int idx) |
| 484 | { |
| 485 | struct ftrace_profile *rec = v; |
| 486 | struct ftrace_profile_page *pg; |
| 487 | |
| 488 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
| 489 | |
| 490 | again: |
Li Zefan | 0296e42 | 2009-06-26 11:15:37 +0800 | [diff] [blame] | 491 | if (idx != 0) |
| 492 | rec++; |
| 493 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 494 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
| 495 | pg = pg->next; |
| 496 | if (!pg) |
| 497 | return NULL; |
| 498 | rec = &pg->records[0]; |
| 499 | if (!rec->counter) |
| 500 | goto again; |
| 501 | } |
| 502 | |
| 503 | return rec; |
| 504 | } |
| 505 | |
| 506 | static void *function_stat_start(struct tracer_stat *trace) |
| 507 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 508 | struct ftrace_profile_stat *stat = |
| 509 | container_of(trace, struct ftrace_profile_stat, stat); |
| 510 | |
| 511 | if (!stat || !stat->start) |
| 512 | return NULL; |
| 513 | |
| 514 | return function_stat_next(&stat->start->records[0], 0); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 515 | } |
| 516 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 517 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 518 | /* function graph compares on total time */ |
| 519 | static int function_stat_cmp(void *p1, void *p2) |
| 520 | { |
| 521 | struct ftrace_profile *a = p1; |
| 522 | struct ftrace_profile *b = p2; |
| 523 | |
| 524 | if (a->time < b->time) |
| 525 | return -1; |
| 526 | if (a->time > b->time) |
| 527 | return 1; |
| 528 | else |
| 529 | return 0; |
| 530 | } |
| 531 | #else |
| 532 | /* not function graph compares against hits */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 533 | static int function_stat_cmp(void *p1, void *p2) |
| 534 | { |
| 535 | struct ftrace_profile *a = p1; |
| 536 | struct ftrace_profile *b = p2; |
| 537 | |
| 538 | if (a->counter < b->counter) |
| 539 | return -1; |
| 540 | if (a->counter > b->counter) |
| 541 | return 1; |
| 542 | else |
| 543 | return 0; |
| 544 | } |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 545 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 546 | |
| 547 | static int function_stat_headers(struct seq_file *m) |
| 548 | { |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 549 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 550 | seq_printf(m, " Function " |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 551 | "Hit Time Avg s^2\n" |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 552 | " -------- " |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 553 | "--- ---- --- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 554 | #else |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 555 | seq_printf(m, " Function Hit\n" |
| 556 | " -------- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 557 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int function_stat_show(struct seq_file *m, void *v) |
| 562 | { |
| 563 | struct ftrace_profile *rec = v; |
| 564 | char str[KSYM_SYMBOL_LEN]; |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 565 | int ret = 0; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 566 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 567 | static struct trace_seq s; |
| 568 | unsigned long long avg; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 569 | unsigned long long stddev; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 570 | #endif |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 571 | mutex_lock(&ftrace_profile_lock); |
| 572 | |
| 573 | /* we raced with function_profile_reset() */ |
| 574 | if (unlikely(rec->counter == 0)) { |
| 575 | ret = -EBUSY; |
| 576 | goto out; |
| 577 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 578 | |
| 579 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 580 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 581 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 582 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 583 | seq_printf(m, " "); |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 584 | avg = rec->time; |
| 585 | do_div(avg, rec->counter); |
| 586 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 587 | /* Sample standard deviation (s^2) */ |
| 588 | if (rec->counter <= 1) |
| 589 | stddev = 0; |
| 590 | else { |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 591 | /* |
| 592 | * Apply Welford's method: |
| 593 | * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) |
| 594 | */ |
| 595 | stddev = rec->counter * rec->time_squared - |
| 596 | rec->time * rec->time; |
| 597 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 598 | /* |
| 599 | * Divide only 1000 for ns^2 -> us^2 conversion. |
| 600 | * trace_print_graph_duration will divide 1000 again. |
| 601 | */ |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 602 | do_div(stddev, rec->counter * (rec->counter - 1) * 1000); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 605 | trace_seq_init(&s); |
| 606 | trace_print_graph_duration(rec->time, &s); |
| 607 | trace_seq_puts(&s, " "); |
| 608 | trace_print_graph_duration(avg, &s); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 609 | trace_seq_puts(&s, " "); |
| 610 | trace_print_graph_duration(stddev, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 611 | trace_print_seq(m, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 612 | #endif |
| 613 | seq_putc(m, '\n'); |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 614 | out: |
| 615 | mutex_unlock(&ftrace_profile_lock); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 616 | |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 617 | return ret; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 618 | } |
| 619 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 620 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 621 | { |
| 622 | struct ftrace_profile_page *pg; |
| 623 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 624 | pg = stat->pages = stat->start; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 625 | |
| 626 | while (pg) { |
| 627 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); |
| 628 | pg->index = 0; |
| 629 | pg = pg->next; |
| 630 | } |
| 631 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 632 | memset(stat->hash, 0, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 633 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
| 634 | } |
| 635 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 636 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 637 | { |
| 638 | struct ftrace_profile_page *pg; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 639 | int functions; |
| 640 | int pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 641 | int i; |
| 642 | |
| 643 | /* If we already allocated, do nothing */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 644 | if (stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 645 | return 0; |
| 646 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 647 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
| 648 | if (!stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 649 | return -ENOMEM; |
| 650 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 651 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 652 | functions = ftrace_update_tot_cnt; |
| 653 | #else |
| 654 | /* |
| 655 | * We do not know the number of functions that exist because |
| 656 | * dynamic tracing is what counts them. With past experience |
| 657 | * we have around 20K functions. That should be more than enough. |
| 658 | * It is highly unlikely we will execute every function in |
| 659 | * the kernel. |
| 660 | */ |
| 661 | functions = 20000; |
| 662 | #endif |
| 663 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 664 | pg = stat->start = stat->pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 665 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 666 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
| 667 | |
Namhyung Kim | 39e30cd | 2013-04-01 21:46:24 +0900 | [diff] [blame] | 668 | for (i = 1; i < pages; i++) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 669 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 670 | if (!pg->next) |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 671 | goto out_free; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 672 | pg = pg->next; |
| 673 | } |
| 674 | |
| 675 | return 0; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 676 | |
| 677 | out_free: |
| 678 | pg = stat->start; |
| 679 | while (pg) { |
| 680 | unsigned long tmp = (unsigned long)pg; |
| 681 | |
| 682 | pg = pg->next; |
| 683 | free_page(tmp); |
| 684 | } |
| 685 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 686 | stat->pages = NULL; |
| 687 | stat->start = NULL; |
| 688 | |
| 689 | return -ENOMEM; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 690 | } |
| 691 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 692 | static int ftrace_profile_init_cpu(int cpu) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 693 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 694 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 695 | int size; |
| 696 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 697 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 698 | |
| 699 | if (stat->hash) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 700 | /* If the profile is already created, simply reset it */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 701 | ftrace_profile_reset(stat); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * We are profiling all functions, but usually only a few thousand |
| 707 | * functions are hit. We'll make a hash of 1024 items. |
| 708 | */ |
| 709 | size = FTRACE_PROFILE_HASH_SIZE; |
| 710 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 711 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 712 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 713 | if (!stat->hash) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 714 | return -ENOMEM; |
| 715 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 716 | /* Preallocate the function profiling pages */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 717 | if (ftrace_profile_pages_init(stat) < 0) { |
| 718 | kfree(stat->hash); |
| 719 | stat->hash = NULL; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 720 | return -ENOMEM; |
| 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 726 | static int ftrace_profile_init(void) |
| 727 | { |
| 728 | int cpu; |
| 729 | int ret = 0; |
| 730 | |
Miao Xie | c4602c1 | 2013-12-16 15:20:01 +0800 | [diff] [blame] | 731 | for_each_possible_cpu(cpu) { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 732 | ret = ftrace_profile_init_cpu(cpu); |
| 733 | if (ret) |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | return ret; |
| 738 | } |
| 739 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 740 | /* interrupts must be disabled */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 741 | static struct ftrace_profile * |
| 742 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 743 | { |
| 744 | struct ftrace_profile *rec; |
| 745 | struct hlist_head *hhd; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 746 | unsigned long key; |
| 747 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 748 | key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 749 | hhd = &stat->hash[key]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 750 | |
| 751 | if (hlist_empty(hhd)) |
| 752 | return NULL; |
| 753 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 754 | hlist_for_each_entry_rcu_notrace(rec, hhd, node) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 755 | if (rec->ip == ip) |
| 756 | return rec; |
| 757 | } |
| 758 | |
| 759 | return NULL; |
| 760 | } |
| 761 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 762 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
| 763 | struct ftrace_profile *rec) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 764 | { |
| 765 | unsigned long key; |
| 766 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 767 | key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 768 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 769 | } |
| 770 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 771 | /* |
| 772 | * The memory is already allocated, this simply finds a new record to use. |
| 773 | */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 774 | static struct ftrace_profile * |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 775 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 776 | { |
| 777 | struct ftrace_profile *rec = NULL; |
| 778 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 779 | /* prevent recursion (from NMIs) */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 780 | if (atomic_inc_return(&stat->disabled) != 1) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 781 | goto out; |
| 782 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 783 | /* |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 784 | * Try to find the function again since an NMI |
| 785 | * could have added it |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 786 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 787 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 788 | if (rec) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 789 | goto out; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 790 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 791 | if (stat->pages->index == PROFILES_PER_PAGE) { |
| 792 | if (!stat->pages->next) |
| 793 | goto out; |
| 794 | stat->pages = stat->pages->next; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 795 | } |
| 796 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 797 | rec = &stat->pages->records[stat->pages->index++]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 798 | rec->ip = ip; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 799 | ftrace_add_profile(stat, rec); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 800 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 801 | out: |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 802 | atomic_dec(&stat->disabled); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 803 | |
| 804 | return rec; |
| 805 | } |
| 806 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 807 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 808 | function_profile_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 809 | struct ftrace_ops *ops, struct pt_regs *regs) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 810 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 811 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 812 | struct ftrace_profile *rec; |
| 813 | unsigned long flags; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 814 | |
| 815 | if (!ftrace_profile_enabled) |
| 816 | return; |
| 817 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 818 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 819 | |
| 820 | stat = &__get_cpu_var(ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 821 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 822 | goto out; |
| 823 | |
| 824 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 825 | if (!rec) { |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 826 | rec = ftrace_profile_alloc(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 827 | if (!rec) |
| 828 | goto out; |
| 829 | } |
| 830 | |
| 831 | rec->counter++; |
| 832 | out: |
| 833 | local_irq_restore(flags); |
| 834 | } |
| 835 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 836 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 837 | static int profile_graph_entry(struct ftrace_graph_ent *trace) |
| 838 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 839 | function_profile_call(trace->func, 0, NULL, NULL); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 840 | return 1; |
| 841 | } |
| 842 | |
| 843 | static void profile_graph_return(struct ftrace_graph_ret *trace) |
| 844 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 845 | struct ftrace_profile_stat *stat; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 846 | unsigned long long calltime; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 847 | struct ftrace_profile *rec; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 848 | unsigned long flags; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 849 | |
| 850 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 851 | stat = &__get_cpu_var(ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 852 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 853 | goto out; |
| 854 | |
Steven Rostedt | 37e44bc | 2010-04-27 21:04:24 -0400 | [diff] [blame] | 855 | /* If the calltime was zero'd ignore it */ |
| 856 | if (!trace->calltime) |
| 857 | goto out; |
| 858 | |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 859 | calltime = trace->rettime - trace->calltime; |
| 860 | |
| 861 | if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) { |
| 862 | int index; |
| 863 | |
| 864 | index = trace->depth; |
| 865 | |
| 866 | /* Append this call time to the parent time to subtract */ |
| 867 | if (index) |
| 868 | current->ret_stack[index - 1].subtime += calltime; |
| 869 | |
| 870 | if (current->ret_stack[index].subtime < calltime) |
| 871 | calltime -= current->ret_stack[index].subtime; |
| 872 | else |
| 873 | calltime = 0; |
| 874 | } |
| 875 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 876 | rec = ftrace_find_profiled_func(stat, trace->func); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 877 | if (rec) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 878 | rec->time += calltime; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 879 | rec->time_squared += calltime * calltime; |
| 880 | } |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 881 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 882 | out: |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 883 | local_irq_restore(flags); |
| 884 | } |
| 885 | |
| 886 | static int register_ftrace_profiler(void) |
| 887 | { |
| 888 | return register_ftrace_graph(&profile_graph_return, |
| 889 | &profile_graph_entry); |
| 890 | } |
| 891 | |
| 892 | static void unregister_ftrace_profiler(void) |
| 893 | { |
| 894 | unregister_ftrace_graph(); |
| 895 | } |
| 896 | #else |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 897 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 898 | .func = function_profile_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 899 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
| 900 | INIT_REGEX_LOCK(ftrace_profile_ops) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 901 | }; |
| 902 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 903 | static int register_ftrace_profiler(void) |
| 904 | { |
| 905 | return register_ftrace_function(&ftrace_profile_ops); |
| 906 | } |
| 907 | |
| 908 | static void unregister_ftrace_profiler(void) |
| 909 | { |
| 910 | unregister_ftrace_function(&ftrace_profile_ops); |
| 911 | } |
| 912 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 913 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 914 | static ssize_t |
| 915 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 916 | size_t cnt, loff_t *ppos) |
| 917 | { |
| 918 | unsigned long val; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 919 | int ret; |
| 920 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 921 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 922 | if (ret) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 923 | return ret; |
| 924 | |
| 925 | val = !!val; |
| 926 | |
| 927 | mutex_lock(&ftrace_profile_lock); |
| 928 | if (ftrace_profile_enabled ^ val) { |
| 929 | if (val) { |
| 930 | ret = ftrace_profile_init(); |
| 931 | if (ret < 0) { |
| 932 | cnt = ret; |
| 933 | goto out; |
| 934 | } |
| 935 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 936 | ret = register_ftrace_profiler(); |
| 937 | if (ret < 0) { |
| 938 | cnt = ret; |
| 939 | goto out; |
| 940 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 941 | ftrace_profile_enabled = 1; |
| 942 | } else { |
| 943 | ftrace_profile_enabled = 0; |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 944 | /* |
| 945 | * unregister_ftrace_profiler calls stop_machine |
| 946 | * so this acts like an synchronize_sched. |
| 947 | */ |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 948 | unregister_ftrace_profiler(); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | out: |
| 952 | mutex_unlock(&ftrace_profile_lock); |
| 953 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 954 | *ppos += cnt; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 955 | |
| 956 | return cnt; |
| 957 | } |
| 958 | |
| 959 | static ssize_t |
| 960 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 961 | size_t cnt, loff_t *ppos) |
| 962 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 963 | char buf[64]; /* big enough to hold a number */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 964 | int r; |
| 965 | |
| 966 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); |
| 967 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 968 | } |
| 969 | |
| 970 | static const struct file_operations ftrace_profile_fops = { |
| 971 | .open = tracing_open_generic, |
| 972 | .read = ftrace_profile_read, |
| 973 | .write = ftrace_profile_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 974 | .llseek = default_llseek, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 975 | }; |
| 976 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 977 | /* used to initialize the real stat files */ |
| 978 | static struct tracer_stat function_stats __initdata = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 979 | .name = "functions", |
| 980 | .stat_start = function_stat_start, |
| 981 | .stat_next = function_stat_next, |
| 982 | .stat_cmp = function_stat_cmp, |
| 983 | .stat_headers = function_stat_headers, |
| 984 | .stat_show = function_stat_show |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 985 | }; |
| 986 | |
Steven Rostedt | 6ab5d66 | 2009-06-04 00:55:45 -0400 | [diff] [blame] | 987 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 988 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 989 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 990 | struct dentry *entry; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 991 | char *name; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 992 | int ret; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 993 | int cpu; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 994 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 995 | for_each_possible_cpu(cpu) { |
| 996 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 997 | |
| 998 | /* allocate enough for function name + cpu number */ |
| 999 | name = kmalloc(32, GFP_KERNEL); |
| 1000 | if (!name) { |
| 1001 | /* |
| 1002 | * The files created are permanent, if something happens |
| 1003 | * we still do not free memory. |
| 1004 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1005 | WARN(1, |
| 1006 | "Could not allocate stat file for cpu %d\n", |
| 1007 | cpu); |
| 1008 | return; |
| 1009 | } |
| 1010 | stat->stat = function_stats; |
| 1011 | snprintf(name, 32, "function%d", cpu); |
| 1012 | stat->stat.name = name; |
| 1013 | ret = register_stat_tracer(&stat->stat); |
| 1014 | if (ret) { |
| 1015 | WARN(1, |
| 1016 | "Could not register function stat for cpu %d\n", |
| 1017 | cpu); |
| 1018 | kfree(name); |
| 1019 | return; |
| 1020 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | entry = debugfs_create_file("function_profile_enabled", 0644, |
| 1024 | d_tracer, NULL, &ftrace_profile_fops); |
| 1025 | if (!entry) |
| 1026 | pr_warning("Could not create debugfs " |
| 1027 | "'function_profile_enabled' entry\n"); |
| 1028 | } |
| 1029 | |
| 1030 | #else /* CONFIG_FUNCTION_PROFILER */ |
Steven Rostedt | 6ab5d66 | 2009-06-04 00:55:45 -0400 | [diff] [blame] | 1031 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1032 | { |
| 1033 | } |
| 1034 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 1035 | |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1036 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
| 1037 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1038 | #ifdef CONFIG_DYNAMIC_FTRACE |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1039 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1040 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1041 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1042 | #endif |
| 1043 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1044 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; |
| 1045 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1046 | struct ftrace_func_probe { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1047 | struct hlist_node node; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1048 | struct ftrace_probe_ops *ops; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1049 | unsigned long flags; |
| 1050 | unsigned long ip; |
| 1051 | void *data; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 1052 | struct list_head free_list; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1053 | }; |
| 1054 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1055 | struct ftrace_func_entry { |
| 1056 | struct hlist_node hlist; |
| 1057 | unsigned long ip; |
| 1058 | }; |
| 1059 | |
| 1060 | struct ftrace_hash { |
| 1061 | unsigned long size_bits; |
| 1062 | struct hlist_head *buckets; |
| 1063 | unsigned long count; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1064 | struct rcu_head rcu; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1065 | }; |
| 1066 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1067 | /* |
| 1068 | * We make these constant because no one should touch them, |
| 1069 | * but they are used as the default "empty hash", to avoid allocating |
| 1070 | * it all the time. These are in a read only section such that if |
| 1071 | * anyone does try to modify it, it will cause an exception. |
| 1072 | */ |
| 1073 | static const struct hlist_head empty_buckets[1]; |
| 1074 | static const struct ftrace_hash empty_hash = { |
| 1075 | .buckets = (struct hlist_head *)empty_buckets, |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1076 | }; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1077 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1078 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 1079 | static struct ftrace_ops global_ops = { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1080 | .func = ftrace_stub, |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1081 | .notrace_hash = EMPTY_HASH, |
| 1082 | .filter_hash = EMPTY_HASH, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 1083 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
| 1084 | INIT_REGEX_LOCK(global_ops) |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1085 | }; |
| 1086 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1087 | struct ftrace_page { |
| 1088 | struct ftrace_page *next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1089 | struct dyn_ftrace *records; |
Steven Rostedt | 431aa3f | 2009-01-06 12:43:01 -0500 | [diff] [blame] | 1090 | int index; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1091 | int size; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 1092 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1093 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1094 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1095 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1096 | |
| 1097 | /* estimate from running different kernels */ |
| 1098 | #define NR_TO_INIT 10000 |
| 1099 | |
| 1100 | static struct ftrace_page *ftrace_pages_start; |
| 1101 | static struct ftrace_page *ftrace_pages; |
| 1102 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1103 | static bool ftrace_hash_empty(struct ftrace_hash *hash) |
| 1104 | { |
| 1105 | return !hash || !hash->count; |
| 1106 | } |
| 1107 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1108 | static struct ftrace_func_entry * |
| 1109 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1110 | { |
| 1111 | unsigned long key; |
| 1112 | struct ftrace_func_entry *entry; |
| 1113 | struct hlist_head *hhd; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1114 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1115 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1116 | return NULL; |
| 1117 | |
| 1118 | if (hash->size_bits > 0) |
| 1119 | key = hash_long(ip, hash->size_bits); |
| 1120 | else |
| 1121 | key = 0; |
| 1122 | |
| 1123 | hhd = &hash->buckets[key]; |
| 1124 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 1125 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1126 | if (entry->ip == ip) |
| 1127 | return entry; |
| 1128 | } |
| 1129 | return NULL; |
| 1130 | } |
| 1131 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1132 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1133 | struct ftrace_func_entry *entry) |
| 1134 | { |
| 1135 | struct hlist_head *hhd; |
| 1136 | unsigned long key; |
| 1137 | |
| 1138 | if (hash->size_bits) |
| 1139 | key = hash_long(entry->ip, hash->size_bits); |
| 1140 | else |
| 1141 | key = 0; |
| 1142 | |
| 1143 | hhd = &hash->buckets[key]; |
| 1144 | hlist_add_head(&entry->hlist, hhd); |
| 1145 | hash->count++; |
| 1146 | } |
| 1147 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1148 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1149 | { |
| 1150 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1151 | |
| 1152 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1153 | if (!entry) |
| 1154 | return -ENOMEM; |
| 1155 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1156 | entry->ip = ip; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1157 | __add_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static void |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1163 | free_hash_entry(struct ftrace_hash *hash, |
| 1164 | struct ftrace_func_entry *entry) |
| 1165 | { |
| 1166 | hlist_del(&entry->hlist); |
| 1167 | kfree(entry); |
| 1168 | hash->count--; |
| 1169 | } |
| 1170 | |
| 1171 | static void |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1172 | remove_hash_entry(struct ftrace_hash *hash, |
| 1173 | struct ftrace_func_entry *entry) |
| 1174 | { |
| 1175 | hlist_del(&entry->hlist); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1176 | hash->count--; |
| 1177 | } |
| 1178 | |
| 1179 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1180 | { |
| 1181 | struct hlist_head *hhd; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1182 | struct hlist_node *tn; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1183 | struct ftrace_func_entry *entry; |
| 1184 | int size = 1 << hash->size_bits; |
| 1185 | int i; |
| 1186 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1187 | if (!hash->count) |
| 1188 | return; |
| 1189 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1190 | for (i = 0; i < size; i++) { |
| 1191 | hhd = &hash->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1192 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1193 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1194 | } |
| 1195 | FTRACE_WARN_ON(hash->count); |
| 1196 | } |
| 1197 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1198 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1199 | { |
| 1200 | if (!hash || hash == EMPTY_HASH) |
| 1201 | return; |
| 1202 | ftrace_hash_clear(hash); |
| 1203 | kfree(hash->buckets); |
| 1204 | kfree(hash); |
| 1205 | } |
| 1206 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1207 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1208 | { |
| 1209 | struct ftrace_hash *hash; |
| 1210 | |
| 1211 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1212 | free_ftrace_hash(hash); |
| 1213 | } |
| 1214 | |
| 1215 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1216 | { |
| 1217 | if (!hash || hash == EMPTY_HASH) |
| 1218 | return; |
| 1219 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); |
| 1220 | } |
| 1221 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1222 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1223 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 1224 | ftrace_ops_init(ops); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1225 | free_ftrace_hash(ops->filter_hash); |
| 1226 | free_ftrace_hash(ops->notrace_hash); |
| 1227 | } |
| 1228 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1229 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1230 | { |
| 1231 | struct ftrace_hash *hash; |
| 1232 | int size; |
| 1233 | |
| 1234 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1235 | if (!hash) |
| 1236 | return NULL; |
| 1237 | |
| 1238 | size = 1 << size_bits; |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1239 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1240 | |
| 1241 | if (!hash->buckets) { |
| 1242 | kfree(hash); |
| 1243 | return NULL; |
| 1244 | } |
| 1245 | |
| 1246 | hash->size_bits = size_bits; |
| 1247 | |
| 1248 | return hash; |
| 1249 | } |
| 1250 | |
| 1251 | static struct ftrace_hash * |
| 1252 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1253 | { |
| 1254 | struct ftrace_func_entry *entry; |
| 1255 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1256 | int size; |
| 1257 | int ret; |
| 1258 | int i; |
| 1259 | |
| 1260 | new_hash = alloc_ftrace_hash(size_bits); |
| 1261 | if (!new_hash) |
| 1262 | return NULL; |
| 1263 | |
| 1264 | /* Empty hash? */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1265 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1266 | return new_hash; |
| 1267 | |
| 1268 | size = 1 << hash->size_bits; |
| 1269 | for (i = 0; i < size; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1270 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1271 | ret = add_hash_entry(new_hash, entry->ip); |
| 1272 | if (ret < 0) |
| 1273 | goto free_hash; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1278 | |
| 1279 | return new_hash; |
| 1280 | |
| 1281 | free_hash: |
| 1282 | free_ftrace_hash(new_hash); |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1286 | static void |
| 1287 | ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash); |
| 1288 | static void |
| 1289 | ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash); |
| 1290 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1291 | static int |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1292 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1293 | struct ftrace_hash **dst, struct ftrace_hash *src) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1294 | { |
| 1295 | struct ftrace_func_entry *entry; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1296 | struct hlist_node *tn; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1297 | struct hlist_head *hhd; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1298 | struct ftrace_hash *old_hash; |
| 1299 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1300 | int size = src->count; |
| 1301 | int bits = 0; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1302 | int ret; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1303 | int i; |
| 1304 | |
| 1305 | /* |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1306 | * Remove the current set, update the hash and add |
| 1307 | * them back. |
| 1308 | */ |
| 1309 | ftrace_hash_rec_disable(ops, enable); |
| 1310 | |
| 1311 | /* |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1312 | * If the new source is empty, just free dst and assign it |
| 1313 | * the empty_hash. |
| 1314 | */ |
| 1315 | if (!src->count) { |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1316 | free_ftrace_hash_rcu(*dst); |
| 1317 | rcu_assign_pointer(*dst, EMPTY_HASH); |
Steven Rostedt | d4d34b9 | 2011-11-04 20:32:39 -0400 | [diff] [blame] | 1318 | /* still need to update the function records */ |
| 1319 | ret = 0; |
| 1320 | goto out; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1321 | } |
| 1322 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1323 | /* |
| 1324 | * Make the hash size about 1/2 the # found |
| 1325 | */ |
| 1326 | for (size /= 2; size; size >>= 1) |
| 1327 | bits++; |
| 1328 | |
| 1329 | /* Don't allocate too much */ |
| 1330 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1331 | bits = FTRACE_HASH_MAX_BITS; |
| 1332 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1333 | ret = -ENOMEM; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1334 | new_hash = alloc_ftrace_hash(bits); |
| 1335 | if (!new_hash) |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1336 | goto out; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1337 | |
| 1338 | size = 1 << src->size_bits; |
| 1339 | for (i = 0; i < size; i++) { |
| 1340 | hhd = &src->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1341 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1342 | remove_hash_entry(src, entry); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1343 | __add_hash_entry(new_hash, entry); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1347 | old_hash = *dst; |
| 1348 | rcu_assign_pointer(*dst, new_hash); |
| 1349 | free_ftrace_hash_rcu(old_hash); |
| 1350 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1351 | ret = 0; |
| 1352 | out: |
| 1353 | /* |
| 1354 | * Enable regardless of ret: |
| 1355 | * On success, we enable the new hash. |
| 1356 | * On failure, we re-enable the original hash. |
| 1357 | */ |
| 1358 | ftrace_hash_rec_enable(ops, enable); |
| 1359 | |
| 1360 | return ret; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1361 | } |
| 1362 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1363 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1364 | * Test the hashes for this ops to see if we want to call |
| 1365 | * the ops->func or not. |
| 1366 | * |
| 1367 | * It's a match if the ip is in the ops->filter_hash or |
| 1368 | * the filter_hash does not exist or is empty, |
| 1369 | * AND |
| 1370 | * the ip is not in the ops->notrace_hash. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 1371 | * |
| 1372 | * This needs to be called with preemption disabled as |
| 1373 | * the hashes are freed with call_rcu_sched(). |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1374 | */ |
| 1375 | static int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1376 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1377 | { |
| 1378 | struct ftrace_hash *filter_hash; |
| 1379 | struct ftrace_hash *notrace_hash; |
| 1380 | int ret; |
| 1381 | |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1382 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1383 | /* |
| 1384 | * There's a small race when adding ops that the ftrace handler |
| 1385 | * that wants regs, may be called without them. We can not |
| 1386 | * allow that handler to be called if regs is NULL. |
| 1387 | */ |
| 1388 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) |
| 1389 | return 0; |
| 1390 | #endif |
| 1391 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 1392 | filter_hash = rcu_dereference_raw_notrace(ops->filter_hash); |
| 1393 | notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1394 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1395 | if ((ftrace_hash_empty(filter_hash) || |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1396 | ftrace_lookup_ip(filter_hash, ip)) && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1397 | (ftrace_hash_empty(notrace_hash) || |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1398 | !ftrace_lookup_ip(notrace_hash, ip))) |
| 1399 | ret = 1; |
| 1400 | else |
| 1401 | ret = 0; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1402 | |
| 1403 | return ret; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1407 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1408 | * you must use a goto. |
| 1409 | */ |
| 1410 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1411 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1412 | int _____i; \ |
| 1413 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1414 | rec = &pg->records[_____i]; |
| 1415 | |
| 1416 | #define while_for_each_ftrace_rec() \ |
| 1417 | } \ |
| 1418 | } |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 1419 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1420 | |
| 1421 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1422 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1423 | const struct dyn_ftrace *key = a; |
| 1424 | const struct dyn_ftrace *rec = b; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1425 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1426 | if (key->flags < rec->ip) |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1427 | return -1; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1428 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
| 1429 | return 1; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1433 | static unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1434 | { |
| 1435 | struct ftrace_page *pg; |
| 1436 | struct dyn_ftrace *rec; |
| 1437 | struct dyn_ftrace key; |
| 1438 | |
| 1439 | key.ip = start; |
| 1440 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 1441 | |
| 1442 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1443 | if (end < pg->records[0].ip || |
| 1444 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 1445 | continue; |
| 1446 | rec = bsearch(&key, pg->records, pg->index, |
| 1447 | sizeof(struct dyn_ftrace), |
| 1448 | ftrace_cmp_recs); |
| 1449 | if (rec) |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1450 | return rec->ip; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1451 | } |
| 1452 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1456 | /** |
| 1457 | * ftrace_location - return true if the ip giving is a traced location |
| 1458 | * @ip: the instruction pointer to check |
| 1459 | * |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1460 | * Returns rec->ip if @ip given is a pointer to a ftrace location. |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1461 | * That is, the instruction that is either a NOP or call to |
| 1462 | * the function tracer. It checks the ftrace internal tables to |
| 1463 | * determine if the address belongs or not. |
| 1464 | */ |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1465 | unsigned long ftrace_location(unsigned long ip) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1466 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1467 | return ftrace_location_range(ip, ip); |
| 1468 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1469 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1470 | /** |
| 1471 | * ftrace_text_reserved - return true if range contains an ftrace location |
| 1472 | * @start: start of range to search |
| 1473 | * @end: end of range to search (inclusive). @end points to the last byte to check. |
| 1474 | * |
| 1475 | * Returns 1 if @start and @end contains a ftrace location. |
| 1476 | * That is, the instruction that is either a NOP or call to |
| 1477 | * the function tracer. It checks the ftrace internal tables to |
| 1478 | * determine if the address belongs or not. |
| 1479 | */ |
Sasha Levin | d88471c | 2013-01-09 18:09:20 -0500 | [diff] [blame] | 1480 | int ftrace_text_reserved(const void *start, const void *end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1481 | { |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1482 | unsigned long ret; |
| 1483 | |
| 1484 | ret = ftrace_location_range((unsigned long)start, |
| 1485 | (unsigned long)end); |
| 1486 | |
| 1487 | return (int)!!ret; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1488 | } |
| 1489 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1490 | static void __ftrace_hash_rec_update(struct ftrace_ops *ops, |
| 1491 | int filter_hash, |
| 1492 | bool inc) |
| 1493 | { |
| 1494 | struct ftrace_hash *hash; |
| 1495 | struct ftrace_hash *other_hash; |
| 1496 | struct ftrace_page *pg; |
| 1497 | struct dyn_ftrace *rec; |
| 1498 | int count = 0; |
| 1499 | int all = 0; |
| 1500 | |
| 1501 | /* Only update if the ops has been registered */ |
| 1502 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1503 | return; |
| 1504 | |
| 1505 | /* |
| 1506 | * In the filter_hash case: |
| 1507 | * If the count is zero, we update all records. |
| 1508 | * Otherwise we just update the items in the hash. |
| 1509 | * |
| 1510 | * In the notrace_hash case: |
| 1511 | * We enable the update in the hash. |
| 1512 | * As disabling notrace means enabling the tracing, |
| 1513 | * and enabling notrace means disabling, the inc variable |
| 1514 | * gets inversed. |
| 1515 | */ |
| 1516 | if (filter_hash) { |
| 1517 | hash = ops->filter_hash; |
| 1518 | other_hash = ops->notrace_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1519 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1520 | all = 1; |
| 1521 | } else { |
| 1522 | inc = !inc; |
| 1523 | hash = ops->notrace_hash; |
| 1524 | other_hash = ops->filter_hash; |
| 1525 | /* |
| 1526 | * If the notrace hash has no items, |
| 1527 | * then there's nothing to do. |
| 1528 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1529 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1530 | return; |
| 1531 | } |
| 1532 | |
| 1533 | do_for_each_ftrace_rec(pg, rec) { |
| 1534 | int in_other_hash = 0; |
| 1535 | int in_hash = 0; |
| 1536 | int match = 0; |
| 1537 | |
| 1538 | if (all) { |
| 1539 | /* |
| 1540 | * Only the filter_hash affects all records. |
| 1541 | * Update if the record is not in the notrace hash. |
| 1542 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1543 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1544 | match = 1; |
| 1545 | } else { |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1546 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
| 1547 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1548 | |
| 1549 | /* |
| 1550 | * |
| 1551 | */ |
| 1552 | if (filter_hash && in_hash && !in_other_hash) |
| 1553 | match = 1; |
| 1554 | else if (!filter_hash && in_hash && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1555 | (in_other_hash || ftrace_hash_empty(other_hash))) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1556 | match = 1; |
| 1557 | } |
| 1558 | if (!match) |
| 1559 | continue; |
| 1560 | |
| 1561 | if (inc) { |
| 1562 | rec->flags++; |
| 1563 | if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX)) |
| 1564 | return; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1565 | /* |
| 1566 | * If any ops wants regs saved for this function |
| 1567 | * then all ops will get saved regs. |
| 1568 | */ |
| 1569 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 1570 | rec->flags |= FTRACE_FL_REGS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1571 | } else { |
| 1572 | if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0)) |
| 1573 | return; |
| 1574 | rec->flags--; |
| 1575 | } |
| 1576 | count++; |
| 1577 | /* Shortcut, if we handled all records, we are done. */ |
| 1578 | if (!all && count == hash->count) |
| 1579 | return; |
| 1580 | } while_for_each_ftrace_rec(); |
| 1581 | } |
| 1582 | |
| 1583 | static void ftrace_hash_rec_disable(struct ftrace_ops *ops, |
| 1584 | int filter_hash) |
| 1585 | { |
| 1586 | __ftrace_hash_rec_update(ops, filter_hash, 0); |
| 1587 | } |
| 1588 | |
| 1589 | static void ftrace_hash_rec_enable(struct ftrace_ops *ops, |
| 1590 | int filter_hash) |
| 1591 | { |
| 1592 | __ftrace_hash_rec_update(ops, filter_hash, 1); |
| 1593 | } |
| 1594 | |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 1595 | static void print_ip_ins(const char *fmt, unsigned char *p) |
| 1596 | { |
| 1597 | int i; |
| 1598 | |
| 1599 | printk(KERN_CONT "%s", fmt); |
| 1600 | |
| 1601 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 1602 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 1603 | } |
| 1604 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1605 | /** |
| 1606 | * ftrace_bug - report and shutdown function tracer |
| 1607 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
| 1608 | * @ip: The address that failed |
| 1609 | * |
| 1610 | * The arch code that enables or disables the function tracing |
| 1611 | * can call ftrace_bug() when it has detected a problem in |
| 1612 | * modifying the code. @failed should be one of either: |
| 1613 | * EFAULT - if the problem happens on reading the @ip address |
| 1614 | * EINVAL - if what is read at @ip is not what was expected |
| 1615 | * EPERM - if the problem happens on writting to the @ip address |
| 1616 | */ |
| 1617 | void ftrace_bug(int failed, unsigned long ip) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1618 | { |
| 1619 | switch (failed) { |
| 1620 | case -EFAULT: |
| 1621 | FTRACE_WARN_ON_ONCE(1); |
| 1622 | pr_info("ftrace faulted on modifying "); |
| 1623 | print_ip_sym(ip); |
| 1624 | break; |
| 1625 | case -EINVAL: |
| 1626 | FTRACE_WARN_ON_ONCE(1); |
| 1627 | pr_info("ftrace failed to modify "); |
| 1628 | print_ip_sym(ip); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1629 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1630 | printk(KERN_CONT "\n"); |
| 1631 | break; |
| 1632 | case -EPERM: |
| 1633 | FTRACE_WARN_ON_ONCE(1); |
| 1634 | pr_info("ftrace faulted on writing "); |
| 1635 | print_ip_sym(ip); |
| 1636 | break; |
| 1637 | default: |
| 1638 | FTRACE_WARN_ON_ONCE(1); |
| 1639 | pr_info("ftrace faulted on unknown error "); |
| 1640 | print_ip_sym(ip); |
| 1641 | } |
| 1642 | } |
| 1643 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1644 | static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1645 | { |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1646 | unsigned long flag = 0UL; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1647 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1648 | /* |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1649 | * If we are updating calls: |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1650 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1651 | * If the record has a ref count, then we need to enable it |
| 1652 | * because someone is using it. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1653 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1654 | * Otherwise we make sure its disabled. |
| 1655 | * |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1656 | * If we are disabling calls, then disable all records that |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1657 | * are enabled. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1658 | */ |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1659 | if (enable && (rec->flags & ~FTRACE_FL_MASK)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1660 | flag = FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1661 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1662 | /* |
| 1663 | * If enabling and the REGS flag does not match the REGS_EN, then |
| 1664 | * do not ignore this record. Set flags to fail the compare against |
| 1665 | * ENABLED. |
| 1666 | */ |
| 1667 | if (flag && |
| 1668 | (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN))) |
| 1669 | flag |= FTRACE_FL_REGS; |
| 1670 | |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1671 | /* If the state of this record hasn't changed, then do nothing */ |
| 1672 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1673 | return FTRACE_UPDATE_IGNORE; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1674 | |
| 1675 | if (flag) { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1676 | /* Save off if rec is being enabled (for return value) */ |
| 1677 | flag ^= rec->flags & FTRACE_FL_ENABLED; |
| 1678 | |
| 1679 | if (update) { |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1680 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1681 | if (flag & FTRACE_FL_REGS) { |
| 1682 | if (rec->flags & FTRACE_FL_REGS) |
| 1683 | rec->flags |= FTRACE_FL_REGS_EN; |
| 1684 | else |
| 1685 | rec->flags &= ~FTRACE_FL_REGS_EN; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | /* |
| 1690 | * If this record is being updated from a nop, then |
| 1691 | * return UPDATE_MAKE_CALL. |
| 1692 | * Otherwise, if the EN flag is set, then return |
| 1693 | * UPDATE_MODIFY_CALL_REGS to tell the caller to convert |
| 1694 | * from the non-save regs, to a save regs function. |
| 1695 | * Otherwise, |
| 1696 | * return UPDATE_MODIFY_CALL to tell the caller to convert |
| 1697 | * from the save regs, to a non-save regs function. |
| 1698 | */ |
| 1699 | if (flag & FTRACE_FL_ENABLED) |
| 1700 | return FTRACE_UPDATE_MAKE_CALL; |
| 1701 | else if (rec->flags & FTRACE_FL_REGS_EN) |
| 1702 | return FTRACE_UPDATE_MODIFY_CALL_REGS; |
| 1703 | else |
| 1704 | return FTRACE_UPDATE_MODIFY_CALL; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1705 | } |
| 1706 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1707 | if (update) { |
| 1708 | /* If there's no more users, clear all flags */ |
| 1709 | if (!(rec->flags & ~FTRACE_FL_MASK)) |
| 1710 | rec->flags = 0; |
| 1711 | else |
| 1712 | /* Just disable the record (keep REGS state) */ |
| 1713 | rec->flags &= ~FTRACE_FL_ENABLED; |
| 1714 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1715 | |
| 1716 | return FTRACE_UPDATE_MAKE_NOP; |
| 1717 | } |
| 1718 | |
| 1719 | /** |
| 1720 | * ftrace_update_record, set a record that now is tracing or not |
| 1721 | * @rec: the record to update |
| 1722 | * @enable: set to 1 if the record is tracing, zero to force disable |
| 1723 | * |
| 1724 | * The records that represent all functions that can be traced need |
| 1725 | * to be updated when tracing has been enabled. |
| 1726 | */ |
| 1727 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) |
| 1728 | { |
| 1729 | return ftrace_check_record(rec, enable, 1); |
| 1730 | } |
| 1731 | |
| 1732 | /** |
| 1733 | * ftrace_test_record, check if the record has been enabled or not |
| 1734 | * @rec: the record to test |
| 1735 | * @enable: set to 1 to check if enabled, 0 if it is disabled |
| 1736 | * |
| 1737 | * The arch code may need to test if a record is already set to |
| 1738 | * tracing to determine how to modify the function code that it |
| 1739 | * represents. |
| 1740 | */ |
| 1741 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) |
| 1742 | { |
| 1743 | return ftrace_check_record(rec, enable, 0); |
| 1744 | } |
| 1745 | |
| 1746 | static int |
| 1747 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
| 1748 | { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1749 | unsigned long ftrace_old_addr; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1750 | unsigned long ftrace_addr; |
| 1751 | int ret; |
| 1752 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1753 | ret = ftrace_update_record(rec, enable); |
| 1754 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1755 | if (rec->flags & FTRACE_FL_REGS) |
| 1756 | ftrace_addr = (unsigned long)FTRACE_REGS_ADDR; |
| 1757 | else |
| 1758 | ftrace_addr = (unsigned long)FTRACE_ADDR; |
| 1759 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1760 | switch (ret) { |
| 1761 | case FTRACE_UPDATE_IGNORE: |
| 1762 | return 0; |
| 1763 | |
| 1764 | case FTRACE_UPDATE_MAKE_CALL: |
| 1765 | return ftrace_make_call(rec, ftrace_addr); |
| 1766 | |
| 1767 | case FTRACE_UPDATE_MAKE_NOP: |
| 1768 | return ftrace_make_nop(NULL, rec, ftrace_addr); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1769 | |
| 1770 | case FTRACE_UPDATE_MODIFY_CALL_REGS: |
| 1771 | case FTRACE_UPDATE_MODIFY_CALL: |
| 1772 | if (rec->flags & FTRACE_FL_REGS) |
| 1773 | ftrace_old_addr = (unsigned long)FTRACE_ADDR; |
| 1774 | else |
| 1775 | ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR; |
| 1776 | |
| 1777 | return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | return -1; /* unknow ftrace bug */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1781 | } |
| 1782 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 1783 | void __weak ftrace_replace_code(int enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1784 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1785 | struct dyn_ftrace *rec; |
| 1786 | struct ftrace_page *pg; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 1787 | int failed; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1788 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 1789 | if (unlikely(ftrace_disabled)) |
| 1790 | return; |
| 1791 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1792 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 1793 | failed = __ftrace_replace_code(rec, enable); |
Zhaolei | fa9d13c | 2009-03-13 17:16:34 +0800 | [diff] [blame] | 1794 | if (failed) { |
Steven Rostedt | 3279ba3 | 2009-10-07 16:57:56 -0400 | [diff] [blame] | 1795 | ftrace_bug(failed, rec->ip); |
| 1796 | /* Stop processing */ |
| 1797 | return; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1798 | } |
| 1799 | } while_for_each_ftrace_rec(); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1800 | } |
| 1801 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1802 | struct ftrace_rec_iter { |
| 1803 | struct ftrace_page *pg; |
| 1804 | int index; |
| 1805 | }; |
| 1806 | |
| 1807 | /** |
| 1808 | * ftrace_rec_iter_start, start up iterating over traced functions |
| 1809 | * |
| 1810 | * Returns an iterator handle that is used to iterate over all |
| 1811 | * the records that represent address locations where functions |
| 1812 | * are traced. |
| 1813 | * |
| 1814 | * May return NULL if no records are available. |
| 1815 | */ |
| 1816 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 1817 | { |
| 1818 | /* |
| 1819 | * We only use a single iterator. |
| 1820 | * Protected by the ftrace_lock mutex. |
| 1821 | */ |
| 1822 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 1823 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 1824 | |
| 1825 | iter->pg = ftrace_pages_start; |
| 1826 | iter->index = 0; |
| 1827 | |
| 1828 | /* Could have empty pages */ |
| 1829 | while (iter->pg && !iter->pg->index) |
| 1830 | iter->pg = iter->pg->next; |
| 1831 | |
| 1832 | if (!iter->pg) |
| 1833 | return NULL; |
| 1834 | |
| 1835 | return iter; |
| 1836 | } |
| 1837 | |
| 1838 | /** |
| 1839 | * ftrace_rec_iter_next, get the next record to process. |
| 1840 | * @iter: The handle to the iterator. |
| 1841 | * |
| 1842 | * Returns the next iterator after the given iterator @iter. |
| 1843 | */ |
| 1844 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 1845 | { |
| 1846 | iter->index++; |
| 1847 | |
| 1848 | if (iter->index >= iter->pg->index) { |
| 1849 | iter->pg = iter->pg->next; |
| 1850 | iter->index = 0; |
| 1851 | |
| 1852 | /* Could have empty pages */ |
| 1853 | while (iter->pg && !iter->pg->index) |
| 1854 | iter->pg = iter->pg->next; |
| 1855 | } |
| 1856 | |
| 1857 | if (!iter->pg) |
| 1858 | return NULL; |
| 1859 | |
| 1860 | return iter; |
| 1861 | } |
| 1862 | |
| 1863 | /** |
| 1864 | * ftrace_rec_iter_record, get the record at the iterator location |
| 1865 | * @iter: The current iterator location |
| 1866 | * |
| 1867 | * Returns the record that the current @iter is at. |
| 1868 | */ |
| 1869 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 1870 | { |
| 1871 | return &iter->pg->records[iter->index]; |
| 1872 | } |
| 1873 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 1874 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1875 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1876 | { |
| 1877 | unsigned long ip; |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 1878 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1879 | |
| 1880 | ip = rec->ip; |
| 1881 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 1882 | if (unlikely(ftrace_disabled)) |
| 1883 | return 0; |
| 1884 | |
Shaohua Li | 25aac9d | 2009-01-09 11:29:40 +0800 | [diff] [blame] | 1885 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 1886 | if (ret) { |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1887 | ftrace_bug(ret, ip); |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 1888 | return 0; |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1889 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 1890 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 1893 | /* |
| 1894 | * archs can override this function if they must do something |
| 1895 | * before the modifying code is performed. |
| 1896 | */ |
| 1897 | int __weak ftrace_arch_code_modify_prepare(void) |
| 1898 | { |
| 1899 | return 0; |
| 1900 | } |
| 1901 | |
| 1902 | /* |
| 1903 | * archs can override this function if they must do something |
| 1904 | * after the modifying code is performed. |
| 1905 | */ |
| 1906 | int __weak ftrace_arch_code_modify_post_process(void) |
| 1907 | { |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 1911 | void ftrace_modify_all_code(int command) |
| 1912 | { |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 1913 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 1914 | int err = 0; |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 1915 | |
| 1916 | /* |
| 1917 | * If the ftrace_caller calls a ftrace_ops func directly, |
| 1918 | * we need to make sure that it only traces functions it |
| 1919 | * expects to trace. When doing the switch of functions, |
| 1920 | * we need to update to the ftrace_ops_list_func first |
| 1921 | * before the transition between old and new calls are set, |
| 1922 | * as the ftrace_ops_list_func will check the ops hashes |
| 1923 | * to make sure the ops are having the right functions |
| 1924 | * traced. |
| 1925 | */ |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 1926 | if (update) { |
| 1927 | err = ftrace_update_ftrace_func(ftrace_ops_list_func); |
| 1928 | if (FTRACE_WARN_ON(err)) |
| 1929 | return; |
| 1930 | } |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 1931 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 1932 | if (command & FTRACE_UPDATE_CALLS) |
| 1933 | ftrace_replace_code(1); |
| 1934 | else if (command & FTRACE_DISABLE_CALLS) |
| 1935 | ftrace_replace_code(0); |
| 1936 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 1937 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
| 1938 | function_trace_op = set_function_trace_op; |
| 1939 | smp_wmb(); |
| 1940 | /* If irqs are disabled, we are in stop machine */ |
| 1941 | if (!irqs_disabled()) |
| 1942 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 1943 | err = ftrace_update_ftrace_func(ftrace_trace_function); |
| 1944 | if (FTRACE_WARN_ON(err)) |
| 1945 | return; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 1946 | } |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 1947 | |
| 1948 | if (command & FTRACE_START_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 1949 | err = ftrace_enable_ftrace_graph_caller(); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 1950 | else if (command & FTRACE_STOP_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 1951 | err = ftrace_disable_ftrace_graph_caller(); |
| 1952 | FTRACE_WARN_ON(err); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 1953 | } |
| 1954 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1955 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1956 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1957 | int *command = data; |
| 1958 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 1959 | ftrace_modify_all_code(*command); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 1960 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | /** |
| 1965 | * ftrace_run_stop_machine, go back to the stop machine method |
| 1966 | * @command: The command to tell ftrace what to do |
| 1967 | * |
| 1968 | * If an arch needs to fall back to the stop machine method, the |
| 1969 | * it can call this function. |
| 1970 | */ |
| 1971 | void ftrace_run_stop_machine(int command) |
| 1972 | { |
| 1973 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 1974 | } |
| 1975 | |
| 1976 | /** |
| 1977 | * arch_ftrace_update_code, modify the code to trace or not trace |
| 1978 | * @command: The command that needs to be done |
| 1979 | * |
| 1980 | * Archs can override this function if it does not need to |
| 1981 | * run stop_machine() to modify code. |
| 1982 | */ |
| 1983 | void __weak arch_ftrace_update_code(int command) |
| 1984 | { |
| 1985 | ftrace_run_stop_machine(command); |
| 1986 | } |
| 1987 | |
| 1988 | static void ftrace_run_update_code(int command) |
| 1989 | { |
| 1990 | int ret; |
| 1991 | |
| 1992 | ret = ftrace_arch_code_modify_prepare(); |
| 1993 | FTRACE_WARN_ON(ret); |
| 1994 | if (ret) |
| 1995 | return; |
| 1996 | /* |
| 1997 | * Do not call function tracer while we update the code. |
| 1998 | * We are in stop machine. |
| 1999 | */ |
| 2000 | function_trace_stop++; |
| 2001 | |
| 2002 | /* |
| 2003 | * By default we use stop_machine() to modify the code. |
| 2004 | * But archs can do what ever they want as long as it |
| 2005 | * is safe. The stop_machine() is the safest, but also |
| 2006 | * produces the most overhead. |
| 2007 | */ |
| 2008 | arch_ftrace_update_code(command); |
| 2009 | |
Steven Rostedt | 6331c28 | 2011-07-13 15:11:02 -0400 | [diff] [blame] | 2010 | function_trace_stop--; |
| 2011 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2012 | ret = ftrace_arch_code_modify_post_process(); |
| 2013 | FTRACE_WARN_ON(ret); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2014 | } |
| 2015 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2016 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2017 | static int ftrace_start_up; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2018 | static int global_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2019 | |
Jiri Slaby | db0fbad | 2014-03-10 21:42:11 +0100 | [diff] [blame] | 2020 | static void control_ops_free(struct ftrace_ops *ops) |
| 2021 | { |
| 2022 | free_percpu(ops->disabled); |
| 2023 | } |
| 2024 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2025 | static void ftrace_startup_enable(int command) |
| 2026 | { |
| 2027 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2028 | saved_ftrace_func = ftrace_trace_function; |
| 2029 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2030 | } |
| 2031 | |
| 2032 | if (!command || !ftrace_enabled) |
| 2033 | return; |
| 2034 | |
| 2035 | ftrace_run_update_code(command); |
| 2036 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2037 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2038 | static int ftrace_startup(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2039 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2040 | bool hash_enable = true; |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2041 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2042 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2043 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2044 | return -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2045 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2046 | ret = __register_ftrace_function(ops); |
| 2047 | if (ret) |
| 2048 | return ret; |
| 2049 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2050 | ftrace_start_up++; |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2051 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2052 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2053 | ops->flags |= FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2054 | if (hash_enable) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2055 | ftrace_hash_rec_enable(ops, 1); |
| 2056 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2057 | ftrace_startup_enable(command); |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2058 | |
| 2059 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2060 | } |
| 2061 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2062 | static int ftrace_shutdown(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2063 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2064 | bool hash_disable = true; |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2065 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2066 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2067 | if (unlikely(ftrace_disabled)) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2068 | return -ENODEV; |
| 2069 | |
| 2070 | ret = __unregister_ftrace_function(ops); |
| 2071 | if (ret) |
| 2072 | return ret; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2073 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2074 | ftrace_start_up--; |
Frederic Weisbecker | 9ea1a15 | 2009-06-20 06:52:21 +0200 | [diff] [blame] | 2075 | /* |
| 2076 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 2077 | * critical but the ftrace_call callers may be never nopped again after |
| 2078 | * further ftrace uses. |
| 2079 | */ |
| 2080 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 2081 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2082 | if (hash_disable) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2083 | ftrace_hash_rec_disable(ops, 1); |
| 2084 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 2085 | if (!global_start_up) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2086 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2087 | |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2088 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2089 | |
| 2090 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2091 | saved_ftrace_func = ftrace_trace_function; |
| 2092 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2093 | } |
| 2094 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2095 | if (!command || !ftrace_enabled) { |
| 2096 | /* |
| 2097 | * If these are control ops, they still need their |
| 2098 | * per_cpu field freed. Since, function tracing is |
| 2099 | * not currently active, we can just free them |
| 2100 | * without synchronizing all CPUs. |
| 2101 | */ |
| 2102 | if (ops->flags & FTRACE_OPS_FL_CONTROL) |
| 2103 | control_ops_free(ops); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2104 | return 0; |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2105 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2106 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2107 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2108 | |
| 2109 | /* |
| 2110 | * Dynamic ops may be freed, we must make sure that all |
| 2111 | * callers are done before leaving this function. |
| 2112 | * The same goes for freeing the per_cpu data of the control |
| 2113 | * ops. |
| 2114 | * |
| 2115 | * Again, normal synchronize_sched() is not good enough. |
| 2116 | * We need to do a hard force of sched synchronization. |
| 2117 | * This is because we use preempt_disable() to do RCU, but |
| 2118 | * the function tracers can be called where RCU is not watching |
| 2119 | * (like before user_exit()). We can not rely on the RCU |
| 2120 | * infrastructure to do the synchronization, thus we must do it |
| 2121 | * ourselves. |
| 2122 | */ |
| 2123 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) { |
| 2124 | schedule_on_each_cpu(ftrace_sync); |
| 2125 | |
| 2126 | if (ops->flags & FTRACE_OPS_FL_CONTROL) |
| 2127 | control_ops_free(ops); |
| 2128 | } |
| 2129 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2130 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2131 | } |
| 2132 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2133 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2134 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2135 | if (unlikely(ftrace_disabled)) |
| 2136 | return; |
| 2137 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2138 | /* Force update next time */ |
| 2139 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2140 | /* ftrace_start_up is true if we want ftrace running */ |
| 2141 | if (ftrace_start_up) |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2142 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2143 | } |
| 2144 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2145 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2146 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2147 | if (unlikely(ftrace_disabled)) |
| 2148 | return; |
| 2149 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2150 | /* ftrace_start_up is true if ftrace is running */ |
| 2151 | if (ftrace_start_up) |
Steven Rostedt | 79e406d | 2010-09-14 22:19:46 -0400 | [diff] [blame] | 2152 | ftrace_run_update_code(FTRACE_DISABLE_CALLS); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2153 | } |
| 2154 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2155 | static cycle_t ftrace_update_time; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2156 | unsigned long ftrace_update_tot_cnt; |
| 2157 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2158 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2159 | { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2160 | /* |
| 2161 | * Filter_hash being empty will default to trace module. |
| 2162 | * But notrace hash requires a test of individual module functions. |
| 2163 | */ |
| 2164 | return ftrace_hash_empty(ops->filter_hash) && |
| 2165 | ftrace_hash_empty(ops->notrace_hash); |
| 2166 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2167 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2168 | /* |
| 2169 | * Check if the current ops references the record. |
| 2170 | * |
| 2171 | * If the ops traces all functions, then it was already accounted for. |
| 2172 | * If the ops does not trace the current record function, skip it. |
| 2173 | * If the ops ignores the function via notrace filter, skip it. |
| 2174 | */ |
| 2175 | static inline bool |
| 2176 | ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 2177 | { |
| 2178 | /* If ops isn't enabled, ignore it */ |
| 2179 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 2180 | return 0; |
| 2181 | |
| 2182 | /* If ops traces all mods, we already accounted for it */ |
| 2183 | if (ops_traces_mod(ops)) |
| 2184 | return 0; |
| 2185 | |
| 2186 | /* The function must be in the filter */ |
| 2187 | if (!ftrace_hash_empty(ops->filter_hash) && |
| 2188 | !ftrace_lookup_ip(ops->filter_hash, rec->ip)) |
| 2189 | return 0; |
| 2190 | |
| 2191 | /* If in notrace hash, we ignore it too */ |
| 2192 | if (ftrace_lookup_ip(ops->notrace_hash, rec->ip)) |
| 2193 | return 0; |
| 2194 | |
| 2195 | return 1; |
| 2196 | } |
| 2197 | |
| 2198 | static int referenced_filters(struct dyn_ftrace *rec) |
| 2199 | { |
| 2200 | struct ftrace_ops *ops; |
| 2201 | int cnt = 0; |
| 2202 | |
| 2203 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { |
| 2204 | if (ops_references_rec(ops, rec)) |
| 2205 | cnt++; |
| 2206 | } |
| 2207 | |
| 2208 | return cnt; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2209 | } |
| 2210 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2211 | static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2212 | { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2213 | struct ftrace_page *pg; |
Lai Jiangshan | e94142a | 2009-03-13 17:51:27 +0800 | [diff] [blame] | 2214 | struct dyn_ftrace *p; |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2215 | cycle_t start, stop; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2216 | unsigned long update_cnt = 0; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2217 | unsigned long ref = 0; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2218 | bool test = false; |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2219 | int i; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2220 | |
| 2221 | /* |
| 2222 | * When adding a module, we need to check if tracers are |
| 2223 | * currently enabled and if they are set to trace all functions. |
| 2224 | * If they are, we need to enable the module functions as well |
| 2225 | * as update the reference counts for those function records. |
| 2226 | */ |
| 2227 | if (mod) { |
| 2228 | struct ftrace_ops *ops; |
| 2229 | |
| 2230 | for (ops = ftrace_ops_list; |
| 2231 | ops != &ftrace_list_end; ops = ops->next) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2232 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
| 2233 | if (ops_traces_mod(ops)) |
| 2234 | ref++; |
| 2235 | else |
| 2236 | test = true; |
| 2237 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2238 | } |
| 2239 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2240 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2241 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2242 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2243 | for (pg = new_pgs; pg; pg = pg->next) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2244 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2245 | for (i = 0; i < pg->index; i++) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2246 | int cnt = ref; |
| 2247 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2248 | /* If something went wrong, bail without enabling anything */ |
| 2249 | if (unlikely(ftrace_disabled)) |
| 2250 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2251 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2252 | p = &pg->records[i]; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2253 | if (test) |
| 2254 | cnt += referenced_filters(p); |
| 2255 | p->flags = cnt; |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 2256 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2257 | /* |
| 2258 | * Do the initial record conversion from mcount jump |
| 2259 | * to the NOP instructions. |
| 2260 | */ |
| 2261 | if (!ftrace_code_disable(mod, p)) |
| 2262 | break; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2263 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2264 | update_cnt++; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2265 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2266 | /* |
| 2267 | * If the tracing is enabled, go ahead and enable the record. |
| 2268 | * |
| 2269 | * The reason not to enable the record immediatelly is the |
| 2270 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 2271 | * correct previous instructions. Making first the NOP |
| 2272 | * conversion puts the module to the correct state, thus |
| 2273 | * passing the ftrace_make_call check. |
| 2274 | */ |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2275 | if (ftrace_start_up && cnt) { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2276 | int failed = __ftrace_replace_code(p, 1); |
| 2277 | if (failed) |
| 2278 | ftrace_bug(failed, p->ip); |
| 2279 | } |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2280 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2283 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2284 | ftrace_update_time = stop - start; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2285 | ftrace_update_tot_cnt += update_cnt; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2286 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2287 | return 0; |
| 2288 | } |
| 2289 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2290 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2291 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2292 | int order; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2293 | int cnt; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2294 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2295 | if (WARN_ON(!count)) |
| 2296 | return -EINVAL; |
| 2297 | |
| 2298 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2299 | |
| 2300 | /* |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2301 | * We want to fill as much as possible. No more than a page |
| 2302 | * may be empty. |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2303 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2304 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
| 2305 | order--; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2306 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2307 | again: |
| 2308 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 2309 | |
| 2310 | if (!pg->records) { |
| 2311 | /* if we can't allocate this size, try something smaller */ |
| 2312 | if (!order) |
| 2313 | return -ENOMEM; |
| 2314 | order >>= 1; |
| 2315 | goto again; |
| 2316 | } |
| 2317 | |
| 2318 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 2319 | pg->size = cnt; |
| 2320 | |
| 2321 | if (cnt > count) |
| 2322 | cnt = count; |
| 2323 | |
| 2324 | return cnt; |
| 2325 | } |
| 2326 | |
| 2327 | static struct ftrace_page * |
| 2328 | ftrace_allocate_pages(unsigned long num_to_init) |
| 2329 | { |
| 2330 | struct ftrace_page *start_pg; |
| 2331 | struct ftrace_page *pg; |
| 2332 | int order; |
| 2333 | int cnt; |
| 2334 | |
| 2335 | if (!num_to_init) |
| 2336 | return 0; |
| 2337 | |
| 2338 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 2339 | if (!pg) |
| 2340 | return NULL; |
| 2341 | |
| 2342 | /* |
| 2343 | * Try to allocate as much as possible in one continues |
| 2344 | * location that fills in all of the space. We want to |
| 2345 | * waste as little space as possible. |
| 2346 | */ |
| 2347 | for (;;) { |
| 2348 | cnt = ftrace_allocate_records(pg, num_to_init); |
| 2349 | if (cnt < 0) |
| 2350 | goto free_pages; |
| 2351 | |
| 2352 | num_to_init -= cnt; |
| 2353 | if (!num_to_init) |
| 2354 | break; |
| 2355 | |
| 2356 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 2357 | if (!pg->next) |
| 2358 | goto free_pages; |
| 2359 | |
| 2360 | pg = pg->next; |
| 2361 | } |
| 2362 | |
| 2363 | return start_pg; |
| 2364 | |
| 2365 | free_pages: |
| 2366 | while (start_pg) { |
| 2367 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 2368 | free_pages((unsigned long)pg->records, order); |
| 2369 | start_pg = pg->next; |
| 2370 | kfree(pg); |
| 2371 | pg = start_pg; |
| 2372 | } |
| 2373 | pr_info("ftrace: FAILED to allocate memory for functions\n"); |
| 2374 | return NULL; |
| 2375 | } |
| 2376 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2377 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 2378 | |
| 2379 | struct ftrace_iterator { |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2380 | loff_t pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2381 | loff_t func_pos; |
| 2382 | struct ftrace_page *pg; |
| 2383 | struct dyn_ftrace *func; |
| 2384 | struct ftrace_func_probe *probe; |
| 2385 | struct trace_parser parser; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2386 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2387 | struct ftrace_ops *ops; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2388 | int hidx; |
| 2389 | int idx; |
| 2390 | unsigned flags; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2391 | }; |
| 2392 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2393 | static void * |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2394 | t_hash_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2395 | { |
| 2396 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2397 | struct hlist_node *hnd = NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2398 | struct hlist_head *hhd; |
| 2399 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2400 | (*pos)++; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2401 | iter->pos = *pos; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2402 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2403 | if (iter->probe) |
| 2404 | hnd = &iter->probe->node; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2405 | retry: |
| 2406 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) |
| 2407 | return NULL; |
| 2408 | |
| 2409 | hhd = &ftrace_func_hash[iter->hidx]; |
| 2410 | |
| 2411 | if (hlist_empty(hhd)) { |
| 2412 | iter->hidx++; |
| 2413 | hnd = NULL; |
| 2414 | goto retry; |
| 2415 | } |
| 2416 | |
| 2417 | if (!hnd) |
| 2418 | hnd = hhd->first; |
| 2419 | else { |
| 2420 | hnd = hnd->next; |
| 2421 | if (!hnd) { |
| 2422 | iter->hidx++; |
| 2423 | goto retry; |
| 2424 | } |
| 2425 | } |
| 2426 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2427 | if (WARN_ON_ONCE(!hnd)) |
| 2428 | return NULL; |
| 2429 | |
| 2430 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); |
| 2431 | |
| 2432 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | static void *t_hash_start(struct seq_file *m, loff_t *pos) |
| 2436 | { |
| 2437 | struct ftrace_iterator *iter = m->private; |
| 2438 | void *p = NULL; |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 2439 | loff_t l; |
| 2440 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 2441 | if (!(iter->flags & FTRACE_ITER_DO_HASH)) |
| 2442 | return NULL; |
| 2443 | |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 2444 | if (iter->func_pos > *pos) |
| 2445 | return NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2446 | |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 2447 | iter->hidx = 0; |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 2448 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2449 | p = t_hash_next(m, &l); |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 2450 | if (!p) |
| 2451 | break; |
| 2452 | } |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2453 | if (!p) |
| 2454 | return NULL; |
| 2455 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2456 | /* Only set this if we have an item */ |
| 2457 | iter->flags |= FTRACE_ITER_HASH; |
| 2458 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2459 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2460 | } |
| 2461 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2462 | static int |
| 2463 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2464 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2465 | struct ftrace_func_probe *rec; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2466 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2467 | rec = iter->probe; |
| 2468 | if (WARN_ON_ONCE(!rec)) |
| 2469 | return -EIO; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2470 | |
Steven Rostedt | 809dcf2 | 2009-02-16 23:06:01 -0500 | [diff] [blame] | 2471 | if (rec->ops->print) |
| 2472 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); |
| 2473 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 2474 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2475 | |
| 2476 | if (rec->data) |
| 2477 | seq_printf(m, ":%p", rec->data); |
| 2478 | seq_putc(m, '\n'); |
| 2479 | |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | static void * |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2484 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 2485 | { |
| 2486 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2487 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2488 | struct dyn_ftrace *rec = NULL; |
| 2489 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2490 | if (unlikely(ftrace_disabled)) |
| 2491 | return NULL; |
| 2492 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2493 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2494 | return t_hash_next(m, pos); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2495 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2496 | (*pos)++; |
Jiri Olsa | 1106b69 | 2011-02-16 17:35:34 +0100 | [diff] [blame] | 2497 | iter->pos = iter->func_pos = *pos; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2498 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2499 | if (iter->flags & FTRACE_ITER_PRINTALL) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 2500 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2501 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2502 | retry: |
| 2503 | if (iter->idx >= iter->pg->index) { |
| 2504 | if (iter->pg->next) { |
| 2505 | iter->pg = iter->pg->next; |
| 2506 | iter->idx = 0; |
| 2507 | goto retry; |
| 2508 | } |
| 2509 | } else { |
| 2510 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 2511 | if (((iter->flags & FTRACE_ITER_FILTER) && |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2512 | !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) || |
Steven Rostedt | 0183fb1c | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 2513 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2514 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2515 | !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) || |
| 2516 | |
| 2517 | ((iter->flags & FTRACE_ITER_ENABLED) && |
Steven Rostedt (Red Hat) | 23ea9c4 | 2013-05-09 19:31:48 -0400 | [diff] [blame] | 2518 | !(rec->flags & FTRACE_FL_ENABLED))) { |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2519 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2520 | rec = NULL; |
| 2521 | goto retry; |
| 2522 | } |
| 2523 | } |
| 2524 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2525 | if (!rec) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 2526 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2527 | |
| 2528 | iter->func = rec; |
| 2529 | |
| 2530 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2531 | } |
| 2532 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2533 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 2534 | { |
| 2535 | iter->pos = 0; |
| 2536 | iter->func_pos = 0; |
Dan Carpenter | 70f77b3f | 2012-06-09 19:10:27 +0300 | [diff] [blame] | 2537 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 2541 | { |
| 2542 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2543 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2544 | void *p = NULL; |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 2545 | loff_t l; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2546 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2547 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2548 | |
| 2549 | if (unlikely(ftrace_disabled)) |
| 2550 | return NULL; |
| 2551 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2552 | /* |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2553 | * If an lseek was done, then reset and start from beginning. |
| 2554 | */ |
| 2555 | if (*pos < iter->pos) |
| 2556 | reset_iter_read(iter); |
| 2557 | |
| 2558 | /* |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2559 | * For set_ftrace_filter reading, if we have the filter |
| 2560 | * off, we can short cut and just print out that all |
| 2561 | * functions are enabled. |
| 2562 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 2563 | if (iter->flags & FTRACE_ITER_FILTER && |
| 2564 | ftrace_hash_empty(ops->filter_hash)) { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2565 | if (*pos > 0) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2566 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2567 | iter->flags |= FTRACE_ITER_PRINTALL; |
Chris Wright | df09162 | 2010-09-09 16:34:59 -0700 | [diff] [blame] | 2568 | /* reset in case of seek/pread */ |
| 2569 | iter->flags &= ~FTRACE_ITER_HASH; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2570 | return iter; |
| 2571 | } |
| 2572 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2573 | if (iter->flags & FTRACE_ITER_HASH) |
| 2574 | return t_hash_start(m, pos); |
| 2575 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2576 | /* |
| 2577 | * Unfortunately, we need to restart at ftrace_pages_start |
| 2578 | * every time we let go of the ftrace_mutex. This is because |
| 2579 | * those pointers can change without the lock. |
| 2580 | */ |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 2581 | iter->pg = ftrace_pages_start; |
| 2582 | iter->idx = 0; |
| 2583 | for (l = 0; l <= *pos; ) { |
| 2584 | p = t_next(m, p, &l); |
| 2585 | if (!p) |
| 2586 | break; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 2587 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 2588 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 2589 | if (!p) |
| 2590 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2591 | |
| 2592 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | static void t_stop(struct seq_file *m, void *p) |
| 2596 | { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2597 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | static int t_show(struct seq_file *m, void *v) |
| 2601 | { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2602 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2603 | struct dyn_ftrace *rec; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2604 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2605 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2606 | return t_hash_show(m, iter); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2607 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2608 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
| 2609 | seq_printf(m, "#### all functions enabled ####\n"); |
| 2610 | return 0; |
| 2611 | } |
| 2612 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2613 | rec = iter->func; |
| 2614 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2615 | if (!rec) |
| 2616 | return 0; |
| 2617 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2618 | seq_printf(m, "%ps", (void *)rec->ip); |
| 2619 | if (iter->flags & FTRACE_ITER_ENABLED) |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2620 | seq_printf(m, " (%ld)%s", |
| 2621 | rec->flags & ~FTRACE_FL_MASK, |
| 2622 | rec->flags & FTRACE_FL_REGS ? " R" : ""); |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2623 | seq_printf(m, "\n"); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2624 | |
| 2625 | return 0; |
| 2626 | } |
| 2627 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 2628 | static const struct seq_operations show_ftrace_seq_ops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2629 | .start = t_start, |
| 2630 | .next = t_next, |
| 2631 | .stop = t_stop, |
| 2632 | .show = t_show, |
| 2633 | }; |
| 2634 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2635 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2636 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 2637 | { |
| 2638 | struct ftrace_iterator *iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2639 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2640 | if (unlikely(ftrace_disabled)) |
| 2641 | return -ENODEV; |
| 2642 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 2643 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 2644 | if (iter) { |
| 2645 | iter->pg = ftrace_pages_start; |
| 2646 | iter->ops = &global_ops; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2647 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2648 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 2649 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2652 | static int |
| 2653 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 2654 | { |
| 2655 | struct ftrace_iterator *iter; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2656 | |
| 2657 | if (unlikely(ftrace_disabled)) |
| 2658 | return -ENODEV; |
| 2659 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 2660 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 2661 | if (iter) { |
| 2662 | iter->pg = ftrace_pages_start; |
| 2663 | iter->flags = FTRACE_ITER_ENABLED; |
| 2664 | iter->ops = &global_ops; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2665 | } |
| 2666 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 2667 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2668 | } |
| 2669 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2670 | static void ftrace_filter_reset(struct ftrace_hash *hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2671 | { |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2672 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2673 | ftrace_hash_clear(hash); |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2674 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2675 | } |
| 2676 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2677 | /** |
| 2678 | * ftrace_regex_open - initialize function tracer filter files |
| 2679 | * @ops: The ftrace_ops that hold the hash filters |
| 2680 | * @flag: The type of filter to process |
| 2681 | * @inode: The inode, usually passed in to your open routine |
| 2682 | * @file: The file, usually passed in to your open routine |
| 2683 | * |
| 2684 | * ftrace_regex_open() initializes the filter files for the |
| 2685 | * @ops. Depending on @flag it may process the filter hash or |
| 2686 | * the notrace hash of @ops. With this called from the open |
| 2687 | * routine, you can use ftrace_filter_write() for the write |
| 2688 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 2689 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 2690 | * tracing_lseek() should be used as the lseek routine, and |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2691 | * release must call ftrace_regex_release(). |
| 2692 | */ |
| 2693 | int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2694 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2695 | struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2696 | { |
| 2697 | struct ftrace_iterator *iter; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2698 | struct ftrace_hash *hash; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2699 | int ret = 0; |
| 2700 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 2701 | ftrace_ops_init(ops); |
| 2702 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2703 | if (unlikely(ftrace_disabled)) |
| 2704 | return -ENODEV; |
| 2705 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2706 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 2707 | if (!iter) |
| 2708 | return -ENOMEM; |
| 2709 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 2710 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
| 2711 | kfree(iter); |
| 2712 | return -ENOMEM; |
| 2713 | } |
| 2714 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 2715 | iter->ops = ops; |
| 2716 | iter->flags = flag; |
| 2717 | |
| 2718 | mutex_lock(&ops->regex_lock); |
| 2719 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2720 | if (flag & FTRACE_ITER_NOTRACE) |
| 2721 | hash = ops->notrace_hash; |
| 2722 | else |
| 2723 | hash = ops->filter_hash; |
| 2724 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2725 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2726 | iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2727 | if (!iter->hash) { |
| 2728 | trace_parser_put(&iter->parser); |
| 2729 | kfree(iter); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 2730 | ret = -ENOMEM; |
| 2731 | goto out_unlock; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2732 | } |
| 2733 | } |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2734 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2735 | if ((file->f_mode & FMODE_WRITE) && |
Steven Rostedt | 8650ae3 | 2009-07-22 23:29:30 -0400 | [diff] [blame] | 2736 | (file->f_flags & O_TRUNC)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2737 | ftrace_filter_reset(iter->hash); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2738 | |
| 2739 | if (file->f_mode & FMODE_READ) { |
| 2740 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2741 | |
| 2742 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 2743 | if (!ret) { |
| 2744 | struct seq_file *m = file->private_data; |
| 2745 | m->private = iter; |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 2746 | } else { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2747 | /* Failed */ |
| 2748 | free_ftrace_hash(iter->hash); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 2749 | trace_parser_put(&iter->parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2750 | kfree(iter); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 2751 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2752 | } else |
| 2753 | file->private_data = iter; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 2754 | |
| 2755 | out_unlock: |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 2756 | mutex_unlock(&ops->regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2757 | |
| 2758 | return ret; |
| 2759 | } |
| 2760 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2761 | static int |
| 2762 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 2763 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 2764 | struct ftrace_ops *ops = inode->i_private; |
| 2765 | |
| 2766 | return ftrace_regex_open(ops, |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 2767 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, |
| 2768 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | static int |
| 2772 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 2773 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 2774 | struct ftrace_ops *ops = inode->i_private; |
| 2775 | |
| 2776 | return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2777 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2778 | } |
| 2779 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2780 | static int ftrace_match(char *str, char *regex, int len, int type) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2781 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2782 | int matched = 0; |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 2783 | int slen; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2784 | |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2785 | switch (type) { |
| 2786 | case MATCH_FULL: |
| 2787 | if (strcmp(str, regex) == 0) |
| 2788 | matched = 1; |
| 2789 | break; |
| 2790 | case MATCH_FRONT_ONLY: |
| 2791 | if (strncmp(str, regex, len) == 0) |
| 2792 | matched = 1; |
| 2793 | break; |
| 2794 | case MATCH_MIDDLE_ONLY: |
| 2795 | if (strstr(str, regex)) |
| 2796 | matched = 1; |
| 2797 | break; |
| 2798 | case MATCH_END_ONLY: |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 2799 | slen = strlen(str); |
| 2800 | if (slen >= len && memcmp(str + slen - len, regex, len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2801 | matched = 1; |
| 2802 | break; |
| 2803 | } |
| 2804 | |
| 2805 | return matched; |
| 2806 | } |
| 2807 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2808 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2809 | enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not) |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 2810 | { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2811 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2812 | int ret = 0; |
| 2813 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2814 | entry = ftrace_lookup_ip(hash, rec->ip); |
| 2815 | if (not) { |
| 2816 | /* Do nothing if it doesn't exist */ |
| 2817 | if (!entry) |
| 2818 | return 0; |
| 2819 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2820 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2821 | } else { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2822 | /* Do nothing if it exists */ |
| 2823 | if (entry) |
| 2824 | return 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2825 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2826 | ret = add_hash_entry(hash, rec->ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2827 | } |
| 2828 | return ret; |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 2829 | } |
| 2830 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2831 | static int |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2832 | ftrace_match_record(struct dyn_ftrace *rec, char *mod, |
| 2833 | char *regex, int len, int type) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2834 | { |
| 2835 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2836 | char *modname; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2837 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2838 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); |
| 2839 | |
| 2840 | if (mod) { |
| 2841 | /* module lookup requires matching the module */ |
| 2842 | if (!modname || strcmp(modname, mod)) |
| 2843 | return 0; |
| 2844 | |
| 2845 | /* blank search means to match all funcs in the mod */ |
| 2846 | if (!len) |
| 2847 | return 1; |
| 2848 | } |
| 2849 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2850 | return ftrace_match(str, regex, len, type); |
| 2851 | } |
| 2852 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2853 | static int |
| 2854 | match_records(struct ftrace_hash *hash, char *buff, |
| 2855 | int len, char *mod, int not) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2856 | { |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2857 | unsigned search_len = 0; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2858 | struct ftrace_page *pg; |
| 2859 | struct dyn_ftrace *rec; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2860 | int type = MATCH_FULL; |
| 2861 | char *search = buff; |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 2862 | int found = 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2863 | int ret; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2864 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2865 | if (len) { |
| 2866 | type = filter_parse_regex(buff, len, &search, ¬); |
| 2867 | search_len = strlen(search); |
| 2868 | } |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2869 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2870 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2871 | |
| 2872 | if (unlikely(ftrace_disabled)) |
| 2873 | goto out_unlock; |
| 2874 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2875 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2876 | if (ftrace_match_record(rec, mod, search, search_len, type)) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2877 | ret = enter_record(hash, rec, not); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2878 | if (ret < 0) { |
| 2879 | found = ret; |
| 2880 | goto out_unlock; |
| 2881 | } |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 2882 | found = 1; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2883 | } |
| 2884 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2885 | out_unlock: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2886 | mutex_unlock(&ftrace_lock); |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 2887 | |
| 2888 | return found; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2889 | } |
| 2890 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2891 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2892 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2893 | { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2894 | return match_records(hash, buff, len, NULL, 0); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2895 | } |
| 2896 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2897 | static int |
| 2898 | ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2899 | { |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2900 | int not = 0; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2901 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2902 | /* blank or '*' mean the same */ |
| 2903 | if (strcmp(buff, "*") == 0) |
| 2904 | buff[0] = 0; |
| 2905 | |
| 2906 | /* handle the case of 'dont filter this module' */ |
| 2907 | if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) { |
| 2908 | buff[0] = 0; |
| 2909 | not = 1; |
| 2910 | } |
| 2911 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2912 | return match_records(hash, buff, strlen(buff), mod, not); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2913 | } |
| 2914 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2915 | /* |
| 2916 | * We register the module command as a template to show others how |
| 2917 | * to register the a command as well. |
| 2918 | */ |
| 2919 | |
| 2920 | static int |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 2921 | ftrace_mod_callback(struct ftrace_hash *hash, |
| 2922 | char *func, char *cmd, char *param, int enable) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2923 | { |
| 2924 | char *mod; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2925 | int ret = -EINVAL; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2926 | |
| 2927 | /* |
| 2928 | * cmd == 'mod' because we only registered this func |
| 2929 | * for the 'mod' ftrace_func_command. |
| 2930 | * But if you register one func with multiple commands, |
| 2931 | * you can tell which command was used by the cmd |
| 2932 | * parameter. |
| 2933 | */ |
| 2934 | |
| 2935 | /* we must have a module name */ |
| 2936 | if (!param) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2937 | return ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2938 | |
| 2939 | mod = strsep(¶m, ":"); |
| 2940 | if (!strlen(mod)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2941 | return ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2942 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2943 | ret = ftrace_match_module_records(hash, func, mod); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2944 | if (!ret) |
| 2945 | ret = -EINVAL; |
| 2946 | if (ret < 0) |
| 2947 | return ret; |
| 2948 | |
| 2949 | return 0; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 2953 | .name = "mod", |
| 2954 | .func = ftrace_mod_callback, |
| 2955 | }; |
| 2956 | |
| 2957 | static int __init ftrace_mod_cmd_init(void) |
| 2958 | { |
| 2959 | return register_ftrace_command(&ftrace_mod_cmd); |
| 2960 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 2961 | core_initcall(ftrace_mod_cmd_init); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2962 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 2963 | static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 2964 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2965 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2966 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2967 | struct hlist_head *hhd; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2968 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2969 | |
| 2970 | key = hash_long(ip, FTRACE_HASH_BITS); |
| 2971 | |
| 2972 | hhd = &ftrace_func_hash[key]; |
| 2973 | |
| 2974 | if (hlist_empty(hhd)) |
| 2975 | return; |
| 2976 | |
| 2977 | /* |
| 2978 | * Disable preemption for these calls to prevent a RCU grace |
| 2979 | * period. This syncs the hash iteration and freeing of items |
| 2980 | * on the hash. rcu_read_lock is too dangerous here. |
| 2981 | */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 2982 | preempt_disable_notrace(); |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 2983 | hlist_for_each_entry_rcu_notrace(entry, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2984 | if (entry->ip == ip) |
| 2985 | entry->ops->func(ip, parent_ip, &entry->data); |
| 2986 | } |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 2987 | preempt_enable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2988 | } |
| 2989 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2990 | static struct ftrace_ops trace_probe_ops __read_mostly = |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2991 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 2992 | .func = function_trace_probe_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 2993 | .flags = FTRACE_OPS_FL_INITIALIZED, |
| 2994 | INIT_REGEX_LOCK(trace_probe_ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2995 | }; |
| 2996 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2997 | static int ftrace_probe_registered; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2998 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2999 | static void __enable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3000 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3001 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3002 | int i; |
| 3003 | |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3004 | if (ftrace_probe_registered) { |
| 3005 | /* still need to update the function call sites */ |
| 3006 | if (ftrace_enabled) |
| 3007 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3008 | return; |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3009 | } |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3010 | |
| 3011 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3012 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3013 | if (hhd->first) |
| 3014 | break; |
| 3015 | } |
| 3016 | /* Nothing registered? */ |
| 3017 | if (i == FTRACE_FUNC_HASHSIZE) |
| 3018 | return; |
| 3019 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3020 | ret = ftrace_startup(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3021 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3022 | ftrace_probe_registered = 1; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3023 | } |
| 3024 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3025 | static void __disable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3026 | { |
| 3027 | int i; |
| 3028 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3029 | if (!ftrace_probe_registered) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3030 | return; |
| 3031 | |
| 3032 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3033 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3034 | if (hhd->first) |
| 3035 | return; |
| 3036 | } |
| 3037 | |
| 3038 | /* no more funcs left */ |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3039 | ftrace_shutdown(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3040 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3041 | ftrace_probe_registered = 0; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3045 | static void ftrace_free_entry(struct ftrace_func_probe *entry) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3046 | { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3047 | if (entry->ops->free) |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3048 | entry->ops->free(entry->ops, entry->ip, &entry->data); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3049 | kfree(entry); |
| 3050 | } |
| 3051 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3052 | int |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3053 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3054 | void *data) |
| 3055 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3056 | struct ftrace_func_probe *entry; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3057 | struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash; |
| 3058 | struct ftrace_hash *hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3059 | struct ftrace_page *pg; |
| 3060 | struct dyn_ftrace *rec; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3061 | int type, len, not; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3062 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3063 | int count = 0; |
| 3064 | char *search; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3065 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3066 | |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 3067 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3068 | len = strlen(search); |
| 3069 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3070 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3071 | if (WARN_ON(not)) |
| 3072 | return -EINVAL; |
| 3073 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3074 | mutex_lock(&trace_probe_ops.regex_lock); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3075 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3076 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 3077 | if (!hash) { |
| 3078 | count = -ENOMEM; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3079 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | if (unlikely(ftrace_disabled)) { |
| 3083 | count = -ENODEV; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3084 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3085 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3086 | |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3087 | mutex_lock(&ftrace_lock); |
| 3088 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3089 | do_for_each_ftrace_rec(pg, rec) { |
| 3090 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3091 | if (!ftrace_match_record(rec, NULL, search, len, type)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3092 | continue; |
| 3093 | |
| 3094 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 3095 | if (!entry) { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3096 | /* If we did not process any, then return error */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3097 | if (!count) |
| 3098 | count = -ENOMEM; |
| 3099 | goto out_unlock; |
| 3100 | } |
| 3101 | |
| 3102 | count++; |
| 3103 | |
| 3104 | entry->data = data; |
| 3105 | |
| 3106 | /* |
| 3107 | * The caller might want to do something special |
| 3108 | * for each function we find. We call the callback |
| 3109 | * to give the caller an opportunity to do so. |
| 3110 | */ |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3111 | if (ops->init) { |
| 3112 | if (ops->init(ops, rec->ip, &entry->data) < 0) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3113 | /* caller does not like this func */ |
| 3114 | kfree(entry); |
| 3115 | continue; |
| 3116 | } |
| 3117 | } |
| 3118 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3119 | ret = enter_record(hash, rec, 0); |
| 3120 | if (ret < 0) { |
| 3121 | kfree(entry); |
| 3122 | count = ret; |
| 3123 | goto out_unlock; |
| 3124 | } |
| 3125 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3126 | entry->ops = ops; |
| 3127 | entry->ip = rec->ip; |
| 3128 | |
| 3129 | key = hash_long(entry->ip, FTRACE_HASH_BITS); |
| 3130 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); |
| 3131 | |
| 3132 | } while_for_each_ftrace_rec(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3133 | |
| 3134 | ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); |
| 3135 | if (ret < 0) |
| 3136 | count = ret; |
| 3137 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3138 | __enable_ftrace_function_probe(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3139 | |
| 3140 | out_unlock: |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3141 | mutex_unlock(&ftrace_lock); |
| 3142 | out: |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3143 | mutex_unlock(&trace_probe_ops.regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3144 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3145 | |
| 3146 | return count; |
| 3147 | } |
| 3148 | |
| 3149 | enum { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3150 | PROBE_TEST_FUNC = 1, |
| 3151 | PROBE_TEST_DATA = 2 |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3152 | }; |
| 3153 | |
| 3154 | static void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3155 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3156 | void *data, int flags) |
| 3157 | { |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3158 | struct ftrace_func_entry *rec_entry; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3159 | struct ftrace_func_probe *entry; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3160 | struct ftrace_func_probe *p; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3161 | struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3162 | struct list_head free_list; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3163 | struct ftrace_hash *hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3164 | struct hlist_node *tmp; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3165 | char str[KSYM_SYMBOL_LEN]; |
| 3166 | int type = MATCH_FULL; |
| 3167 | int i, len = 0; |
| 3168 | char *search; |
| 3169 | |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3170 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3171 | glob = NULL; |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3172 | else if (glob) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3173 | int not; |
| 3174 | |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 3175 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3176 | len = strlen(search); |
| 3177 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3178 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3179 | if (WARN_ON(not)) |
| 3180 | return; |
| 3181 | } |
| 3182 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3183 | mutex_lock(&trace_probe_ops.regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3184 | |
| 3185 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 3186 | if (!hash) |
| 3187 | /* Hmm, should report this somehow */ |
| 3188 | goto out_unlock; |
| 3189 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3190 | INIT_LIST_HEAD(&free_list); |
| 3191 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3192 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3193 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3194 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3195 | hlist_for_each_entry_safe(entry, tmp, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3196 | |
| 3197 | /* break up if statements for readability */ |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3198 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3199 | continue; |
| 3200 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3201 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3202 | continue; |
| 3203 | |
| 3204 | /* do this last, since it is the most expensive */ |
| 3205 | if (glob) { |
| 3206 | kallsyms_lookup(entry->ip, NULL, NULL, |
| 3207 | NULL, str); |
| 3208 | if (!ftrace_match(str, glob, len, type)) |
| 3209 | continue; |
| 3210 | } |
| 3211 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3212 | rec_entry = ftrace_lookup_ip(hash, entry->ip); |
| 3213 | /* It is possible more than one entry had this ip */ |
| 3214 | if (rec_entry) |
| 3215 | free_hash_entry(hash, rec_entry); |
| 3216 | |
Steven Rostedt (Red Hat) | 740466b | 2013-03-13 11:15:19 -0400 | [diff] [blame] | 3217 | hlist_del_rcu(&entry->node); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3218 | list_add(&entry->free_list, &free_list); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3219 | } |
| 3220 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3221 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3222 | __disable_ftrace_function_probe(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3223 | /* |
| 3224 | * Remove after the disable is called. Otherwise, if the last |
| 3225 | * probe is removed, a null hash means *all enabled*. |
| 3226 | */ |
| 3227 | ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3228 | synchronize_sched(); |
| 3229 | list_for_each_entry_safe(entry, p, &free_list, free_list) { |
| 3230 | list_del(&entry->free_list); |
| 3231 | ftrace_free_entry(entry); |
| 3232 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3233 | mutex_unlock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3234 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3235 | out_unlock: |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3236 | mutex_unlock(&trace_probe_ops.regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3237 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3241 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3242 | void *data) |
| 3243 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3244 | __unregister_ftrace_function_probe(glob, ops, data, |
| 3245 | PROBE_TEST_FUNC | PROBE_TEST_DATA); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3249 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3250 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3251 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3252 | } |
| 3253 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3254 | void unregister_ftrace_function_probe_all(char *glob) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3255 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3256 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3257 | } |
| 3258 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3259 | static LIST_HEAD(ftrace_commands); |
| 3260 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 3261 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 3262 | /* |
| 3263 | * Currently we only register ftrace commands from __init, so mark this |
| 3264 | * __init too. |
| 3265 | */ |
| 3266 | __init int register_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3267 | { |
| 3268 | struct ftrace_func_command *p; |
| 3269 | int ret = 0; |
| 3270 | |
| 3271 | mutex_lock(&ftrace_cmd_mutex); |
| 3272 | list_for_each_entry(p, &ftrace_commands, list) { |
| 3273 | if (strcmp(cmd->name, p->name) == 0) { |
| 3274 | ret = -EBUSY; |
| 3275 | goto out_unlock; |
| 3276 | } |
| 3277 | } |
| 3278 | list_add(&cmd->list, &ftrace_commands); |
| 3279 | out_unlock: |
| 3280 | mutex_unlock(&ftrace_cmd_mutex); |
| 3281 | |
| 3282 | return ret; |
| 3283 | } |
| 3284 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 3285 | /* |
| 3286 | * Currently we only unregister ftrace commands from __init, so mark |
| 3287 | * this __init too. |
| 3288 | */ |
| 3289 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3290 | { |
| 3291 | struct ftrace_func_command *p, *n; |
| 3292 | int ret = -ENODEV; |
| 3293 | |
| 3294 | mutex_lock(&ftrace_cmd_mutex); |
| 3295 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 3296 | if (strcmp(cmd->name, p->name) == 0) { |
| 3297 | ret = 0; |
| 3298 | list_del_init(&p->list); |
| 3299 | goto out_unlock; |
| 3300 | } |
| 3301 | } |
| 3302 | out_unlock: |
| 3303 | mutex_unlock(&ftrace_cmd_mutex); |
| 3304 | |
| 3305 | return ret; |
| 3306 | } |
| 3307 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3308 | static int ftrace_process_regex(struct ftrace_hash *hash, |
| 3309 | char *buff, int len, int enable) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3310 | { |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3311 | char *func, *command, *next = buff; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3312 | struct ftrace_func_command *p; |
GuoWen Li | 0aff1c0 | 2011-06-01 19:18:47 +0800 | [diff] [blame] | 3313 | int ret = -EINVAL; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3314 | |
| 3315 | func = strsep(&next, ":"); |
| 3316 | |
| 3317 | if (!next) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3318 | ret = ftrace_match_records(hash, func, len); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3319 | if (!ret) |
| 3320 | ret = -EINVAL; |
| 3321 | if (ret < 0) |
| 3322 | return ret; |
| 3323 | return 0; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3324 | } |
| 3325 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3326 | /* command found */ |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3327 | |
| 3328 | command = strsep(&next, ":"); |
| 3329 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3330 | mutex_lock(&ftrace_cmd_mutex); |
| 3331 | list_for_each_entry(p, &ftrace_commands, list) { |
| 3332 | if (strcmp(p->name, command) == 0) { |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 3333 | ret = p->func(hash, func, command, next, enable); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3334 | goto out_unlock; |
| 3335 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3336 | } |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3337 | out_unlock: |
| 3338 | mutex_unlock(&ftrace_cmd_mutex); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3339 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3340 | return ret; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3341 | } |
| 3342 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3343 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3344 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 3345 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3346 | { |
| 3347 | struct ftrace_iterator *iter; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3348 | struct trace_parser *parser; |
| 3349 | ssize_t ret, read; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3350 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3351 | if (!cnt) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3352 | return 0; |
| 3353 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3354 | if (file->f_mode & FMODE_READ) { |
| 3355 | struct seq_file *m = file->private_data; |
| 3356 | iter = m->private; |
| 3357 | } else |
| 3358 | iter = file->private_data; |
| 3359 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3360 | if (unlikely(ftrace_disabled)) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3361 | return -ENODEV; |
| 3362 | |
| 3363 | /* iter->hash is a local copy, so we don't need regex_lock */ |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3364 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3365 | parser = &iter->parser; |
| 3366 | read = trace_get_user(parser, ubuf, cnt, ppos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3367 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3368 | if (read >= 0 && trace_parser_loaded(parser) && |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3369 | !trace_parser_cont(parser)) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3370 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3371 | parser->idx, enable); |
Li Zefan | 313254a | 2009-12-08 11:15:30 +0800 | [diff] [blame] | 3372 | trace_parser_clear(parser); |
Steven Rostedt (Red Hat) | 7c088b5 | 2013-05-09 11:35:12 -0400 | [diff] [blame] | 3373 | if (ret < 0) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3374 | goto out; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3375 | } |
| 3376 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3377 | ret = read; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3378 | out: |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3379 | return ret; |
| 3380 | } |
| 3381 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3382 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3383 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 3384 | size_t cnt, loff_t *ppos) |
| 3385 | { |
| 3386 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 3387 | } |
| 3388 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3389 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3390 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 3391 | size_t cnt, loff_t *ppos) |
| 3392 | { |
| 3393 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 3394 | } |
| 3395 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3396 | static int |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 3397 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
| 3398 | { |
| 3399 | struct ftrace_func_entry *entry; |
| 3400 | |
| 3401 | if (!ftrace_location(ip)) |
| 3402 | return -EINVAL; |
| 3403 | |
| 3404 | if (remove) { |
| 3405 | entry = ftrace_lookup_ip(hash, ip); |
| 3406 | if (!entry) |
| 3407 | return -ENOENT; |
| 3408 | free_hash_entry(hash, entry); |
| 3409 | return 0; |
| 3410 | } |
| 3411 | |
| 3412 | return add_hash_entry(hash, ip); |
| 3413 | } |
| 3414 | |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 3415 | static void ftrace_ops_update_code(struct ftrace_ops *ops) |
| 3416 | { |
| 3417 | if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled) |
| 3418 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
| 3419 | } |
| 3420 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 3421 | static int |
| 3422 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 3423 | unsigned long ip, int remove, int reset, int enable) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3424 | { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3425 | struct ftrace_hash **orig_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3426 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3427 | int ret; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3428 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3429 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3430 | return -ENODEV; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3431 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3432 | mutex_lock(&ops->regex_lock); |
| 3433 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3434 | if (enable) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3435 | orig_hash = &ops->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3436 | else |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3437 | orig_hash = &ops->notrace_hash; |
| 3438 | |
| 3439 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3440 | if (!hash) { |
| 3441 | ret = -ENOMEM; |
| 3442 | goto out_regex_unlock; |
| 3443 | } |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3444 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3445 | if (reset) |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3446 | ftrace_filter_reset(hash); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3447 | if (buf && !ftrace_match_records(hash, buf, len)) { |
| 3448 | ret = -EINVAL; |
| 3449 | goto out_regex_unlock; |
| 3450 | } |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 3451 | if (ip) { |
| 3452 | ret = ftrace_match_addr(hash, ip, remove); |
| 3453 | if (ret < 0) |
| 3454 | goto out_regex_unlock; |
| 3455 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3456 | |
| 3457 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 3458 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 3459 | if (!ret) |
| 3460 | ftrace_ops_update_code(ops); |
Steven Rostedt | 072126f | 2011-07-13 15:08:31 -0400 | [diff] [blame] | 3461 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3462 | mutex_unlock(&ftrace_lock); |
| 3463 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3464 | out_regex_unlock: |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3465 | mutex_unlock(&ops->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3466 | |
| 3467 | free_ftrace_hash(hash); |
| 3468 | return ret; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3469 | } |
| 3470 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 3471 | static int |
| 3472 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, |
| 3473 | int reset, int enable) |
| 3474 | { |
| 3475 | return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); |
| 3476 | } |
| 3477 | |
| 3478 | /** |
| 3479 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address |
| 3480 | * @ops - the ops to set the filter with |
| 3481 | * @ip - the address to add to or remove from the filter. |
| 3482 | * @remove - non zero to remove the ip from the filter |
| 3483 | * @reset - non zero to reset all filters before applying this filter. |
| 3484 | * |
| 3485 | * Filters denote which functions should be enabled when tracing is enabled |
| 3486 | * If @ip is NULL, it failes to update filter. |
| 3487 | */ |
| 3488 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, |
| 3489 | int remove, int reset) |
| 3490 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3491 | ftrace_ops_init(ops); |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 3492 | return ftrace_set_addr(ops, ip, remove, reset, 1); |
| 3493 | } |
| 3494 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); |
| 3495 | |
| 3496 | static int |
| 3497 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 3498 | int reset, int enable) |
| 3499 | { |
| 3500 | return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); |
| 3501 | } |
| 3502 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3503 | /** |
| 3504 | * ftrace_set_filter - set a function to filter on in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3505 | * @ops - the ops to set the filter with |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3506 | * @buf - the string that holds the function filter text. |
| 3507 | * @len - the length of the string. |
| 3508 | * @reset - non zero to reset all filters before applying this filter. |
| 3509 | * |
| 3510 | * Filters denote which functions should be enabled when tracing is enabled. |
| 3511 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 3512 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3513 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3514 | int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3515 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3516 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3517 | return ftrace_set_regex(ops, buf, len, reset, 1); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3518 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3519 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3520 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3521 | /** |
| 3522 | * ftrace_set_notrace - set a function to not trace in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3523 | * @ops - the ops to set the notrace filter with |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3524 | * @buf - the string that holds the function notrace text. |
| 3525 | * @len - the length of the string. |
| 3526 | * @reset - non zero to reset all filters before applying this filter. |
| 3527 | * |
| 3528 | * Notrace Filters denote which functions should not be enabled when tracing |
| 3529 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 3530 | * for tracing. |
| 3531 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3532 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3533 | int len, int reset) |
| 3534 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3535 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3536 | return ftrace_set_regex(ops, buf, len, reset, 0); |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3537 | } |
| 3538 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 3539 | /** |
| 3540 | * ftrace_set_filter - set a function to filter on in ftrace |
| 3541 | * @ops - the ops to set the filter with |
| 3542 | * @buf - the string that holds the function filter text. |
| 3543 | * @len - the length of the string. |
| 3544 | * @reset - non zero to reset all filters before applying this filter. |
| 3545 | * |
| 3546 | * Filters denote which functions should be enabled when tracing is enabled. |
| 3547 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 3548 | */ |
| 3549 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 3550 | { |
| 3551 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
| 3552 | } |
| 3553 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 3554 | |
| 3555 | /** |
| 3556 | * ftrace_set_notrace - set a function to not trace in ftrace |
| 3557 | * @ops - the ops to set the notrace filter with |
| 3558 | * @buf - the string that holds the function notrace text. |
| 3559 | * @len - the length of the string. |
| 3560 | * @reset - non zero to reset all filters before applying this filter. |
| 3561 | * |
| 3562 | * Notrace Filters denote which functions should not be enabled when tracing |
| 3563 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 3564 | * for tracing. |
| 3565 | */ |
| 3566 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3567 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3568 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3569 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3570 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3571 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3572 | /* |
| 3573 | * command line interface to allow users to set filters on boot up. |
| 3574 | */ |
| 3575 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 3576 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 3577 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 3578 | |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 3579 | /* Used by function selftest to not test if filter is set */ |
| 3580 | bool ftrace_filter_param __initdata; |
| 3581 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3582 | static int __init set_ftrace_notrace(char *str) |
| 3583 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 3584 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 3585 | strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3586 | return 1; |
| 3587 | } |
| 3588 | __setup("ftrace_notrace=", set_ftrace_notrace); |
| 3589 | |
| 3590 | static int __init set_ftrace_filter(char *str) |
| 3591 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 3592 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 3593 | strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3594 | return 1; |
| 3595 | } |
| 3596 | __setup("ftrace_filter=", set_ftrace_filter); |
| 3597 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3598 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Lai Jiangshan | f6060f4 | 2009-11-05 11:16:17 +0800 | [diff] [blame] | 3599 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3600 | static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer); |
Steven Rostedt | 801c29f | 2010-03-05 20:02:19 -0500 | [diff] [blame] | 3601 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3602 | static int __init set_graph_function(char *str) |
| 3603 | { |
Frederic Weisbecker | 06f43d6 | 2009-10-14 20:43:39 +0200 | [diff] [blame] | 3604 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3605 | return 1; |
| 3606 | } |
| 3607 | __setup("ftrace_graph_filter=", set_graph_function); |
| 3608 | |
| 3609 | static void __init set_ftrace_early_graph(char *buf) |
| 3610 | { |
| 3611 | int ret; |
| 3612 | char *func; |
| 3613 | |
| 3614 | while (buf) { |
| 3615 | func = strsep(&buf, ","); |
| 3616 | /* we allow only one expression at a time */ |
| 3617 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3618 | FTRACE_GRAPH_MAX_FUNCS, func); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3619 | if (ret) |
| 3620 | printk(KERN_DEBUG "ftrace: function %s not " |
| 3621 | "traceable\n", func); |
| 3622 | } |
| 3623 | } |
| 3624 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 3625 | |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 3626 | void __init |
| 3627 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3628 | { |
| 3629 | char *func; |
| 3630 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3631 | ftrace_ops_init(ops); |
| 3632 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3633 | while (buf) { |
| 3634 | func = strsep(&buf, ","); |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3635 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | static void __init set_ftrace_early_filters(void) |
| 3640 | { |
| 3641 | if (ftrace_filter_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 3642 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3643 | if (ftrace_notrace_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 3644 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3645 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 3646 | if (ftrace_graph_buf[0]) |
| 3647 | set_ftrace_early_graph(ftrace_graph_buf); |
| 3648 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3649 | } |
| 3650 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3651 | int ftrace_regex_release(struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3652 | { |
| 3653 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 3654 | struct ftrace_iterator *iter; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3655 | struct ftrace_hash **orig_hash; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3656 | struct trace_parser *parser; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 3657 | int filter_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3658 | int ret; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3659 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3660 | if (file->f_mode & FMODE_READ) { |
| 3661 | iter = m->private; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3662 | seq_release(inode, file); |
| 3663 | } else |
| 3664 | iter = file->private_data; |
| 3665 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3666 | parser = &iter->parser; |
| 3667 | if (trace_parser_loaded(parser)) { |
| 3668 | parser->buffer[parser->idx] = 0; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3669 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3670 | } |
| 3671 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3672 | trace_parser_put(parser); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3673 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3674 | mutex_lock(&iter->ops->regex_lock); |
| 3675 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3676 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 3677 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 3678 | |
| 3679 | if (filter_hash) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3680 | orig_hash = &iter->ops->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 3681 | else |
| 3682 | orig_hash = &iter->ops->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3683 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3684 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 3685 | ret = ftrace_hash_move(iter->ops, filter_hash, |
| 3686 | orig_hash, iter->hash); |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 3687 | if (!ret) |
| 3688 | ftrace_ops_update_code(iter->ops); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 3689 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3690 | mutex_unlock(&ftrace_lock); |
| 3691 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3692 | |
| 3693 | mutex_unlock(&iter->ops->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3694 | free_ftrace_hash(iter->hash); |
| 3695 | kfree(iter); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3696 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3697 | return 0; |
| 3698 | } |
| 3699 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3700 | static const struct file_operations ftrace_avail_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3701 | .open = ftrace_avail_open, |
| 3702 | .read = seq_read, |
| 3703 | .llseek = seq_lseek, |
Li Zefan | 3be04b4 | 2009-08-17 16:54:03 +0800 | [diff] [blame] | 3704 | .release = seq_release_private, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3705 | }; |
| 3706 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3707 | static const struct file_operations ftrace_enabled_fops = { |
| 3708 | .open = ftrace_enabled_open, |
| 3709 | .read = seq_read, |
| 3710 | .llseek = seq_lseek, |
| 3711 | .release = seq_release_private, |
| 3712 | }; |
| 3713 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3714 | static const struct file_operations ftrace_filter_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3715 | .open = ftrace_filter_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 3716 | .read = seq_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3717 | .write = ftrace_filter_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3718 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3719 | .release = ftrace_regex_release, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3720 | }; |
| 3721 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3722 | static const struct file_operations ftrace_notrace_fops = { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3723 | .open = ftrace_notrace_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 3724 | .read = seq_read, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3725 | .write = ftrace_notrace_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3726 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3727 | .release = ftrace_regex_release, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3728 | }; |
| 3729 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3730 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 3731 | |
| 3732 | static DEFINE_MUTEX(graph_lock); |
| 3733 | |
| 3734 | int ftrace_graph_count; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 3735 | int ftrace_graph_notrace_count; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3736 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 3737 | unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3738 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3739 | struct ftrace_graph_data { |
| 3740 | unsigned long *table; |
| 3741 | size_t size; |
| 3742 | int *count; |
| 3743 | const struct seq_operations *seq_ops; |
| 3744 | }; |
| 3745 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3746 | static void * |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3747 | __g_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3748 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3749 | struct ftrace_graph_data *fgd = m->private; |
| 3750 | |
| 3751 | if (*pos >= *fgd->count) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3752 | return NULL; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3753 | return &fgd->table[*pos]; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3754 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3755 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3756 | static void * |
| 3757 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 3758 | { |
| 3759 | (*pos)++; |
| 3760 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 3764 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3765 | struct ftrace_graph_data *fgd = m->private; |
| 3766 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3767 | mutex_lock(&graph_lock); |
| 3768 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3769 | /* Nothing, tell g_show to print all functions are enabled */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3770 | if (!*fgd->count && !*pos) |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3771 | return (void *)1; |
| 3772 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3773 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | static void g_stop(struct seq_file *m, void *p) |
| 3777 | { |
| 3778 | mutex_unlock(&graph_lock); |
| 3779 | } |
| 3780 | |
| 3781 | static int g_show(struct seq_file *m, void *v) |
| 3782 | { |
| 3783 | unsigned long *ptr = v; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3784 | |
| 3785 | if (!ptr) |
| 3786 | return 0; |
| 3787 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3788 | if (ptr == (unsigned long *)1) { |
| 3789 | seq_printf(m, "#### all functions enabled ####\n"); |
| 3790 | return 0; |
| 3791 | } |
| 3792 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 3793 | seq_printf(m, "%ps\n", (void *)*ptr); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3794 | |
| 3795 | return 0; |
| 3796 | } |
| 3797 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3798 | static const struct seq_operations ftrace_graph_seq_ops = { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3799 | .start = g_start, |
| 3800 | .next = g_next, |
| 3801 | .stop = g_stop, |
| 3802 | .show = g_show, |
| 3803 | }; |
| 3804 | |
| 3805 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3806 | __ftrace_graph_open(struct inode *inode, struct file *file, |
| 3807 | struct ftrace_graph_data *fgd) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3808 | { |
| 3809 | int ret = 0; |
| 3810 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3811 | mutex_lock(&graph_lock); |
| 3812 | if ((file->f_mode & FMODE_WRITE) && |
Steven Rostedt | 8650ae3 | 2009-07-22 23:29:30 -0400 | [diff] [blame] | 3813 | (file->f_flags & O_TRUNC)) { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3814 | *fgd->count = 0; |
| 3815 | memset(fgd->table, 0, fgd->size * sizeof(*fgd->table)); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3816 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3817 | mutex_unlock(&graph_lock); |
| 3818 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3819 | if (file->f_mode & FMODE_READ) { |
| 3820 | ret = seq_open(file, fgd->seq_ops); |
| 3821 | if (!ret) { |
| 3822 | struct seq_file *m = file->private_data; |
| 3823 | m->private = fgd; |
| 3824 | } |
| 3825 | } else |
| 3826 | file->private_data = fgd; |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 3827 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3828 | return ret; |
| 3829 | } |
| 3830 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3831 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3832 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 3833 | { |
| 3834 | struct ftrace_graph_data *fgd; |
| 3835 | |
| 3836 | if (unlikely(ftrace_disabled)) |
| 3837 | return -ENODEV; |
| 3838 | |
| 3839 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 3840 | if (fgd == NULL) |
| 3841 | return -ENOMEM; |
| 3842 | |
| 3843 | fgd->table = ftrace_graph_funcs; |
| 3844 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 3845 | fgd->count = &ftrace_graph_count; |
| 3846 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 3847 | |
| 3848 | return __ftrace_graph_open(inode, file, fgd); |
| 3849 | } |
| 3850 | |
| 3851 | static int |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 3852 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) |
| 3853 | { |
| 3854 | struct ftrace_graph_data *fgd; |
| 3855 | |
| 3856 | if (unlikely(ftrace_disabled)) |
| 3857 | return -ENODEV; |
| 3858 | |
| 3859 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 3860 | if (fgd == NULL) |
| 3861 | return -ENOMEM; |
| 3862 | |
| 3863 | fgd->table = ftrace_graph_notrace_funcs; |
| 3864 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 3865 | fgd->count = &ftrace_graph_notrace_count; |
| 3866 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 3867 | |
| 3868 | return __ftrace_graph_open(inode, file, fgd); |
| 3869 | } |
| 3870 | |
| 3871 | static int |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 3872 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 3873 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3874 | if (file->f_mode & FMODE_READ) { |
| 3875 | struct seq_file *m = file->private_data; |
| 3876 | |
| 3877 | kfree(m->private); |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 3878 | seq_release(inode, file); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3879 | } else { |
| 3880 | kfree(file->private_data); |
| 3881 | } |
| 3882 | |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 3883 | return 0; |
| 3884 | } |
| 3885 | |
| 3886 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3887 | ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3888 | { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3889 | struct dyn_ftrace *rec; |
| 3890 | struct ftrace_page *pg; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3891 | int search_len; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3892 | int fail = 1; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3893 | int type, not; |
| 3894 | char *search; |
| 3895 | bool exists; |
| 3896 | int i; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3897 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3898 | /* decode regex */ |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 3899 | type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3900 | if (!not && *idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3901 | return -EBUSY; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3902 | |
| 3903 | search_len = strlen(search); |
| 3904 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3905 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3906 | |
| 3907 | if (unlikely(ftrace_disabled)) { |
| 3908 | mutex_unlock(&ftrace_lock); |
| 3909 | return -ENODEV; |
| 3910 | } |
| 3911 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3912 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3913 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3914 | if (ftrace_match_record(rec, NULL, search, search_len, type)) { |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3915 | /* if it is in the array */ |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3916 | exists = false; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3917 | for (i = 0; i < *idx; i++) { |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3918 | if (array[i] == rec->ip) { |
| 3919 | exists = true; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3920 | break; |
| 3921 | } |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | if (!not) { |
| 3925 | fail = 0; |
| 3926 | if (!exists) { |
| 3927 | array[(*idx)++] = rec->ip; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3928 | if (*idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3929 | goto out; |
| 3930 | } |
| 3931 | } else { |
| 3932 | if (exists) { |
| 3933 | array[i] = array[--(*idx)]; |
| 3934 | array[*idx] = 0; |
| 3935 | fail = 0; |
| 3936 | } |
| 3937 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3938 | } |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3939 | } while_for_each_ftrace_rec(); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3940 | out: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3941 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3942 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3943 | if (fail) |
| 3944 | return -EINVAL; |
| 3945 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3946 | return 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | static ssize_t |
| 3950 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 3951 | size_t cnt, loff_t *ppos) |
| 3952 | { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3953 | struct trace_parser parser; |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 3954 | ssize_t read, ret = 0; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3955 | struct ftrace_graph_data *fgd = file->private_data; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3956 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3957 | if (!cnt) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3958 | return 0; |
| 3959 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 3960 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) |
| 3961 | return -ENOMEM; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3962 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3963 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
| 3964 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3965 | if (read >= 0 && trace_parser_loaded((&parser))) { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3966 | parser.buffer[parser.idx] = 0; |
| 3967 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 3968 | mutex_lock(&graph_lock); |
| 3969 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3970 | /* we allow only one expression at a time */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 3971 | ret = ftrace_set_func(fgd->table, fgd->count, fgd->size, |
| 3972 | parser.buffer); |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 3973 | |
| 3974 | mutex_unlock(&graph_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3975 | } |
| 3976 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 3977 | if (!ret) |
| 3978 | ret = read; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 3979 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3980 | trace_parser_put(&parser); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3981 | |
| 3982 | return ret; |
| 3983 | } |
| 3984 | |
| 3985 | static const struct file_operations ftrace_graph_fops = { |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 3986 | .open = ftrace_graph_open, |
| 3987 | .read = seq_read, |
| 3988 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3989 | .llseek = tracing_lseek, |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 3990 | .release = ftrace_graph_release, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3991 | }; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 3992 | |
| 3993 | static const struct file_operations ftrace_graph_notrace_fops = { |
| 3994 | .open = ftrace_graph_notrace_open, |
| 3995 | .read = seq_read, |
| 3996 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3997 | .llseek = tracing_lseek, |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 3998 | .release = ftrace_graph_release, |
| 3999 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4000 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4001 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4002 | void ftrace_create_filter_files(struct ftrace_ops *ops, |
| 4003 | struct dentry *parent) |
| 4004 | { |
| 4005 | |
| 4006 | trace_create_file("set_ftrace_filter", 0644, parent, |
| 4007 | ops, &ftrace_filter_fops); |
| 4008 | |
| 4009 | trace_create_file("set_ftrace_notrace", 0644, parent, |
| 4010 | ops, &ftrace_notrace_fops); |
| 4011 | } |
| 4012 | |
| 4013 | /* |
| 4014 | * The name "destroy_filter_files" is really a misnomer. Although |
| 4015 | * in the future, it may actualy delete the files, but this is |
| 4016 | * really intended to make sure the ops passed in are disabled |
| 4017 | * and that when this function returns, the caller is free to |
| 4018 | * free the ops. |
| 4019 | * |
| 4020 | * The "destroy" name is only to match the "create" name that this |
| 4021 | * should be paired with. |
| 4022 | */ |
| 4023 | void ftrace_destroy_filter_files(struct ftrace_ops *ops) |
| 4024 | { |
| 4025 | mutex_lock(&ftrace_lock); |
| 4026 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 4027 | ftrace_shutdown(ops, 0); |
| 4028 | ops->flags |= FTRACE_OPS_FL_DELETED; |
| 4029 | mutex_unlock(&ftrace_lock); |
| 4030 | } |
| 4031 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4032 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4033 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4034 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4035 | trace_create_file("available_filter_functions", 0444, |
| 4036 | d_tracer, NULL, &ftrace_avail_fops); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4037 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4038 | trace_create_file("enabled_functions", 0444, |
| 4039 | d_tracer, NULL, &ftrace_enabled_fops); |
| 4040 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4041 | ftrace_create_filter_files(&global_ops, d_tracer); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 4042 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4043 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4044 | trace_create_file("set_graph_function", 0444, d_tracer, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4045 | NULL, |
| 4046 | &ftrace_graph_fops); |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4047 | trace_create_file("set_graph_notrace", 0444, d_tracer, |
| 4048 | NULL, |
| 4049 | &ftrace_graph_notrace_fops); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4050 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4051 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4052 | return 0; |
| 4053 | } |
| 4054 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4055 | static int ftrace_cmp_ips(const void *a, const void *b) |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4056 | { |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4057 | const unsigned long *ipa = a; |
| 4058 | const unsigned long *ipb = b; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4059 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4060 | if (*ipa > *ipb) |
| 4061 | return 1; |
| 4062 | if (*ipa < *ipb) |
| 4063 | return -1; |
| 4064 | return 0; |
| 4065 | } |
| 4066 | |
| 4067 | static void ftrace_swap_ips(void *a, void *b, int size) |
| 4068 | { |
| 4069 | unsigned long *ipa = a; |
| 4070 | unsigned long *ipb = b; |
| 4071 | unsigned long t; |
| 4072 | |
| 4073 | t = *ipa; |
| 4074 | *ipa = *ipb; |
| 4075 | *ipb = t; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4076 | } |
| 4077 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 4078 | static int ftrace_process_locs(struct module *mod, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4079 | unsigned long *start, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4080 | unsigned long *end) |
| 4081 | { |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4082 | struct ftrace_page *start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4083 | struct ftrace_page *pg; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4084 | struct dyn_ftrace *rec; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4085 | unsigned long count; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4086 | unsigned long *p; |
| 4087 | unsigned long addr; |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4088 | unsigned long flags = 0; /* Shut up gcc */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4089 | int ret = -ENOMEM; |
| 4090 | |
| 4091 | count = end - start; |
| 4092 | |
| 4093 | if (!count) |
| 4094 | return 0; |
| 4095 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4096 | sort(start, count, sizeof(*start), |
| 4097 | ftrace_cmp_ips, ftrace_swap_ips); |
| 4098 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4099 | start_pg = ftrace_allocate_pages(count); |
| 4100 | if (!start_pg) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4101 | return -ENOMEM; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4102 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4103 | mutex_lock(&ftrace_lock); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4104 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4105 | /* |
| 4106 | * Core and each module needs their own pages, as |
| 4107 | * modules will free them when they are removed. |
| 4108 | * Force a new page to be allocated for modules. |
| 4109 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4110 | if (!mod) { |
| 4111 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 4112 | /* First initialization */ |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4113 | ftrace_pages = ftrace_pages_start = start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4114 | } else { |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4115 | if (!ftrace_pages) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4116 | goto out; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4117 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4118 | if (WARN_ON(ftrace_pages->next)) { |
| 4119 | /* Hmm, we have free pages? */ |
| 4120 | while (ftrace_pages->next) |
| 4121 | ftrace_pages = ftrace_pages->next; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4122 | } |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4123 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4124 | ftrace_pages->next = start_pg; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4125 | } |
| 4126 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4127 | p = start; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4128 | pg = start_pg; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4129 | while (p < end) { |
| 4130 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4131 | /* |
| 4132 | * Some architecture linkers will pad between |
| 4133 | * the different mcount_loc sections of different |
| 4134 | * object files to satisfy alignments. |
| 4135 | * Skip any NULL pointers. |
| 4136 | */ |
| 4137 | if (!addr) |
| 4138 | continue; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4139 | |
| 4140 | if (pg->index == pg->size) { |
| 4141 | /* We should have allocated enough */ |
| 4142 | if (WARN_ON(!pg->next)) |
| 4143 | break; |
| 4144 | pg = pg->next; |
| 4145 | } |
| 4146 | |
| 4147 | rec = &pg->records[pg->index++]; |
| 4148 | rec->ip = addr; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4149 | } |
| 4150 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4151 | /* We should have used all pages */ |
| 4152 | WARN_ON(pg->next); |
| 4153 | |
| 4154 | /* Assign the last page to ftrace_pages */ |
| 4155 | ftrace_pages = pg; |
| 4156 | |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4157 | /* |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4158 | * We only need to disable interrupts on start up |
| 4159 | * because we are modifying code that an interrupt |
| 4160 | * may execute, and the modification is not atomic. |
| 4161 | * But for modules, nothing runs the code we modify |
| 4162 | * until we are finished with it, and there's no |
| 4163 | * reason to cause large interrupt latencies while we do it. |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4164 | */ |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4165 | if (!mod) |
| 4166 | local_irq_save(flags); |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 4167 | ftrace_update_code(mod, start_pg); |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4168 | if (!mod) |
| 4169 | local_irq_restore(flags); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4170 | ret = 0; |
| 4171 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4172 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4173 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4174 | return ret; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4175 | } |
| 4176 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4177 | #ifdef CONFIG_MODULES |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4178 | |
| 4179 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 4180 | |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4181 | void ftrace_release_mod(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4182 | { |
| 4183 | struct dyn_ftrace *rec; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4184 | struct ftrace_page **last_pg; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4185 | struct ftrace_page *pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4186 | int order; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4187 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4188 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4189 | |
| 4190 | if (ftrace_disabled) |
| 4191 | goto out_unlock; |
| 4192 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4193 | /* |
| 4194 | * Each module has its own ftrace_pages, remove |
| 4195 | * them from the list. |
| 4196 | */ |
| 4197 | last_pg = &ftrace_pages_start; |
| 4198 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 4199 | rec = &pg->records[0]; |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4200 | if (within_module_core(rec->ip, mod)) { |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4201 | /* |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4202 | * As core pages are first, the first |
| 4203 | * page should never be a module page. |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4204 | */ |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4205 | if (WARN_ON(pg == ftrace_pages_start)) |
| 4206 | goto out_unlock; |
| 4207 | |
| 4208 | /* Check if we are deleting the last page */ |
| 4209 | if (pg == ftrace_pages) |
| 4210 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 4211 | |
| 4212 | *last_pg = pg->next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4213 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 4214 | free_pages((unsigned long)pg->records, order); |
| 4215 | kfree(pg); |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4216 | } else |
| 4217 | last_pg = &pg->next; |
| 4218 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4219 | out_unlock: |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4220 | mutex_unlock(&ftrace_lock); |
| 4221 | } |
| 4222 | |
| 4223 | static void ftrace_init_module(struct module *mod, |
| 4224 | unsigned long *start, unsigned long *end) |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 4225 | { |
Steven Rostedt | 00fd61a | 2008-08-15 21:40:04 -0400 | [diff] [blame] | 4226 | if (ftrace_disabled || start == end) |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 4227 | return; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 4228 | ftrace_process_locs(mod, start, end); |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 4229 | } |
| 4230 | |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4231 | static int ftrace_module_notify_enter(struct notifier_block *self, |
| 4232 | unsigned long val, void *data) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4233 | { |
| 4234 | struct module *mod = data; |
| 4235 | |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4236 | if (val == MODULE_STATE_COMING) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4237 | ftrace_init_module(mod, mod->ftrace_callsites, |
| 4238 | mod->ftrace_callsites + |
| 4239 | mod->num_ftrace_callsites); |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4240 | return 0; |
| 4241 | } |
| 4242 | |
| 4243 | static int ftrace_module_notify_exit(struct notifier_block *self, |
| 4244 | unsigned long val, void *data) |
| 4245 | { |
| 4246 | struct module *mod = data; |
| 4247 | |
| 4248 | if (val == MODULE_STATE_GOING) |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4249 | ftrace_release_mod(mod); |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4250 | |
| 4251 | return 0; |
| 4252 | } |
| 4253 | #else |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4254 | static int ftrace_module_notify_enter(struct notifier_block *self, |
| 4255 | unsigned long val, void *data) |
| 4256 | { |
| 4257 | return 0; |
| 4258 | } |
| 4259 | static int ftrace_module_notify_exit(struct notifier_block *self, |
| 4260 | unsigned long val, void *data) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4261 | { |
| 4262 | return 0; |
| 4263 | } |
| 4264 | #endif /* CONFIG_MODULES */ |
| 4265 | |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4266 | struct notifier_block ftrace_module_enter_nb = { |
| 4267 | .notifier_call = ftrace_module_notify_enter, |
Steven Rostedt | c1bf08a | 2012-12-14 09:48:15 -0500 | [diff] [blame] | 4268 | .priority = INT_MAX, /* Run before anything that can use kprobes */ |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4269 | }; |
| 4270 | |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4271 | struct notifier_block ftrace_module_exit_nb = { |
| 4272 | .notifier_call = ftrace_module_notify_exit, |
| 4273 | .priority = INT_MIN, /* Run after anything that can remove kprobes */ |
| 4274 | }; |
| 4275 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4276 | void __init ftrace_init(void) |
| 4277 | { |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 4278 | extern unsigned long __start_mcount_loc[]; |
| 4279 | extern unsigned long __stop_mcount_loc[]; |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 4280 | unsigned long count, flags; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4281 | int ret; |
| 4282 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4283 | local_irq_save(flags); |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 4284 | ret = ftrace_dyn_arch_init(); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4285 | local_irq_restore(flags); |
Jiri Slaby | af64a7c | 2014-02-24 19:59:58 +0100 | [diff] [blame] | 4286 | if (ret) |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4287 | goto failed; |
| 4288 | |
| 4289 | count = __stop_mcount_loc - __start_mcount_loc; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 4290 | if (!count) { |
| 4291 | pr_info("ftrace: No functions to be traced?\n"); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4292 | goto failed; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | pr_info("ftrace: allocating %ld entries in %ld pages\n", |
| 4296 | count, count / ENTRIES_PER_PAGE + 1); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4297 | |
| 4298 | last_ftrace_enabled = ftrace_enabled = 1; |
| 4299 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 4300 | ret = ftrace_process_locs(NULL, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4301 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4302 | __stop_mcount_loc); |
| 4303 | |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4304 | ret = register_module_notifier(&ftrace_module_enter_nb); |
Ming Lei | 24ed0c4 | 2009-05-17 15:31:38 +0800 | [diff] [blame] | 4305 | if (ret) |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4306 | pr_warning("Failed to register trace ftrace module enter notifier\n"); |
| 4307 | |
| 4308 | ret = register_module_notifier(&ftrace_module_exit_nb); |
| 4309 | if (ret) |
| 4310 | pr_warning("Failed to register trace ftrace module exit notifier\n"); |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4311 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4312 | set_ftrace_early_filters(); |
| 4313 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4314 | return; |
| 4315 | failed: |
| 4316 | ftrace_disabled = 1; |
| 4317 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4318 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4319 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 4320 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 4321 | static struct ftrace_ops global_ops = { |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 4322 | .func = ftrace_stub, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4323 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
| 4324 | INIT_REGEX_LOCK(global_ops) |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 4325 | }; |
| 4326 | |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 4327 | static int __init ftrace_nodyn_init(void) |
| 4328 | { |
| 4329 | ftrace_enabled = 1; |
| 4330 | return 0; |
| 4331 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 4332 | core_initcall(ftrace_nodyn_init); |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 4333 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4334 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
| 4335 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 4336 | /* Keep as macros so we do not need to define the commands */ |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 4337 | # define ftrace_startup(ops, command) \ |
| 4338 | ({ \ |
| 4339 | int ___ret = __register_ftrace_function(ops); \ |
| 4340 | if (!___ret) \ |
| 4341 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ |
| 4342 | ___ret; \ |
Steven Rostedt | 3b6cfdb | 2011-05-23 15:33:49 -0400 | [diff] [blame] | 4343 | }) |
Steven Rostedt (Red Hat) | 1fcc155 | 2014-02-19 15:12:18 -0500 | [diff] [blame] | 4344 | # define ftrace_shutdown(ops, command) \ |
| 4345 | ({ \ |
| 4346 | int ___ret = __unregister_ftrace_function(ops); \ |
| 4347 | if (!___ret) \ |
| 4348 | (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \ |
| 4349 | ___ret; \ |
| 4350 | }) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 4351 | |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4352 | # define ftrace_startup_sysctl() do { } while (0) |
| 4353 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4354 | |
| 4355 | static inline int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 4356 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4357 | { |
| 4358 | return 1; |
| 4359 | } |
| 4360 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4361 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 4362 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 4363 | __init void ftrace_init_global_array_ops(struct trace_array *tr) |
| 4364 | { |
| 4365 | tr->ops = &global_ops; |
| 4366 | tr->ops->private = tr; |
| 4367 | } |
| 4368 | |
| 4369 | void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) |
| 4370 | { |
| 4371 | /* If we filter on pids, update to use the pid function */ |
| 4372 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { |
| 4373 | if (WARN_ON(tr->ops->func != ftrace_stub)) |
| 4374 | printk("ftrace ops had %pS for function\n", |
| 4375 | tr->ops->func); |
| 4376 | /* Only the top level instance does pid tracing */ |
| 4377 | if (!list_empty(&ftrace_pids)) { |
| 4378 | set_ftrace_pid_function(func); |
| 4379 | func = ftrace_pid_func; |
| 4380 | } |
| 4381 | } |
| 4382 | tr->ops->func = func; |
| 4383 | tr->ops->private = tr; |
| 4384 | } |
| 4385 | |
| 4386 | void ftrace_reset_array_ops(struct trace_array *tr) |
| 4387 | { |
| 4388 | tr->ops->func = ftrace_stub; |
| 4389 | } |
| 4390 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4391 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4392 | ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4393 | struct ftrace_ops *op, struct pt_regs *regs) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 4394 | { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 4395 | if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT))) |
| 4396 | return; |
| 4397 | |
| 4398 | /* |
| 4399 | * Some of the ops may be dynamically allocated, |
| 4400 | * they must be freed after a synchronize_sched(). |
| 4401 | */ |
| 4402 | preempt_disable_notrace(); |
| 4403 | trace_recursion_set(TRACE_CONTROL_BIT); |
Steven Rostedt (Red Hat) | b5aa3a4 | 2013-11-04 18:34:44 -0500 | [diff] [blame] | 4404 | |
| 4405 | /* |
| 4406 | * Control funcs (perf) uses RCU. Only trace if |
| 4407 | * RCU is currently active. |
| 4408 | */ |
| 4409 | if (!rcu_is_watching()) |
| 4410 | goto out; |
| 4411 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 4412 | do_for_each_ftrace_op(op, ftrace_control_list) { |
Steven Rostedt (Red Hat) | 395b97a | 2013-03-27 09:31:28 -0400 | [diff] [blame] | 4413 | if (!(op->flags & FTRACE_OPS_FL_STUB) && |
| 4414 | !ftrace_function_local_disabled(op) && |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 4415 | ftrace_ops_test(op, ip, regs)) |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4416 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 4417 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | b5aa3a4 | 2013-11-04 18:34:44 -0500 | [diff] [blame] | 4418 | out: |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 4419 | trace_recursion_clear(TRACE_CONTROL_BIT); |
| 4420 | preempt_enable_notrace(); |
| 4421 | } |
| 4422 | |
| 4423 | static struct ftrace_ops control_ops = { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4424 | .func = ftrace_ops_control_func, |
| 4425 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
| 4426 | INIT_REGEX_LOCK(control_ops) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 4427 | }; |
| 4428 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4429 | static inline void |
| 4430 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4431 | struct ftrace_ops *ignored, struct pt_regs *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4432 | { |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 4433 | struct ftrace_ops *op; |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 4434 | int bit; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4435 | |
Steven Rostedt | ccf3672 | 2012-06-05 09:44:25 -0400 | [diff] [blame] | 4436 | if (function_trace_stop) |
| 4437 | return; |
| 4438 | |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 4439 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 4440 | if (bit < 0) |
| 4441 | return; |
Steven Rostedt | c29f122 | 2012-11-02 17:17:59 -0400 | [diff] [blame] | 4442 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 4443 | /* |
| 4444 | * Some of the ops may be dynamically allocated, |
| 4445 | * they must be freed after a synchronize_sched(). |
| 4446 | */ |
| 4447 | preempt_disable_notrace(); |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 4448 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 4449 | if (ftrace_ops_test(op, ip, regs)) { |
| 4450 | if (WARN_ON(!op->func)) { |
| 4451 | function_trace_stop = 1; |
| 4452 | printk("op=%p %pS\n", op, op); |
| 4453 | goto out; |
| 4454 | } |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4455 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 4456 | } |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 4457 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 4458 | out: |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 4459 | preempt_enable_notrace(); |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 4460 | trace_clear_recursion(bit); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4461 | } |
| 4462 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4463 | /* |
| 4464 | * Some archs only support passing ip and parent_ip. Even though |
| 4465 | * the list function ignores the op parameter, we do not want any |
| 4466 | * C side effects, where a function is called without the caller |
| 4467 | * sending a third parameter. |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4468 | * Archs are to support both the regs and ftrace_ops at the same time. |
| 4469 | * If they support ftrace_ops, it is assumed they support regs. |
| 4470 | * If call backs want to use regs, they must either check for regs |
Masami Hiramatsu | 06aeaae | 2012-09-28 17:15:17 +0900 | [diff] [blame] | 4471 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
| 4472 | * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved. |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4473 | * An architecture can pass partial regs with ftrace_ops and still |
| 4474 | * set the ARCH_SUPPORT_FTARCE_OPS. |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4475 | */ |
| 4476 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 4477 | static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4478 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4479 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4480 | __ftrace_ops_list_func(ip, parent_ip, NULL, regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4481 | } |
| 4482 | #else |
| 4483 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip) |
| 4484 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 4485 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4486 | } |
| 4487 | #endif |
| 4488 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4489 | static void clear_ftrace_swapper(void) |
| 4490 | { |
| 4491 | struct task_struct *p; |
| 4492 | int cpu; |
| 4493 | |
| 4494 | get_online_cpus(); |
| 4495 | for_each_online_cpu(cpu) { |
| 4496 | p = idle_task(cpu); |
| 4497 | clear_tsk_trace_trace(p); |
| 4498 | } |
| 4499 | put_online_cpus(); |
| 4500 | } |
| 4501 | |
| 4502 | static void set_ftrace_swapper(void) |
| 4503 | { |
| 4504 | struct task_struct *p; |
| 4505 | int cpu; |
| 4506 | |
| 4507 | get_online_cpus(); |
| 4508 | for_each_online_cpu(cpu) { |
| 4509 | p = idle_task(cpu); |
| 4510 | set_tsk_trace_trace(p); |
| 4511 | } |
| 4512 | put_online_cpus(); |
| 4513 | } |
| 4514 | |
| 4515 | static void clear_ftrace_pid(struct pid *pid) |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4516 | { |
| 4517 | struct task_struct *p; |
| 4518 | |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4519 | rcu_read_lock(); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4520 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4521 | clear_tsk_trace_trace(p); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4522 | } while_each_pid_task(pid, PIDTYPE_PID, p); |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4523 | rcu_read_unlock(); |
| 4524 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4525 | put_pid(pid); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4526 | } |
| 4527 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4528 | static void set_ftrace_pid(struct pid *pid) |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4529 | { |
| 4530 | struct task_struct *p; |
| 4531 | |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4532 | rcu_read_lock(); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4533 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
| 4534 | set_tsk_trace_trace(p); |
| 4535 | } while_each_pid_task(pid, PIDTYPE_PID, p); |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4536 | rcu_read_unlock(); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4537 | } |
| 4538 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4539 | static void clear_ftrace_pid_task(struct pid *pid) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4540 | { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4541 | if (pid == ftrace_swapper_pid) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4542 | clear_ftrace_swapper(); |
| 4543 | else |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4544 | clear_ftrace_pid(pid); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | static void set_ftrace_pid_task(struct pid *pid) |
| 4548 | { |
| 4549 | if (pid == ftrace_swapper_pid) |
| 4550 | set_ftrace_swapper(); |
| 4551 | else |
| 4552 | set_ftrace_pid(pid); |
| 4553 | } |
| 4554 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4555 | static int ftrace_pid_add(int p) |
| 4556 | { |
| 4557 | struct pid *pid; |
| 4558 | struct ftrace_pid *fpid; |
| 4559 | int ret = -EINVAL; |
| 4560 | |
| 4561 | mutex_lock(&ftrace_lock); |
| 4562 | |
| 4563 | if (!p) |
| 4564 | pid = ftrace_swapper_pid; |
| 4565 | else |
| 4566 | pid = find_get_pid(p); |
| 4567 | |
| 4568 | if (!pid) |
| 4569 | goto out; |
| 4570 | |
| 4571 | ret = 0; |
| 4572 | |
| 4573 | list_for_each_entry(fpid, &ftrace_pids, list) |
| 4574 | if (fpid->pid == pid) |
| 4575 | goto out_put; |
| 4576 | |
| 4577 | ret = -ENOMEM; |
| 4578 | |
| 4579 | fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); |
| 4580 | if (!fpid) |
| 4581 | goto out_put; |
| 4582 | |
| 4583 | list_add(&fpid->list, &ftrace_pids); |
| 4584 | fpid->pid = pid; |
| 4585 | |
| 4586 | set_ftrace_pid_task(pid); |
| 4587 | |
| 4588 | ftrace_update_pid_func(); |
| 4589 | ftrace_startup_enable(0); |
| 4590 | |
| 4591 | mutex_unlock(&ftrace_lock); |
| 4592 | return 0; |
| 4593 | |
| 4594 | out_put: |
| 4595 | if (pid != ftrace_swapper_pid) |
| 4596 | put_pid(pid); |
| 4597 | |
| 4598 | out: |
| 4599 | mutex_unlock(&ftrace_lock); |
| 4600 | return ret; |
| 4601 | } |
| 4602 | |
| 4603 | static void ftrace_pid_reset(void) |
| 4604 | { |
| 4605 | struct ftrace_pid *fpid, *safe; |
| 4606 | |
| 4607 | mutex_lock(&ftrace_lock); |
| 4608 | list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { |
| 4609 | struct pid *pid = fpid->pid; |
| 4610 | |
| 4611 | clear_ftrace_pid_task(pid); |
| 4612 | |
| 4613 | list_del(&fpid->list); |
| 4614 | kfree(fpid); |
| 4615 | } |
| 4616 | |
| 4617 | ftrace_update_pid_func(); |
| 4618 | ftrace_startup_enable(0); |
| 4619 | |
| 4620 | mutex_unlock(&ftrace_lock); |
| 4621 | } |
| 4622 | |
| 4623 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
| 4624 | { |
| 4625 | mutex_lock(&ftrace_lock); |
| 4626 | |
| 4627 | if (list_empty(&ftrace_pids) && (!*pos)) |
| 4628 | return (void *) 1; |
| 4629 | |
| 4630 | return seq_list_start(&ftrace_pids, *pos); |
| 4631 | } |
| 4632 | |
| 4633 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 4634 | { |
| 4635 | if (v == (void *)1) |
| 4636 | return NULL; |
| 4637 | |
| 4638 | return seq_list_next(v, &ftrace_pids, pos); |
| 4639 | } |
| 4640 | |
| 4641 | static void fpid_stop(struct seq_file *m, void *p) |
| 4642 | { |
| 4643 | mutex_unlock(&ftrace_lock); |
| 4644 | } |
| 4645 | |
| 4646 | static int fpid_show(struct seq_file *m, void *v) |
| 4647 | { |
| 4648 | const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); |
| 4649 | |
| 4650 | if (v == (void *)1) { |
| 4651 | seq_printf(m, "no pid\n"); |
| 4652 | return 0; |
| 4653 | } |
| 4654 | |
| 4655 | if (fpid->pid == ftrace_swapper_pid) |
| 4656 | seq_printf(m, "swapper tasks\n"); |
| 4657 | else |
| 4658 | seq_printf(m, "%u\n", pid_vnr(fpid->pid)); |
| 4659 | |
| 4660 | return 0; |
| 4661 | } |
| 4662 | |
| 4663 | static const struct seq_operations ftrace_pid_sops = { |
| 4664 | .start = fpid_start, |
| 4665 | .next = fpid_next, |
| 4666 | .stop = fpid_stop, |
| 4667 | .show = fpid_show, |
| 4668 | }; |
| 4669 | |
| 4670 | static int |
| 4671 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 4672 | { |
| 4673 | int ret = 0; |
| 4674 | |
| 4675 | if ((file->f_mode & FMODE_WRITE) && |
| 4676 | (file->f_flags & O_TRUNC)) |
| 4677 | ftrace_pid_reset(); |
| 4678 | |
| 4679 | if (file->f_mode & FMODE_READ) |
| 4680 | ret = seq_open(file, &ftrace_pid_sops); |
| 4681 | |
| 4682 | return ret; |
| 4683 | } |
| 4684 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4685 | static ssize_t |
| 4686 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 4687 | size_t cnt, loff_t *ppos) |
| 4688 | { |
Ingo Molnar | 457dc92 | 2009-11-23 11:03:28 +0100 | [diff] [blame] | 4689 | char buf[64], *tmp; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4690 | long val; |
| 4691 | int ret; |
| 4692 | |
| 4693 | if (cnt >= sizeof(buf)) |
| 4694 | return -EINVAL; |
| 4695 | |
| 4696 | if (copy_from_user(&buf, ubuf, cnt)) |
| 4697 | return -EFAULT; |
| 4698 | |
| 4699 | buf[cnt] = 0; |
| 4700 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4701 | /* |
| 4702 | * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" |
| 4703 | * to clean the filter quietly. |
| 4704 | */ |
Ingo Molnar | 457dc92 | 2009-11-23 11:03:28 +0100 | [diff] [blame] | 4705 | tmp = strstrip(buf); |
| 4706 | if (strlen(tmp) == 0) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4707 | return 1; |
| 4708 | |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 4709 | ret = kstrtol(tmp, 10, &val); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4710 | if (ret < 0) |
| 4711 | return ret; |
| 4712 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4713 | ret = ftrace_pid_add(val); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4714 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4715 | return ret ? ret : cnt; |
| 4716 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4717 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4718 | static int |
| 4719 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 4720 | { |
| 4721 | if (file->f_mode & FMODE_READ) |
| 4722 | seq_release(inode, file); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4723 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4724 | return 0; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4725 | } |
| 4726 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4727 | static const struct file_operations ftrace_pid_fops = { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4728 | .open = ftrace_pid_open, |
| 4729 | .write = ftrace_pid_write, |
| 4730 | .read = seq_read, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4731 | .llseek = tracing_lseek, |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4732 | .release = ftrace_pid_release, |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4733 | }; |
| 4734 | |
| 4735 | static __init int ftrace_init_debugfs(void) |
| 4736 | { |
| 4737 | struct dentry *d_tracer; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4738 | |
| 4739 | d_tracer = tracing_init_dentry(); |
| 4740 | if (!d_tracer) |
| 4741 | return 0; |
| 4742 | |
| 4743 | ftrace_init_dyn_debugfs(d_tracer); |
| 4744 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4745 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
| 4746 | NULL, &ftrace_pid_fops); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 4747 | |
| 4748 | ftrace_profile_debugfs(d_tracer); |
| 4749 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4750 | return 0; |
| 4751 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4752 | fs_initcall(ftrace_init_debugfs); |
| 4753 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4754 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 4755 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 4756 | * |
| 4757 | * This function should be used by panic code. It stops ftrace |
| 4758 | * but in a not so nice way. If you need to simply kill ftrace |
| 4759 | * from a non-atomic section, use ftrace_kill. |
| 4760 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 4761 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 4762 | { |
| 4763 | ftrace_disabled = 1; |
| 4764 | ftrace_enabled = 0; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 4765 | clear_ftrace_function(); |
| 4766 | } |
| 4767 | |
| 4768 | /** |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 4769 | * Test if ftrace is dead or not. |
| 4770 | */ |
| 4771 | int ftrace_is_dead(void) |
| 4772 | { |
| 4773 | return ftrace_disabled; |
| 4774 | } |
| 4775 | |
| 4776 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4777 | * register_ftrace_function - register a function for profiling |
| 4778 | * @ops - ops structure that holds the function for profiling. |
| 4779 | * |
| 4780 | * Register a function to be called by all functions in the |
| 4781 | * kernel. |
| 4782 | * |
| 4783 | * Note: @ops->func and all the functions it calls must be labeled |
| 4784 | * with "notrace", otherwise it will go into a |
| 4785 | * recursive loop. |
| 4786 | */ |
| 4787 | int register_ftrace_function(struct ftrace_ops *ops) |
| 4788 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4789 | int ret = -1; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4790 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4791 | ftrace_ops_init(ops); |
| 4792 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4793 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4794 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 4795 | ret = ftrace_startup(ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4796 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4797 | mutex_unlock(&ftrace_lock); |
Borislav Petkov | 8d240dd | 2012-03-29 19:11:40 +0200 | [diff] [blame] | 4798 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4799 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4800 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 4801 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4802 | |
| 4803 | /** |
Uwe Kleine-Koenig | 3263292 | 2009-01-12 23:35:50 +0100 | [diff] [blame] | 4804 | * unregister_ftrace_function - unregister a function for profiling. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4805 | * @ops - ops structure that holds the function to unregister |
| 4806 | * |
| 4807 | * Unregister a function that was added to be called by ftrace profiling. |
| 4808 | */ |
| 4809 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 4810 | { |
| 4811 | int ret; |
| 4812 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4813 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 4814 | ret = ftrace_shutdown(ops, 0); |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4815 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4816 | |
| 4817 | return ret; |
| 4818 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 4819 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4820 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4821 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4822 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 4823 | void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4824 | loff_t *ppos) |
| 4825 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4826 | int ret = -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4827 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4828 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4829 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4830 | if (unlikely(ftrace_disabled)) |
| 4831 | goto out; |
| 4832 | |
| 4833 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4834 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 4835 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4836 | goto out; |
| 4837 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 4838 | last_ftrace_enabled = !!ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4839 | |
| 4840 | if (ftrace_enabled) { |
| 4841 | |
| 4842 | ftrace_startup_sysctl(); |
| 4843 | |
| 4844 | /* we are starting ftrace again */ |
Jan Kiszka | 5000c41 | 2013-03-26 17:53:03 +0100 | [diff] [blame] | 4845 | if (ftrace_ops_list != &ftrace_list_end) |
| 4846 | update_ftrace_function(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4847 | |
| 4848 | } else { |
| 4849 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 4850 | ftrace_trace_function = ftrace_stub; |
| 4851 | |
| 4852 | ftrace_shutdown_sysctl(); |
| 4853 | } |
| 4854 | |
| 4855 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4856 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4857 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4858 | } |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 4859 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4860 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4861 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4862 | static int ftrace_graph_active; |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 4863 | static struct notifier_block ftrace_suspend_notifier; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4864 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 4865 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 4866 | { |
| 4867 | return 0; |
| 4868 | } |
| 4869 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 4870 | /* The callbacks that hook a function */ |
| 4871 | trace_func_graph_ret_t ftrace_graph_return = |
| 4872 | (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 4873 | trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 4874 | static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4875 | |
| 4876 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 4877 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 4878 | { |
| 4879 | int i; |
| 4880 | int ret = 0; |
| 4881 | unsigned long flags; |
| 4882 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 4883 | struct task_struct *g, *t; |
| 4884 | |
| 4885 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 4886 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH |
| 4887 | * sizeof(struct ftrace_ret_stack), |
| 4888 | GFP_KERNEL); |
| 4889 | if (!ret_stack_list[i]) { |
| 4890 | start = 0; |
| 4891 | end = i; |
| 4892 | ret = -ENOMEM; |
| 4893 | goto free; |
| 4894 | } |
| 4895 | } |
| 4896 | |
| 4897 | read_lock_irqsave(&tasklist_lock, flags); |
| 4898 | do_each_thread(g, t) { |
| 4899 | if (start == end) { |
| 4900 | ret = -EAGAIN; |
| 4901 | goto unlock; |
| 4902 | } |
| 4903 | |
| 4904 | if (t->ret_stack == NULL) { |
Frederic Weisbecker | 380c4b1 | 2008-12-06 03:43:41 +0100 | [diff] [blame] | 4905 | atomic_set(&t->tracing_graph_pause, 0); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4906 | atomic_set(&t->trace_overrun, 0); |
Steven Rostedt | 26c0162 | 2009-06-02 14:01:19 -0400 | [diff] [blame] | 4907 | t->curr_ret_stack = -1; |
| 4908 | /* Make sure the tasks see the -1 first: */ |
| 4909 | smp_wmb(); |
| 4910 | t->ret_stack = ret_stack_list[start++]; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4911 | } |
| 4912 | } while_each_thread(g, t); |
| 4913 | |
| 4914 | unlock: |
| 4915 | read_unlock_irqrestore(&tasklist_lock, flags); |
| 4916 | free: |
| 4917 | for (i = start; i < end; i++) |
| 4918 | kfree(ret_stack_list[i]); |
| 4919 | return ret; |
| 4920 | } |
| 4921 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4922 | static void |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 4923 | ftrace_graph_probe_sched_switch(void *ignore, |
| 4924 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4925 | { |
| 4926 | unsigned long long timestamp; |
| 4927 | int index; |
| 4928 | |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 4929 | /* |
| 4930 | * Does the user want to count the time a function was asleep. |
| 4931 | * If so, do not update the time stamps. |
| 4932 | */ |
| 4933 | if (trace_flags & TRACE_ITER_SLEEP_TIME) |
| 4934 | return; |
| 4935 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4936 | timestamp = trace_clock_local(); |
| 4937 | |
| 4938 | prev->ftrace_timestamp = timestamp; |
| 4939 | |
| 4940 | /* only process tasks that we timestamped */ |
| 4941 | if (!next->ftrace_timestamp) |
| 4942 | return; |
| 4943 | |
| 4944 | /* |
| 4945 | * Update all the counters in next to make up for the |
| 4946 | * time next was sleeping. |
| 4947 | */ |
| 4948 | timestamp -= next->ftrace_timestamp; |
| 4949 | |
| 4950 | for (index = next->curr_ret_stack; index >= 0; index--) |
| 4951 | next->ret_stack[index].calltime += timestamp; |
| 4952 | } |
| 4953 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4954 | /* Allocate a return stack for each task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4955 | static int start_graph_tracing(void) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4956 | { |
| 4957 | struct ftrace_ret_stack **ret_stack_list; |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 4958 | int ret, cpu; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4959 | |
| 4960 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * |
| 4961 | sizeof(struct ftrace_ret_stack *), |
| 4962 | GFP_KERNEL); |
| 4963 | |
| 4964 | if (!ret_stack_list) |
| 4965 | return -ENOMEM; |
| 4966 | |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 4967 | /* The cpu_boot init_task->ret_stack will never be freed */ |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 4968 | for_each_online_cpu(cpu) { |
| 4969 | if (!idle_task(cpu)->ret_stack) |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 4970 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 4971 | } |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 4972 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4973 | do { |
| 4974 | ret = alloc_retstack_tasklist(ret_stack_list); |
| 4975 | } while (ret == -EAGAIN); |
| 4976 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4977 | if (!ret) { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 4978 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4979 | if (ret) |
| 4980 | pr_info("ftrace_graph: Couldn't activate tracepoint" |
| 4981 | " probe to kernel_sched_switch\n"); |
| 4982 | } |
| 4983 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4984 | kfree(ret_stack_list); |
| 4985 | return ret; |
| 4986 | } |
| 4987 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 4988 | /* |
| 4989 | * Hibernation protection. |
| 4990 | * The state of the current task is too much unstable during |
| 4991 | * suspend/restore to disk. We want to protect against that. |
| 4992 | */ |
| 4993 | static int |
| 4994 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, |
| 4995 | void *unused) |
| 4996 | { |
| 4997 | switch (state) { |
| 4998 | case PM_HIBERNATION_PREPARE: |
| 4999 | pause_graph_tracing(); |
| 5000 | break; |
| 5001 | |
| 5002 | case PM_POST_HIBERNATION: |
| 5003 | unpause_graph_tracing(); |
| 5004 | break; |
| 5005 | } |
| 5006 | return NOTIFY_DONE; |
| 5007 | } |
| 5008 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5009 | /* Just a place holder for function graph */ |
| 5010 | static struct ftrace_ops fgraph_ops __read_mostly = { |
| 5011 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame^] | 5012 | .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_RECURSION_SAFE, |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5013 | }; |
| 5014 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5015 | static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) |
| 5016 | { |
| 5017 | if (!ftrace_ops_test(&global_ops, trace->func, NULL)) |
| 5018 | return 0; |
| 5019 | return __ftrace_graph_entry(trace); |
| 5020 | } |
| 5021 | |
| 5022 | /* |
| 5023 | * The function graph tracer should only trace the functions defined |
| 5024 | * by set_ftrace_filter and set_ftrace_notrace. If another function |
| 5025 | * tracer ops is registered, the graph tracer requires testing the |
| 5026 | * function against the global ops, and not just trace any function |
| 5027 | * that any ftrace_ops registered. |
| 5028 | */ |
| 5029 | static void update_function_graph_func(void) |
| 5030 | { |
| 5031 | if (ftrace_ops_list == &ftrace_list_end || |
| 5032 | (ftrace_ops_list == &global_ops && |
| 5033 | global_ops.next == &ftrace_list_end)) |
| 5034 | ftrace_graph_entry = __ftrace_graph_entry; |
| 5035 | else |
| 5036 | ftrace_graph_entry = ftrace_graph_entry_test; |
| 5037 | } |
| 5038 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5039 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 5040 | trace_func_graph_ent_t entryfunc) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5041 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5042 | int ret = 0; |
| 5043 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5044 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5045 | |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5046 | /* we currently allow only one tracer registered at a time */ |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5047 | if (ftrace_graph_active) { |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5048 | ret = -EBUSY; |
| 5049 | goto out; |
| 5050 | } |
| 5051 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5052 | ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call; |
| 5053 | register_pm_notifier(&ftrace_suspend_notifier); |
| 5054 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5055 | ftrace_graph_active++; |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5056 | ret = start_graph_tracing(); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5057 | if (ret) { |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5058 | ftrace_graph_active--; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5059 | goto out; |
| 5060 | } |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5061 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5062 | ftrace_graph_return = retfunc; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5063 | |
| 5064 | /* |
| 5065 | * Update the indirect function to the entryfunc, and the |
| 5066 | * function that gets called to the entry_test first. Then |
| 5067 | * call the update fgraph entry function to determine if |
| 5068 | * the entryfunc should be called directly or not. |
| 5069 | */ |
| 5070 | __ftrace_graph_entry = entryfunc; |
| 5071 | ftrace_graph_entry = ftrace_graph_entry_test; |
| 5072 | update_function_graph_func(); |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5073 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5074 | ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5075 | |
| 5076 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5077 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5078 | return ret; |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5079 | } |
| 5080 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5081 | void unregister_ftrace_graph(void) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5082 | { |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5083 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5084 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5085 | if (unlikely(!ftrace_graph_active)) |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5086 | goto out; |
| 5087 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5088 | ftrace_graph_active--; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5089 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5090 | ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5091 | __ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5092 | ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET); |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5093 | unregister_pm_notifier(&ftrace_suspend_notifier); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5094 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5095 | |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5096 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5097 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5098 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5099 | |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5100 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
| 5101 | |
| 5102 | static void |
| 5103 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) |
| 5104 | { |
| 5105 | atomic_set(&t->tracing_graph_pause, 0); |
| 5106 | atomic_set(&t->trace_overrun, 0); |
| 5107 | t->ftrace_timestamp = 0; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5108 | /* make curr_ret_stack visible before we add the ret_stack */ |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5109 | smp_wmb(); |
| 5110 | t->ret_stack = ret_stack; |
| 5111 | } |
| 5112 | |
| 5113 | /* |
| 5114 | * Allocate a return stack for the idle task. May be the first |
| 5115 | * time through, or it may be done by CPU hotplug online. |
| 5116 | */ |
| 5117 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) |
| 5118 | { |
| 5119 | t->curr_ret_stack = -1; |
| 5120 | /* |
| 5121 | * The idle task has no parent, it either has its own |
| 5122 | * stack or no stack at all. |
| 5123 | */ |
| 5124 | if (t->ret_stack) |
| 5125 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); |
| 5126 | |
| 5127 | if (ftrace_graph_active) { |
| 5128 | struct ftrace_ret_stack *ret_stack; |
| 5129 | |
| 5130 | ret_stack = per_cpu(idle_ret_stack, cpu); |
| 5131 | if (!ret_stack) { |
| 5132 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
| 5133 | * sizeof(struct ftrace_ret_stack), |
| 5134 | GFP_KERNEL); |
| 5135 | if (!ret_stack) |
| 5136 | return; |
| 5137 | per_cpu(idle_ret_stack, cpu) = ret_stack; |
| 5138 | } |
| 5139 | graph_init_task(t, ret_stack); |
| 5140 | } |
| 5141 | } |
| 5142 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5143 | /* Allocate a return stack for newly created task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5144 | void ftrace_graph_init_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5145 | { |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 5146 | /* Make sure we do not use the parent ret_stack */ |
| 5147 | t->ret_stack = NULL; |
Steven Rostedt | ea14eb7 | 2010-03-12 19:41:23 -0500 | [diff] [blame] | 5148 | t->curr_ret_stack = -1; |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 5149 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5150 | if (ftrace_graph_active) { |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 5151 | struct ftrace_ret_stack *ret_stack; |
| 5152 | |
| 5153 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5154 | * sizeof(struct ftrace_ret_stack), |
| 5155 | GFP_KERNEL); |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 5156 | if (!ret_stack) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5157 | return; |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5158 | graph_init_task(t, ret_stack); |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 5159 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5160 | } |
| 5161 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5162 | void ftrace_graph_exit_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5163 | { |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 5164 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 5165 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5166 | t->ret_stack = NULL; |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 5167 | /* NULL must become visible to IRQs before we free it: */ |
| 5168 | barrier(); |
| 5169 | |
| 5170 | kfree(ret_stack); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5171 | } |
Steven Rostedt | 14a866c | 2008-12-02 23:50:02 -0500 | [diff] [blame] | 5172 | |
| 5173 | void ftrace_graph_stop(void) |
| 5174 | { |
| 5175 | ftrace_stop(); |
| 5176 | } |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5177 | #endif |