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