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