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