Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Infrastructure for profiling code inserted by 'gcc -pg'. |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 5 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> |
| 6 | * |
| 7 | * Originally ported from the -rt patch by: |
| 8 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 9 | * |
| 10 | * Based on code in the latency_tracer, that is: |
| 11 | * |
| 12 | * Copyright (C) 2004-2006 Ingo Molnar |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 13 | * Copyright (C) 2004 Nadia Yvette Chambers |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 16 | #include <linux/stop_machine.h> |
| 17 | #include <linux/clocksource.h> |
| 18 | #include <linux/kallsyms.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 20 | #include <linux/suspend.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 21 | #include <linux/debugfs.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 22 | #include <linux/hardirq.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 23 | #include <linux/kthread.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 25 | #include <linux/bsearch.h> |
Paul Gortmaker | 56d82e0 | 2011-05-26 17:53:52 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 27 | #include <linux/ftrace.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 28 | #include <linux/sysctl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 30 | #include <linux/ctype.h> |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 31 | #include <linux/sort.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 32 | #include <linux/list.h> |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 33 | #include <linux/hash.h> |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 34 | #include <linux/rcupdate.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 35 | |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 36 | #include <trace/events/sched.h> |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 37 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 38 | #include <asm/setup.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 39 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 40 | #include "trace_output.h" |
Steven Rostedt | bac429f | 2009-03-20 12:50:56 -0400 | [diff] [blame] | 41 | #include "trace_stat.h" |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 42 | |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 43 | #define FTRACE_WARN_ON(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 44 | ({ \ |
| 45 | int ___r = cond; \ |
| 46 | if (WARN_ON(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 47 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 48 | ___r; \ |
| 49 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 50 | |
| 51 | #define FTRACE_WARN_ON_ONCE(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 52 | ({ \ |
| 53 | int ___r = cond; \ |
| 54 | if (WARN_ON_ONCE(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 55 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 56 | ___r; \ |
| 57 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 58 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 59 | /* hash bits for specific function selection */ |
| 60 | #define FTRACE_HASH_BITS 7 |
| 61 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 62 | #define FTRACE_HASH_DEFAULT_BITS 10 |
| 63 | #define FTRACE_HASH_MAX_BITS 12 |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 64 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 65 | #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 66 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 67 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 68 | #define INIT_OPS_HASH(opsname) \ |
| 69 | .func_hash = &opsname.local_hash, \ |
| 70 | .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 71 | #define ASSIGN_OPS_HASH(opsname, val) \ |
| 72 | .func_hash = val, \ |
| 73 | .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 74 | #else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 75 | #define INIT_OPS_HASH(opsname) |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 76 | #define ASSIGN_OPS_HASH(opsname, val) |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 77 | #endif |
| 78 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 79 | static struct ftrace_ops ftrace_list_end __read_mostly = { |
| 80 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | 395b97a | 2013-03-27 09:31:28 -0400 | [diff] [blame] | 81 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 82 | INIT_OPS_HASH(ftrace_list_end) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 85 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 86 | int ftrace_enabled __read_mostly; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 87 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 88 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 89 | /* Current function tracing op */ |
| 90 | 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] | 91 | /* What to set function_trace_op to */ |
| 92 | static struct ftrace_ops *set_function_trace_op; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 93 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 94 | /* List for set_ftrace_pid's pids. */ |
| 95 | LIST_HEAD(ftrace_pids); |
| 96 | struct ftrace_pid { |
| 97 | struct list_head list; |
| 98 | struct pid *pid; |
| 99 | }; |
| 100 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 101 | /* |
| 102 | * ftrace_disabled is set when an anomaly is discovered. |
| 103 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 104 | */ |
| 105 | static int ftrace_disabled __read_mostly; |
| 106 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 107 | static DEFINE_MUTEX(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 108 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 109 | static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 110 | 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] | 111 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 112 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 113 | static struct ftrace_ops global_ops; |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 114 | static struct ftrace_ops control_ops; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 115 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 116 | static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip, |
| 117 | struct ftrace_ops *op, struct pt_regs *regs); |
| 118 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 119 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 120 | 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] | 121 | struct ftrace_ops *op, struct pt_regs *regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 122 | #else |
| 123 | /* See comment below, where ftrace_ops_list_func is defined */ |
| 124 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip); |
| 125 | #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops) |
| 126 | #endif |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 127 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 128 | /* |
| 129 | * Traverse the ftrace_global_list, invoking all entries. The reason that we |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 130 | * 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] | 131 | * 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] | 132 | * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 133 | * concurrent insertions into the ftrace_global_list. |
| 134 | * |
| 135 | * Silly Alpha and silly pointer-speculation compiler optimizations! |
| 136 | */ |
| 137 | #define do_for_each_ftrace_op(op, list) \ |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 138 | op = rcu_dereference_raw_notrace(list); \ |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 139 | do |
| 140 | |
| 141 | /* |
| 142 | * Optimized for just a single item in the list (as that is the normal case). |
| 143 | */ |
| 144 | #define while_for_each_ftrace_op(op) \ |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 145 | while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \ |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 146 | unlikely((op) != &ftrace_list_end)) |
| 147 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 148 | static inline void ftrace_ops_init(struct ftrace_ops *ops) |
| 149 | { |
| 150 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 151 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 152 | mutex_init(&ops->local_hash.regex_lock); |
| 153 | ops->func_hash = &ops->local_hash; |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 154 | ops->flags |= FTRACE_OPS_FL_INITIALIZED; |
| 155 | } |
| 156 | #endif |
| 157 | } |
| 158 | |
Steven Rostedt | ea701f1 | 2012-07-20 13:08:05 -0400 | [diff] [blame] | 159 | /** |
| 160 | * ftrace_nr_registered_ops - return number of ops registered |
| 161 | * |
| 162 | * Returns the number of ftrace_ops registered and tracing functions |
| 163 | */ |
| 164 | int ftrace_nr_registered_ops(void) |
| 165 | { |
| 166 | struct ftrace_ops *ops; |
| 167 | int cnt = 0; |
| 168 | |
| 169 | mutex_lock(&ftrace_lock); |
| 170 | |
| 171 | for (ops = ftrace_ops_list; |
| 172 | ops != &ftrace_list_end; ops = ops->next) |
| 173 | cnt++; |
| 174 | |
| 175 | mutex_unlock(&ftrace_lock); |
| 176 | |
| 177 | return cnt; |
| 178 | } |
| 179 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 180 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 181 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 182 | { |
Steven Rostedt | 0ef8cde | 2008-12-03 15:36:58 -0500 | [diff] [blame] | 183 | if (!test_tsk_trace_trace(current)) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 184 | return; |
| 185 | |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 186 | ftrace_pid_function(ip, parent_ip, op, regs); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static void set_ftrace_pid_function(ftrace_func_t func) |
| 190 | { |
| 191 | /* do not set ftrace_pid_function to itself! */ |
| 192 | if (func != ftrace_pid_func) |
| 193 | ftrace_pid_function = func; |
| 194 | } |
| 195 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 196 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 197 | * clear_ftrace_function - reset the ftrace function |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 198 | * |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 199 | * This NULLs the ftrace function and in essence stops |
| 200 | * tracing. There may be lag |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 201 | */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 202 | void clear_ftrace_function(void) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 203 | { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 204 | ftrace_trace_function = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 205 | ftrace_pid_function = ftrace_stub; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 206 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 207 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 208 | static void control_ops_disable_all(struct ftrace_ops *ops) |
| 209 | { |
| 210 | int cpu; |
| 211 | |
| 212 | for_each_possible_cpu(cpu) |
| 213 | *per_cpu_ptr(ops->disabled, cpu) = 1; |
| 214 | } |
| 215 | |
| 216 | static int control_ops_alloc(struct ftrace_ops *ops) |
| 217 | { |
| 218 | int __percpu *disabled; |
| 219 | |
| 220 | disabled = alloc_percpu(int); |
| 221 | if (!disabled) |
| 222 | return -ENOMEM; |
| 223 | |
| 224 | ops->disabled = disabled; |
| 225 | control_ops_disable_all(ops); |
| 226 | return 0; |
| 227 | } |
| 228 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 229 | static void ftrace_sync(struct work_struct *work) |
| 230 | { |
| 231 | /* |
| 232 | * This function is just a stub to implement a hard force |
| 233 | * of synchronize_sched(). This requires synchronizing |
| 234 | * tasks even in userspace and idle. |
| 235 | * |
| 236 | * Yes, function tracing is rude. |
| 237 | */ |
| 238 | } |
| 239 | |
| 240 | static void ftrace_sync_ipi(void *data) |
| 241 | { |
| 242 | /* Probably not needed, but do it anyway */ |
| 243 | smp_rmb(); |
| 244 | } |
| 245 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 246 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 247 | static void update_function_graph_func(void); |
| 248 | #else |
| 249 | static inline void update_function_graph_func(void) { } |
| 250 | #endif |
| 251 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 252 | static void update_ftrace_function(void) |
| 253 | { |
| 254 | ftrace_func_t func; |
| 255 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 256 | /* |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 257 | * Prepare the ftrace_ops that the arch callback will use. |
| 258 | * If there's only one ftrace_ops registered, the ftrace_ops_list |
| 259 | * will point to the ops we want. |
| 260 | */ |
| 261 | set_function_trace_op = ftrace_ops_list; |
| 262 | |
| 263 | /* If there's no ftrace_ops registered, just call the stub function */ |
| 264 | if (ftrace_ops_list == &ftrace_list_end) { |
| 265 | func = ftrace_stub; |
| 266 | |
| 267 | /* |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 268 | * 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] | 269 | * recursion safe and not dynamic and the arch supports passing ops, |
| 270 | * then have the mcount trampoline call the function directly. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 271 | */ |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 272 | } else if (ftrace_ops_list->next == &ftrace_list_end) { |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 273 | func = ftrace_ops_get_func(ftrace_ops_list); |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 274 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 275 | } else { |
| 276 | /* Just use the default ftrace_ops */ |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 277 | set_function_trace_op = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 278 | func = ftrace_ops_list_func; |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 279 | } |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 280 | |
Steven Rostedt (Red Hat) | 5f8bf2d2 | 2014-07-15 11:05:12 -0400 | [diff] [blame] | 281 | update_function_graph_func(); |
| 282 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 283 | /* If there's no change, then do nothing more here */ |
| 284 | if (ftrace_trace_function == func) |
| 285 | return; |
| 286 | |
| 287 | /* |
| 288 | * If we are using the list function, it doesn't care |
| 289 | * about the function_trace_ops. |
| 290 | */ |
| 291 | if (func == ftrace_ops_list_func) { |
| 292 | ftrace_trace_function = func; |
| 293 | /* |
| 294 | * Don't even bother setting function_trace_ops, |
| 295 | * it would be racy to do so anyway. |
| 296 | */ |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | #ifndef CONFIG_DYNAMIC_FTRACE |
| 301 | /* |
| 302 | * For static tracing, we need to be a bit more careful. |
| 303 | * The function change takes affect immediately. Thus, |
| 304 | * we need to coorditate the setting of the function_trace_ops |
| 305 | * with the setting of the ftrace_trace_function. |
| 306 | * |
| 307 | * Set the function to the list ops, which will call the |
| 308 | * function we want, albeit indirectly, but it handles the |
| 309 | * ftrace_ops and doesn't depend on function_trace_op. |
| 310 | */ |
| 311 | ftrace_trace_function = ftrace_ops_list_func; |
| 312 | /* |
| 313 | * Make sure all CPUs see this. Yes this is slow, but static |
| 314 | * tracing is slow and nasty to have enabled. |
| 315 | */ |
| 316 | schedule_on_each_cpu(ftrace_sync); |
| 317 | /* Now all cpus are using the list ops. */ |
| 318 | function_trace_op = set_function_trace_op; |
| 319 | /* Make sure the function_trace_op is visible on all CPUs */ |
| 320 | smp_wmb(); |
| 321 | /* Nasty way to force a rmb on all cpus */ |
| 322 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
| 323 | /* OK, we are all set to update the ftrace_trace_function now! */ |
| 324 | #endif /* !CONFIG_DYNAMIC_FTRACE */ |
| 325 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 326 | ftrace_trace_function = func; |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 327 | } |
| 328 | |
Jiaxing Wang | 7eea4fc | 2014-04-20 23:10:43 +0800 | [diff] [blame] | 329 | int using_ftrace_ops_list_func(void) |
| 330 | { |
| 331 | return ftrace_trace_function == ftrace_ops_list_func; |
| 332 | } |
| 333 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 334 | 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] | 335 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 336 | ops->next = *list; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 337 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 338 | * We are entering ops into the list but another |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 339 | * CPU might be walking that list. We need to make sure |
| 340 | * the ops->next pointer is valid before another CPU sees |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 341 | * the ops pointer included into the list. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 342 | */ |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 343 | rcu_assign_pointer(*list, ops); |
| 344 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 345 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 346 | static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
| 347 | { |
| 348 | struct ftrace_ops **p; |
| 349 | |
| 350 | /* |
| 351 | * If we are removing the last function, then simply point |
| 352 | * to the ftrace_stub. |
| 353 | */ |
| 354 | if (*list == ops && ops->next == &ftrace_list_end) { |
| 355 | *list = &ftrace_list_end; |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
| 360 | if (*p == ops) |
| 361 | break; |
| 362 | |
| 363 | if (*p != ops) |
| 364 | return -1; |
| 365 | |
| 366 | *p = (*p)->next; |
| 367 | return 0; |
| 368 | } |
| 369 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 370 | static void add_ftrace_list_ops(struct ftrace_ops **list, |
| 371 | struct ftrace_ops *main_ops, |
| 372 | struct ftrace_ops *ops) |
| 373 | { |
| 374 | int first = *list == &ftrace_list_end; |
| 375 | add_ftrace_ops(list, ops); |
| 376 | if (first) |
| 377 | add_ftrace_ops(&ftrace_ops_list, main_ops); |
| 378 | } |
| 379 | |
| 380 | static int remove_ftrace_list_ops(struct ftrace_ops **list, |
| 381 | struct ftrace_ops *main_ops, |
| 382 | struct ftrace_ops *ops) |
| 383 | { |
| 384 | int ret = remove_ftrace_ops(list, ops); |
| 385 | if (!ret && *list == &ftrace_list_end) |
| 386 | ret = remove_ftrace_ops(&ftrace_ops_list, main_ops); |
| 387 | return ret; |
| 388 | } |
| 389 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 390 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 391 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 392 | static int __register_ftrace_function(struct ftrace_ops *ops) |
| 393 | { |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 394 | if (ops->flags & FTRACE_OPS_FL_DELETED) |
| 395 | return -EINVAL; |
| 396 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 397 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 398 | return -EBUSY; |
| 399 | |
Masami Hiramatsu | 06aeaae | 2012-09-28 17:15:17 +0900 | [diff] [blame] | 400 | #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 401 | /* |
| 402 | * If the ftrace_ops specifies SAVE_REGS, then it only can be used |
| 403 | * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. |
| 404 | * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. |
| 405 | */ |
| 406 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && |
| 407 | !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) |
| 408 | return -EINVAL; |
| 409 | |
| 410 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) |
| 411 | ops->flags |= FTRACE_OPS_FL_SAVE_REGS; |
| 412 | #endif |
| 413 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 414 | if (!core_kernel_data((unsigned long)ops)) |
| 415 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; |
| 416 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 417 | if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 418 | if (control_ops_alloc(ops)) |
| 419 | return -ENOMEM; |
| 420 | add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops); |
Steven Rostedt (Red Hat) | fe578ba | 2014-11-13 19:40:56 -0500 | [diff] [blame] | 421 | /* The control_ops needs the trampoline update */ |
| 422 | ops = &control_ops; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 423 | } else |
| 424 | add_ftrace_ops(&ftrace_ops_list, ops); |
| 425 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 426 | ftrace_update_trampoline(ops); |
| 427 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 428 | if (ftrace_enabled) |
| 429 | update_ftrace_function(); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 430 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 434 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 435 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 436 | int ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 437 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 438 | if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) |
| 439 | return -EBUSY; |
| 440 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 441 | if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 442 | ret = remove_ftrace_list_ops(&ftrace_control_list, |
| 443 | &control_ops, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 444 | } else |
| 445 | ret = remove_ftrace_ops(&ftrace_ops_list, ops); |
| 446 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 447 | if (ret < 0) |
| 448 | return ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 449 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 450 | if (ftrace_enabled) |
| 451 | update_ftrace_function(); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 452 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 453 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 456 | static void ftrace_update_pid_func(void) |
| 457 | { |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 458 | /* Only do something if we are tracing something */ |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 459 | if (ftrace_trace_function == ftrace_stub) |
KOSAKI Motohiro | 10dd3eb | 2009-03-06 15:29:04 +0900 | [diff] [blame] | 460 | return; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 461 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 462 | update_ftrace_function(); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 463 | } |
| 464 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 465 | #ifdef CONFIG_FUNCTION_PROFILER |
| 466 | struct ftrace_profile { |
| 467 | struct hlist_node node; |
| 468 | unsigned long ip; |
| 469 | unsigned long counter; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 470 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 471 | unsigned long long time; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 472 | unsigned long long time_squared; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 473 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | struct ftrace_profile_page { |
| 477 | struct ftrace_profile_page *next; |
| 478 | unsigned long index; |
| 479 | struct ftrace_profile records[]; |
| 480 | }; |
| 481 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 482 | struct ftrace_profile_stat { |
| 483 | atomic_t disabled; |
| 484 | struct hlist_head *hash; |
| 485 | struct ftrace_profile_page *pages; |
| 486 | struct ftrace_profile_page *start; |
| 487 | struct tracer_stat stat; |
| 488 | }; |
| 489 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 490 | #define PROFILE_RECORDS_SIZE \ |
| 491 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) |
| 492 | |
| 493 | #define PROFILES_PER_PAGE \ |
| 494 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) |
| 495 | |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 496 | static int ftrace_profile_enabled __read_mostly; |
| 497 | |
| 498 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 499 | static DEFINE_MUTEX(ftrace_profile_lock); |
| 500 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 501 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 502 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 503 | #define FTRACE_PROFILE_HASH_BITS 10 |
| 504 | #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 505 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 506 | static void * |
| 507 | function_stat_next(void *v, int idx) |
| 508 | { |
| 509 | struct ftrace_profile *rec = v; |
| 510 | struct ftrace_profile_page *pg; |
| 511 | |
| 512 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
| 513 | |
| 514 | again: |
Li Zefan | 0296e42 | 2009-06-26 11:15:37 +0800 | [diff] [blame] | 515 | if (idx != 0) |
| 516 | rec++; |
| 517 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 518 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
| 519 | pg = pg->next; |
| 520 | if (!pg) |
| 521 | return NULL; |
| 522 | rec = &pg->records[0]; |
| 523 | if (!rec->counter) |
| 524 | goto again; |
| 525 | } |
| 526 | |
| 527 | return rec; |
| 528 | } |
| 529 | |
| 530 | static void *function_stat_start(struct tracer_stat *trace) |
| 531 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 532 | struct ftrace_profile_stat *stat = |
| 533 | container_of(trace, struct ftrace_profile_stat, stat); |
| 534 | |
| 535 | if (!stat || !stat->start) |
| 536 | return NULL; |
| 537 | |
| 538 | return function_stat_next(&stat->start->records[0], 0); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 541 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 542 | /* function graph compares on total time */ |
| 543 | static int function_stat_cmp(void *p1, void *p2) |
| 544 | { |
| 545 | struct ftrace_profile *a = p1; |
| 546 | struct ftrace_profile *b = p2; |
| 547 | |
| 548 | if (a->time < b->time) |
| 549 | return -1; |
| 550 | if (a->time > b->time) |
| 551 | return 1; |
| 552 | else |
| 553 | return 0; |
| 554 | } |
| 555 | #else |
| 556 | /* not function graph compares against hits */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 557 | static int function_stat_cmp(void *p1, void *p2) |
| 558 | { |
| 559 | struct ftrace_profile *a = p1; |
| 560 | struct ftrace_profile *b = p2; |
| 561 | |
| 562 | if (a->counter < b->counter) |
| 563 | return -1; |
| 564 | if (a->counter > b->counter) |
| 565 | return 1; |
| 566 | else |
| 567 | return 0; |
| 568 | } |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 569 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 570 | |
| 571 | static int function_stat_headers(struct seq_file *m) |
| 572 | { |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 573 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 574 | seq_puts(m, " Function " |
| 575 | "Hit Time Avg s^2\n" |
| 576 | " -------- " |
| 577 | "--- ---- --- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 578 | #else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 579 | seq_puts(m, " Function Hit\n" |
| 580 | " -------- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 581 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static int function_stat_show(struct seq_file *m, void *v) |
| 586 | { |
| 587 | struct ftrace_profile *rec = v; |
| 588 | char str[KSYM_SYMBOL_LEN]; |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 589 | int ret = 0; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 590 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 591 | static struct trace_seq s; |
| 592 | unsigned long long avg; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 593 | unsigned long long stddev; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 594 | #endif |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 595 | mutex_lock(&ftrace_profile_lock); |
| 596 | |
| 597 | /* we raced with function_profile_reset() */ |
| 598 | if (unlikely(rec->counter == 0)) { |
| 599 | ret = -EBUSY; |
| 600 | goto out; |
| 601 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 602 | |
| 603 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 604 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 605 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 606 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 607 | seq_puts(m, " "); |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 608 | avg = rec->time; |
| 609 | do_div(avg, rec->counter); |
| 610 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 611 | /* Sample standard deviation (s^2) */ |
| 612 | if (rec->counter <= 1) |
| 613 | stddev = 0; |
| 614 | else { |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 615 | /* |
| 616 | * Apply Welford's method: |
| 617 | * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) |
| 618 | */ |
| 619 | stddev = rec->counter * rec->time_squared - |
| 620 | rec->time * rec->time; |
| 621 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 622 | /* |
| 623 | * Divide only 1000 for ns^2 -> us^2 conversion. |
| 624 | * trace_print_graph_duration will divide 1000 again. |
| 625 | */ |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 626 | do_div(stddev, rec->counter * (rec->counter - 1) * 1000); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 629 | trace_seq_init(&s); |
| 630 | trace_print_graph_duration(rec->time, &s); |
| 631 | trace_seq_puts(&s, " "); |
| 632 | trace_print_graph_duration(avg, &s); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 633 | trace_seq_puts(&s, " "); |
| 634 | trace_print_graph_duration(stddev, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 635 | trace_print_seq(m, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 636 | #endif |
| 637 | seq_putc(m, '\n'); |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 638 | out: |
| 639 | mutex_unlock(&ftrace_profile_lock); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 640 | |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 641 | return ret; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 644 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 645 | { |
| 646 | struct ftrace_profile_page *pg; |
| 647 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 648 | pg = stat->pages = stat->start; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 649 | |
| 650 | while (pg) { |
| 651 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); |
| 652 | pg->index = 0; |
| 653 | pg = pg->next; |
| 654 | } |
| 655 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 656 | memset(stat->hash, 0, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 657 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
| 658 | } |
| 659 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 660 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 661 | { |
| 662 | struct ftrace_profile_page *pg; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 663 | int functions; |
| 664 | int pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 665 | int i; |
| 666 | |
| 667 | /* If we already allocated, do nothing */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 668 | if (stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 669 | return 0; |
| 670 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 671 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
| 672 | if (!stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 673 | return -ENOMEM; |
| 674 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 675 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 676 | functions = ftrace_update_tot_cnt; |
| 677 | #else |
| 678 | /* |
| 679 | * We do not know the number of functions that exist because |
| 680 | * dynamic tracing is what counts them. With past experience |
| 681 | * we have around 20K functions. That should be more than enough. |
| 682 | * It is highly unlikely we will execute every function in |
| 683 | * the kernel. |
| 684 | */ |
| 685 | functions = 20000; |
| 686 | #endif |
| 687 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 688 | pg = stat->start = stat->pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 689 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 690 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
| 691 | |
Namhyung Kim | 39e30cd | 2013-04-01 21:46:24 +0900 | [diff] [blame] | 692 | for (i = 1; i < pages; i++) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 693 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 694 | if (!pg->next) |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 695 | goto out_free; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 696 | pg = pg->next; |
| 697 | } |
| 698 | |
| 699 | return 0; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 700 | |
| 701 | out_free: |
| 702 | pg = stat->start; |
| 703 | while (pg) { |
| 704 | unsigned long tmp = (unsigned long)pg; |
| 705 | |
| 706 | pg = pg->next; |
| 707 | free_page(tmp); |
| 708 | } |
| 709 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 710 | stat->pages = NULL; |
| 711 | stat->start = NULL; |
| 712 | |
| 713 | return -ENOMEM; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 714 | } |
| 715 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 716 | static int ftrace_profile_init_cpu(int cpu) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 717 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 718 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 719 | int size; |
| 720 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 721 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 722 | |
| 723 | if (stat->hash) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 724 | /* If the profile is already created, simply reset it */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 725 | ftrace_profile_reset(stat); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * We are profiling all functions, but usually only a few thousand |
| 731 | * functions are hit. We'll make a hash of 1024 items. |
| 732 | */ |
| 733 | size = FTRACE_PROFILE_HASH_SIZE; |
| 734 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 735 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 736 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 737 | if (!stat->hash) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 738 | return -ENOMEM; |
| 739 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 740 | /* Preallocate the function profiling pages */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 741 | if (ftrace_profile_pages_init(stat) < 0) { |
| 742 | kfree(stat->hash); |
| 743 | stat->hash = NULL; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 744 | return -ENOMEM; |
| 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 750 | static int ftrace_profile_init(void) |
| 751 | { |
| 752 | int cpu; |
| 753 | int ret = 0; |
| 754 | |
Miao Xie | c4602c1 | 2013-12-16 15:20:01 +0800 | [diff] [blame] | 755 | for_each_possible_cpu(cpu) { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 756 | ret = ftrace_profile_init_cpu(cpu); |
| 757 | if (ret) |
| 758 | break; |
| 759 | } |
| 760 | |
| 761 | return ret; |
| 762 | } |
| 763 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 764 | /* interrupts must be disabled */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 765 | static struct ftrace_profile * |
| 766 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 767 | { |
| 768 | struct ftrace_profile *rec; |
| 769 | struct hlist_head *hhd; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 770 | unsigned long key; |
| 771 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 772 | key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 773 | hhd = &stat->hash[key]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 774 | |
| 775 | if (hlist_empty(hhd)) |
| 776 | return NULL; |
| 777 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 778 | hlist_for_each_entry_rcu_notrace(rec, hhd, node) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 779 | if (rec->ip == ip) |
| 780 | return rec; |
| 781 | } |
| 782 | |
| 783 | return NULL; |
| 784 | } |
| 785 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 786 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
| 787 | struct ftrace_profile *rec) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 788 | { |
| 789 | unsigned long key; |
| 790 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 791 | key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 792 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 793 | } |
| 794 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 795 | /* |
| 796 | * The memory is already allocated, this simply finds a new record to use. |
| 797 | */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 798 | static struct ftrace_profile * |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 799 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 800 | { |
| 801 | struct ftrace_profile *rec = NULL; |
| 802 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 803 | /* prevent recursion (from NMIs) */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 804 | if (atomic_inc_return(&stat->disabled) != 1) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 805 | goto out; |
| 806 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 807 | /* |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 808 | * Try to find the function again since an NMI |
| 809 | * could have added it |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 810 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 811 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 812 | if (rec) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 813 | goto out; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 814 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 815 | if (stat->pages->index == PROFILES_PER_PAGE) { |
| 816 | if (!stat->pages->next) |
| 817 | goto out; |
| 818 | stat->pages = stat->pages->next; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 819 | } |
| 820 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 821 | rec = &stat->pages->records[stat->pages->index++]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 822 | rec->ip = ip; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 823 | ftrace_add_profile(stat, rec); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 824 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 825 | out: |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 826 | atomic_dec(&stat->disabled); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 827 | |
| 828 | return rec; |
| 829 | } |
| 830 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 831 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 832 | function_profile_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 833 | struct ftrace_ops *ops, struct pt_regs *regs) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 834 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 835 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 836 | struct ftrace_profile *rec; |
| 837 | unsigned long flags; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 838 | |
| 839 | if (!ftrace_profile_enabled) |
| 840 | return; |
| 841 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 842 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 843 | |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 844 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 845 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 846 | goto out; |
| 847 | |
| 848 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 849 | if (!rec) { |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 850 | rec = ftrace_profile_alloc(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 851 | if (!rec) |
| 852 | goto out; |
| 853 | } |
| 854 | |
| 855 | rec->counter++; |
| 856 | out: |
| 857 | local_irq_restore(flags); |
| 858 | } |
| 859 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 860 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 861 | static int profile_graph_entry(struct ftrace_graph_ent *trace) |
| 862 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 863 | function_profile_call(trace->func, 0, NULL, NULL); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 864 | return 1; |
| 865 | } |
| 866 | |
| 867 | static void profile_graph_return(struct ftrace_graph_ret *trace) |
| 868 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 869 | struct ftrace_profile_stat *stat; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 870 | unsigned long long calltime; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 871 | struct ftrace_profile *rec; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 872 | unsigned long flags; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 873 | |
| 874 | local_irq_save(flags); |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 875 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 876 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 877 | goto out; |
| 878 | |
Steven Rostedt | 37e44bc | 2010-04-27 21:04:24 -0400 | [diff] [blame] | 879 | /* If the calltime was zero'd ignore it */ |
| 880 | if (!trace->calltime) |
| 881 | goto out; |
| 882 | |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 883 | calltime = trace->rettime - trace->calltime; |
| 884 | |
| 885 | if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) { |
| 886 | int index; |
| 887 | |
| 888 | index = trace->depth; |
| 889 | |
| 890 | /* Append this call time to the parent time to subtract */ |
| 891 | if (index) |
| 892 | current->ret_stack[index - 1].subtime += calltime; |
| 893 | |
| 894 | if (current->ret_stack[index].subtime < calltime) |
| 895 | calltime -= current->ret_stack[index].subtime; |
| 896 | else |
| 897 | calltime = 0; |
| 898 | } |
| 899 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 900 | rec = ftrace_find_profiled_func(stat, trace->func); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 901 | if (rec) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 902 | rec->time += calltime; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 903 | rec->time_squared += calltime * calltime; |
| 904 | } |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 905 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 906 | out: |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 907 | local_irq_restore(flags); |
| 908 | } |
| 909 | |
| 910 | static int register_ftrace_profiler(void) |
| 911 | { |
| 912 | return register_ftrace_graph(&profile_graph_return, |
| 913 | &profile_graph_entry); |
| 914 | } |
| 915 | |
| 916 | static void unregister_ftrace_profiler(void) |
| 917 | { |
| 918 | unregister_ftrace_graph(); |
| 919 | } |
| 920 | #else |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 921 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 922 | .func = function_profile_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 923 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 924 | INIT_OPS_HASH(ftrace_profile_ops) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 925 | }; |
| 926 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 927 | static int register_ftrace_profiler(void) |
| 928 | { |
| 929 | return register_ftrace_function(&ftrace_profile_ops); |
| 930 | } |
| 931 | |
| 932 | static void unregister_ftrace_profiler(void) |
| 933 | { |
| 934 | unregister_ftrace_function(&ftrace_profile_ops); |
| 935 | } |
| 936 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 937 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 938 | static ssize_t |
| 939 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 940 | size_t cnt, loff_t *ppos) |
| 941 | { |
| 942 | unsigned long val; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 943 | int ret; |
| 944 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 945 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 946 | if (ret) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 947 | return ret; |
| 948 | |
| 949 | val = !!val; |
| 950 | |
| 951 | mutex_lock(&ftrace_profile_lock); |
| 952 | if (ftrace_profile_enabled ^ val) { |
| 953 | if (val) { |
| 954 | ret = ftrace_profile_init(); |
| 955 | if (ret < 0) { |
| 956 | cnt = ret; |
| 957 | goto out; |
| 958 | } |
| 959 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 960 | ret = register_ftrace_profiler(); |
| 961 | if (ret < 0) { |
| 962 | cnt = ret; |
| 963 | goto out; |
| 964 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 965 | ftrace_profile_enabled = 1; |
| 966 | } else { |
| 967 | ftrace_profile_enabled = 0; |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 968 | /* |
| 969 | * unregister_ftrace_profiler calls stop_machine |
| 970 | * so this acts like an synchronize_sched. |
| 971 | */ |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 972 | unregister_ftrace_profiler(); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | out: |
| 976 | mutex_unlock(&ftrace_profile_lock); |
| 977 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 978 | *ppos += cnt; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 979 | |
| 980 | return cnt; |
| 981 | } |
| 982 | |
| 983 | static ssize_t |
| 984 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 985 | size_t cnt, loff_t *ppos) |
| 986 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 987 | char buf[64]; /* big enough to hold a number */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 988 | int r; |
| 989 | |
| 990 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); |
| 991 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 992 | } |
| 993 | |
| 994 | static const struct file_operations ftrace_profile_fops = { |
| 995 | .open = tracing_open_generic, |
| 996 | .read = ftrace_profile_read, |
| 997 | .write = ftrace_profile_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 998 | .llseek = default_llseek, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 999 | }; |
| 1000 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1001 | /* used to initialize the real stat files */ |
| 1002 | static struct tracer_stat function_stats __initdata = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 1003 | .name = "functions", |
| 1004 | .stat_start = function_stat_start, |
| 1005 | .stat_next = function_stat_next, |
| 1006 | .stat_cmp = function_stat_cmp, |
| 1007 | .stat_headers = function_stat_headers, |
| 1008 | .stat_show = function_stat_show |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1009 | }; |
| 1010 | |
Steven Rostedt | 6ab5d66 | 2009-06-04 00:55:45 -0400 | [diff] [blame] | 1011 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1012 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1013 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1014 | struct dentry *entry; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1015 | char *name; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1016 | int ret; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1017 | int cpu; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1018 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1019 | for_each_possible_cpu(cpu) { |
| 1020 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 1021 | |
| 1022 | /* allocate enough for function name + cpu number */ |
| 1023 | name = kmalloc(32, GFP_KERNEL); |
| 1024 | if (!name) { |
| 1025 | /* |
| 1026 | * The files created are permanent, if something happens |
| 1027 | * we still do not free memory. |
| 1028 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1029 | WARN(1, |
| 1030 | "Could not allocate stat file for cpu %d\n", |
| 1031 | cpu); |
| 1032 | return; |
| 1033 | } |
| 1034 | stat->stat = function_stats; |
| 1035 | snprintf(name, 32, "function%d", cpu); |
| 1036 | stat->stat.name = name; |
| 1037 | ret = register_stat_tracer(&stat->stat); |
| 1038 | if (ret) { |
| 1039 | WARN(1, |
| 1040 | "Could not register function stat for cpu %d\n", |
| 1041 | cpu); |
| 1042 | kfree(name); |
| 1043 | return; |
| 1044 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | entry = debugfs_create_file("function_profile_enabled", 0644, |
| 1048 | d_tracer, NULL, &ftrace_profile_fops); |
| 1049 | if (!entry) |
| 1050 | pr_warning("Could not create debugfs " |
| 1051 | "'function_profile_enabled' entry\n"); |
| 1052 | } |
| 1053 | |
| 1054 | #else /* CONFIG_FUNCTION_PROFILER */ |
Steven Rostedt | 6ab5d66 | 2009-06-04 00:55:45 -0400 | [diff] [blame] | 1055 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1056 | { |
| 1057 | } |
| 1058 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 1059 | |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1060 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
| 1061 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1062 | #ifdef CONFIG_DYNAMIC_FTRACE |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1063 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1064 | static struct ftrace_ops *removed_ops; |
| 1065 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 1066 | /* |
| 1067 | * Set when doing a global update, like enabling all recs or disabling them. |
| 1068 | * It is not set when just updating a single ftrace_ops. |
| 1069 | */ |
| 1070 | static bool update_all_ops; |
| 1071 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1072 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1073 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1074 | #endif |
| 1075 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1076 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; |
| 1077 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1078 | struct ftrace_func_probe { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1079 | struct hlist_node node; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1080 | struct ftrace_probe_ops *ops; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1081 | unsigned long flags; |
| 1082 | unsigned long ip; |
| 1083 | void *data; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 1084 | struct list_head free_list; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1085 | }; |
| 1086 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1087 | struct ftrace_func_entry { |
| 1088 | struct hlist_node hlist; |
| 1089 | unsigned long ip; |
| 1090 | }; |
| 1091 | |
| 1092 | struct ftrace_hash { |
| 1093 | unsigned long size_bits; |
| 1094 | struct hlist_head *buckets; |
| 1095 | unsigned long count; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1096 | struct rcu_head rcu; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1097 | }; |
| 1098 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1099 | /* |
| 1100 | * We make these constant because no one should touch them, |
| 1101 | * but they are used as the default "empty hash", to avoid allocating |
| 1102 | * it all the time. These are in a read only section such that if |
| 1103 | * anyone does try to modify it, it will cause an exception. |
| 1104 | */ |
| 1105 | static const struct hlist_head empty_buckets[1]; |
| 1106 | static const struct ftrace_hash empty_hash = { |
| 1107 | .buckets = (struct hlist_head *)empty_buckets, |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1108 | }; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1109 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1110 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 1111 | static struct ftrace_ops global_ops = { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1112 | .func = ftrace_stub, |
| 1113 | .local_hash.notrace_hash = EMPTY_HASH, |
| 1114 | .local_hash.filter_hash = EMPTY_HASH, |
| 1115 | INIT_OPS_HASH(global_ops) |
| 1116 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 1117 | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1118 | }; |
| 1119 | |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1120 | /* |
| 1121 | * This is used by __kernel_text_address() to return true if the |
Steven Rostedt (Red Hat) | 0af2649 | 2014-11-20 10:05:36 -0500 | [diff] [blame] | 1122 | * address is on a dynamically allocated trampoline that would |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1123 | * not return true for either core_kernel_text() or |
| 1124 | * is_module_text_address(). |
| 1125 | */ |
| 1126 | bool is_ftrace_trampoline(unsigned long addr) |
| 1127 | { |
| 1128 | struct ftrace_ops *op; |
| 1129 | bool ret = false; |
| 1130 | |
| 1131 | /* |
| 1132 | * Some of the ops may be dynamically allocated, |
| 1133 | * they are freed after a synchronize_sched(). |
| 1134 | */ |
| 1135 | preempt_disable_notrace(); |
| 1136 | |
| 1137 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1138 | /* |
| 1139 | * This is to check for dynamically allocated trampolines. |
| 1140 | * Trampolines that are in kernel text will have |
| 1141 | * core_kernel_text() return true. |
| 1142 | */ |
| 1143 | if (op->trampoline && op->trampoline_size) |
| 1144 | if (addr >= op->trampoline && |
| 1145 | addr < op->trampoline + op->trampoline_size) { |
| 1146 | ret = true; |
| 1147 | goto out; |
| 1148 | } |
| 1149 | } while_for_each_ftrace_op(op); |
| 1150 | |
| 1151 | out: |
| 1152 | preempt_enable_notrace(); |
| 1153 | |
| 1154 | return ret; |
| 1155 | } |
| 1156 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1157 | struct ftrace_page { |
| 1158 | struct ftrace_page *next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1159 | struct dyn_ftrace *records; |
Steven Rostedt | 431aa3f | 2009-01-06 12:43:01 -0500 | [diff] [blame] | 1160 | int index; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1161 | int size; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 1162 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1163 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1164 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1165 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1166 | |
| 1167 | /* estimate from running different kernels */ |
| 1168 | #define NR_TO_INIT 10000 |
| 1169 | |
| 1170 | static struct ftrace_page *ftrace_pages_start; |
| 1171 | static struct ftrace_page *ftrace_pages; |
| 1172 | |
Steven Rostedt (Red Hat) | 68f4096 | 2014-05-01 12:44:50 -0400 | [diff] [blame] | 1173 | static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash) |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1174 | { |
| 1175 | return !hash || !hash->count; |
| 1176 | } |
| 1177 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1178 | static struct ftrace_func_entry * |
| 1179 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1180 | { |
| 1181 | unsigned long key; |
| 1182 | struct ftrace_func_entry *entry; |
| 1183 | struct hlist_head *hhd; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1184 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1185 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1186 | return NULL; |
| 1187 | |
| 1188 | if (hash->size_bits > 0) |
| 1189 | key = hash_long(ip, hash->size_bits); |
| 1190 | else |
| 1191 | key = 0; |
| 1192 | |
| 1193 | hhd = &hash->buckets[key]; |
| 1194 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 1195 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1196 | if (entry->ip == ip) |
| 1197 | return entry; |
| 1198 | } |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1202 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1203 | struct ftrace_func_entry *entry) |
| 1204 | { |
| 1205 | struct hlist_head *hhd; |
| 1206 | unsigned long key; |
| 1207 | |
| 1208 | if (hash->size_bits) |
| 1209 | key = hash_long(entry->ip, hash->size_bits); |
| 1210 | else |
| 1211 | key = 0; |
| 1212 | |
| 1213 | hhd = &hash->buckets[key]; |
| 1214 | hlist_add_head(&entry->hlist, hhd); |
| 1215 | hash->count++; |
| 1216 | } |
| 1217 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1218 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1219 | { |
| 1220 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1221 | |
| 1222 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1223 | if (!entry) |
| 1224 | return -ENOMEM; |
| 1225 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1226 | entry->ip = ip; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1227 | __add_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static void |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1233 | free_hash_entry(struct ftrace_hash *hash, |
| 1234 | struct ftrace_func_entry *entry) |
| 1235 | { |
| 1236 | hlist_del(&entry->hlist); |
| 1237 | kfree(entry); |
| 1238 | hash->count--; |
| 1239 | } |
| 1240 | |
| 1241 | static void |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1242 | remove_hash_entry(struct ftrace_hash *hash, |
| 1243 | struct ftrace_func_entry *entry) |
| 1244 | { |
| 1245 | hlist_del(&entry->hlist); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1246 | hash->count--; |
| 1247 | } |
| 1248 | |
| 1249 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1250 | { |
| 1251 | struct hlist_head *hhd; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1252 | struct hlist_node *tn; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1253 | struct ftrace_func_entry *entry; |
| 1254 | int size = 1 << hash->size_bits; |
| 1255 | int i; |
| 1256 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1257 | if (!hash->count) |
| 1258 | return; |
| 1259 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1260 | for (i = 0; i < size; i++) { |
| 1261 | hhd = &hash->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1262 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1263 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1264 | } |
| 1265 | FTRACE_WARN_ON(hash->count); |
| 1266 | } |
| 1267 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1268 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1269 | { |
| 1270 | if (!hash || hash == EMPTY_HASH) |
| 1271 | return; |
| 1272 | ftrace_hash_clear(hash); |
| 1273 | kfree(hash->buckets); |
| 1274 | kfree(hash); |
| 1275 | } |
| 1276 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1277 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1278 | { |
| 1279 | struct ftrace_hash *hash; |
| 1280 | |
| 1281 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1282 | free_ftrace_hash(hash); |
| 1283 | } |
| 1284 | |
| 1285 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1286 | { |
| 1287 | if (!hash || hash == EMPTY_HASH) |
| 1288 | return; |
| 1289 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); |
| 1290 | } |
| 1291 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1292 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1293 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 1294 | ftrace_ops_init(ops); |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1295 | free_ftrace_hash(ops->func_hash->filter_hash); |
| 1296 | free_ftrace_hash(ops->func_hash->notrace_hash); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1299 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1300 | { |
| 1301 | struct ftrace_hash *hash; |
| 1302 | int size; |
| 1303 | |
| 1304 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1305 | if (!hash) |
| 1306 | return NULL; |
| 1307 | |
| 1308 | size = 1 << size_bits; |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1309 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1310 | |
| 1311 | if (!hash->buckets) { |
| 1312 | kfree(hash); |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | |
| 1316 | hash->size_bits = size_bits; |
| 1317 | |
| 1318 | return hash; |
| 1319 | } |
| 1320 | |
| 1321 | static struct ftrace_hash * |
| 1322 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1323 | { |
| 1324 | struct ftrace_func_entry *entry; |
| 1325 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1326 | int size; |
| 1327 | int ret; |
| 1328 | int i; |
| 1329 | |
| 1330 | new_hash = alloc_ftrace_hash(size_bits); |
| 1331 | if (!new_hash) |
| 1332 | return NULL; |
| 1333 | |
| 1334 | /* Empty hash? */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1335 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1336 | return new_hash; |
| 1337 | |
| 1338 | size = 1 << hash->size_bits; |
| 1339 | for (i = 0; i < size; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1340 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1341 | ret = add_hash_entry(new_hash, entry->ip); |
| 1342 | if (ret < 0) |
| 1343 | goto free_hash; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1348 | |
| 1349 | return new_hash; |
| 1350 | |
| 1351 | free_hash: |
| 1352 | free_ftrace_hash(new_hash); |
| 1353 | return NULL; |
| 1354 | } |
| 1355 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1356 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1357 | ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1358 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1359 | ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1360 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1361 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1362 | struct ftrace_hash *new_hash); |
| 1363 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1364 | static int |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1365 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1366 | struct ftrace_hash **dst, struct ftrace_hash *src) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1367 | { |
| 1368 | struct ftrace_func_entry *entry; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1369 | struct hlist_node *tn; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1370 | struct hlist_head *hhd; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1371 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1372 | int size = src->count; |
| 1373 | int bits = 0; |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1374 | int ret; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1375 | int i; |
| 1376 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1377 | /* Reject setting notrace hash on IPMODIFY ftrace_ops */ |
| 1378 | if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) |
| 1379 | return -EINVAL; |
| 1380 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1381 | /* |
| 1382 | * If the new source is empty, just free dst and assign it |
| 1383 | * the empty_hash. |
| 1384 | */ |
| 1385 | if (!src->count) { |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1386 | new_hash = EMPTY_HASH; |
| 1387 | goto update; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1388 | } |
| 1389 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1390 | /* |
| 1391 | * Make the hash size about 1/2 the # found |
| 1392 | */ |
| 1393 | for (size /= 2; size; size >>= 1) |
| 1394 | bits++; |
| 1395 | |
| 1396 | /* Don't allocate too much */ |
| 1397 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1398 | bits = FTRACE_HASH_MAX_BITS; |
| 1399 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1400 | new_hash = alloc_ftrace_hash(bits); |
| 1401 | if (!new_hash) |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1402 | return -ENOMEM; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1403 | |
| 1404 | size = 1 << src->size_bits; |
| 1405 | for (i = 0; i < size; i++) { |
| 1406 | hhd = &src->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1407 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1408 | remove_hash_entry(src, entry); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1409 | __add_hash_entry(new_hash, entry); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1413 | update: |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1414 | /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ |
| 1415 | if (enable) { |
| 1416 | /* IPMODIFY should be updated only when filter_hash updating */ |
| 1417 | ret = ftrace_hash_ipmodify_update(ops, new_hash); |
| 1418 | if (ret < 0) { |
| 1419 | free_ftrace_hash(new_hash); |
| 1420 | return ret; |
| 1421 | } |
| 1422 | } |
| 1423 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1424 | /* |
| 1425 | * Remove the current set, update the hash and add |
| 1426 | * them back. |
| 1427 | */ |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1428 | ftrace_hash_rec_disable_modify(ops, enable); |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1429 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1430 | rcu_assign_pointer(*dst, new_hash); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1431 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1432 | ftrace_hash_rec_enable_modify(ops, enable); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1433 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1434 | return 0; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1435 | } |
| 1436 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1437 | static bool hash_contains_ip(unsigned long ip, |
| 1438 | struct ftrace_ops_hash *hash) |
| 1439 | { |
| 1440 | /* |
| 1441 | * The function record is a match if it exists in the filter |
| 1442 | * hash and not in the notrace hash. Note, an emty hash is |
| 1443 | * considered a match for the filter hash, but an empty |
| 1444 | * notrace hash is considered not in the notrace hash. |
| 1445 | */ |
| 1446 | return (ftrace_hash_empty(hash->filter_hash) || |
| 1447 | ftrace_lookup_ip(hash->filter_hash, ip)) && |
| 1448 | (ftrace_hash_empty(hash->notrace_hash) || |
| 1449 | !ftrace_lookup_ip(hash->notrace_hash, ip)); |
| 1450 | } |
| 1451 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1452 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1453 | * Test the hashes for this ops to see if we want to call |
| 1454 | * the ops->func or not. |
| 1455 | * |
| 1456 | * It's a match if the ip is in the ops->filter_hash or |
| 1457 | * the filter_hash does not exist or is empty, |
| 1458 | * AND |
| 1459 | * the ip is not in the ops->notrace_hash. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 1460 | * |
| 1461 | * This needs to be called with preemption disabled as |
| 1462 | * the hashes are freed with call_rcu_sched(). |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1463 | */ |
| 1464 | static int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1465 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1466 | { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1467 | struct ftrace_ops_hash hash; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1468 | int ret; |
| 1469 | |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1470 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1471 | /* |
| 1472 | * There's a small race when adding ops that the ftrace handler |
| 1473 | * that wants regs, may be called without them. We can not |
| 1474 | * allow that handler to be called if regs is NULL. |
| 1475 | */ |
| 1476 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) |
| 1477 | return 0; |
| 1478 | #endif |
| 1479 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1480 | hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash); |
| 1481 | hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1482 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1483 | if (hash_contains_ip(ip, &hash)) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1484 | ret = 1; |
| 1485 | else |
| 1486 | ret = 0; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1487 | |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1492 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1493 | * you must use a goto. |
| 1494 | */ |
| 1495 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1496 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1497 | int _____i; \ |
| 1498 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1499 | rec = &pg->records[_____i]; |
| 1500 | |
| 1501 | #define while_for_each_ftrace_rec() \ |
| 1502 | } \ |
| 1503 | } |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 1504 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1505 | |
| 1506 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1507 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1508 | const struct dyn_ftrace *key = a; |
| 1509 | const struct dyn_ftrace *rec = b; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1510 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1511 | if (key->flags < rec->ip) |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1512 | return -1; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1513 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
| 1514 | return 1; |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1518 | static unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1519 | { |
| 1520 | struct ftrace_page *pg; |
| 1521 | struct dyn_ftrace *rec; |
| 1522 | struct dyn_ftrace key; |
| 1523 | |
| 1524 | key.ip = start; |
| 1525 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 1526 | |
| 1527 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1528 | if (end < pg->records[0].ip || |
| 1529 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 1530 | continue; |
| 1531 | rec = bsearch(&key, pg->records, pg->index, |
| 1532 | sizeof(struct dyn_ftrace), |
| 1533 | ftrace_cmp_recs); |
| 1534 | if (rec) |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1535 | return rec->ip; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1538 | return 0; |
| 1539 | } |
| 1540 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1541 | /** |
| 1542 | * ftrace_location - return true if the ip giving is a traced location |
| 1543 | * @ip: the instruction pointer to check |
| 1544 | * |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1545 | * 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] | 1546 | * That is, the instruction that is either a NOP or call to |
| 1547 | * the function tracer. It checks the ftrace internal tables to |
| 1548 | * determine if the address belongs or not. |
| 1549 | */ |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1550 | unsigned long ftrace_location(unsigned long ip) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1551 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1552 | return ftrace_location_range(ip, ip); |
| 1553 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1554 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1555 | /** |
| 1556 | * ftrace_text_reserved - return true if range contains an ftrace location |
| 1557 | * @start: start of range to search |
| 1558 | * @end: end of range to search (inclusive). @end points to the last byte to check. |
| 1559 | * |
| 1560 | * Returns 1 if @start and @end contains a ftrace location. |
| 1561 | * That is, the instruction that is either a NOP or call to |
| 1562 | * the function tracer. It checks the ftrace internal tables to |
| 1563 | * determine if the address belongs or not. |
| 1564 | */ |
Sasha Levin | d88471c | 2013-01-09 18:09:20 -0500 | [diff] [blame] | 1565 | int ftrace_text_reserved(const void *start, const void *end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1566 | { |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1567 | unsigned long ret; |
| 1568 | |
| 1569 | ret = ftrace_location_range((unsigned long)start, |
| 1570 | (unsigned long)end); |
| 1571 | |
| 1572 | return (int)!!ret; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1573 | } |
| 1574 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1575 | /* Test if ops registered to this rec needs regs */ |
| 1576 | static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) |
| 1577 | { |
| 1578 | struct ftrace_ops *ops; |
| 1579 | bool keep_regs = false; |
| 1580 | |
| 1581 | for (ops = ftrace_ops_list; |
| 1582 | ops != &ftrace_list_end; ops = ops->next) { |
| 1583 | /* pass rec in as regs to have non-NULL val */ |
| 1584 | if (ftrace_ops_test(ops, rec->ip, rec)) { |
| 1585 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1586 | keep_regs = true; |
| 1587 | break; |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | return keep_regs; |
| 1593 | } |
| 1594 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1595 | static void __ftrace_hash_rec_update(struct ftrace_ops *ops, |
| 1596 | int filter_hash, |
| 1597 | bool inc) |
| 1598 | { |
| 1599 | struct ftrace_hash *hash; |
| 1600 | struct ftrace_hash *other_hash; |
| 1601 | struct ftrace_page *pg; |
| 1602 | struct dyn_ftrace *rec; |
| 1603 | int count = 0; |
| 1604 | int all = 0; |
| 1605 | |
| 1606 | /* Only update if the ops has been registered */ |
| 1607 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1608 | return; |
| 1609 | |
| 1610 | /* |
| 1611 | * In the filter_hash case: |
| 1612 | * If the count is zero, we update all records. |
| 1613 | * Otherwise we just update the items in the hash. |
| 1614 | * |
| 1615 | * In the notrace_hash case: |
| 1616 | * We enable the update in the hash. |
| 1617 | * As disabling notrace means enabling the tracing, |
| 1618 | * and enabling notrace means disabling, the inc variable |
| 1619 | * gets inversed. |
| 1620 | */ |
| 1621 | if (filter_hash) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1622 | hash = ops->func_hash->filter_hash; |
| 1623 | other_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1624 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1625 | all = 1; |
| 1626 | } else { |
| 1627 | inc = !inc; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1628 | hash = ops->func_hash->notrace_hash; |
| 1629 | other_hash = ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1630 | /* |
| 1631 | * If the notrace hash has no items, |
| 1632 | * then there's nothing to do. |
| 1633 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1634 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1635 | return; |
| 1636 | } |
| 1637 | |
| 1638 | do_for_each_ftrace_rec(pg, rec) { |
| 1639 | int in_other_hash = 0; |
| 1640 | int in_hash = 0; |
| 1641 | int match = 0; |
| 1642 | |
| 1643 | if (all) { |
| 1644 | /* |
| 1645 | * Only the filter_hash affects all records. |
| 1646 | * Update if the record is not in the notrace hash. |
| 1647 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1648 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1649 | match = 1; |
| 1650 | } else { |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1651 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
| 1652 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1653 | |
| 1654 | /* |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1655 | * If filter_hash is set, we want to match all functions |
| 1656 | * that are in the hash but not in the other hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1657 | * |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1658 | * If filter_hash is not set, then we are decrementing. |
| 1659 | * That means we match anything that is in the hash |
| 1660 | * and also in the other_hash. That is, we need to turn |
| 1661 | * off functions in the other hash because they are disabled |
| 1662 | * by this hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1663 | */ |
| 1664 | if (filter_hash && in_hash && !in_other_hash) |
| 1665 | match = 1; |
| 1666 | else if (!filter_hash && in_hash && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1667 | (in_other_hash || ftrace_hash_empty(other_hash))) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1668 | match = 1; |
| 1669 | } |
| 1670 | if (!match) |
| 1671 | continue; |
| 1672 | |
| 1673 | if (inc) { |
| 1674 | rec->flags++; |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1675 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1676 | return; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1677 | |
| 1678 | /* |
| 1679 | * If there's only a single callback registered to a |
| 1680 | * function, and the ops has a trampoline registered |
| 1681 | * for it, then we can call it directly. |
| 1682 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1683 | if (ftrace_rec_count(rec) == 1 && ops->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1684 | rec->flags |= FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1685 | else |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1686 | /* |
| 1687 | * If we are adding another function callback |
| 1688 | * to this function, and the previous had a |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 1689 | * custom trampoline in use, then we need to go |
| 1690 | * back to the default trampoline. |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1691 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1692 | rec->flags &= ~FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1693 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1694 | /* |
| 1695 | * If any ops wants regs saved for this function |
| 1696 | * then all ops will get saved regs. |
| 1697 | */ |
| 1698 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 1699 | rec->flags |= FTRACE_FL_REGS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1700 | } else { |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1701 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1702 | return; |
| 1703 | rec->flags--; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1704 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1705 | /* |
| 1706 | * If the rec had REGS enabled and the ops that is |
| 1707 | * being removed had REGS set, then see if there is |
| 1708 | * still any ops for this record that wants regs. |
| 1709 | * If not, we can stop recording them. |
| 1710 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1711 | if (ftrace_rec_count(rec) > 0 && |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1712 | rec->flags & FTRACE_FL_REGS && |
| 1713 | ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1714 | if (!test_rec_ops_needs_regs(rec)) |
| 1715 | rec->flags &= ~FTRACE_FL_REGS; |
| 1716 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1717 | |
| 1718 | /* |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1719 | * If the rec had TRAMP enabled, then it needs to |
| 1720 | * be cleared. As TRAMP can only be enabled iff |
| 1721 | * there is only a single ops attached to it. |
| 1722 | * In otherwords, always disable it on decrementing. |
| 1723 | * In the future, we may set it if rec count is |
| 1724 | * decremented to one, and the ops that is left |
| 1725 | * has a trampoline. |
| 1726 | */ |
| 1727 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 1728 | |
| 1729 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1730 | * flags will be cleared in ftrace_check_record() |
| 1731 | * if rec count is zero. |
| 1732 | */ |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1733 | } |
| 1734 | count++; |
| 1735 | /* Shortcut, if we handled all records, we are done. */ |
| 1736 | if (!all && count == hash->count) |
| 1737 | return; |
| 1738 | } while_for_each_ftrace_rec(); |
| 1739 | } |
| 1740 | |
| 1741 | static void ftrace_hash_rec_disable(struct ftrace_ops *ops, |
| 1742 | int filter_hash) |
| 1743 | { |
| 1744 | __ftrace_hash_rec_update(ops, filter_hash, 0); |
| 1745 | } |
| 1746 | |
| 1747 | static void ftrace_hash_rec_enable(struct ftrace_ops *ops, |
| 1748 | int filter_hash) |
| 1749 | { |
| 1750 | __ftrace_hash_rec_update(ops, filter_hash, 1); |
| 1751 | } |
| 1752 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1753 | static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, |
| 1754 | int filter_hash, int inc) |
| 1755 | { |
| 1756 | struct ftrace_ops *op; |
| 1757 | |
| 1758 | __ftrace_hash_rec_update(ops, filter_hash, inc); |
| 1759 | |
| 1760 | if (ops->func_hash != &global_ops.local_hash) |
| 1761 | return; |
| 1762 | |
| 1763 | /* |
| 1764 | * If the ops shares the global_ops hash, then we need to update |
| 1765 | * all ops that are enabled and use this hash. |
| 1766 | */ |
| 1767 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1768 | /* Already done */ |
| 1769 | if (op == ops) |
| 1770 | continue; |
| 1771 | if (op->func_hash == &global_ops.local_hash) |
| 1772 | __ftrace_hash_rec_update(op, filter_hash, inc); |
| 1773 | } while_for_each_ftrace_op(op); |
| 1774 | } |
| 1775 | |
| 1776 | static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, |
| 1777 | int filter_hash) |
| 1778 | { |
| 1779 | ftrace_hash_rec_update_modify(ops, filter_hash, 0); |
| 1780 | } |
| 1781 | |
| 1782 | static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, |
| 1783 | int filter_hash) |
| 1784 | { |
| 1785 | ftrace_hash_rec_update_modify(ops, filter_hash, 1); |
| 1786 | } |
| 1787 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1788 | /* |
| 1789 | * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK |
| 1790 | * or no-needed to update, -EBUSY if it detects a conflict of the flag |
| 1791 | * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. |
| 1792 | * Note that old_hash and new_hash has below meanings |
| 1793 | * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) |
| 1794 | * - If the hash is EMPTY_HASH, it hits nothing |
| 1795 | * - Anything else hits the recs which match the hash entries. |
| 1796 | */ |
| 1797 | static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, |
| 1798 | struct ftrace_hash *old_hash, |
| 1799 | struct ftrace_hash *new_hash) |
| 1800 | { |
| 1801 | struct ftrace_page *pg; |
| 1802 | struct dyn_ftrace *rec, *end = NULL; |
| 1803 | int in_old, in_new; |
| 1804 | |
| 1805 | /* Only update if the ops has been registered */ |
| 1806 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1807 | return 0; |
| 1808 | |
| 1809 | if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 1810 | return 0; |
| 1811 | |
| 1812 | /* |
| 1813 | * Since the IPMODIFY is a very address sensitive action, we do not |
| 1814 | * allow ftrace_ops to set all functions to new hash. |
| 1815 | */ |
| 1816 | if (!new_hash || !old_hash) |
| 1817 | return -EINVAL; |
| 1818 | |
| 1819 | /* Update rec->flags */ |
| 1820 | do_for_each_ftrace_rec(pg, rec) { |
| 1821 | /* We need to update only differences of filter_hash */ |
| 1822 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1823 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1824 | if (in_old == in_new) |
| 1825 | continue; |
| 1826 | |
| 1827 | if (in_new) { |
| 1828 | /* New entries must ensure no others are using it */ |
| 1829 | if (rec->flags & FTRACE_FL_IPMODIFY) |
| 1830 | goto rollback; |
| 1831 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1832 | } else /* Removed entry */ |
| 1833 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1834 | } while_for_each_ftrace_rec(); |
| 1835 | |
| 1836 | return 0; |
| 1837 | |
| 1838 | rollback: |
| 1839 | end = rec; |
| 1840 | |
| 1841 | /* Roll back what we did above */ |
| 1842 | do_for_each_ftrace_rec(pg, rec) { |
| 1843 | if (rec == end) |
| 1844 | goto err_out; |
| 1845 | |
| 1846 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1847 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1848 | if (in_old == in_new) |
| 1849 | continue; |
| 1850 | |
| 1851 | if (in_new) |
| 1852 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1853 | else |
| 1854 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1855 | } while_for_each_ftrace_rec(); |
| 1856 | |
| 1857 | err_out: |
| 1858 | return -EBUSY; |
| 1859 | } |
| 1860 | |
| 1861 | static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) |
| 1862 | { |
| 1863 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1864 | |
| 1865 | if (ftrace_hash_empty(hash)) |
| 1866 | hash = NULL; |
| 1867 | |
| 1868 | return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash); |
| 1869 | } |
| 1870 | |
| 1871 | /* Disabling always succeeds */ |
| 1872 | static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) |
| 1873 | { |
| 1874 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1875 | |
| 1876 | if (ftrace_hash_empty(hash)) |
| 1877 | hash = NULL; |
| 1878 | |
| 1879 | __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH); |
| 1880 | } |
| 1881 | |
| 1882 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1883 | struct ftrace_hash *new_hash) |
| 1884 | { |
| 1885 | struct ftrace_hash *old_hash = ops->func_hash->filter_hash; |
| 1886 | |
| 1887 | if (ftrace_hash_empty(old_hash)) |
| 1888 | old_hash = NULL; |
| 1889 | |
| 1890 | if (ftrace_hash_empty(new_hash)) |
| 1891 | new_hash = NULL; |
| 1892 | |
| 1893 | return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash); |
| 1894 | } |
| 1895 | |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 1896 | static void print_ip_ins(const char *fmt, unsigned char *p) |
| 1897 | { |
| 1898 | int i; |
| 1899 | |
| 1900 | printk(KERN_CONT "%s", fmt); |
| 1901 | |
| 1902 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 1903 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 1904 | } |
| 1905 | |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1906 | static struct ftrace_ops * |
| 1907 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); |
| 1908 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1909 | /** |
| 1910 | * ftrace_bug - report and shutdown function tracer |
| 1911 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1912 | * @rec: The record that failed |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1913 | * |
| 1914 | * The arch code that enables or disables the function tracing |
| 1915 | * can call ftrace_bug() when it has detected a problem in |
| 1916 | * modifying the code. @failed should be one of either: |
| 1917 | * EFAULT - if the problem happens on reading the @ip address |
| 1918 | * EINVAL - if what is read at @ip is not what was expected |
| 1919 | * EPERM - if the problem happens on writting to the @ip address |
| 1920 | */ |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1921 | void ftrace_bug(int failed, struct dyn_ftrace *rec) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1922 | { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1923 | unsigned long ip = rec ? rec->ip : 0; |
| 1924 | |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1925 | switch (failed) { |
| 1926 | case -EFAULT: |
| 1927 | FTRACE_WARN_ON_ONCE(1); |
| 1928 | pr_info("ftrace faulted on modifying "); |
| 1929 | print_ip_sym(ip); |
| 1930 | break; |
| 1931 | case -EINVAL: |
| 1932 | FTRACE_WARN_ON_ONCE(1); |
| 1933 | pr_info("ftrace failed to modify "); |
| 1934 | print_ip_sym(ip); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1935 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1936 | pr_cont("\n"); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1937 | break; |
| 1938 | case -EPERM: |
| 1939 | FTRACE_WARN_ON_ONCE(1); |
| 1940 | pr_info("ftrace faulted on writing "); |
| 1941 | print_ip_sym(ip); |
| 1942 | break; |
| 1943 | default: |
| 1944 | FTRACE_WARN_ON_ONCE(1); |
| 1945 | pr_info("ftrace faulted on unknown error "); |
| 1946 | print_ip_sym(ip); |
| 1947 | } |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1948 | if (rec) { |
| 1949 | struct ftrace_ops *ops = NULL; |
| 1950 | |
| 1951 | pr_info("ftrace record flags: %lx\n", rec->flags); |
| 1952 | pr_cont(" (%ld)%s", ftrace_rec_count(rec), |
| 1953 | rec->flags & FTRACE_FL_REGS ? " R" : " "); |
| 1954 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 1955 | ops = ftrace_find_tramp_ops_any(rec); |
| 1956 | if (ops) |
| 1957 | pr_cont("\ttramp: %pS", |
| 1958 | (void *)ops->trampoline); |
| 1959 | else |
| 1960 | pr_cont("\ttramp: ERROR!"); |
| 1961 | |
| 1962 | } |
| 1963 | ip = ftrace_get_addr_curr(rec); |
| 1964 | pr_cont(" expected tramp: %lx\n", ip); |
| 1965 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1968 | 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] | 1969 | { |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1970 | unsigned long flag = 0UL; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1971 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1972 | /* |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1973 | * If we are updating calls: |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1974 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1975 | * If the record has a ref count, then we need to enable it |
| 1976 | * because someone is using it. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1977 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1978 | * Otherwise we make sure its disabled. |
| 1979 | * |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1980 | * If we are disabling calls, then disable all records that |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1981 | * are enabled. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1982 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1983 | if (enable && ftrace_rec_count(rec)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1984 | flag = FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1985 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1986 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1987 | * If enabling and the REGS flag does not match the REGS_EN, or |
| 1988 | * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore |
| 1989 | * this record. Set flags to fail the compare against ENABLED. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1990 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1991 | if (flag) { |
| 1992 | if (!(rec->flags & FTRACE_FL_REGS) != |
| 1993 | !(rec->flags & FTRACE_FL_REGS_EN)) |
| 1994 | flag |= FTRACE_FL_REGS; |
| 1995 | |
| 1996 | if (!(rec->flags & FTRACE_FL_TRAMP) != |
| 1997 | !(rec->flags & FTRACE_FL_TRAMP_EN)) |
| 1998 | flag |= FTRACE_FL_TRAMP; |
| 1999 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2000 | |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2001 | /* If the state of this record hasn't changed, then do nothing */ |
| 2002 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2003 | return FTRACE_UPDATE_IGNORE; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2004 | |
| 2005 | if (flag) { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2006 | /* Save off if rec is being enabled (for return value) */ |
| 2007 | flag ^= rec->flags & FTRACE_FL_ENABLED; |
| 2008 | |
| 2009 | if (update) { |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2010 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2011 | if (flag & FTRACE_FL_REGS) { |
| 2012 | if (rec->flags & FTRACE_FL_REGS) |
| 2013 | rec->flags |= FTRACE_FL_REGS_EN; |
| 2014 | else |
| 2015 | rec->flags &= ~FTRACE_FL_REGS_EN; |
| 2016 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2017 | if (flag & FTRACE_FL_TRAMP) { |
| 2018 | if (rec->flags & FTRACE_FL_TRAMP) |
| 2019 | rec->flags |= FTRACE_FL_TRAMP_EN; |
| 2020 | else |
| 2021 | rec->flags &= ~FTRACE_FL_TRAMP_EN; |
| 2022 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * If this record is being updated from a nop, then |
| 2027 | * return UPDATE_MAKE_CALL. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2028 | * Otherwise, |
| 2029 | * return UPDATE_MODIFY_CALL to tell the caller to convert |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2030 | * from the save regs, to a non-save regs function or |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2031 | * vice versa, or from a trampoline call. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2032 | */ |
| 2033 | if (flag & FTRACE_FL_ENABLED) |
| 2034 | return FTRACE_UPDATE_MAKE_CALL; |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2035 | |
| 2036 | return FTRACE_UPDATE_MODIFY_CALL; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2039 | if (update) { |
| 2040 | /* If there's no more users, clear all flags */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2041 | if (!ftrace_rec_count(rec)) |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2042 | rec->flags = 0; |
| 2043 | else |
| 2044 | /* Just disable the record (keep REGS state) */ |
| 2045 | rec->flags &= ~FTRACE_FL_ENABLED; |
| 2046 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2047 | |
| 2048 | return FTRACE_UPDATE_MAKE_NOP; |
| 2049 | } |
| 2050 | |
| 2051 | /** |
| 2052 | * ftrace_update_record, set a record that now is tracing or not |
| 2053 | * @rec: the record to update |
| 2054 | * @enable: set to 1 if the record is tracing, zero to force disable |
| 2055 | * |
| 2056 | * The records that represent all functions that can be traced need |
| 2057 | * to be updated when tracing has been enabled. |
| 2058 | */ |
| 2059 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) |
| 2060 | { |
| 2061 | return ftrace_check_record(rec, enable, 1); |
| 2062 | } |
| 2063 | |
| 2064 | /** |
| 2065 | * ftrace_test_record, check if the record has been enabled or not |
| 2066 | * @rec: the record to test |
| 2067 | * @enable: set to 1 to check if enabled, 0 if it is disabled |
| 2068 | * |
| 2069 | * The arch code may need to test if a record is already set to |
| 2070 | * tracing to determine how to modify the function code that it |
| 2071 | * represents. |
| 2072 | */ |
| 2073 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) |
| 2074 | { |
| 2075 | return ftrace_check_record(rec, enable, 0); |
| 2076 | } |
| 2077 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2078 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2079 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) |
| 2080 | { |
| 2081 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2082 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2083 | |
| 2084 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2085 | |
| 2086 | if (!op->trampoline) |
| 2087 | continue; |
| 2088 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2089 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2090 | return op; |
| 2091 | } while_for_each_ftrace_op(op); |
| 2092 | |
| 2093 | return NULL; |
| 2094 | } |
| 2095 | |
| 2096 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2097 | ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) |
| 2098 | { |
| 2099 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2100 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2101 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2102 | /* |
| 2103 | * Need to check removed ops first. |
| 2104 | * If they are being removed, and this rec has a tramp, |
| 2105 | * and this rec is in the ops list, then it would be the |
| 2106 | * one with the tramp. |
| 2107 | */ |
| 2108 | if (removed_ops) { |
| 2109 | if (hash_contains_ip(ip, &removed_ops->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2110 | return removed_ops; |
| 2111 | } |
| 2112 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2113 | /* |
| 2114 | * Need to find the current trampoline for a rec. |
| 2115 | * Now, a trampoline is only attached to a rec if there |
| 2116 | * was a single 'ops' attached to it. But this can be called |
| 2117 | * when we are adding another op to the rec or removing the |
| 2118 | * current one. Thus, if the op is being added, we can |
| 2119 | * ignore it because it hasn't attached itself to the rec |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2120 | * yet. |
| 2121 | * |
| 2122 | * If an ops is being modified (hooking to different functions) |
| 2123 | * then we don't care about the new functions that are being |
| 2124 | * added, just the old ones (that are probably being removed). |
| 2125 | * |
| 2126 | * If we are adding an ops to a function that already is using |
| 2127 | * a trampoline, it needs to be removed (trampolines are only |
| 2128 | * for single ops connected), then an ops that is not being |
| 2129 | * modified also needs to be checked. |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2130 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2131 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2132 | |
| 2133 | if (!op->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2134 | continue; |
| 2135 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2136 | /* |
| 2137 | * If the ops is being added, it hasn't gotten to |
| 2138 | * the point to be removed from this tree yet. |
| 2139 | */ |
| 2140 | if (op->flags & FTRACE_OPS_FL_ADDING) |
| 2141 | continue; |
| 2142 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2143 | |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2144 | /* |
| 2145 | * If the ops is being modified and is in the old |
| 2146 | * hash, then it is probably being removed from this |
| 2147 | * function. |
| 2148 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2149 | if ((op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2150 | hash_contains_ip(ip, &op->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2151 | return op; |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2152 | /* |
| 2153 | * If the ops is not being added or modified, and it's |
| 2154 | * in its normal filter hash, then this must be the one |
| 2155 | * we want! |
| 2156 | */ |
| 2157 | if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2158 | hash_contains_ip(ip, op->func_hash)) |
| 2159 | return op; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2160 | |
| 2161 | } while_for_each_ftrace_op(op); |
| 2162 | |
| 2163 | return NULL; |
| 2164 | } |
| 2165 | |
| 2166 | static struct ftrace_ops * |
| 2167 | ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) |
| 2168 | { |
| 2169 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2170 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2171 | |
| 2172 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2173 | /* pass rec in as regs to have non-NULL val */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2174 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2175 | return op; |
| 2176 | } while_for_each_ftrace_op(op); |
| 2177 | |
| 2178 | return NULL; |
| 2179 | } |
| 2180 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2181 | /** |
| 2182 | * ftrace_get_addr_new - Get the call address to set to |
| 2183 | * @rec: The ftrace record descriptor |
| 2184 | * |
| 2185 | * If the record has the FTRACE_FL_REGS set, that means that it |
| 2186 | * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS |
| 2187 | * is not not set, then it wants to convert to the normal callback. |
| 2188 | * |
| 2189 | * Returns the address of the trampoline to set to |
| 2190 | */ |
| 2191 | unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) |
| 2192 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2193 | struct ftrace_ops *ops; |
| 2194 | |
| 2195 | /* Trampolines take precedence over regs */ |
| 2196 | if (rec->flags & FTRACE_FL_TRAMP) { |
| 2197 | ops = ftrace_find_tramp_ops_new(rec); |
| 2198 | if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 2199 | pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n", |
| 2200 | (void *)rec->ip, (void *)rec->ip, rec->flags); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2201 | /* Ftrace is shutting down, return anything */ |
| 2202 | return (unsigned long)FTRACE_ADDR; |
| 2203 | } |
| 2204 | return ops->trampoline; |
| 2205 | } |
| 2206 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2207 | if (rec->flags & FTRACE_FL_REGS) |
| 2208 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2209 | else |
| 2210 | return (unsigned long)FTRACE_ADDR; |
| 2211 | } |
| 2212 | |
| 2213 | /** |
| 2214 | * ftrace_get_addr_curr - Get the call address that is already there |
| 2215 | * @rec: The ftrace record descriptor |
| 2216 | * |
| 2217 | * The FTRACE_FL_REGS_EN is set when the record already points to |
| 2218 | * a function that saves all the regs. Basically the '_EN' version |
| 2219 | * represents the current state of the function. |
| 2220 | * |
| 2221 | * Returns the address of the trampoline that is currently being called |
| 2222 | */ |
| 2223 | unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) |
| 2224 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2225 | struct ftrace_ops *ops; |
| 2226 | |
| 2227 | /* Trampolines take precedence over regs */ |
| 2228 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2229 | ops = ftrace_find_tramp_ops_curr(rec); |
| 2230 | if (FTRACE_WARN_ON(!ops)) { |
| 2231 | pr_warning("Bad trampoline accounting at: %p (%pS)\n", |
| 2232 | (void *)rec->ip, (void *)rec->ip); |
| 2233 | /* Ftrace is shutting down, return anything */ |
| 2234 | return (unsigned long)FTRACE_ADDR; |
| 2235 | } |
| 2236 | return ops->trampoline; |
| 2237 | } |
| 2238 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2239 | if (rec->flags & FTRACE_FL_REGS_EN) |
| 2240 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2241 | else |
| 2242 | return (unsigned long)FTRACE_ADDR; |
| 2243 | } |
| 2244 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2245 | static int |
| 2246 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
| 2247 | { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2248 | unsigned long ftrace_old_addr; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2249 | unsigned long ftrace_addr; |
| 2250 | int ret; |
| 2251 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2252 | ftrace_addr = ftrace_get_addr_new(rec); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2253 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2254 | /* This needs to be done before we call ftrace_update_record */ |
| 2255 | ftrace_old_addr = ftrace_get_addr_curr(rec); |
| 2256 | |
| 2257 | ret = ftrace_update_record(rec, enable); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2258 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2259 | switch (ret) { |
| 2260 | case FTRACE_UPDATE_IGNORE: |
| 2261 | return 0; |
| 2262 | |
| 2263 | case FTRACE_UPDATE_MAKE_CALL: |
| 2264 | return ftrace_make_call(rec, ftrace_addr); |
| 2265 | |
| 2266 | case FTRACE_UPDATE_MAKE_NOP: |
Steven Rostedt (Red Hat) | 39b5552 | 2014-08-17 20:59:10 -0400 | [diff] [blame] | 2267 | return ftrace_make_nop(NULL, rec, ftrace_old_addr); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2268 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2269 | case FTRACE_UPDATE_MODIFY_CALL: |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2270 | return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | return -1; /* unknow ftrace bug */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2276 | void __weak ftrace_replace_code(int enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2277 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2278 | struct dyn_ftrace *rec; |
| 2279 | struct ftrace_page *pg; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2280 | int failed; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2281 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2282 | if (unlikely(ftrace_disabled)) |
| 2283 | return; |
| 2284 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2285 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2286 | failed = __ftrace_replace_code(rec, enable); |
Zhaolei | fa9d13c | 2009-03-13 17:16:34 +0800 | [diff] [blame] | 2287 | if (failed) { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2288 | ftrace_bug(failed, rec); |
Steven Rostedt | 3279ba3 | 2009-10-07 16:57:56 -0400 | [diff] [blame] | 2289 | /* Stop processing */ |
| 2290 | return; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2291 | } |
| 2292 | } while_for_each_ftrace_rec(); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2293 | } |
| 2294 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2295 | struct ftrace_rec_iter { |
| 2296 | struct ftrace_page *pg; |
| 2297 | int index; |
| 2298 | }; |
| 2299 | |
| 2300 | /** |
| 2301 | * ftrace_rec_iter_start, start up iterating over traced functions |
| 2302 | * |
| 2303 | * Returns an iterator handle that is used to iterate over all |
| 2304 | * the records that represent address locations where functions |
| 2305 | * are traced. |
| 2306 | * |
| 2307 | * May return NULL if no records are available. |
| 2308 | */ |
| 2309 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 2310 | { |
| 2311 | /* |
| 2312 | * We only use a single iterator. |
| 2313 | * Protected by the ftrace_lock mutex. |
| 2314 | */ |
| 2315 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 2316 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 2317 | |
| 2318 | iter->pg = ftrace_pages_start; |
| 2319 | iter->index = 0; |
| 2320 | |
| 2321 | /* Could have empty pages */ |
| 2322 | while (iter->pg && !iter->pg->index) |
| 2323 | iter->pg = iter->pg->next; |
| 2324 | |
| 2325 | if (!iter->pg) |
| 2326 | return NULL; |
| 2327 | |
| 2328 | return iter; |
| 2329 | } |
| 2330 | |
| 2331 | /** |
| 2332 | * ftrace_rec_iter_next, get the next record to process. |
| 2333 | * @iter: The handle to the iterator. |
| 2334 | * |
| 2335 | * Returns the next iterator after the given iterator @iter. |
| 2336 | */ |
| 2337 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 2338 | { |
| 2339 | iter->index++; |
| 2340 | |
| 2341 | if (iter->index >= iter->pg->index) { |
| 2342 | iter->pg = iter->pg->next; |
| 2343 | iter->index = 0; |
| 2344 | |
| 2345 | /* Could have empty pages */ |
| 2346 | while (iter->pg && !iter->pg->index) |
| 2347 | iter->pg = iter->pg->next; |
| 2348 | } |
| 2349 | |
| 2350 | if (!iter->pg) |
| 2351 | return NULL; |
| 2352 | |
| 2353 | return iter; |
| 2354 | } |
| 2355 | |
| 2356 | /** |
| 2357 | * ftrace_rec_iter_record, get the record at the iterator location |
| 2358 | * @iter: The current iterator location |
| 2359 | * |
| 2360 | * Returns the record that the current @iter is at. |
| 2361 | */ |
| 2362 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 2363 | { |
| 2364 | return &iter->pg->records[iter->index]; |
| 2365 | } |
| 2366 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2367 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2368 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2369 | { |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2370 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2371 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2372 | if (unlikely(ftrace_disabled)) |
| 2373 | return 0; |
| 2374 | |
Shaohua Li | 25aac9d | 2009-01-09 11:29:40 +0800 | [diff] [blame] | 2375 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2376 | if (ret) { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2377 | ftrace_bug(ret, rec); |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2378 | return 0; |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2379 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2380 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2383 | /* |
| 2384 | * archs can override this function if they must do something |
| 2385 | * before the modifying code is performed. |
| 2386 | */ |
| 2387 | int __weak ftrace_arch_code_modify_prepare(void) |
| 2388 | { |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | * archs can override this function if they must do something |
| 2394 | * after the modifying code is performed. |
| 2395 | */ |
| 2396 | int __weak ftrace_arch_code_modify_post_process(void) |
| 2397 | { |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2401 | void ftrace_modify_all_code(int command) |
| 2402 | { |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2403 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2404 | int err = 0; |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2405 | |
| 2406 | /* |
| 2407 | * If the ftrace_caller calls a ftrace_ops func directly, |
| 2408 | * we need to make sure that it only traces functions it |
| 2409 | * expects to trace. When doing the switch of functions, |
| 2410 | * we need to update to the ftrace_ops_list_func first |
| 2411 | * before the transition between old and new calls are set, |
| 2412 | * as the ftrace_ops_list_func will check the ops hashes |
| 2413 | * to make sure the ops are having the right functions |
| 2414 | * traced. |
| 2415 | */ |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2416 | if (update) { |
| 2417 | err = ftrace_update_ftrace_func(ftrace_ops_list_func); |
| 2418 | if (FTRACE_WARN_ON(err)) |
| 2419 | return; |
| 2420 | } |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2421 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2422 | if (command & FTRACE_UPDATE_CALLS) |
| 2423 | ftrace_replace_code(1); |
| 2424 | else if (command & FTRACE_DISABLE_CALLS) |
| 2425 | ftrace_replace_code(0); |
| 2426 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2427 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
| 2428 | function_trace_op = set_function_trace_op; |
| 2429 | smp_wmb(); |
| 2430 | /* If irqs are disabled, we are in stop machine */ |
| 2431 | if (!irqs_disabled()) |
| 2432 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2433 | err = ftrace_update_ftrace_func(ftrace_trace_function); |
| 2434 | if (FTRACE_WARN_ON(err)) |
| 2435 | return; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2436 | } |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2437 | |
| 2438 | if (command & FTRACE_START_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2439 | err = ftrace_enable_ftrace_graph_caller(); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2440 | else if (command & FTRACE_STOP_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2441 | err = ftrace_disable_ftrace_graph_caller(); |
| 2442 | FTRACE_WARN_ON(err); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2443 | } |
| 2444 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2445 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2446 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2447 | int *command = data; |
| 2448 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2449 | ftrace_modify_all_code(*command); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 2450 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2451 | return 0; |
| 2452 | } |
| 2453 | |
| 2454 | /** |
| 2455 | * ftrace_run_stop_machine, go back to the stop machine method |
| 2456 | * @command: The command to tell ftrace what to do |
| 2457 | * |
| 2458 | * If an arch needs to fall back to the stop machine method, the |
| 2459 | * it can call this function. |
| 2460 | */ |
| 2461 | void ftrace_run_stop_machine(int command) |
| 2462 | { |
| 2463 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 2464 | } |
| 2465 | |
| 2466 | /** |
| 2467 | * arch_ftrace_update_code, modify the code to trace or not trace |
| 2468 | * @command: The command that needs to be done |
| 2469 | * |
| 2470 | * Archs can override this function if it does not need to |
| 2471 | * run stop_machine() to modify code. |
| 2472 | */ |
| 2473 | void __weak arch_ftrace_update_code(int command) |
| 2474 | { |
| 2475 | ftrace_run_stop_machine(command); |
| 2476 | } |
| 2477 | |
| 2478 | static void ftrace_run_update_code(int command) |
| 2479 | { |
| 2480 | int ret; |
| 2481 | |
| 2482 | ret = ftrace_arch_code_modify_prepare(); |
| 2483 | FTRACE_WARN_ON(ret); |
| 2484 | if (ret) |
| 2485 | return; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2486 | |
| 2487 | /* |
| 2488 | * By default we use stop_machine() to modify the code. |
| 2489 | * But archs can do what ever they want as long as it |
| 2490 | * is safe. The stop_machine() is the safest, but also |
| 2491 | * produces the most overhead. |
| 2492 | */ |
| 2493 | arch_ftrace_update_code(command); |
| 2494 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2495 | ret = ftrace_arch_code_modify_post_process(); |
| 2496 | FTRACE_WARN_ON(ret); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2499 | static void ftrace_run_modify_code(struct ftrace_ops *ops, int command, |
| 2500 | struct ftrace_hash *old_hash) |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2501 | { |
| 2502 | ops->flags |= FTRACE_OPS_FL_MODIFYING; |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2503 | ops->old_hash.filter_hash = old_hash; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2504 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2505 | ops->old_hash.filter_hash = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2506 | ops->flags &= ~FTRACE_OPS_FL_MODIFYING; |
| 2507 | } |
| 2508 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2509 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2510 | static int ftrace_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2511 | |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2512 | void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) |
| 2513 | { |
| 2514 | } |
| 2515 | |
Jiri Slaby | db0fbad | 2014-03-10 21:42:11 +0100 | [diff] [blame] | 2516 | static void control_ops_free(struct ftrace_ops *ops) |
| 2517 | { |
| 2518 | free_percpu(ops->disabled); |
| 2519 | } |
| 2520 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2521 | static void ftrace_startup_enable(int command) |
| 2522 | { |
| 2523 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2524 | saved_ftrace_func = ftrace_trace_function; |
| 2525 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2526 | } |
| 2527 | |
| 2528 | if (!command || !ftrace_enabled) |
| 2529 | return; |
| 2530 | |
| 2531 | ftrace_run_update_code(command); |
| 2532 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2533 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2534 | static void ftrace_startup_all(int command) |
| 2535 | { |
| 2536 | update_all_ops = true; |
| 2537 | ftrace_startup_enable(command); |
| 2538 | update_all_ops = false; |
| 2539 | } |
| 2540 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2541 | static int ftrace_startup(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2542 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2543 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2544 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2545 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2546 | return -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2547 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2548 | ret = __register_ftrace_function(ops); |
| 2549 | if (ret) |
| 2550 | return ret; |
| 2551 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2552 | ftrace_start_up++; |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2553 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2554 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2555 | /* |
| 2556 | * Note that ftrace probes uses this to start up |
| 2557 | * and modify functions it will probe. But we still |
| 2558 | * set the ADDING flag for modification, as probes |
| 2559 | * do not have trampolines. If they add them in the |
| 2560 | * future, then the probes will need to distinguish |
| 2561 | * between adding and updating probes. |
| 2562 | */ |
| 2563 | ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; |
Steven Rostedt (Red Hat) | 66209a5 | 2014-05-06 21:57:49 -0400 | [diff] [blame] | 2564 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2565 | ret = ftrace_hash_ipmodify_enable(ops); |
| 2566 | if (ret < 0) { |
| 2567 | /* Rollback registration process */ |
| 2568 | __unregister_ftrace_function(ops); |
| 2569 | ftrace_start_up--; |
| 2570 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 2571 | return ret; |
| 2572 | } |
| 2573 | |
Steven Rostedt (Red Hat) | 66209a5 | 2014-05-06 21:57:49 -0400 | [diff] [blame] | 2574 | ftrace_hash_rec_enable(ops, 1); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2575 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2576 | ftrace_startup_enable(command); |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2577 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2578 | ops->flags &= ~FTRACE_OPS_FL_ADDING; |
| 2579 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2580 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2583 | static int ftrace_shutdown(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2584 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2585 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2586 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2587 | if (unlikely(ftrace_disabled)) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2588 | return -ENODEV; |
| 2589 | |
| 2590 | ret = __unregister_ftrace_function(ops); |
| 2591 | if (ret) |
| 2592 | return ret; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2593 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2594 | ftrace_start_up--; |
Frederic Weisbecker | 9ea1a15 | 2009-06-20 06:52:21 +0200 | [diff] [blame] | 2595 | /* |
| 2596 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 2597 | * critical but the ftrace_call callers may be never nopped again after |
| 2598 | * further ftrace uses. |
| 2599 | */ |
| 2600 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 2601 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2602 | /* Disabling ipmodify never fails */ |
| 2603 | ftrace_hash_ipmodify_disable(ops); |
Steven Rostedt (Red Hat) | 66209a5 | 2014-05-06 21:57:49 -0400 | [diff] [blame] | 2604 | ftrace_hash_rec_disable(ops, 1); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2605 | |
Namhyung Kim | a737e6d | 2014-06-12 23:56:12 +0900 | [diff] [blame] | 2606 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2607 | |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2608 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2609 | |
| 2610 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2611 | saved_ftrace_func = ftrace_trace_function; |
| 2612 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2613 | } |
| 2614 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2615 | if (!command || !ftrace_enabled) { |
| 2616 | /* |
| 2617 | * If these are control ops, they still need their |
| 2618 | * per_cpu field freed. Since, function tracing is |
| 2619 | * not currently active, we can just free them |
| 2620 | * without synchronizing all CPUs. |
| 2621 | */ |
| 2622 | if (ops->flags & FTRACE_OPS_FL_CONTROL) |
| 2623 | control_ops_free(ops); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2624 | return 0; |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2625 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2626 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2627 | /* |
| 2628 | * If the ops uses a trampoline, then it needs to be |
| 2629 | * tested first on update. |
| 2630 | */ |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2631 | ops->flags |= FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2632 | removed_ops = ops; |
| 2633 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2634 | /* The trampoline logic checks the old hashes */ |
| 2635 | ops->old_hash.filter_hash = ops->func_hash->filter_hash; |
| 2636 | ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; |
| 2637 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2638 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2639 | |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2640 | /* |
| 2641 | * If there's no more ops registered with ftrace, run a |
| 2642 | * sanity check to make sure all rec flags are cleared. |
| 2643 | */ |
| 2644 | if (ftrace_ops_list == &ftrace_list_end) { |
| 2645 | struct ftrace_page *pg; |
| 2646 | struct dyn_ftrace *rec; |
| 2647 | |
| 2648 | do_for_each_ftrace_rec(pg, rec) { |
| 2649 | if (FTRACE_WARN_ON_ONCE(rec->flags)) |
| 2650 | pr_warn(" %pS flags:%lx\n", |
| 2651 | (void *)rec->ip, rec->flags); |
| 2652 | } while_for_each_ftrace_rec(); |
| 2653 | } |
| 2654 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2655 | ops->old_hash.filter_hash = NULL; |
| 2656 | ops->old_hash.notrace_hash = NULL; |
| 2657 | |
| 2658 | removed_ops = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2659 | ops->flags &= ~FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2660 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2661 | /* |
| 2662 | * Dynamic ops may be freed, we must make sure that all |
| 2663 | * callers are done before leaving this function. |
| 2664 | * The same goes for freeing the per_cpu data of the control |
| 2665 | * ops. |
| 2666 | * |
| 2667 | * Again, normal synchronize_sched() is not good enough. |
| 2668 | * We need to do a hard force of sched synchronization. |
| 2669 | * This is because we use preempt_disable() to do RCU, but |
| 2670 | * the function tracers can be called where RCU is not watching |
| 2671 | * (like before user_exit()). We can not rely on the RCU |
| 2672 | * infrastructure to do the synchronization, thus we must do it |
| 2673 | * ourselves. |
| 2674 | */ |
| 2675 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) { |
| 2676 | schedule_on_each_cpu(ftrace_sync); |
| 2677 | |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2678 | arch_ftrace_trampoline_free(ops); |
| 2679 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2680 | if (ops->flags & FTRACE_OPS_FL_CONTROL) |
| 2681 | control_ops_free(ops); |
| 2682 | } |
| 2683 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2684 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2687 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2688 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2689 | if (unlikely(ftrace_disabled)) |
| 2690 | return; |
| 2691 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2692 | /* Force update next time */ |
| 2693 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2694 | /* ftrace_start_up is true if we want ftrace running */ |
| 2695 | if (ftrace_start_up) |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2696 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2697 | } |
| 2698 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2699 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2700 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2701 | if (unlikely(ftrace_disabled)) |
| 2702 | return; |
| 2703 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2704 | /* ftrace_start_up is true if ftrace is running */ |
| 2705 | if (ftrace_start_up) |
Steven Rostedt | 79e406d | 2010-09-14 22:19:46 -0400 | [diff] [blame] | 2706 | ftrace_run_update_code(FTRACE_DISABLE_CALLS); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2707 | } |
| 2708 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2709 | static cycle_t ftrace_update_time; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2710 | unsigned long ftrace_update_tot_cnt; |
| 2711 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2712 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2713 | { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2714 | /* |
| 2715 | * Filter_hash being empty will default to trace module. |
| 2716 | * But notrace hash requires a test of individual module functions. |
| 2717 | */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2718 | return ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2719 | ftrace_hash_empty(ops->func_hash->notrace_hash); |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2720 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2721 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2722 | /* |
| 2723 | * Check if the current ops references the record. |
| 2724 | * |
| 2725 | * If the ops traces all functions, then it was already accounted for. |
| 2726 | * If the ops does not trace the current record function, skip it. |
| 2727 | * If the ops ignores the function via notrace filter, skip it. |
| 2728 | */ |
| 2729 | static inline bool |
| 2730 | ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 2731 | { |
| 2732 | /* If ops isn't enabled, ignore it */ |
| 2733 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 2734 | return 0; |
| 2735 | |
| 2736 | /* If ops traces all mods, we already accounted for it */ |
| 2737 | if (ops_traces_mod(ops)) |
| 2738 | return 0; |
| 2739 | |
| 2740 | /* The function must be in the filter */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2741 | if (!ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2742 | !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2743 | return 0; |
| 2744 | |
| 2745 | /* If in notrace hash, we ignore it too */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2746 | if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2747 | return 0; |
| 2748 | |
| 2749 | return 1; |
| 2750 | } |
| 2751 | |
| 2752 | static int referenced_filters(struct dyn_ftrace *rec) |
| 2753 | { |
| 2754 | struct ftrace_ops *ops; |
| 2755 | int cnt = 0; |
| 2756 | |
| 2757 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { |
| 2758 | if (ops_references_rec(ops, rec)) |
| 2759 | cnt++; |
| 2760 | } |
| 2761 | |
| 2762 | return cnt; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2763 | } |
| 2764 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2765 | 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] | 2766 | { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2767 | struct ftrace_page *pg; |
Lai Jiangshan | e94142a | 2009-03-13 17:51:27 +0800 | [diff] [blame] | 2768 | struct dyn_ftrace *p; |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2769 | cycle_t start, stop; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2770 | unsigned long update_cnt = 0; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2771 | unsigned long ref = 0; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2772 | bool test = false; |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2773 | int i; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2774 | |
| 2775 | /* |
| 2776 | * When adding a module, we need to check if tracers are |
| 2777 | * currently enabled and if they are set to trace all functions. |
| 2778 | * If they are, we need to enable the module functions as well |
| 2779 | * as update the reference counts for those function records. |
| 2780 | */ |
| 2781 | if (mod) { |
| 2782 | struct ftrace_ops *ops; |
| 2783 | |
| 2784 | for (ops = ftrace_ops_list; |
| 2785 | ops != &ftrace_list_end; ops = ops->next) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2786 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
| 2787 | if (ops_traces_mod(ops)) |
| 2788 | ref++; |
| 2789 | else |
| 2790 | test = true; |
| 2791 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2792 | } |
| 2793 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2794 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2795 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2796 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2797 | for (pg = new_pgs; pg; pg = pg->next) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2798 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2799 | for (i = 0; i < pg->index; i++) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2800 | int cnt = ref; |
| 2801 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2802 | /* If something went wrong, bail without enabling anything */ |
| 2803 | if (unlikely(ftrace_disabled)) |
| 2804 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2805 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2806 | p = &pg->records[i]; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2807 | if (test) |
| 2808 | cnt += referenced_filters(p); |
| 2809 | p->flags = cnt; |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 2810 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2811 | /* |
| 2812 | * Do the initial record conversion from mcount jump |
| 2813 | * to the NOP instructions. |
| 2814 | */ |
| 2815 | if (!ftrace_code_disable(mod, p)) |
| 2816 | break; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2817 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2818 | update_cnt++; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2819 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2820 | /* |
| 2821 | * If the tracing is enabled, go ahead and enable the record. |
| 2822 | * |
| 2823 | * The reason not to enable the record immediatelly is the |
| 2824 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 2825 | * correct previous instructions. Making first the NOP |
| 2826 | * conversion puts the module to the correct state, thus |
| 2827 | * passing the ftrace_make_call check. |
| 2828 | */ |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2829 | if (ftrace_start_up && cnt) { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2830 | int failed = __ftrace_replace_code(p, 1); |
| 2831 | if (failed) |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2832 | ftrace_bug(failed, p); |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2833 | } |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2834 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2835 | } |
| 2836 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2837 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2838 | ftrace_update_time = stop - start; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2839 | ftrace_update_tot_cnt += update_cnt; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2840 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2841 | return 0; |
| 2842 | } |
| 2843 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2844 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2845 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2846 | int order; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2847 | int cnt; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2848 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2849 | if (WARN_ON(!count)) |
| 2850 | return -EINVAL; |
| 2851 | |
| 2852 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2853 | |
| 2854 | /* |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2855 | * We want to fill as much as possible. No more than a page |
| 2856 | * may be empty. |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2857 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2858 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
| 2859 | order--; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2860 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2861 | again: |
| 2862 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 2863 | |
| 2864 | if (!pg->records) { |
| 2865 | /* if we can't allocate this size, try something smaller */ |
| 2866 | if (!order) |
| 2867 | return -ENOMEM; |
| 2868 | order >>= 1; |
| 2869 | goto again; |
| 2870 | } |
| 2871 | |
| 2872 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 2873 | pg->size = cnt; |
| 2874 | |
| 2875 | if (cnt > count) |
| 2876 | cnt = count; |
| 2877 | |
| 2878 | return cnt; |
| 2879 | } |
| 2880 | |
| 2881 | static struct ftrace_page * |
| 2882 | ftrace_allocate_pages(unsigned long num_to_init) |
| 2883 | { |
| 2884 | struct ftrace_page *start_pg; |
| 2885 | struct ftrace_page *pg; |
| 2886 | int order; |
| 2887 | int cnt; |
| 2888 | |
| 2889 | if (!num_to_init) |
| 2890 | return 0; |
| 2891 | |
| 2892 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 2893 | if (!pg) |
| 2894 | return NULL; |
| 2895 | |
| 2896 | /* |
| 2897 | * Try to allocate as much as possible in one continues |
| 2898 | * location that fills in all of the space. We want to |
| 2899 | * waste as little space as possible. |
| 2900 | */ |
| 2901 | for (;;) { |
| 2902 | cnt = ftrace_allocate_records(pg, num_to_init); |
| 2903 | if (cnt < 0) |
| 2904 | goto free_pages; |
| 2905 | |
| 2906 | num_to_init -= cnt; |
| 2907 | if (!num_to_init) |
| 2908 | break; |
| 2909 | |
| 2910 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 2911 | if (!pg->next) |
| 2912 | goto free_pages; |
| 2913 | |
| 2914 | pg = pg->next; |
| 2915 | } |
| 2916 | |
| 2917 | return start_pg; |
| 2918 | |
| 2919 | free_pages: |
Namhyung Kim | 1f61be00 | 2014-06-11 17:06:53 +0900 | [diff] [blame] | 2920 | pg = start_pg; |
| 2921 | while (pg) { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2922 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 2923 | free_pages((unsigned long)pg->records, order); |
| 2924 | start_pg = pg->next; |
| 2925 | kfree(pg); |
| 2926 | pg = start_pg; |
| 2927 | } |
| 2928 | pr_info("ftrace: FAILED to allocate memory for functions\n"); |
| 2929 | return NULL; |
| 2930 | } |
| 2931 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2932 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 2933 | |
| 2934 | struct ftrace_iterator { |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2935 | loff_t pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2936 | loff_t func_pos; |
| 2937 | struct ftrace_page *pg; |
| 2938 | struct dyn_ftrace *func; |
| 2939 | struct ftrace_func_probe *probe; |
| 2940 | struct trace_parser parser; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2941 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2942 | struct ftrace_ops *ops; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2943 | int hidx; |
| 2944 | int idx; |
| 2945 | unsigned flags; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2946 | }; |
| 2947 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2948 | static void * |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2949 | t_hash_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2950 | { |
| 2951 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2952 | struct hlist_node *hnd = NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2953 | struct hlist_head *hhd; |
| 2954 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2955 | (*pos)++; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2956 | iter->pos = *pos; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2957 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2958 | if (iter->probe) |
| 2959 | hnd = &iter->probe->node; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2960 | retry: |
| 2961 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) |
| 2962 | return NULL; |
| 2963 | |
| 2964 | hhd = &ftrace_func_hash[iter->hidx]; |
| 2965 | |
| 2966 | if (hlist_empty(hhd)) { |
| 2967 | iter->hidx++; |
| 2968 | hnd = NULL; |
| 2969 | goto retry; |
| 2970 | } |
| 2971 | |
| 2972 | if (!hnd) |
| 2973 | hnd = hhd->first; |
| 2974 | else { |
| 2975 | hnd = hnd->next; |
| 2976 | if (!hnd) { |
| 2977 | iter->hidx++; |
| 2978 | goto retry; |
| 2979 | } |
| 2980 | } |
| 2981 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2982 | if (WARN_ON_ONCE(!hnd)) |
| 2983 | return NULL; |
| 2984 | |
| 2985 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); |
| 2986 | |
| 2987 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | static void *t_hash_start(struct seq_file *m, loff_t *pos) |
| 2991 | { |
| 2992 | struct ftrace_iterator *iter = m->private; |
| 2993 | void *p = NULL; |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 2994 | loff_t l; |
| 2995 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 2996 | if (!(iter->flags & FTRACE_ITER_DO_HASH)) |
| 2997 | return NULL; |
| 2998 | |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 2999 | if (iter->func_pos > *pos) |
| 3000 | return NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3001 | |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3002 | iter->hidx = 0; |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3003 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3004 | p = t_hash_next(m, &l); |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3005 | if (!p) |
| 3006 | break; |
| 3007 | } |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3008 | if (!p) |
| 3009 | return NULL; |
| 3010 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3011 | /* Only set this if we have an item */ |
| 3012 | iter->flags |= FTRACE_ITER_HASH; |
| 3013 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3014 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3015 | } |
| 3016 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3017 | static int |
| 3018 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3019 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3020 | struct ftrace_func_probe *rec; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3021 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3022 | rec = iter->probe; |
| 3023 | if (WARN_ON_ONCE(!rec)) |
| 3024 | return -EIO; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3025 | |
Steven Rostedt | 809dcf2 | 2009-02-16 23:06:01 -0500 | [diff] [blame] | 3026 | if (rec->ops->print) |
| 3027 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); |
| 3028 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 3029 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3030 | |
| 3031 | if (rec->data) |
| 3032 | seq_printf(m, ":%p", rec->data); |
| 3033 | seq_putc(m, '\n'); |
| 3034 | |
| 3035 | return 0; |
| 3036 | } |
| 3037 | |
| 3038 | static void * |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3039 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 3040 | { |
| 3041 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3042 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3043 | struct dyn_ftrace *rec = NULL; |
| 3044 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3045 | if (unlikely(ftrace_disabled)) |
| 3046 | return NULL; |
| 3047 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3048 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3049 | return t_hash_next(m, pos); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3050 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3051 | (*pos)++; |
Jiri Olsa | 1106b69 | 2011-02-16 17:35:34 +0100 | [diff] [blame] | 3052 | iter->pos = iter->func_pos = *pos; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3053 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3054 | if (iter->flags & FTRACE_ITER_PRINTALL) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3055 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3056 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3057 | retry: |
| 3058 | if (iter->idx >= iter->pg->index) { |
| 3059 | if (iter->pg->next) { |
| 3060 | iter->pg = iter->pg->next; |
| 3061 | iter->idx = 0; |
| 3062 | goto retry; |
| 3063 | } |
| 3064 | } else { |
| 3065 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3066 | if (((iter->flags & FTRACE_ITER_FILTER) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3067 | !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) || |
Steven Rostedt | 0183fb1c | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3068 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3069 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3070 | !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) || |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3071 | |
| 3072 | ((iter->flags & FTRACE_ITER_ENABLED) && |
Steven Rostedt (Red Hat) | 23ea9c4 | 2013-05-09 19:31:48 -0400 | [diff] [blame] | 3073 | !(rec->flags & FTRACE_FL_ENABLED))) { |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3074 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3075 | rec = NULL; |
| 3076 | goto retry; |
| 3077 | } |
| 3078 | } |
| 3079 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3080 | if (!rec) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3081 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3082 | |
| 3083 | iter->func = rec; |
| 3084 | |
| 3085 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3086 | } |
| 3087 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3088 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 3089 | { |
| 3090 | iter->pos = 0; |
| 3091 | iter->func_pos = 0; |
Dan Carpenter | 70f77b3f | 2012-06-09 19:10:27 +0300 | [diff] [blame] | 3092 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 3096 | { |
| 3097 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3098 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3099 | void *p = NULL; |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3100 | loff_t l; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3101 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3102 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3103 | |
| 3104 | if (unlikely(ftrace_disabled)) |
| 3105 | return NULL; |
| 3106 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3107 | /* |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3108 | * If an lseek was done, then reset and start from beginning. |
| 3109 | */ |
| 3110 | if (*pos < iter->pos) |
| 3111 | reset_iter_read(iter); |
| 3112 | |
| 3113 | /* |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3114 | * For set_ftrace_filter reading, if we have the filter |
| 3115 | * off, we can short cut and just print out that all |
| 3116 | * functions are enabled. |
| 3117 | */ |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3118 | if ((iter->flags & FTRACE_ITER_FILTER && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3119 | ftrace_hash_empty(ops->func_hash->filter_hash)) || |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3120 | (iter->flags & FTRACE_ITER_NOTRACE && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3121 | ftrace_hash_empty(ops->func_hash->notrace_hash))) { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3122 | if (*pos > 0) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3123 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3124 | iter->flags |= FTRACE_ITER_PRINTALL; |
Chris Wright | df09162 | 2010-09-09 16:34:59 -0700 | [diff] [blame] | 3125 | /* reset in case of seek/pread */ |
| 3126 | iter->flags &= ~FTRACE_ITER_HASH; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3127 | return iter; |
| 3128 | } |
| 3129 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3130 | if (iter->flags & FTRACE_ITER_HASH) |
| 3131 | return t_hash_start(m, pos); |
| 3132 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3133 | /* |
| 3134 | * Unfortunately, we need to restart at ftrace_pages_start |
| 3135 | * every time we let go of the ftrace_mutex. This is because |
| 3136 | * those pointers can change without the lock. |
| 3137 | */ |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3138 | iter->pg = ftrace_pages_start; |
| 3139 | iter->idx = 0; |
| 3140 | for (l = 0; l <= *pos; ) { |
| 3141 | p = t_next(m, p, &l); |
| 3142 | if (!p) |
| 3143 | break; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 3144 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 3145 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3146 | if (!p) |
| 3147 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3148 | |
| 3149 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | static void t_stop(struct seq_file *m, void *p) |
| 3153 | { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3154 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3155 | } |
| 3156 | |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3157 | void * __weak |
| 3158 | arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 3159 | { |
| 3160 | return NULL; |
| 3161 | } |
| 3162 | |
| 3163 | static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, |
| 3164 | struct dyn_ftrace *rec) |
| 3165 | { |
| 3166 | void *ptr; |
| 3167 | |
| 3168 | ptr = arch_ftrace_trampoline_func(ops, rec); |
| 3169 | if (ptr) |
| 3170 | seq_printf(m, " ->%pS", ptr); |
| 3171 | } |
| 3172 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3173 | static int t_show(struct seq_file *m, void *v) |
| 3174 | { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3175 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3176 | struct dyn_ftrace *rec; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3177 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3178 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3179 | return t_hash_show(m, iter); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3180 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3181 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3182 | if (iter->flags & FTRACE_ITER_NOTRACE) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3183 | seq_puts(m, "#### no functions disabled ####\n"); |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3184 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3185 | seq_puts(m, "#### all functions enabled ####\n"); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3186 | return 0; |
| 3187 | } |
| 3188 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3189 | rec = iter->func; |
| 3190 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3191 | if (!rec) |
| 3192 | return 0; |
| 3193 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3194 | seq_printf(m, "%ps", (void *)rec->ip); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3195 | if (iter->flags & FTRACE_ITER_ENABLED) { |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3196 | struct ftrace_ops *ops = NULL; |
| 3197 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3198 | seq_printf(m, " (%ld)%s%s", |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 3199 | ftrace_rec_count(rec), |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3200 | rec->flags & FTRACE_FL_REGS ? " R" : " ", |
| 3201 | rec->flags & FTRACE_FL_IPMODIFY ? " I" : " "); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3202 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 3203 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 3204 | if (ops) |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3205 | seq_printf(m, "\ttramp: %pS", |
| 3206 | (void *)ops->trampoline); |
| 3207 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3208 | seq_puts(m, "\ttramp: ERROR!"); |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3209 | |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3210 | } |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3211 | add_trampoline_func(m, ops, rec); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3212 | } |
| 3213 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3214 | seq_putc(m, '\n'); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3215 | |
| 3216 | return 0; |
| 3217 | } |
| 3218 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3219 | static const struct seq_operations show_ftrace_seq_ops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3220 | .start = t_start, |
| 3221 | .next = t_next, |
| 3222 | .stop = t_stop, |
| 3223 | .show = t_show, |
| 3224 | }; |
| 3225 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3226 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3227 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 3228 | { |
| 3229 | struct ftrace_iterator *iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3230 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3231 | if (unlikely(ftrace_disabled)) |
| 3232 | return -ENODEV; |
| 3233 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3234 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3235 | if (iter) { |
| 3236 | iter->pg = ftrace_pages_start; |
| 3237 | iter->ops = &global_ops; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3238 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3239 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3240 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3241 | } |
| 3242 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3243 | static int |
| 3244 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 3245 | { |
| 3246 | struct ftrace_iterator *iter; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3247 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3248 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3249 | if (iter) { |
| 3250 | iter->pg = ftrace_pages_start; |
| 3251 | iter->flags = FTRACE_ITER_ENABLED; |
| 3252 | iter->ops = &global_ops; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3253 | } |
| 3254 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3255 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3256 | } |
| 3257 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3258 | /** |
| 3259 | * ftrace_regex_open - initialize function tracer filter files |
| 3260 | * @ops: The ftrace_ops that hold the hash filters |
| 3261 | * @flag: The type of filter to process |
| 3262 | * @inode: The inode, usually passed in to your open routine |
| 3263 | * @file: The file, usually passed in to your open routine |
| 3264 | * |
| 3265 | * ftrace_regex_open() initializes the filter files for the |
| 3266 | * @ops. Depending on @flag it may process the filter hash or |
| 3267 | * the notrace hash of @ops. With this called from the open |
| 3268 | * routine, you can use ftrace_filter_write() for the write |
| 3269 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 3270 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3271 | * tracing_lseek() should be used as the lseek routine, and |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3272 | * release must call ftrace_regex_release(). |
| 3273 | */ |
| 3274 | int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3275 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3276 | struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3277 | { |
| 3278 | struct ftrace_iterator *iter; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3279 | struct ftrace_hash *hash; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3280 | int ret = 0; |
| 3281 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3282 | ftrace_ops_init(ops); |
| 3283 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3284 | if (unlikely(ftrace_disabled)) |
| 3285 | return -ENODEV; |
| 3286 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3287 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 3288 | if (!iter) |
| 3289 | return -ENOMEM; |
| 3290 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3291 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
| 3292 | kfree(iter); |
| 3293 | return -ENOMEM; |
| 3294 | } |
| 3295 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3296 | iter->ops = ops; |
| 3297 | iter->flags = flag; |
| 3298 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3299 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3300 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3301 | if (flag & FTRACE_ITER_NOTRACE) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3302 | hash = ops->func_hash->notrace_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3303 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3304 | hash = ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3305 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3306 | if (file->f_mode & FMODE_WRITE) { |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3307 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 3308 | |
| 3309 | if (file->f_flags & O_TRUNC) |
| 3310 | iter->hash = alloc_ftrace_hash(size_bits); |
| 3311 | else |
| 3312 | iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); |
| 3313 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3314 | if (!iter->hash) { |
| 3315 | trace_parser_put(&iter->parser); |
| 3316 | kfree(iter); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3317 | ret = -ENOMEM; |
| 3318 | goto out_unlock; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3319 | } |
| 3320 | } |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3321 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3322 | if (file->f_mode & FMODE_READ) { |
| 3323 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3324 | |
| 3325 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 3326 | if (!ret) { |
| 3327 | struct seq_file *m = file->private_data; |
| 3328 | m->private = iter; |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3329 | } else { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3330 | /* Failed */ |
| 3331 | free_ftrace_hash(iter->hash); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3332 | trace_parser_put(&iter->parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3333 | kfree(iter); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3334 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3335 | } else |
| 3336 | file->private_data = iter; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3337 | |
| 3338 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3339 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3340 | |
| 3341 | return ret; |
| 3342 | } |
| 3343 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3344 | static int |
| 3345 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 3346 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3347 | struct ftrace_ops *ops = inode->i_private; |
| 3348 | |
| 3349 | return ftrace_regex_open(ops, |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3350 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, |
| 3351 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | static int |
| 3355 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 3356 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3357 | struct ftrace_ops *ops = inode->i_private; |
| 3358 | |
| 3359 | return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3360 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3361 | } |
| 3362 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3363 | static int ftrace_match(char *str, char *regex, int len, int type) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3364 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3365 | int matched = 0; |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3366 | int slen; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3367 | |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3368 | switch (type) { |
| 3369 | case MATCH_FULL: |
| 3370 | if (strcmp(str, regex) == 0) |
| 3371 | matched = 1; |
| 3372 | break; |
| 3373 | case MATCH_FRONT_ONLY: |
| 3374 | if (strncmp(str, regex, len) == 0) |
| 3375 | matched = 1; |
| 3376 | break; |
| 3377 | case MATCH_MIDDLE_ONLY: |
| 3378 | if (strstr(str, regex)) |
| 3379 | matched = 1; |
| 3380 | break; |
| 3381 | case MATCH_END_ONLY: |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3382 | slen = strlen(str); |
| 3383 | if (slen >= len && memcmp(str + slen - len, regex, len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3384 | matched = 1; |
| 3385 | break; |
| 3386 | } |
| 3387 | |
| 3388 | return matched; |
| 3389 | } |
| 3390 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3391 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3392 | enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not) |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 3393 | { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3394 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3395 | int ret = 0; |
| 3396 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3397 | entry = ftrace_lookup_ip(hash, rec->ip); |
| 3398 | if (not) { |
| 3399 | /* Do nothing if it doesn't exist */ |
| 3400 | if (!entry) |
| 3401 | return 0; |
| 3402 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3403 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3404 | } else { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3405 | /* Do nothing if it exists */ |
| 3406 | if (entry) |
| 3407 | return 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3408 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3409 | ret = add_hash_entry(hash, rec->ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3410 | } |
| 3411 | return ret; |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 3412 | } |
| 3413 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3414 | static int |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3415 | ftrace_match_record(struct dyn_ftrace *rec, char *mod, |
| 3416 | char *regex, int len, int type) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3417 | { |
| 3418 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3419 | char *modname; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3420 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3421 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); |
| 3422 | |
| 3423 | if (mod) { |
| 3424 | /* module lookup requires matching the module */ |
| 3425 | if (!modname || strcmp(modname, mod)) |
| 3426 | return 0; |
| 3427 | |
| 3428 | /* blank search means to match all funcs in the mod */ |
| 3429 | if (!len) |
| 3430 | return 1; |
| 3431 | } |
| 3432 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3433 | return ftrace_match(str, regex, len, type); |
| 3434 | } |
| 3435 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3436 | static int |
| 3437 | match_records(struct ftrace_hash *hash, char *buff, |
| 3438 | int len, char *mod, int not) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3439 | { |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3440 | unsigned search_len = 0; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3441 | struct ftrace_page *pg; |
| 3442 | struct dyn_ftrace *rec; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3443 | int type = MATCH_FULL; |
| 3444 | char *search = buff; |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3445 | int found = 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3446 | int ret; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3447 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3448 | if (len) { |
| 3449 | type = filter_parse_regex(buff, len, &search, ¬); |
| 3450 | search_len = strlen(search); |
| 3451 | } |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3452 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3453 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3454 | |
| 3455 | if (unlikely(ftrace_disabled)) |
| 3456 | goto out_unlock; |
| 3457 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3458 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3459 | if (ftrace_match_record(rec, mod, search, search_len, type)) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3460 | ret = enter_record(hash, rec, not); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3461 | if (ret < 0) { |
| 3462 | found = ret; |
| 3463 | goto out_unlock; |
| 3464 | } |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3465 | found = 1; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3466 | } |
| 3467 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3468 | out_unlock: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3469 | mutex_unlock(&ftrace_lock); |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3470 | |
| 3471 | return found; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3472 | } |
| 3473 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3474 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3475 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3476 | { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3477 | return match_records(hash, buff, len, NULL, 0); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3478 | } |
| 3479 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3480 | static int |
| 3481 | ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3482 | { |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3483 | int not = 0; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3484 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3485 | /* blank or '*' mean the same */ |
| 3486 | if (strcmp(buff, "*") == 0) |
| 3487 | buff[0] = 0; |
| 3488 | |
| 3489 | /* handle the case of 'dont filter this module' */ |
| 3490 | if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) { |
| 3491 | buff[0] = 0; |
| 3492 | not = 1; |
| 3493 | } |
| 3494 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3495 | return match_records(hash, buff, strlen(buff), mod, not); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3496 | } |
| 3497 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3498 | /* |
| 3499 | * We register the module command as a template to show others how |
| 3500 | * to register the a command as well. |
| 3501 | */ |
| 3502 | |
| 3503 | static int |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 3504 | ftrace_mod_callback(struct ftrace_hash *hash, |
| 3505 | char *func, char *cmd, char *param, int enable) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3506 | { |
| 3507 | char *mod; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3508 | int ret = -EINVAL; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3509 | |
| 3510 | /* |
| 3511 | * cmd == 'mod' because we only registered this func |
| 3512 | * for the 'mod' ftrace_func_command. |
| 3513 | * But if you register one func with multiple commands, |
| 3514 | * you can tell which command was used by the cmd |
| 3515 | * parameter. |
| 3516 | */ |
| 3517 | |
| 3518 | /* we must have a module name */ |
| 3519 | if (!param) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3520 | return ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3521 | |
| 3522 | mod = strsep(¶m, ":"); |
| 3523 | if (!strlen(mod)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3524 | return ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3525 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3526 | ret = ftrace_match_module_records(hash, func, mod); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3527 | if (!ret) |
| 3528 | ret = -EINVAL; |
| 3529 | if (ret < 0) |
| 3530 | return ret; |
| 3531 | |
| 3532 | return 0; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 3536 | .name = "mod", |
| 3537 | .func = ftrace_mod_callback, |
| 3538 | }; |
| 3539 | |
| 3540 | static int __init ftrace_mod_cmd_init(void) |
| 3541 | { |
| 3542 | return register_ftrace_command(&ftrace_mod_cmd); |
| 3543 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 3544 | core_initcall(ftrace_mod_cmd_init); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3545 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 3546 | 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] | 3547 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3548 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3549 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3550 | struct hlist_head *hhd; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3551 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3552 | |
| 3553 | key = hash_long(ip, FTRACE_HASH_BITS); |
| 3554 | |
| 3555 | hhd = &ftrace_func_hash[key]; |
| 3556 | |
| 3557 | if (hlist_empty(hhd)) |
| 3558 | return; |
| 3559 | |
| 3560 | /* |
| 3561 | * Disable preemption for these calls to prevent a RCU grace |
| 3562 | * period. This syncs the hash iteration and freeing of items |
| 3563 | * on the hash. rcu_read_lock is too dangerous here. |
| 3564 | */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3565 | preempt_disable_notrace(); |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 3566 | hlist_for_each_entry_rcu_notrace(entry, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3567 | if (entry->ip == ip) |
| 3568 | entry->ops->func(ip, parent_ip, &entry->data); |
| 3569 | } |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3570 | preempt_enable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3571 | } |
| 3572 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3573 | static struct ftrace_ops trace_probe_ops __read_mostly = |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3574 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 3575 | .func = function_trace_probe_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3576 | .flags = FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3577 | INIT_OPS_HASH(trace_probe_ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3578 | }; |
| 3579 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3580 | static int ftrace_probe_registered; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3581 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3582 | static void __enable_ftrace_function_probe(struct ftrace_hash *old_hash) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3583 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3584 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3585 | int i; |
| 3586 | |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3587 | if (ftrace_probe_registered) { |
| 3588 | /* still need to update the function call sites */ |
| 3589 | if (ftrace_enabled) |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3590 | ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, |
| 3591 | old_hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3592 | return; |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3593 | } |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3594 | |
| 3595 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3596 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3597 | if (hhd->first) |
| 3598 | break; |
| 3599 | } |
| 3600 | /* Nothing registered? */ |
| 3601 | if (i == FTRACE_FUNC_HASHSIZE) |
| 3602 | return; |
| 3603 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3604 | ret = ftrace_startup(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3605 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3606 | ftrace_probe_registered = 1; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3607 | } |
| 3608 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3609 | static void __disable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3610 | { |
| 3611 | int i; |
| 3612 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3613 | if (!ftrace_probe_registered) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3614 | return; |
| 3615 | |
| 3616 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3617 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3618 | if (hhd->first) |
| 3619 | return; |
| 3620 | } |
| 3621 | |
| 3622 | /* no more funcs left */ |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3623 | ftrace_shutdown(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3624 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3625 | ftrace_probe_registered = 0; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3629 | static void ftrace_free_entry(struct ftrace_func_probe *entry) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3630 | { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3631 | if (entry->ops->free) |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3632 | entry->ops->free(entry->ops, entry->ip, &entry->data); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3633 | kfree(entry); |
| 3634 | } |
| 3635 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3636 | int |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3637 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3638 | void *data) |
| 3639 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3640 | struct ftrace_func_probe *entry; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3641 | struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3642 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3643 | struct ftrace_hash *hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3644 | struct ftrace_page *pg; |
| 3645 | struct dyn_ftrace *rec; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3646 | int type, len, not; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3647 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3648 | int count = 0; |
| 3649 | char *search; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3650 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3651 | |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 3652 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3653 | len = strlen(search); |
| 3654 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3655 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3656 | if (WARN_ON(not)) |
| 3657 | return -EINVAL; |
| 3658 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3659 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3660 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3661 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3662 | if (!hash) { |
| 3663 | count = -ENOMEM; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3664 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | if (unlikely(ftrace_disabled)) { |
| 3668 | count = -ENODEV; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3669 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3670 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3671 | |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3672 | mutex_lock(&ftrace_lock); |
| 3673 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3674 | do_for_each_ftrace_rec(pg, rec) { |
| 3675 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3676 | if (!ftrace_match_record(rec, NULL, search, len, type)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3677 | continue; |
| 3678 | |
| 3679 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 3680 | if (!entry) { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3681 | /* If we did not process any, then return error */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3682 | if (!count) |
| 3683 | count = -ENOMEM; |
| 3684 | goto out_unlock; |
| 3685 | } |
| 3686 | |
| 3687 | count++; |
| 3688 | |
| 3689 | entry->data = data; |
| 3690 | |
| 3691 | /* |
| 3692 | * The caller might want to do something special |
| 3693 | * for each function we find. We call the callback |
| 3694 | * to give the caller an opportunity to do so. |
| 3695 | */ |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3696 | if (ops->init) { |
| 3697 | if (ops->init(ops, rec->ip, &entry->data) < 0) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3698 | /* caller does not like this func */ |
| 3699 | kfree(entry); |
| 3700 | continue; |
| 3701 | } |
| 3702 | } |
| 3703 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3704 | ret = enter_record(hash, rec, 0); |
| 3705 | if (ret < 0) { |
| 3706 | kfree(entry); |
| 3707 | count = ret; |
| 3708 | goto out_unlock; |
| 3709 | } |
| 3710 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3711 | entry->ops = ops; |
| 3712 | entry->ip = rec->ip; |
| 3713 | |
| 3714 | key = hash_long(entry->ip, FTRACE_HASH_BITS); |
| 3715 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); |
| 3716 | |
| 3717 | } while_for_each_ftrace_rec(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3718 | |
| 3719 | ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3720 | |
| 3721 | __enable_ftrace_function_probe(old_hash); |
| 3722 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3723 | if (!ret) |
| 3724 | free_ftrace_hash_rcu(old_hash); |
| 3725 | else |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3726 | count = ret; |
| 3727 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3728 | out_unlock: |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3729 | mutex_unlock(&ftrace_lock); |
| 3730 | out: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3731 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3732 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3733 | |
| 3734 | return count; |
| 3735 | } |
| 3736 | |
| 3737 | enum { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3738 | PROBE_TEST_FUNC = 1, |
| 3739 | PROBE_TEST_DATA = 2 |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3740 | }; |
| 3741 | |
| 3742 | static void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3743 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3744 | void *data, int flags) |
| 3745 | { |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3746 | struct ftrace_func_entry *rec_entry; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3747 | struct ftrace_func_probe *entry; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3748 | struct ftrace_func_probe *p; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3749 | struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3750 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3751 | struct list_head free_list; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3752 | struct ftrace_hash *hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3753 | struct hlist_node *tmp; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3754 | char str[KSYM_SYMBOL_LEN]; |
| 3755 | int type = MATCH_FULL; |
| 3756 | int i, len = 0; |
| 3757 | char *search; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3758 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3759 | |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3760 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3761 | glob = NULL; |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3762 | else if (glob) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3763 | int not; |
| 3764 | |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 3765 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3766 | len = strlen(search); |
| 3767 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3768 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3769 | if (WARN_ON(not)) |
| 3770 | return; |
| 3771 | } |
| 3772 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3773 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3774 | |
| 3775 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 3776 | if (!hash) |
| 3777 | /* Hmm, should report this somehow */ |
| 3778 | goto out_unlock; |
| 3779 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3780 | INIT_LIST_HEAD(&free_list); |
| 3781 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3782 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3783 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3784 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3785 | hlist_for_each_entry_safe(entry, tmp, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3786 | |
| 3787 | /* break up if statements for readability */ |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3788 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3789 | continue; |
| 3790 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3791 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3792 | continue; |
| 3793 | |
| 3794 | /* do this last, since it is the most expensive */ |
| 3795 | if (glob) { |
| 3796 | kallsyms_lookup(entry->ip, NULL, NULL, |
| 3797 | NULL, str); |
| 3798 | if (!ftrace_match(str, glob, len, type)) |
| 3799 | continue; |
| 3800 | } |
| 3801 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3802 | rec_entry = ftrace_lookup_ip(hash, entry->ip); |
| 3803 | /* It is possible more than one entry had this ip */ |
| 3804 | if (rec_entry) |
| 3805 | free_hash_entry(hash, rec_entry); |
| 3806 | |
Steven Rostedt (Red Hat) | 740466b | 2013-03-13 11:15:19 -0400 | [diff] [blame] | 3807 | hlist_del_rcu(&entry->node); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3808 | list_add(&entry->free_list, &free_list); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3809 | } |
| 3810 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3811 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3812 | __disable_ftrace_function_probe(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3813 | /* |
| 3814 | * Remove after the disable is called. Otherwise, if the last |
| 3815 | * probe is removed, a null hash means *all enabled*. |
| 3816 | */ |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3817 | ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3818 | synchronize_sched(); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3819 | if (!ret) |
| 3820 | free_ftrace_hash_rcu(old_hash); |
| 3821 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3822 | list_for_each_entry_safe(entry, p, &free_list, free_list) { |
| 3823 | list_del(&entry->free_list); |
| 3824 | ftrace_free_entry(entry); |
| 3825 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3826 | mutex_unlock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3827 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3828 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3829 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3830 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3834 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3835 | void *data) |
| 3836 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3837 | __unregister_ftrace_function_probe(glob, ops, data, |
| 3838 | PROBE_TEST_FUNC | PROBE_TEST_DATA); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3842 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3843 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3844 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3845 | } |
| 3846 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3847 | void unregister_ftrace_function_probe_all(char *glob) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3848 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3849 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3850 | } |
| 3851 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3852 | static LIST_HEAD(ftrace_commands); |
| 3853 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 3854 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 3855 | /* |
| 3856 | * Currently we only register ftrace commands from __init, so mark this |
| 3857 | * __init too. |
| 3858 | */ |
| 3859 | __init int register_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3860 | { |
| 3861 | struct ftrace_func_command *p; |
| 3862 | int ret = 0; |
| 3863 | |
| 3864 | mutex_lock(&ftrace_cmd_mutex); |
| 3865 | list_for_each_entry(p, &ftrace_commands, list) { |
| 3866 | if (strcmp(cmd->name, p->name) == 0) { |
| 3867 | ret = -EBUSY; |
| 3868 | goto out_unlock; |
| 3869 | } |
| 3870 | } |
| 3871 | list_add(&cmd->list, &ftrace_commands); |
| 3872 | out_unlock: |
| 3873 | mutex_unlock(&ftrace_cmd_mutex); |
| 3874 | |
| 3875 | return ret; |
| 3876 | } |
| 3877 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 3878 | /* |
| 3879 | * Currently we only unregister ftrace commands from __init, so mark |
| 3880 | * this __init too. |
| 3881 | */ |
| 3882 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3883 | { |
| 3884 | struct ftrace_func_command *p, *n; |
| 3885 | int ret = -ENODEV; |
| 3886 | |
| 3887 | mutex_lock(&ftrace_cmd_mutex); |
| 3888 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 3889 | if (strcmp(cmd->name, p->name) == 0) { |
| 3890 | ret = 0; |
| 3891 | list_del_init(&p->list); |
| 3892 | goto out_unlock; |
| 3893 | } |
| 3894 | } |
| 3895 | out_unlock: |
| 3896 | mutex_unlock(&ftrace_cmd_mutex); |
| 3897 | |
| 3898 | return ret; |
| 3899 | } |
| 3900 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3901 | static int ftrace_process_regex(struct ftrace_hash *hash, |
| 3902 | char *buff, int len, int enable) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3903 | { |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3904 | char *func, *command, *next = buff; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3905 | struct ftrace_func_command *p; |
GuoWen Li | 0aff1c0 | 2011-06-01 19:18:47 +0800 | [diff] [blame] | 3906 | int ret = -EINVAL; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3907 | |
| 3908 | func = strsep(&next, ":"); |
| 3909 | |
| 3910 | if (!next) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3911 | ret = ftrace_match_records(hash, func, len); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3912 | if (!ret) |
| 3913 | ret = -EINVAL; |
| 3914 | if (ret < 0) |
| 3915 | return ret; |
| 3916 | return 0; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3917 | } |
| 3918 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3919 | /* command found */ |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3920 | |
| 3921 | command = strsep(&next, ":"); |
| 3922 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3923 | mutex_lock(&ftrace_cmd_mutex); |
| 3924 | list_for_each_entry(p, &ftrace_commands, list) { |
| 3925 | if (strcmp(p->name, command) == 0) { |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 3926 | ret = p->func(hash, func, command, next, enable); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3927 | goto out_unlock; |
| 3928 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3929 | } |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3930 | out_unlock: |
| 3931 | mutex_unlock(&ftrace_cmd_mutex); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3932 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3933 | return ret; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3934 | } |
| 3935 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3936 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3937 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 3938 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3939 | { |
| 3940 | struct ftrace_iterator *iter; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3941 | struct trace_parser *parser; |
| 3942 | ssize_t ret, read; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3943 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3944 | if (!cnt) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3945 | return 0; |
| 3946 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3947 | if (file->f_mode & FMODE_READ) { |
| 3948 | struct seq_file *m = file->private_data; |
| 3949 | iter = m->private; |
| 3950 | } else |
| 3951 | iter = file->private_data; |
| 3952 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3953 | if (unlikely(ftrace_disabled)) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3954 | return -ENODEV; |
| 3955 | |
| 3956 | /* 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] | 3957 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3958 | parser = &iter->parser; |
| 3959 | read = trace_get_user(parser, ubuf, cnt, ppos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3960 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3961 | if (read >= 0 && trace_parser_loaded(parser) && |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3962 | !trace_parser_cont(parser)) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3963 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3964 | parser->idx, enable); |
Li Zefan | 313254a | 2009-12-08 11:15:30 +0800 | [diff] [blame] | 3965 | trace_parser_clear(parser); |
Steven Rostedt (Red Hat) | 7c088b5 | 2013-05-09 11:35:12 -0400 | [diff] [blame] | 3966 | if (ret < 0) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3967 | goto out; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3968 | } |
| 3969 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3970 | ret = read; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3971 | out: |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3972 | return ret; |
| 3973 | } |
| 3974 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3975 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3976 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 3977 | size_t cnt, loff_t *ppos) |
| 3978 | { |
| 3979 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 3980 | } |
| 3981 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3982 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3983 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 3984 | size_t cnt, loff_t *ppos) |
| 3985 | { |
| 3986 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 3987 | } |
| 3988 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3989 | static int |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 3990 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
| 3991 | { |
| 3992 | struct ftrace_func_entry *entry; |
| 3993 | |
| 3994 | if (!ftrace_location(ip)) |
| 3995 | return -EINVAL; |
| 3996 | |
| 3997 | if (remove) { |
| 3998 | entry = ftrace_lookup_ip(hash, ip); |
| 3999 | if (!entry) |
| 4000 | return -ENOENT; |
| 4001 | free_hash_entry(hash, entry); |
| 4002 | return 0; |
| 4003 | } |
| 4004 | |
| 4005 | return add_hash_entry(hash, ip); |
| 4006 | } |
| 4007 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4008 | static void ftrace_ops_update_code(struct ftrace_ops *ops, |
| 4009 | struct ftrace_hash *old_hash) |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4010 | { |
| 4011 | if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled) |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4012 | ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4013 | } |
| 4014 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4015 | static int |
| 4016 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4017 | unsigned long ip, int remove, int reset, int enable) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4018 | { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4019 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4020 | struct ftrace_hash *old_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4021 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4022 | int ret; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4023 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4024 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4025 | return -ENODEV; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4026 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4027 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4028 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4029 | if (enable) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4030 | orig_hash = &ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4031 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4032 | orig_hash = &ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4033 | |
Wang Nan | b972cc5 | 2014-07-15 08:40:20 +0800 | [diff] [blame] | 4034 | if (reset) |
| 4035 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4036 | else |
| 4037 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 4038 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4039 | if (!hash) { |
| 4040 | ret = -ENOMEM; |
| 4041 | goto out_regex_unlock; |
| 4042 | } |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4043 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4044 | if (buf && !ftrace_match_records(hash, buf, len)) { |
| 4045 | ret = -EINVAL; |
| 4046 | goto out_regex_unlock; |
| 4047 | } |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4048 | if (ip) { |
| 4049 | ret = ftrace_match_addr(hash, ip, remove); |
| 4050 | if (ret < 0) |
| 4051 | goto out_regex_unlock; |
| 4052 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4053 | |
| 4054 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4055 | old_hash = *orig_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4056 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4057 | if (!ret) { |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4058 | ftrace_ops_update_code(ops, old_hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4059 | free_ftrace_hash_rcu(old_hash); |
| 4060 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4061 | mutex_unlock(&ftrace_lock); |
| 4062 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4063 | out_regex_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4064 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4065 | |
| 4066 | free_ftrace_hash(hash); |
| 4067 | return ret; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4068 | } |
| 4069 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4070 | static int |
| 4071 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, |
| 4072 | int reset, int enable) |
| 4073 | { |
| 4074 | return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); |
| 4075 | } |
| 4076 | |
| 4077 | /** |
| 4078 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address |
| 4079 | * @ops - the ops to set the filter with |
| 4080 | * @ip - the address to add to or remove from the filter. |
| 4081 | * @remove - non zero to remove the ip from the filter |
| 4082 | * @reset - non zero to reset all filters before applying this filter. |
| 4083 | * |
| 4084 | * Filters denote which functions should be enabled when tracing is enabled |
| 4085 | * If @ip is NULL, it failes to update filter. |
| 4086 | */ |
| 4087 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, |
| 4088 | int remove, int reset) |
| 4089 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4090 | ftrace_ops_init(ops); |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4091 | return ftrace_set_addr(ops, ip, remove, reset, 1); |
| 4092 | } |
| 4093 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); |
| 4094 | |
| 4095 | static int |
| 4096 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4097 | int reset, int enable) |
| 4098 | { |
| 4099 | return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); |
| 4100 | } |
| 4101 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4102 | /** |
| 4103 | * ftrace_set_filter - set a function to filter on in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4104 | * @ops - the ops to set the filter with |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4105 | * @buf - the string that holds the function filter text. |
| 4106 | * @len - the length of the string. |
| 4107 | * @reset - non zero to reset all filters before applying this filter. |
| 4108 | * |
| 4109 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4110 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4111 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4112 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4113 | int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4114 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4115 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4116 | return ftrace_set_regex(ops, buf, len, reset, 1); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4117 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4118 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4119 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4120 | /** |
| 4121 | * ftrace_set_notrace - set a function to not trace in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4122 | * @ops - the ops to set the notrace filter with |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4123 | * @buf - the string that holds the function notrace text. |
| 4124 | * @len - the length of the string. |
| 4125 | * @reset - non zero to reset all filters before applying this filter. |
| 4126 | * |
| 4127 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4128 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4129 | * for tracing. |
| 4130 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4131 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4132 | int len, int reset) |
| 4133 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4134 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4135 | return ftrace_set_regex(ops, buf, len, reset, 0); |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4136 | } |
| 4137 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 4138 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4139 | * ftrace_set_global_filter - set a function to filter on with global tracers |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4140 | * @buf - the string that holds the function filter text. |
| 4141 | * @len - the length of the string. |
| 4142 | * @reset - non zero to reset all filters before applying this filter. |
| 4143 | * |
| 4144 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4145 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4146 | */ |
| 4147 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 4148 | { |
| 4149 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
| 4150 | } |
| 4151 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 4152 | |
| 4153 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4154 | * ftrace_set_global_notrace - set a function to not trace with global tracers |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4155 | * @buf - the string that holds the function notrace text. |
| 4156 | * @len - the length of the string. |
| 4157 | * @reset - non zero to reset all filters before applying this filter. |
| 4158 | * |
| 4159 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4160 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4161 | * for tracing. |
| 4162 | */ |
| 4163 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4164 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4165 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4166 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4167 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4168 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4169 | /* |
| 4170 | * command line interface to allow users to set filters on boot up. |
| 4171 | */ |
| 4172 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 4173 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4174 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4175 | |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4176 | /* Used by function selftest to not test if filter is set */ |
| 4177 | bool ftrace_filter_param __initdata; |
| 4178 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4179 | static int __init set_ftrace_notrace(char *str) |
| 4180 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4181 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4182 | strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4183 | return 1; |
| 4184 | } |
| 4185 | __setup("ftrace_notrace=", set_ftrace_notrace); |
| 4186 | |
| 4187 | static int __init set_ftrace_filter(char *str) |
| 4188 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4189 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4190 | strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4191 | return 1; |
| 4192 | } |
| 4193 | __setup("ftrace_filter=", set_ftrace_filter); |
| 4194 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4195 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Lai Jiangshan | f6060f4 | 2009-11-05 11:16:17 +0800 | [diff] [blame] | 4196 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4197 | static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4198 | 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] | 4199 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 4200 | static unsigned long save_global_trampoline; |
| 4201 | static unsigned long save_global_flags; |
| 4202 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4203 | static int __init set_graph_function(char *str) |
| 4204 | { |
Frederic Weisbecker | 06f43d6 | 2009-10-14 20:43:39 +0200 | [diff] [blame] | 4205 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4206 | return 1; |
| 4207 | } |
| 4208 | __setup("ftrace_graph_filter=", set_graph_function); |
| 4209 | |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4210 | static int __init set_graph_notrace_function(char *str) |
| 4211 | { |
| 4212 | strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 4213 | return 1; |
| 4214 | } |
| 4215 | __setup("ftrace_graph_notrace=", set_graph_notrace_function); |
| 4216 | |
| 4217 | static void __init set_ftrace_early_graph(char *buf, int enable) |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4218 | { |
| 4219 | int ret; |
| 4220 | char *func; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4221 | unsigned long *table = ftrace_graph_funcs; |
| 4222 | int *count = &ftrace_graph_count; |
| 4223 | |
| 4224 | if (!enable) { |
| 4225 | table = ftrace_graph_notrace_funcs; |
| 4226 | count = &ftrace_graph_notrace_count; |
| 4227 | } |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4228 | |
| 4229 | while (buf) { |
| 4230 | func = strsep(&buf, ","); |
| 4231 | /* we allow only one expression at a time */ |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4232 | ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4233 | if (ret) |
| 4234 | printk(KERN_DEBUG "ftrace: function %s not " |
| 4235 | "traceable\n", func); |
| 4236 | } |
| 4237 | } |
| 4238 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4239 | |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4240 | void __init |
| 4241 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4242 | { |
| 4243 | char *func; |
| 4244 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4245 | ftrace_ops_init(ops); |
| 4246 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4247 | while (buf) { |
| 4248 | func = strsep(&buf, ","); |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4249 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | static void __init set_ftrace_early_filters(void) |
| 4254 | { |
| 4255 | if (ftrace_filter_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4256 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4257 | if (ftrace_notrace_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4258 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4259 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4260 | if (ftrace_graph_buf[0]) |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4261 | set_ftrace_early_graph(ftrace_graph_buf, 1); |
| 4262 | if (ftrace_graph_notrace_buf[0]) |
| 4263 | set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4264 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4265 | } |
| 4266 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4267 | int ftrace_regex_release(struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4268 | { |
| 4269 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 4270 | struct ftrace_iterator *iter; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4271 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4272 | struct ftrace_hash *old_hash; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4273 | struct trace_parser *parser; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4274 | int filter_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4275 | int ret; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4276 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4277 | if (file->f_mode & FMODE_READ) { |
| 4278 | iter = m->private; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4279 | seq_release(inode, file); |
| 4280 | } else |
| 4281 | iter = file->private_data; |
| 4282 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4283 | parser = &iter->parser; |
| 4284 | if (trace_parser_loaded(parser)) { |
| 4285 | parser->buffer[parser->idx] = 0; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4286 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4287 | } |
| 4288 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4289 | trace_parser_put(parser); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4290 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4291 | mutex_lock(&iter->ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4292 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4293 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4294 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 4295 | |
| 4296 | if (filter_hash) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4297 | orig_hash = &iter->ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4298 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4299 | orig_hash = &iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4300 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4301 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4302 | old_hash = *orig_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4303 | ret = ftrace_hash_move(iter->ops, filter_hash, |
| 4304 | orig_hash, iter->hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4305 | if (!ret) { |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4306 | ftrace_ops_update_code(iter->ops, old_hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4307 | free_ftrace_hash_rcu(old_hash); |
| 4308 | } |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4309 | mutex_unlock(&ftrace_lock); |
| 4310 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4311 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4312 | mutex_unlock(&iter->ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4313 | free_ftrace_hash(iter->hash); |
| 4314 | kfree(iter); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4315 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4316 | return 0; |
| 4317 | } |
| 4318 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4319 | static const struct file_operations ftrace_avail_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4320 | .open = ftrace_avail_open, |
| 4321 | .read = seq_read, |
| 4322 | .llseek = seq_lseek, |
Li Zefan | 3be04b4 | 2009-08-17 16:54:03 +0800 | [diff] [blame] | 4323 | .release = seq_release_private, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4324 | }; |
| 4325 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4326 | static const struct file_operations ftrace_enabled_fops = { |
| 4327 | .open = ftrace_enabled_open, |
| 4328 | .read = seq_read, |
| 4329 | .llseek = seq_lseek, |
| 4330 | .release = seq_release_private, |
| 4331 | }; |
| 4332 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4333 | static const struct file_operations ftrace_filter_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4334 | .open = ftrace_filter_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4335 | .read = seq_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4336 | .write = ftrace_filter_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4337 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4338 | .release = ftrace_regex_release, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4339 | }; |
| 4340 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4341 | static const struct file_operations ftrace_notrace_fops = { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4342 | .open = ftrace_notrace_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4343 | .read = seq_read, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4344 | .write = ftrace_notrace_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4345 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4346 | .release = ftrace_regex_release, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4347 | }; |
| 4348 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4349 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4350 | |
| 4351 | static DEFINE_MUTEX(graph_lock); |
| 4352 | |
| 4353 | int ftrace_graph_count; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4354 | int ftrace_graph_notrace_count; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4355 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4356 | unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4357 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4358 | struct ftrace_graph_data { |
| 4359 | unsigned long *table; |
| 4360 | size_t size; |
| 4361 | int *count; |
| 4362 | const struct seq_operations *seq_ops; |
| 4363 | }; |
| 4364 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4365 | static void * |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4366 | __g_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4367 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4368 | struct ftrace_graph_data *fgd = m->private; |
| 4369 | |
| 4370 | if (*pos >= *fgd->count) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4371 | return NULL; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4372 | return &fgd->table[*pos]; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4373 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4374 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4375 | static void * |
| 4376 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 4377 | { |
| 4378 | (*pos)++; |
| 4379 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 4383 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4384 | struct ftrace_graph_data *fgd = m->private; |
| 4385 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4386 | mutex_lock(&graph_lock); |
| 4387 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4388 | /* Nothing, tell g_show to print all functions are enabled */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4389 | if (!*fgd->count && !*pos) |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4390 | return (void *)1; |
| 4391 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4392 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | static void g_stop(struct seq_file *m, void *p) |
| 4396 | { |
| 4397 | mutex_unlock(&graph_lock); |
| 4398 | } |
| 4399 | |
| 4400 | static int g_show(struct seq_file *m, void *v) |
| 4401 | { |
| 4402 | unsigned long *ptr = v; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4403 | |
| 4404 | if (!ptr) |
| 4405 | return 0; |
| 4406 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4407 | if (ptr == (unsigned long *)1) { |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4408 | struct ftrace_graph_data *fgd = m->private; |
| 4409 | |
| 4410 | if (fgd->table == ftrace_graph_funcs) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4411 | seq_puts(m, "#### all functions enabled ####\n"); |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4412 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4413 | seq_puts(m, "#### no functions disabled ####\n"); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4414 | return 0; |
| 4415 | } |
| 4416 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 4417 | seq_printf(m, "%ps\n", (void *)*ptr); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4418 | |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 4422 | static const struct seq_operations ftrace_graph_seq_ops = { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4423 | .start = g_start, |
| 4424 | .next = g_next, |
| 4425 | .stop = g_stop, |
| 4426 | .show = g_show, |
| 4427 | }; |
| 4428 | |
| 4429 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4430 | __ftrace_graph_open(struct inode *inode, struct file *file, |
| 4431 | struct ftrace_graph_data *fgd) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4432 | { |
| 4433 | int ret = 0; |
| 4434 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4435 | mutex_lock(&graph_lock); |
| 4436 | if ((file->f_mode & FMODE_WRITE) && |
Steven Rostedt | 8650ae3 | 2009-07-22 23:29:30 -0400 | [diff] [blame] | 4437 | (file->f_flags & O_TRUNC)) { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4438 | *fgd->count = 0; |
| 4439 | memset(fgd->table, 0, fgd->size * sizeof(*fgd->table)); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4440 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4441 | mutex_unlock(&graph_lock); |
| 4442 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4443 | if (file->f_mode & FMODE_READ) { |
| 4444 | ret = seq_open(file, fgd->seq_ops); |
| 4445 | if (!ret) { |
| 4446 | struct seq_file *m = file->private_data; |
| 4447 | m->private = fgd; |
| 4448 | } |
| 4449 | } else |
| 4450 | file->private_data = fgd; |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 4451 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4452 | return ret; |
| 4453 | } |
| 4454 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4455 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4456 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 4457 | { |
| 4458 | struct ftrace_graph_data *fgd; |
| 4459 | |
| 4460 | if (unlikely(ftrace_disabled)) |
| 4461 | return -ENODEV; |
| 4462 | |
| 4463 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4464 | if (fgd == NULL) |
| 4465 | return -ENOMEM; |
| 4466 | |
| 4467 | fgd->table = ftrace_graph_funcs; |
| 4468 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 4469 | fgd->count = &ftrace_graph_count; |
| 4470 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4471 | |
| 4472 | return __ftrace_graph_open(inode, file, fgd); |
| 4473 | } |
| 4474 | |
| 4475 | static int |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4476 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) |
| 4477 | { |
| 4478 | struct ftrace_graph_data *fgd; |
| 4479 | |
| 4480 | if (unlikely(ftrace_disabled)) |
| 4481 | return -ENODEV; |
| 4482 | |
| 4483 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4484 | if (fgd == NULL) |
| 4485 | return -ENOMEM; |
| 4486 | |
| 4487 | fgd->table = ftrace_graph_notrace_funcs; |
| 4488 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 4489 | fgd->count = &ftrace_graph_notrace_count; |
| 4490 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4491 | |
| 4492 | return __ftrace_graph_open(inode, file, fgd); |
| 4493 | } |
| 4494 | |
| 4495 | static int |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4496 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 4497 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4498 | if (file->f_mode & FMODE_READ) { |
| 4499 | struct seq_file *m = file->private_data; |
| 4500 | |
| 4501 | kfree(m->private); |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4502 | seq_release(inode, file); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4503 | } else { |
| 4504 | kfree(file->private_data); |
| 4505 | } |
| 4506 | |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4507 | return 0; |
| 4508 | } |
| 4509 | |
| 4510 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4511 | ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4512 | { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4513 | struct dyn_ftrace *rec; |
| 4514 | struct ftrace_page *pg; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4515 | int search_len; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4516 | int fail = 1; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4517 | int type, not; |
| 4518 | char *search; |
| 4519 | bool exists; |
| 4520 | int i; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4521 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4522 | /* decode regex */ |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 4523 | type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4524 | if (!not && *idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4525 | return -EBUSY; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4526 | |
| 4527 | search_len = strlen(search); |
| 4528 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4529 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4530 | |
| 4531 | if (unlikely(ftrace_disabled)) { |
| 4532 | mutex_unlock(&ftrace_lock); |
| 4533 | return -ENODEV; |
| 4534 | } |
| 4535 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4536 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4537 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 4538 | if (ftrace_match_record(rec, NULL, search, search_len, type)) { |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4539 | /* if it is in the array */ |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4540 | exists = false; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4541 | for (i = 0; i < *idx; i++) { |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4542 | if (array[i] == rec->ip) { |
| 4543 | exists = true; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4544 | break; |
| 4545 | } |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | if (!not) { |
| 4549 | fail = 0; |
| 4550 | if (!exists) { |
| 4551 | array[(*idx)++] = rec->ip; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4552 | if (*idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4553 | goto out; |
| 4554 | } |
| 4555 | } else { |
| 4556 | if (exists) { |
| 4557 | array[i] = array[--(*idx)]; |
| 4558 | array[*idx] = 0; |
| 4559 | fail = 0; |
| 4560 | } |
| 4561 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4562 | } |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4563 | } while_for_each_ftrace_rec(); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4564 | out: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4565 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4566 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4567 | if (fail) |
| 4568 | return -EINVAL; |
| 4569 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4570 | return 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | static ssize_t |
| 4574 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 4575 | size_t cnt, loff_t *ppos) |
| 4576 | { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4577 | struct trace_parser parser; |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4578 | ssize_t read, ret = 0; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4579 | struct ftrace_graph_data *fgd = file->private_data; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4580 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4581 | if (!cnt) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4582 | return 0; |
| 4583 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4584 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) |
| 4585 | return -ENOMEM; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4586 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4587 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
| 4588 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4589 | if (read >= 0 && trace_parser_loaded((&parser))) { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4590 | parser.buffer[parser.idx] = 0; |
| 4591 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4592 | mutex_lock(&graph_lock); |
| 4593 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4594 | /* we allow only one expression at a time */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4595 | ret = ftrace_set_func(fgd->table, fgd->count, fgd->size, |
| 4596 | parser.buffer); |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4597 | |
| 4598 | mutex_unlock(&graph_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4599 | } |
| 4600 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4601 | if (!ret) |
| 4602 | ret = read; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 4603 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4604 | trace_parser_put(&parser); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4605 | |
| 4606 | return ret; |
| 4607 | } |
| 4608 | |
| 4609 | static const struct file_operations ftrace_graph_fops = { |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4610 | .open = ftrace_graph_open, |
| 4611 | .read = seq_read, |
| 4612 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4613 | .llseek = tracing_lseek, |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4614 | .release = ftrace_graph_release, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4615 | }; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4616 | |
| 4617 | static const struct file_operations ftrace_graph_notrace_fops = { |
| 4618 | .open = ftrace_graph_notrace_open, |
| 4619 | .read = seq_read, |
| 4620 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4621 | .llseek = tracing_lseek, |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4622 | .release = ftrace_graph_release, |
| 4623 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4624 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4625 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4626 | void ftrace_create_filter_files(struct ftrace_ops *ops, |
| 4627 | struct dentry *parent) |
| 4628 | { |
| 4629 | |
| 4630 | trace_create_file("set_ftrace_filter", 0644, parent, |
| 4631 | ops, &ftrace_filter_fops); |
| 4632 | |
| 4633 | trace_create_file("set_ftrace_notrace", 0644, parent, |
| 4634 | ops, &ftrace_notrace_fops); |
| 4635 | } |
| 4636 | |
| 4637 | /* |
| 4638 | * The name "destroy_filter_files" is really a misnomer. Although |
| 4639 | * in the future, it may actualy delete the files, but this is |
| 4640 | * really intended to make sure the ops passed in are disabled |
| 4641 | * and that when this function returns, the caller is free to |
| 4642 | * free the ops. |
| 4643 | * |
| 4644 | * The "destroy" name is only to match the "create" name that this |
| 4645 | * should be paired with. |
| 4646 | */ |
| 4647 | void ftrace_destroy_filter_files(struct ftrace_ops *ops) |
| 4648 | { |
| 4649 | mutex_lock(&ftrace_lock); |
| 4650 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 4651 | ftrace_shutdown(ops, 0); |
| 4652 | ops->flags |= FTRACE_OPS_FL_DELETED; |
| 4653 | mutex_unlock(&ftrace_lock); |
| 4654 | } |
| 4655 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4656 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4657 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4658 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4659 | trace_create_file("available_filter_functions", 0444, |
| 4660 | d_tracer, NULL, &ftrace_avail_fops); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4661 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4662 | trace_create_file("enabled_functions", 0444, |
| 4663 | d_tracer, NULL, &ftrace_enabled_fops); |
| 4664 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4665 | ftrace_create_filter_files(&global_ops, d_tracer); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 4666 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4667 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4668 | trace_create_file("set_graph_function", 0444, d_tracer, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4669 | NULL, |
| 4670 | &ftrace_graph_fops); |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4671 | trace_create_file("set_graph_notrace", 0444, d_tracer, |
| 4672 | NULL, |
| 4673 | &ftrace_graph_notrace_fops); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4674 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4675 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4676 | return 0; |
| 4677 | } |
| 4678 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4679 | static int ftrace_cmp_ips(const void *a, const void *b) |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4680 | { |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4681 | const unsigned long *ipa = a; |
| 4682 | const unsigned long *ipb = b; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4683 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4684 | if (*ipa > *ipb) |
| 4685 | return 1; |
| 4686 | if (*ipa < *ipb) |
| 4687 | return -1; |
| 4688 | return 0; |
| 4689 | } |
| 4690 | |
| 4691 | static void ftrace_swap_ips(void *a, void *b, int size) |
| 4692 | { |
| 4693 | unsigned long *ipa = a; |
| 4694 | unsigned long *ipb = b; |
| 4695 | unsigned long t; |
| 4696 | |
| 4697 | t = *ipa; |
| 4698 | *ipa = *ipb; |
| 4699 | *ipb = t; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4700 | } |
| 4701 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 4702 | static int ftrace_process_locs(struct module *mod, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4703 | unsigned long *start, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4704 | unsigned long *end) |
| 4705 | { |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4706 | struct ftrace_page *start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4707 | struct ftrace_page *pg; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4708 | struct dyn_ftrace *rec; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4709 | unsigned long count; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4710 | unsigned long *p; |
| 4711 | unsigned long addr; |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4712 | unsigned long flags = 0; /* Shut up gcc */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4713 | int ret = -ENOMEM; |
| 4714 | |
| 4715 | count = end - start; |
| 4716 | |
| 4717 | if (!count) |
| 4718 | return 0; |
| 4719 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4720 | sort(start, count, sizeof(*start), |
| 4721 | ftrace_cmp_ips, ftrace_swap_ips); |
| 4722 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4723 | start_pg = ftrace_allocate_pages(count); |
| 4724 | if (!start_pg) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4725 | return -ENOMEM; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4726 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4727 | mutex_lock(&ftrace_lock); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4728 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4729 | /* |
| 4730 | * Core and each module needs their own pages, as |
| 4731 | * modules will free them when they are removed. |
| 4732 | * Force a new page to be allocated for modules. |
| 4733 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4734 | if (!mod) { |
| 4735 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 4736 | /* First initialization */ |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4737 | ftrace_pages = ftrace_pages_start = start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4738 | } else { |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4739 | if (!ftrace_pages) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4740 | goto out; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4741 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4742 | if (WARN_ON(ftrace_pages->next)) { |
| 4743 | /* Hmm, we have free pages? */ |
| 4744 | while (ftrace_pages->next) |
| 4745 | ftrace_pages = ftrace_pages->next; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4746 | } |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4747 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4748 | ftrace_pages->next = start_pg; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4749 | } |
| 4750 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4751 | p = start; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4752 | pg = start_pg; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4753 | while (p < end) { |
| 4754 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4755 | /* |
| 4756 | * Some architecture linkers will pad between |
| 4757 | * the different mcount_loc sections of different |
| 4758 | * object files to satisfy alignments. |
| 4759 | * Skip any NULL pointers. |
| 4760 | */ |
| 4761 | if (!addr) |
| 4762 | continue; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4763 | |
| 4764 | if (pg->index == pg->size) { |
| 4765 | /* We should have allocated enough */ |
| 4766 | if (WARN_ON(!pg->next)) |
| 4767 | break; |
| 4768 | pg = pg->next; |
| 4769 | } |
| 4770 | |
| 4771 | rec = &pg->records[pg->index++]; |
| 4772 | rec->ip = addr; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4773 | } |
| 4774 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4775 | /* We should have used all pages */ |
| 4776 | WARN_ON(pg->next); |
| 4777 | |
| 4778 | /* Assign the last page to ftrace_pages */ |
| 4779 | ftrace_pages = pg; |
| 4780 | |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4781 | /* |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4782 | * We only need to disable interrupts on start up |
| 4783 | * because we are modifying code that an interrupt |
| 4784 | * may execute, and the modification is not atomic. |
| 4785 | * But for modules, nothing runs the code we modify |
| 4786 | * until we are finished with it, and there's no |
| 4787 | * reason to cause large interrupt latencies while we do it. |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4788 | */ |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4789 | if (!mod) |
| 4790 | local_irq_save(flags); |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 4791 | ftrace_update_code(mod, start_pg); |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4792 | if (!mod) |
| 4793 | local_irq_restore(flags); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4794 | ret = 0; |
| 4795 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4796 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4797 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4798 | return ret; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4799 | } |
| 4800 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4801 | #ifdef CONFIG_MODULES |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4802 | |
| 4803 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 4804 | |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4805 | void ftrace_release_mod(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4806 | { |
| 4807 | struct dyn_ftrace *rec; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4808 | struct ftrace_page **last_pg; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4809 | struct ftrace_page *pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4810 | int order; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4811 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4812 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4813 | |
| 4814 | if (ftrace_disabled) |
| 4815 | goto out_unlock; |
| 4816 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4817 | /* |
| 4818 | * Each module has its own ftrace_pages, remove |
| 4819 | * them from the list. |
| 4820 | */ |
| 4821 | last_pg = &ftrace_pages_start; |
| 4822 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 4823 | rec = &pg->records[0]; |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4824 | if (within_module_core(rec->ip, mod)) { |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4825 | /* |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4826 | * As core pages are first, the first |
| 4827 | * page should never be a module page. |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4828 | */ |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4829 | if (WARN_ON(pg == ftrace_pages_start)) |
| 4830 | goto out_unlock; |
| 4831 | |
| 4832 | /* Check if we are deleting the last page */ |
| 4833 | if (pg == ftrace_pages) |
| 4834 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 4835 | |
| 4836 | *last_pg = pg->next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4837 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 4838 | free_pages((unsigned long)pg->records, order); |
| 4839 | kfree(pg); |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4840 | } else |
| 4841 | last_pg = &pg->next; |
| 4842 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4843 | out_unlock: |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4844 | mutex_unlock(&ftrace_lock); |
| 4845 | } |
| 4846 | |
| 4847 | static void ftrace_init_module(struct module *mod, |
| 4848 | unsigned long *start, unsigned long *end) |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 4849 | { |
Steven Rostedt | 00fd61a | 2008-08-15 21:40:04 -0400 | [diff] [blame] | 4850 | if (ftrace_disabled || start == end) |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 4851 | return; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 4852 | ftrace_process_locs(mod, start, end); |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 4853 | } |
| 4854 | |
Steven Rostedt (Red Hat) | a949ae5 | 2014-04-24 10:40:12 -0400 | [diff] [blame] | 4855 | void ftrace_module_init(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4856 | { |
Steven Rostedt (Red Hat) | a949ae5 | 2014-04-24 10:40:12 -0400 | [diff] [blame] | 4857 | ftrace_init_module(mod, mod->ftrace_callsites, |
| 4858 | mod->ftrace_callsites + |
| 4859 | mod->num_ftrace_callsites); |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | static int ftrace_module_notify_exit(struct notifier_block *self, |
| 4863 | unsigned long val, void *data) |
| 4864 | { |
| 4865 | struct module *mod = data; |
| 4866 | |
| 4867 | if (val == MODULE_STATE_GOING) |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4868 | ftrace_release_mod(mod); |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4869 | |
| 4870 | return 0; |
| 4871 | } |
| 4872 | #else |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4873 | static int ftrace_module_notify_exit(struct notifier_block *self, |
| 4874 | unsigned long val, void *data) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4875 | { |
| 4876 | return 0; |
| 4877 | } |
| 4878 | #endif /* CONFIG_MODULES */ |
| 4879 | |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4880 | struct notifier_block ftrace_module_exit_nb = { |
| 4881 | .notifier_call = ftrace_module_notify_exit, |
| 4882 | .priority = INT_MIN, /* Run after anything that can remove kprobes */ |
| 4883 | }; |
| 4884 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4885 | void __init ftrace_init(void) |
| 4886 | { |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 4887 | extern unsigned long __start_mcount_loc[]; |
| 4888 | extern unsigned long __stop_mcount_loc[]; |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 4889 | unsigned long count, flags; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4890 | int ret; |
| 4891 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4892 | local_irq_save(flags); |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 4893 | ret = ftrace_dyn_arch_init(); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4894 | local_irq_restore(flags); |
Jiri Slaby | af64a7c | 2014-02-24 19:59:58 +0100 | [diff] [blame] | 4895 | if (ret) |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4896 | goto failed; |
| 4897 | |
| 4898 | count = __stop_mcount_loc - __start_mcount_loc; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 4899 | if (!count) { |
| 4900 | pr_info("ftrace: No functions to be traced?\n"); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4901 | goto failed; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | pr_info("ftrace: allocating %ld entries in %ld pages\n", |
| 4905 | count, count / ENTRIES_PER_PAGE + 1); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4906 | |
| 4907 | last_ftrace_enabled = ftrace_enabled = 1; |
| 4908 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 4909 | ret = ftrace_process_locs(NULL, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4910 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4911 | __stop_mcount_loc); |
| 4912 | |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 4913 | ret = register_module_notifier(&ftrace_module_exit_nb); |
| 4914 | if (ret) |
| 4915 | pr_warning("Failed to register trace ftrace module exit notifier\n"); |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4916 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4917 | set_ftrace_early_filters(); |
| 4918 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4919 | return; |
| 4920 | failed: |
| 4921 | ftrace_disabled = 1; |
| 4922 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4923 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 4924 | /* Do nothing if arch does not support this */ |
| 4925 | void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) |
| 4926 | { |
| 4927 | } |
| 4928 | |
| 4929 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 4930 | { |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 4931 | |
| 4932 | /* |
| 4933 | * Currently there's no safe way to free a trampoline when the kernel |
| 4934 | * is configured with PREEMPT. That is because a task could be preempted |
| 4935 | * when it jumped to the trampoline, it may be preempted for a long time |
| 4936 | * depending on the system load, and currently there's no way to know |
| 4937 | * when it will be off the trampoline. If the trampoline is freed |
| 4938 | * too early, when the task runs again, it will be executing on freed |
| 4939 | * memory and crash. |
| 4940 | */ |
| 4941 | #ifdef CONFIG_PREEMPT |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 4942 | /* Currently, only non dynamic ops can have a trampoline */ |
| 4943 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) |
| 4944 | return; |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 4945 | #endif |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 4946 | |
| 4947 | arch_ftrace_update_trampoline(ops); |
| 4948 | } |
| 4949 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4950 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 4951 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 4952 | static struct ftrace_ops global_ops = { |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 4953 | .func = ftrace_stub, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4954 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 4955 | }; |
| 4956 | |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 4957 | static int __init ftrace_nodyn_init(void) |
| 4958 | { |
| 4959 | ftrace_enabled = 1; |
| 4960 | return 0; |
| 4961 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 4962 | core_initcall(ftrace_nodyn_init); |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 4963 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4964 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
| 4965 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 4966 | static inline void ftrace_startup_all(int command) { } |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 4967 | /* 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] | 4968 | # define ftrace_startup(ops, command) \ |
| 4969 | ({ \ |
| 4970 | int ___ret = __register_ftrace_function(ops); \ |
| 4971 | if (!___ret) \ |
| 4972 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ |
| 4973 | ___ret; \ |
Steven Rostedt | 3b6cfdb | 2011-05-23 15:33:49 -0400 | [diff] [blame] | 4974 | }) |
Steven Rostedt (Red Hat) | 1fcc155 | 2014-02-19 15:12:18 -0500 | [diff] [blame] | 4975 | # define ftrace_shutdown(ops, command) \ |
| 4976 | ({ \ |
| 4977 | int ___ret = __unregister_ftrace_function(ops); \ |
| 4978 | if (!___ret) \ |
| 4979 | (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \ |
| 4980 | ___ret; \ |
| 4981 | }) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 4982 | |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4983 | # define ftrace_startup_sysctl() do { } while (0) |
| 4984 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4985 | |
| 4986 | static inline int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 4987 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4988 | { |
| 4989 | return 1; |
| 4990 | } |
| 4991 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 4992 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 4993 | { |
| 4994 | } |
| 4995 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4996 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 4997 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 4998 | __init void ftrace_init_global_array_ops(struct trace_array *tr) |
| 4999 | { |
| 5000 | tr->ops = &global_ops; |
| 5001 | tr->ops->private = tr; |
| 5002 | } |
| 5003 | |
| 5004 | void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) |
| 5005 | { |
| 5006 | /* If we filter on pids, update to use the pid function */ |
| 5007 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { |
| 5008 | if (WARN_ON(tr->ops->func != ftrace_stub)) |
| 5009 | printk("ftrace ops had %pS for function\n", |
| 5010 | tr->ops->func); |
| 5011 | /* Only the top level instance does pid tracing */ |
| 5012 | if (!list_empty(&ftrace_pids)) { |
| 5013 | set_ftrace_pid_function(func); |
| 5014 | func = ftrace_pid_func; |
| 5015 | } |
| 5016 | } |
| 5017 | tr->ops->func = func; |
| 5018 | tr->ops->private = tr; |
| 5019 | } |
| 5020 | |
| 5021 | void ftrace_reset_array_ops(struct trace_array *tr) |
| 5022 | { |
| 5023 | tr->ops->func = ftrace_stub; |
| 5024 | } |
| 5025 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5026 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5027 | ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5028 | struct ftrace_ops *op, struct pt_regs *regs) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 5029 | { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 5030 | if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT))) |
| 5031 | return; |
| 5032 | |
| 5033 | /* |
| 5034 | * Some of the ops may be dynamically allocated, |
| 5035 | * they must be freed after a synchronize_sched(). |
| 5036 | */ |
| 5037 | preempt_disable_notrace(); |
| 5038 | trace_recursion_set(TRACE_CONTROL_BIT); |
Steven Rostedt (Red Hat) | b5aa3a4 | 2013-11-04 18:34:44 -0500 | [diff] [blame] | 5039 | |
| 5040 | /* |
| 5041 | * Control funcs (perf) uses RCU. Only trace if |
| 5042 | * RCU is currently active. |
| 5043 | */ |
| 5044 | if (!rcu_is_watching()) |
| 5045 | goto out; |
| 5046 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5047 | do_for_each_ftrace_op(op, ftrace_control_list) { |
Steven Rostedt (Red Hat) | 395b97a | 2013-03-27 09:31:28 -0400 | [diff] [blame] | 5048 | if (!(op->flags & FTRACE_OPS_FL_STUB) && |
| 5049 | !ftrace_function_local_disabled(op) && |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 5050 | ftrace_ops_test(op, ip, regs)) |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5051 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5052 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | b5aa3a4 | 2013-11-04 18:34:44 -0500 | [diff] [blame] | 5053 | out: |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 5054 | trace_recursion_clear(TRACE_CONTROL_BIT); |
| 5055 | preempt_enable_notrace(); |
| 5056 | } |
| 5057 | |
| 5058 | static struct ftrace_ops control_ops = { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 5059 | .func = ftrace_ops_control_func, |
| 5060 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 5061 | INIT_OPS_HASH(control_ops) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 5062 | }; |
| 5063 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5064 | static inline void |
| 5065 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5066 | struct ftrace_ops *ignored, struct pt_regs *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5067 | { |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5068 | struct ftrace_ops *op; |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5069 | int bit; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5070 | |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5071 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5072 | if (bit < 0) |
| 5073 | return; |
Steven Rostedt | c29f122 | 2012-11-02 17:17:59 -0400 | [diff] [blame] | 5074 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5075 | /* |
| 5076 | * Some of the ops may be dynamically allocated, |
| 5077 | * they must be freed after a synchronize_sched(). |
| 5078 | */ |
| 5079 | preempt_disable_notrace(); |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5080 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5081 | if (ftrace_ops_test(op, ip, regs)) { |
Steven Rostedt (Red Hat) | 1d48d59 | 2014-06-25 11:54:03 -0400 | [diff] [blame] | 5082 | if (FTRACE_WARN_ON(!op->func)) { |
| 5083 | pr_warn("op=%p %pS\n", op, op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5084 | goto out; |
| 5085 | } |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5086 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5087 | } |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5088 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5089 | out: |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5090 | preempt_enable_notrace(); |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5091 | trace_clear_recursion(bit); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5092 | } |
| 5093 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5094 | /* |
| 5095 | * Some archs only support passing ip and parent_ip. Even though |
| 5096 | * the list function ignores the op parameter, we do not want any |
| 5097 | * C side effects, where a function is called without the caller |
| 5098 | * sending a third parameter. |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5099 | * Archs are to support both the regs and ftrace_ops at the same time. |
| 5100 | * If they support ftrace_ops, it is assumed they support regs. |
| 5101 | * 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] | 5102 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
| 5103 | * 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] | 5104 | * An architecture can pass partial regs with ftrace_ops and still |
| 5105 | * set the ARCH_SUPPORT_FTARCE_OPS. |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5106 | */ |
| 5107 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 5108 | 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] | 5109 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5110 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5111 | __ftrace_ops_list_func(ip, parent_ip, NULL, regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5112 | } |
| 5113 | #else |
| 5114 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip) |
| 5115 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5116 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5117 | } |
| 5118 | #endif |
| 5119 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5120 | /* |
| 5121 | * If there's only one function registered but it does not support |
| 5122 | * recursion, this function will be called by the mcount trampoline. |
| 5123 | * This function will handle recursion protection. |
| 5124 | */ |
| 5125 | static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip, |
| 5126 | struct ftrace_ops *op, struct pt_regs *regs) |
| 5127 | { |
| 5128 | int bit; |
| 5129 | |
| 5130 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5131 | if (bit < 0) |
| 5132 | return; |
| 5133 | |
| 5134 | op->func(ip, parent_ip, op, regs); |
| 5135 | |
| 5136 | trace_clear_recursion(bit); |
| 5137 | } |
| 5138 | |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5139 | /** |
| 5140 | * ftrace_ops_get_func - get the function a trampoline should call |
| 5141 | * @ops: the ops to get the function for |
| 5142 | * |
| 5143 | * Normally the mcount trampoline will call the ops->func, but there |
| 5144 | * are times that it should not. For example, if the ops does not |
| 5145 | * have its own recursion protection, then it should call the |
| 5146 | * ftrace_ops_recurs_func() instead. |
| 5147 | * |
| 5148 | * Returns the function that the trampoline should call for @ops. |
| 5149 | */ |
| 5150 | ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) |
| 5151 | { |
| 5152 | /* |
| 5153 | * If this is a dynamic ops or we force list func, |
| 5154 | * then it needs to call the list anyway. |
| 5155 | */ |
| 5156 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC) |
| 5157 | return ftrace_ops_list_func; |
| 5158 | |
| 5159 | /* |
| 5160 | * If the func handles its own recursion, call it directly. |
| 5161 | * Otherwise call the recursion protected function that |
| 5162 | * will call the ftrace ops function. |
| 5163 | */ |
| 5164 | if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE)) |
| 5165 | return ftrace_ops_recurs_func; |
| 5166 | |
| 5167 | return ops->func; |
| 5168 | } |
| 5169 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5170 | static void clear_ftrace_swapper(void) |
| 5171 | { |
| 5172 | struct task_struct *p; |
| 5173 | int cpu; |
| 5174 | |
| 5175 | get_online_cpus(); |
| 5176 | for_each_online_cpu(cpu) { |
| 5177 | p = idle_task(cpu); |
| 5178 | clear_tsk_trace_trace(p); |
| 5179 | } |
| 5180 | put_online_cpus(); |
| 5181 | } |
| 5182 | |
| 5183 | static void set_ftrace_swapper(void) |
| 5184 | { |
| 5185 | struct task_struct *p; |
| 5186 | int cpu; |
| 5187 | |
| 5188 | get_online_cpus(); |
| 5189 | for_each_online_cpu(cpu) { |
| 5190 | p = idle_task(cpu); |
| 5191 | set_tsk_trace_trace(p); |
| 5192 | } |
| 5193 | put_online_cpus(); |
| 5194 | } |
| 5195 | |
| 5196 | static void clear_ftrace_pid(struct pid *pid) |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5197 | { |
| 5198 | struct task_struct *p; |
| 5199 | |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 5200 | rcu_read_lock(); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5201 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5202 | clear_tsk_trace_trace(p); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5203 | } while_each_pid_task(pid, PIDTYPE_PID, p); |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 5204 | rcu_read_unlock(); |
| 5205 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5206 | put_pid(pid); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5207 | } |
| 5208 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5209 | static void set_ftrace_pid(struct pid *pid) |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5210 | { |
| 5211 | struct task_struct *p; |
| 5212 | |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 5213 | rcu_read_lock(); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5214 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
| 5215 | set_tsk_trace_trace(p); |
| 5216 | } while_each_pid_task(pid, PIDTYPE_PID, p); |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 5217 | rcu_read_unlock(); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5218 | } |
| 5219 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5220 | static void clear_ftrace_pid_task(struct pid *pid) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5221 | { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5222 | if (pid == ftrace_swapper_pid) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5223 | clear_ftrace_swapper(); |
| 5224 | else |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5225 | clear_ftrace_pid(pid); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | static void set_ftrace_pid_task(struct pid *pid) |
| 5229 | { |
| 5230 | if (pid == ftrace_swapper_pid) |
| 5231 | set_ftrace_swapper(); |
| 5232 | else |
| 5233 | set_ftrace_pid(pid); |
| 5234 | } |
| 5235 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5236 | static int ftrace_pid_add(int p) |
| 5237 | { |
| 5238 | struct pid *pid; |
| 5239 | struct ftrace_pid *fpid; |
| 5240 | int ret = -EINVAL; |
| 5241 | |
| 5242 | mutex_lock(&ftrace_lock); |
| 5243 | |
| 5244 | if (!p) |
| 5245 | pid = ftrace_swapper_pid; |
| 5246 | else |
| 5247 | pid = find_get_pid(p); |
| 5248 | |
| 5249 | if (!pid) |
| 5250 | goto out; |
| 5251 | |
| 5252 | ret = 0; |
| 5253 | |
| 5254 | list_for_each_entry(fpid, &ftrace_pids, list) |
| 5255 | if (fpid->pid == pid) |
| 5256 | goto out_put; |
| 5257 | |
| 5258 | ret = -ENOMEM; |
| 5259 | |
| 5260 | fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); |
| 5261 | if (!fpid) |
| 5262 | goto out_put; |
| 5263 | |
| 5264 | list_add(&fpid->list, &ftrace_pids); |
| 5265 | fpid->pid = pid; |
| 5266 | |
| 5267 | set_ftrace_pid_task(pid); |
| 5268 | |
| 5269 | ftrace_update_pid_func(); |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5270 | |
| 5271 | ftrace_startup_all(0); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5272 | |
| 5273 | mutex_unlock(&ftrace_lock); |
| 5274 | return 0; |
| 5275 | |
| 5276 | out_put: |
| 5277 | if (pid != ftrace_swapper_pid) |
| 5278 | put_pid(pid); |
| 5279 | |
| 5280 | out: |
| 5281 | mutex_unlock(&ftrace_lock); |
| 5282 | return ret; |
| 5283 | } |
| 5284 | |
| 5285 | static void ftrace_pid_reset(void) |
| 5286 | { |
| 5287 | struct ftrace_pid *fpid, *safe; |
| 5288 | |
| 5289 | mutex_lock(&ftrace_lock); |
| 5290 | list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { |
| 5291 | struct pid *pid = fpid->pid; |
| 5292 | |
| 5293 | clear_ftrace_pid_task(pid); |
| 5294 | |
| 5295 | list_del(&fpid->list); |
| 5296 | kfree(fpid); |
| 5297 | } |
| 5298 | |
| 5299 | ftrace_update_pid_func(); |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5300 | ftrace_startup_all(0); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5301 | |
| 5302 | mutex_unlock(&ftrace_lock); |
| 5303 | } |
| 5304 | |
| 5305 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
| 5306 | { |
| 5307 | mutex_lock(&ftrace_lock); |
| 5308 | |
| 5309 | if (list_empty(&ftrace_pids) && (!*pos)) |
| 5310 | return (void *) 1; |
| 5311 | |
| 5312 | return seq_list_start(&ftrace_pids, *pos); |
| 5313 | } |
| 5314 | |
| 5315 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 5316 | { |
| 5317 | if (v == (void *)1) |
| 5318 | return NULL; |
| 5319 | |
| 5320 | return seq_list_next(v, &ftrace_pids, pos); |
| 5321 | } |
| 5322 | |
| 5323 | static void fpid_stop(struct seq_file *m, void *p) |
| 5324 | { |
| 5325 | mutex_unlock(&ftrace_lock); |
| 5326 | } |
| 5327 | |
| 5328 | static int fpid_show(struct seq_file *m, void *v) |
| 5329 | { |
| 5330 | const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); |
| 5331 | |
| 5332 | if (v == (void *)1) { |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 5333 | seq_puts(m, "no pid\n"); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5334 | return 0; |
| 5335 | } |
| 5336 | |
| 5337 | if (fpid->pid == ftrace_swapper_pid) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 5338 | seq_puts(m, "swapper tasks\n"); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5339 | else |
| 5340 | seq_printf(m, "%u\n", pid_vnr(fpid->pid)); |
| 5341 | |
| 5342 | return 0; |
| 5343 | } |
| 5344 | |
| 5345 | static const struct seq_operations ftrace_pid_sops = { |
| 5346 | .start = fpid_start, |
| 5347 | .next = fpid_next, |
| 5348 | .stop = fpid_stop, |
| 5349 | .show = fpid_show, |
| 5350 | }; |
| 5351 | |
| 5352 | static int |
| 5353 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 5354 | { |
| 5355 | int ret = 0; |
| 5356 | |
| 5357 | if ((file->f_mode & FMODE_WRITE) && |
| 5358 | (file->f_flags & O_TRUNC)) |
| 5359 | ftrace_pid_reset(); |
| 5360 | |
| 5361 | if (file->f_mode & FMODE_READ) |
| 5362 | ret = seq_open(file, &ftrace_pid_sops); |
| 5363 | |
| 5364 | return ret; |
| 5365 | } |
| 5366 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5367 | static ssize_t |
| 5368 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 5369 | size_t cnt, loff_t *ppos) |
| 5370 | { |
Ingo Molnar | 457dc92 | 2009-11-23 11:03:28 +0100 | [diff] [blame] | 5371 | char buf[64], *tmp; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5372 | long val; |
| 5373 | int ret; |
| 5374 | |
| 5375 | if (cnt >= sizeof(buf)) |
| 5376 | return -EINVAL; |
| 5377 | |
| 5378 | if (copy_from_user(&buf, ubuf, cnt)) |
| 5379 | return -EFAULT; |
| 5380 | |
| 5381 | buf[cnt] = 0; |
| 5382 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5383 | /* |
| 5384 | * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" |
| 5385 | * to clean the filter quietly. |
| 5386 | */ |
Ingo Molnar | 457dc92 | 2009-11-23 11:03:28 +0100 | [diff] [blame] | 5387 | tmp = strstrip(buf); |
| 5388 | if (strlen(tmp) == 0) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5389 | return 1; |
| 5390 | |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 5391 | ret = kstrtol(tmp, 10, &val); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5392 | if (ret < 0) |
| 5393 | return ret; |
| 5394 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5395 | ret = ftrace_pid_add(val); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5396 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5397 | return ret ? ret : cnt; |
| 5398 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5399 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5400 | static int |
| 5401 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 5402 | { |
| 5403 | if (file->f_mode & FMODE_READ) |
| 5404 | seq_release(inode, file); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5405 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5406 | return 0; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5407 | } |
| 5408 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 5409 | static const struct file_operations ftrace_pid_fops = { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5410 | .open = ftrace_pid_open, |
| 5411 | .write = ftrace_pid_write, |
| 5412 | .read = seq_read, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5413 | .llseek = tracing_lseek, |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5414 | .release = ftrace_pid_release, |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5415 | }; |
| 5416 | |
| 5417 | static __init int ftrace_init_debugfs(void) |
| 5418 | { |
| 5419 | struct dentry *d_tracer; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5420 | |
| 5421 | d_tracer = tracing_init_dentry(); |
| 5422 | if (!d_tracer) |
| 5423 | return 0; |
| 5424 | |
| 5425 | ftrace_init_dyn_debugfs(d_tracer); |
| 5426 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 5427 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
| 5428 | NULL, &ftrace_pid_fops); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 5429 | |
| 5430 | ftrace_profile_debugfs(d_tracer); |
| 5431 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5432 | return 0; |
| 5433 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5434 | fs_initcall(ftrace_init_debugfs); |
| 5435 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5436 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5437 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5438 | * |
| 5439 | * This function should be used by panic code. It stops ftrace |
| 5440 | * but in a not so nice way. If you need to simply kill ftrace |
| 5441 | * from a non-atomic section, use ftrace_kill. |
| 5442 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5443 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5444 | { |
| 5445 | ftrace_disabled = 1; |
| 5446 | ftrace_enabled = 0; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5447 | clear_ftrace_function(); |
| 5448 | } |
| 5449 | |
| 5450 | /** |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 5451 | * Test if ftrace is dead or not. |
| 5452 | */ |
| 5453 | int ftrace_is_dead(void) |
| 5454 | { |
| 5455 | return ftrace_disabled; |
| 5456 | } |
| 5457 | |
| 5458 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5459 | * register_ftrace_function - register a function for profiling |
| 5460 | * @ops - ops structure that holds the function for profiling. |
| 5461 | * |
| 5462 | * Register a function to be called by all functions in the |
| 5463 | * kernel. |
| 5464 | * |
| 5465 | * Note: @ops->func and all the functions it calls must be labeled |
| 5466 | * with "notrace", otherwise it will go into a |
| 5467 | * recursive loop. |
| 5468 | */ |
| 5469 | int register_ftrace_function(struct ftrace_ops *ops) |
| 5470 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5471 | int ret = -1; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5472 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 5473 | ftrace_ops_init(ops); |
| 5474 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5475 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5476 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5477 | ret = ftrace_startup(ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5478 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5479 | mutex_unlock(&ftrace_lock); |
Borislav Petkov | 8d240dd | 2012-03-29 19:11:40 +0200 | [diff] [blame] | 5480 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5481 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5482 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5483 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5484 | |
| 5485 | /** |
Uwe Kleine-Koenig | 3263292 | 2009-01-12 23:35:50 +0100 | [diff] [blame] | 5486 | * unregister_ftrace_function - unregister a function for profiling. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5487 | * @ops - ops structure that holds the function to unregister |
| 5488 | * |
| 5489 | * Unregister a function that was added to be called by ftrace profiling. |
| 5490 | */ |
| 5491 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 5492 | { |
| 5493 | int ret; |
| 5494 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5495 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5496 | ret = ftrace_shutdown(ops, 0); |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5497 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5498 | |
| 5499 | return ret; |
| 5500 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5501 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5502 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 5503 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5504 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 5505 | void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5506 | loff_t *ppos) |
| 5507 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5508 | int ret = -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5509 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5510 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5511 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5512 | if (unlikely(ftrace_disabled)) |
| 5513 | goto out; |
| 5514 | |
| 5515 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5516 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5517 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5518 | goto out; |
| 5519 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5520 | last_ftrace_enabled = !!ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5521 | |
| 5522 | if (ftrace_enabled) { |
| 5523 | |
| 5524 | ftrace_startup_sysctl(); |
| 5525 | |
| 5526 | /* we are starting ftrace again */ |
Jan Kiszka | 5000c41 | 2013-03-26 17:53:03 +0100 | [diff] [blame] | 5527 | if (ftrace_ops_list != &ftrace_list_end) |
| 5528 | update_ftrace_function(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5529 | |
| 5530 | } else { |
| 5531 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 5532 | ftrace_trace_function = ftrace_stub; |
| 5533 | |
| 5534 | ftrace_shutdown_sysctl(); |
| 5535 | } |
| 5536 | |
| 5537 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5538 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5539 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5540 | } |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 5541 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5542 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5543 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5544 | static struct ftrace_ops graph_ops = { |
| 5545 | .func = ftrace_stub, |
| 5546 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 5547 | FTRACE_OPS_FL_INITIALIZED | |
| 5548 | FTRACE_OPS_FL_STUB, |
| 5549 | #ifdef FTRACE_GRAPH_TRAMP_ADDR |
| 5550 | .trampoline = FTRACE_GRAPH_TRAMP_ADDR, |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 5551 | /* trampoline_size is only needed for dynamically allocated tramps */ |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5552 | #endif |
| 5553 | ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash) |
| 5554 | }; |
| 5555 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5556 | static int ftrace_graph_active; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5557 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5558 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 5559 | { |
| 5560 | return 0; |
| 5561 | } |
| 5562 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5563 | /* The callbacks that hook a function */ |
| 5564 | trace_func_graph_ret_t ftrace_graph_return = |
| 5565 | (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5566 | 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] | 5567 | 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] | 5568 | |
| 5569 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 5570 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 5571 | { |
| 5572 | int i; |
| 5573 | int ret = 0; |
| 5574 | unsigned long flags; |
| 5575 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 5576 | struct task_struct *g, *t; |
| 5577 | |
| 5578 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 5579 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH |
| 5580 | * sizeof(struct ftrace_ret_stack), |
| 5581 | GFP_KERNEL); |
| 5582 | if (!ret_stack_list[i]) { |
| 5583 | start = 0; |
| 5584 | end = i; |
| 5585 | ret = -ENOMEM; |
| 5586 | goto free; |
| 5587 | } |
| 5588 | } |
| 5589 | |
| 5590 | read_lock_irqsave(&tasklist_lock, flags); |
| 5591 | do_each_thread(g, t) { |
| 5592 | if (start == end) { |
| 5593 | ret = -EAGAIN; |
| 5594 | goto unlock; |
| 5595 | } |
| 5596 | |
| 5597 | if (t->ret_stack == NULL) { |
Frederic Weisbecker | 380c4b1 | 2008-12-06 03:43:41 +0100 | [diff] [blame] | 5598 | atomic_set(&t->tracing_graph_pause, 0); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5599 | atomic_set(&t->trace_overrun, 0); |
Steven Rostedt | 26c0162 | 2009-06-02 14:01:19 -0400 | [diff] [blame] | 5600 | t->curr_ret_stack = -1; |
| 5601 | /* Make sure the tasks see the -1 first: */ |
| 5602 | smp_wmb(); |
| 5603 | t->ret_stack = ret_stack_list[start++]; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5604 | } |
| 5605 | } while_each_thread(g, t); |
| 5606 | |
| 5607 | unlock: |
| 5608 | read_unlock_irqrestore(&tasklist_lock, flags); |
| 5609 | free: |
| 5610 | for (i = start; i < end; i++) |
| 5611 | kfree(ret_stack_list[i]); |
| 5612 | return ret; |
| 5613 | } |
| 5614 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5615 | static void |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5616 | ftrace_graph_probe_sched_switch(void *ignore, |
| 5617 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5618 | { |
| 5619 | unsigned long long timestamp; |
| 5620 | int index; |
| 5621 | |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 5622 | /* |
| 5623 | * Does the user want to count the time a function was asleep. |
| 5624 | * If so, do not update the time stamps. |
| 5625 | */ |
| 5626 | if (trace_flags & TRACE_ITER_SLEEP_TIME) |
| 5627 | return; |
| 5628 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5629 | timestamp = trace_clock_local(); |
| 5630 | |
| 5631 | prev->ftrace_timestamp = timestamp; |
| 5632 | |
| 5633 | /* only process tasks that we timestamped */ |
| 5634 | if (!next->ftrace_timestamp) |
| 5635 | return; |
| 5636 | |
| 5637 | /* |
| 5638 | * Update all the counters in next to make up for the |
| 5639 | * time next was sleeping. |
| 5640 | */ |
| 5641 | timestamp -= next->ftrace_timestamp; |
| 5642 | |
| 5643 | for (index = next->curr_ret_stack; index >= 0; index--) |
| 5644 | next->ret_stack[index].calltime += timestamp; |
| 5645 | } |
| 5646 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5647 | /* Allocate a return stack for each task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5648 | static int start_graph_tracing(void) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5649 | { |
| 5650 | struct ftrace_ret_stack **ret_stack_list; |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5651 | int ret, cpu; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5652 | |
| 5653 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * |
| 5654 | sizeof(struct ftrace_ret_stack *), |
| 5655 | GFP_KERNEL); |
| 5656 | |
| 5657 | if (!ret_stack_list) |
| 5658 | return -ENOMEM; |
| 5659 | |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5660 | /* The cpu_boot init_task->ret_stack will never be freed */ |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 5661 | for_each_online_cpu(cpu) { |
| 5662 | if (!idle_task(cpu)->ret_stack) |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5663 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 5664 | } |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5665 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5666 | do { |
| 5667 | ret = alloc_retstack_tasklist(ret_stack_list); |
| 5668 | } while (ret == -EAGAIN); |
| 5669 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5670 | if (!ret) { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5671 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5672 | if (ret) |
| 5673 | pr_info("ftrace_graph: Couldn't activate tracepoint" |
| 5674 | " probe to kernel_sched_switch\n"); |
| 5675 | } |
| 5676 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5677 | kfree(ret_stack_list); |
| 5678 | return ret; |
| 5679 | } |
| 5680 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5681 | /* |
| 5682 | * Hibernation protection. |
| 5683 | * The state of the current task is too much unstable during |
| 5684 | * suspend/restore to disk. We want to protect against that. |
| 5685 | */ |
| 5686 | static int |
| 5687 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, |
| 5688 | void *unused) |
| 5689 | { |
| 5690 | switch (state) { |
| 5691 | case PM_HIBERNATION_PREPARE: |
| 5692 | pause_graph_tracing(); |
| 5693 | break; |
| 5694 | |
| 5695 | case PM_POST_HIBERNATION: |
| 5696 | unpause_graph_tracing(); |
| 5697 | break; |
| 5698 | } |
| 5699 | return NOTIFY_DONE; |
| 5700 | } |
| 5701 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5702 | static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) |
| 5703 | { |
| 5704 | if (!ftrace_ops_test(&global_ops, trace->func, NULL)) |
| 5705 | return 0; |
| 5706 | return __ftrace_graph_entry(trace); |
| 5707 | } |
| 5708 | |
| 5709 | /* |
| 5710 | * The function graph tracer should only trace the functions defined |
| 5711 | * by set_ftrace_filter and set_ftrace_notrace. If another function |
| 5712 | * tracer ops is registered, the graph tracer requires testing the |
| 5713 | * function against the global ops, and not just trace any function |
| 5714 | * that any ftrace_ops registered. |
| 5715 | */ |
| 5716 | static void update_function_graph_func(void) |
| 5717 | { |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5718 | struct ftrace_ops *op; |
| 5719 | bool do_test = false; |
| 5720 | |
| 5721 | /* |
| 5722 | * The graph and global ops share the same set of functions |
| 5723 | * to test. If any other ops is on the list, then |
| 5724 | * the graph tracing needs to test if its the function |
| 5725 | * it should call. |
| 5726 | */ |
| 5727 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 5728 | if (op != &global_ops && op != &graph_ops && |
| 5729 | op != &ftrace_list_end) { |
| 5730 | do_test = true; |
| 5731 | /* in double loop, break out with goto */ |
| 5732 | goto out; |
| 5733 | } |
| 5734 | } while_for_each_ftrace_op(op); |
| 5735 | out: |
| 5736 | if (do_test) |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5737 | ftrace_graph_entry = ftrace_graph_entry_test; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5738 | else |
| 5739 | ftrace_graph_entry = __ftrace_graph_entry; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5740 | } |
| 5741 | |
Mathias Krause | 8275f69 | 2014-03-30 15:31:50 +0200 | [diff] [blame] | 5742 | static struct notifier_block ftrace_suspend_notifier = { |
| 5743 | .notifier_call = ftrace_suspend_notifier_call, |
| 5744 | }; |
| 5745 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5746 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 5747 | trace_func_graph_ent_t entryfunc) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5748 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5749 | int ret = 0; |
| 5750 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5751 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5752 | |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5753 | /* we currently allow only one tracer registered at a time */ |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5754 | if (ftrace_graph_active) { |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5755 | ret = -EBUSY; |
| 5756 | goto out; |
| 5757 | } |
| 5758 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5759 | register_pm_notifier(&ftrace_suspend_notifier); |
| 5760 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5761 | ftrace_graph_active++; |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5762 | ret = start_graph_tracing(); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5763 | if (ret) { |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5764 | ftrace_graph_active--; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5765 | goto out; |
| 5766 | } |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5767 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5768 | ftrace_graph_return = retfunc; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5769 | |
| 5770 | /* |
| 5771 | * Update the indirect function to the entryfunc, and the |
| 5772 | * function that gets called to the entry_test first. Then |
| 5773 | * call the update fgraph entry function to determine if |
| 5774 | * the entryfunc should be called directly or not. |
| 5775 | */ |
| 5776 | __ftrace_graph_entry = entryfunc; |
| 5777 | ftrace_graph_entry = ftrace_graph_entry_test; |
| 5778 | update_function_graph_func(); |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5779 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5780 | ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5781 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5782 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5783 | return ret; |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5784 | } |
| 5785 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5786 | void unregister_ftrace_graph(void) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5787 | { |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5788 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5789 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5790 | if (unlikely(!ftrace_graph_active)) |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5791 | goto out; |
| 5792 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5793 | ftrace_graph_active--; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5794 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5795 | ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5796 | __ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5797 | ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET); |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5798 | unregister_pm_notifier(&ftrace_suspend_notifier); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5799 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5800 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5801 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 5802 | /* |
| 5803 | * Function graph does not allocate the trampoline, but |
| 5804 | * other global_ops do. We need to reset the ALLOC_TRAMP flag |
| 5805 | * if one was used. |
| 5806 | */ |
| 5807 | global_ops.trampoline = save_global_trampoline; |
| 5808 | if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP) |
| 5809 | global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP; |
| 5810 | #endif |
| 5811 | |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5812 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5813 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5814 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5815 | |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5816 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
| 5817 | |
| 5818 | static void |
| 5819 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) |
| 5820 | { |
| 5821 | atomic_set(&t->tracing_graph_pause, 0); |
| 5822 | atomic_set(&t->trace_overrun, 0); |
| 5823 | t->ftrace_timestamp = 0; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5824 | /* make curr_ret_stack visible before we add the ret_stack */ |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5825 | smp_wmb(); |
| 5826 | t->ret_stack = ret_stack; |
| 5827 | } |
| 5828 | |
| 5829 | /* |
| 5830 | * Allocate a return stack for the idle task. May be the first |
| 5831 | * time through, or it may be done by CPU hotplug online. |
| 5832 | */ |
| 5833 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) |
| 5834 | { |
| 5835 | t->curr_ret_stack = -1; |
| 5836 | /* |
| 5837 | * The idle task has no parent, it either has its own |
| 5838 | * stack or no stack at all. |
| 5839 | */ |
| 5840 | if (t->ret_stack) |
| 5841 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); |
| 5842 | |
| 5843 | if (ftrace_graph_active) { |
| 5844 | struct ftrace_ret_stack *ret_stack; |
| 5845 | |
| 5846 | ret_stack = per_cpu(idle_ret_stack, cpu); |
| 5847 | if (!ret_stack) { |
| 5848 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
| 5849 | * sizeof(struct ftrace_ret_stack), |
| 5850 | GFP_KERNEL); |
| 5851 | if (!ret_stack) |
| 5852 | return; |
| 5853 | per_cpu(idle_ret_stack, cpu) = ret_stack; |
| 5854 | } |
| 5855 | graph_init_task(t, ret_stack); |
| 5856 | } |
| 5857 | } |
| 5858 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5859 | /* Allocate a return stack for newly created task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5860 | void ftrace_graph_init_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5861 | { |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 5862 | /* Make sure we do not use the parent ret_stack */ |
| 5863 | t->ret_stack = NULL; |
Steven Rostedt | ea14eb7 | 2010-03-12 19:41:23 -0500 | [diff] [blame] | 5864 | t->curr_ret_stack = -1; |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 5865 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5866 | if (ftrace_graph_active) { |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 5867 | struct ftrace_ret_stack *ret_stack; |
| 5868 | |
| 5869 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5870 | * sizeof(struct ftrace_ret_stack), |
| 5871 | GFP_KERNEL); |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 5872 | if (!ret_stack) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5873 | return; |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5874 | graph_init_task(t, ret_stack); |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 5875 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5876 | } |
| 5877 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5878 | void ftrace_graph_exit_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5879 | { |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 5880 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 5881 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5882 | t->ret_stack = NULL; |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 5883 | /* NULL must become visible to IRQs before we free it: */ |
| 5884 | barrier(); |
| 5885 | |
| 5886 | kfree(ret_stack); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5887 | } |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5888 | #endif |