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