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