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 (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 21 | #include <linux/tracefs.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> |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 35 | #include <linux/kprobes.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 36 | |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 37 | #include <trace/events/sched.h> |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 38 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 39 | #include <asm/setup.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 40 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 41 | #include "trace_output.h" |
Steven Rostedt | bac429f | 2009-03-20 12:50:56 -0400 | [diff] [blame] | 42 | #include "trace_stat.h" |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 43 | |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 44 | #define FTRACE_WARN_ON(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 45 | ({ \ |
| 46 | int ___r = cond; \ |
| 47 | if (WARN_ON(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 48 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 49 | ___r; \ |
| 50 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 51 | |
| 52 | #define FTRACE_WARN_ON_ONCE(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 53 | ({ \ |
| 54 | int ___r = cond; \ |
| 55 | if (WARN_ON_ONCE(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 56 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 57 | ___r; \ |
| 58 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 59 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 60 | /* hash bits for specific function selection */ |
| 61 | #define FTRACE_HASH_BITS 7 |
| 62 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 63 | #define FTRACE_HASH_DEFAULT_BITS 10 |
| 64 | #define FTRACE_HASH_MAX_BITS 12 |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 65 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 66 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 67 | #define INIT_OPS_HASH(opsname) \ |
| 68 | .func_hash = &opsname.local_hash, \ |
| 69 | .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] | 70 | #define ASSIGN_OPS_HASH(opsname, val) \ |
| 71 | .func_hash = val, \ |
| 72 | .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 73 | #else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 74 | #define INIT_OPS_HASH(opsname) |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 75 | #define ASSIGN_OPS_HASH(opsname, val) |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 76 | #endif |
| 77 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 78 | static struct ftrace_ops ftrace_list_end __read_mostly = { |
| 79 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | 395b97a | 2013-03-27 09:31:28 -0400 | [diff] [blame] | 80 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 81 | INIT_OPS_HASH(ftrace_list_end) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 84 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 85 | int ftrace_enabled __read_mostly; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 86 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 87 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 88 | /* Current function tracing op */ |
| 89 | 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] | 90 | /* What to set function_trace_op to */ |
| 91 | static struct ftrace_ops *set_function_trace_op; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 92 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 93 | static bool ftrace_pids_enabled(struct ftrace_ops *ops) |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 94 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 95 | struct trace_array *tr; |
| 96 | |
| 97 | if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private) |
| 98 | return false; |
| 99 | |
| 100 | tr = ops->private; |
| 101 | |
| 102 | return tr->function_pids != NULL; |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 106 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 107 | /* |
| 108 | * ftrace_disabled is set when an anomaly is discovered. |
| 109 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 110 | */ |
| 111 | static int ftrace_disabled __read_mostly; |
| 112 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 113 | static DEFINE_MUTEX(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 114 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 115 | 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] | 116 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 117 | static struct ftrace_ops global_ops; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 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 (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 183 | struct trace_array *tr = op->private; |
| 184 | |
| 185 | if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid)) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 186 | return; |
| 187 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 188 | op->saved_func(ip, parent_ip, op, regs); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 191 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 192 | * clear_ftrace_function - reset the ftrace function |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 193 | * |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 194 | * This NULLs the ftrace function and in essence stops |
| 195 | * tracing. There may be lag |
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 | void clear_ftrace_function(void) |
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 | ftrace_trace_function = ftrace_stub; |
| 200 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 201 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 202 | static void per_cpu_ops_disable_all(struct ftrace_ops *ops) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 203 | { |
| 204 | int cpu; |
| 205 | |
| 206 | for_each_possible_cpu(cpu) |
| 207 | *per_cpu_ptr(ops->disabled, cpu) = 1; |
| 208 | } |
| 209 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 210 | static int per_cpu_ops_alloc(struct ftrace_ops *ops) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 211 | { |
| 212 | int __percpu *disabled; |
| 213 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 214 | if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU))) |
| 215 | return -EINVAL; |
| 216 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 217 | disabled = alloc_percpu(int); |
| 218 | if (!disabled) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | ops->disabled = disabled; |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 222 | per_cpu_ops_disable_all(ops); |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 226 | static void ftrace_sync(struct work_struct *work) |
| 227 | { |
| 228 | /* |
| 229 | * This function is just a stub to implement a hard force |
| 230 | * of synchronize_sched(). This requires synchronizing |
| 231 | * tasks even in userspace and idle. |
| 232 | * |
| 233 | * Yes, function tracing is rude. |
| 234 | */ |
| 235 | } |
| 236 | |
| 237 | static void ftrace_sync_ipi(void *data) |
| 238 | { |
| 239 | /* Probably not needed, but do it anyway */ |
| 240 | smp_rmb(); |
| 241 | } |
| 242 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 243 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 244 | static void update_function_graph_func(void); |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 245 | |
| 246 | /* Both enabled by default (can be cleared by function_graph tracer flags */ |
| 247 | static bool fgraph_sleep_time = true; |
| 248 | static bool fgraph_graph_time = true; |
| 249 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 250 | #else |
| 251 | static inline void update_function_graph_func(void) { } |
| 252 | #endif |
| 253 | |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 254 | |
| 255 | static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops) |
| 256 | { |
| 257 | /* |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 258 | * If this is a dynamic, RCU, or per CPU ops, or we force list func, |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 259 | * then it needs to call the list anyway. |
| 260 | */ |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 261 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU | |
| 262 | FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC) |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 263 | return ftrace_ops_list_func; |
| 264 | |
| 265 | return ftrace_ops_get_func(ops); |
| 266 | } |
| 267 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 268 | static void update_ftrace_function(void) |
| 269 | { |
| 270 | ftrace_func_t func; |
| 271 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 272 | /* |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 273 | * Prepare the ftrace_ops that the arch callback will use. |
| 274 | * If there's only one ftrace_ops registered, the ftrace_ops_list |
| 275 | * will point to the ops we want. |
| 276 | */ |
| 277 | set_function_trace_op = ftrace_ops_list; |
| 278 | |
| 279 | /* If there's no ftrace_ops registered, just call the stub function */ |
| 280 | if (ftrace_ops_list == &ftrace_list_end) { |
| 281 | func = ftrace_stub; |
| 282 | |
| 283 | /* |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 284 | * 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] | 285 | * recursion safe and not dynamic and the arch supports passing ops, |
| 286 | * then have the mcount trampoline call the function directly. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 287 | */ |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 288 | } else if (ftrace_ops_list->next == &ftrace_list_end) { |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 289 | func = ftrace_ops_get_list_func(ftrace_ops_list); |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 290 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 291 | } else { |
| 292 | /* Just use the default ftrace_ops */ |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 293 | set_function_trace_op = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 294 | func = ftrace_ops_list_func; |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 295 | } |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 296 | |
Steven Rostedt (Red Hat) | 5f8bf2d2 | 2014-07-15 11:05:12 -0400 | [diff] [blame] | 297 | update_function_graph_func(); |
| 298 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 299 | /* If there's no change, then do nothing more here */ |
| 300 | if (ftrace_trace_function == func) |
| 301 | return; |
| 302 | |
| 303 | /* |
| 304 | * If we are using the list function, it doesn't care |
| 305 | * about the function_trace_ops. |
| 306 | */ |
| 307 | if (func == ftrace_ops_list_func) { |
| 308 | ftrace_trace_function = func; |
| 309 | /* |
| 310 | * Don't even bother setting function_trace_ops, |
| 311 | * it would be racy to do so anyway. |
| 312 | */ |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | #ifndef CONFIG_DYNAMIC_FTRACE |
| 317 | /* |
| 318 | * For static tracing, we need to be a bit more careful. |
| 319 | * The function change takes affect immediately. Thus, |
| 320 | * we need to coorditate the setting of the function_trace_ops |
| 321 | * with the setting of the ftrace_trace_function. |
| 322 | * |
| 323 | * Set the function to the list ops, which will call the |
| 324 | * function we want, albeit indirectly, but it handles the |
| 325 | * ftrace_ops and doesn't depend on function_trace_op. |
| 326 | */ |
| 327 | ftrace_trace_function = ftrace_ops_list_func; |
| 328 | /* |
| 329 | * Make sure all CPUs see this. Yes this is slow, but static |
| 330 | * tracing is slow and nasty to have enabled. |
| 331 | */ |
| 332 | schedule_on_each_cpu(ftrace_sync); |
| 333 | /* Now all cpus are using the list ops. */ |
| 334 | function_trace_op = set_function_trace_op; |
| 335 | /* Make sure the function_trace_op is visible on all CPUs */ |
| 336 | smp_wmb(); |
| 337 | /* Nasty way to force a rmb on all cpus */ |
| 338 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
| 339 | /* OK, we are all set to update the ftrace_trace_function now! */ |
| 340 | #endif /* !CONFIG_DYNAMIC_FTRACE */ |
| 341 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 342 | ftrace_trace_function = func; |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Jiaxing Wang | 7eea4fc | 2014-04-20 23:10:43 +0800 | [diff] [blame] | 345 | int using_ftrace_ops_list_func(void) |
| 346 | { |
| 347 | return ftrace_trace_function == ftrace_ops_list_func; |
| 348 | } |
| 349 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 350 | 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] | 351 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 352 | ops->next = *list; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 353 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 354 | * We are entering ops into the list but another |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 355 | * CPU might be walking that list. We need to make sure |
| 356 | * the ops->next pointer is valid before another CPU sees |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 357 | * the ops pointer included into the list. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 358 | */ |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 359 | rcu_assign_pointer(*list, ops); |
| 360 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 361 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 362 | static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
| 363 | { |
| 364 | struct ftrace_ops **p; |
| 365 | |
| 366 | /* |
| 367 | * If we are removing the last function, then simply point |
| 368 | * to the ftrace_stub. |
| 369 | */ |
| 370 | if (*list == ops && ops->next == &ftrace_list_end) { |
| 371 | *list = &ftrace_list_end; |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
| 376 | if (*p == ops) |
| 377 | break; |
| 378 | |
| 379 | if (*p != ops) |
| 380 | return -1; |
| 381 | |
| 382 | *p = (*p)->next; |
| 383 | return 0; |
| 384 | } |
| 385 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 386 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 387 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 388 | static int __register_ftrace_function(struct ftrace_ops *ops) |
| 389 | { |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 390 | if (ops->flags & FTRACE_OPS_FL_DELETED) |
| 391 | return -EINVAL; |
| 392 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 393 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 394 | return -EBUSY; |
| 395 | |
Masami Hiramatsu | 06aeaae | 2012-09-28 17:15:17 +0900 | [diff] [blame] | 396 | #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 397 | /* |
| 398 | * If the ftrace_ops specifies SAVE_REGS, then it only can be used |
| 399 | * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. |
| 400 | * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. |
| 401 | */ |
| 402 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && |
| 403 | !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) |
| 404 | return -EINVAL; |
| 405 | |
| 406 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) |
| 407 | ops->flags |= FTRACE_OPS_FL_SAVE_REGS; |
| 408 | #endif |
| 409 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 410 | if (!core_kernel_data((unsigned long)ops)) |
| 411 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; |
| 412 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 413 | if (ops->flags & FTRACE_OPS_FL_PER_CPU) { |
| 414 | if (per_cpu_ops_alloc(ops)) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 415 | return -ENOMEM; |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | add_ftrace_ops(&ftrace_ops_list, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 419 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 420 | /* Always save the function, and reset at unregistering */ |
| 421 | ops->saved_func = ops->func; |
| 422 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 423 | if (ftrace_pids_enabled(ops)) |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 424 | ops->func = ftrace_pid_func; |
| 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) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 441 | ret = remove_ftrace_ops(&ftrace_ops_list, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 442 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 443 | if (ret < 0) |
| 444 | return ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 445 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 446 | if (ftrace_enabled) |
| 447 | update_ftrace_function(); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 448 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 449 | ops->func = ops->saved_func; |
| 450 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 451 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 454 | static void ftrace_update_pid_func(void) |
| 455 | { |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 456 | struct ftrace_ops *op; |
| 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 (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 462 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 463 | if (op->flags & FTRACE_OPS_FL_PID) { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 464 | op->func = ftrace_pids_enabled(op) ? |
| 465 | ftrace_pid_func : op->saved_func; |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 466 | ftrace_update_trampoline(op); |
| 467 | } |
| 468 | } while_for_each_ftrace_op(op); |
| 469 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 470 | update_ftrace_function(); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 471 | } |
| 472 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 473 | #ifdef CONFIG_FUNCTION_PROFILER |
| 474 | struct ftrace_profile { |
| 475 | struct hlist_node node; |
| 476 | unsigned long ip; |
| 477 | unsigned long counter; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 478 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 479 | unsigned long long time; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 480 | unsigned long long time_squared; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 481 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | struct ftrace_profile_page { |
| 485 | struct ftrace_profile_page *next; |
| 486 | unsigned long index; |
| 487 | struct ftrace_profile records[]; |
| 488 | }; |
| 489 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 490 | struct ftrace_profile_stat { |
| 491 | atomic_t disabled; |
| 492 | struct hlist_head *hash; |
| 493 | struct ftrace_profile_page *pages; |
| 494 | struct ftrace_profile_page *start; |
| 495 | struct tracer_stat stat; |
| 496 | }; |
| 497 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 498 | #define PROFILE_RECORDS_SIZE \ |
| 499 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) |
| 500 | |
| 501 | #define PROFILES_PER_PAGE \ |
| 502 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) |
| 503 | |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 504 | static int ftrace_profile_enabled __read_mostly; |
| 505 | |
| 506 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 507 | static DEFINE_MUTEX(ftrace_profile_lock); |
| 508 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 509 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 510 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 511 | #define FTRACE_PROFILE_HASH_BITS 10 |
| 512 | #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 513 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 514 | static void * |
| 515 | function_stat_next(void *v, int idx) |
| 516 | { |
| 517 | struct ftrace_profile *rec = v; |
| 518 | struct ftrace_profile_page *pg; |
| 519 | |
| 520 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
| 521 | |
| 522 | again: |
Li Zefan | 0296e42 | 2009-06-26 11:15:37 +0800 | [diff] [blame] | 523 | if (idx != 0) |
| 524 | rec++; |
| 525 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 526 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
| 527 | pg = pg->next; |
| 528 | if (!pg) |
| 529 | return NULL; |
| 530 | rec = &pg->records[0]; |
| 531 | if (!rec->counter) |
| 532 | goto again; |
| 533 | } |
| 534 | |
| 535 | return rec; |
| 536 | } |
| 537 | |
| 538 | static void *function_stat_start(struct tracer_stat *trace) |
| 539 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 540 | struct ftrace_profile_stat *stat = |
| 541 | container_of(trace, struct ftrace_profile_stat, stat); |
| 542 | |
| 543 | if (!stat || !stat->start) |
| 544 | return NULL; |
| 545 | |
| 546 | return function_stat_next(&stat->start->records[0], 0); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 549 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 550 | /* function graph compares on total time */ |
| 551 | static int function_stat_cmp(void *p1, void *p2) |
| 552 | { |
| 553 | struct ftrace_profile *a = p1; |
| 554 | struct ftrace_profile *b = p2; |
| 555 | |
| 556 | if (a->time < b->time) |
| 557 | return -1; |
| 558 | if (a->time > b->time) |
| 559 | return 1; |
| 560 | else |
| 561 | return 0; |
| 562 | } |
| 563 | #else |
| 564 | /* not function graph compares against hits */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 565 | static int function_stat_cmp(void *p1, void *p2) |
| 566 | { |
| 567 | struct ftrace_profile *a = p1; |
| 568 | struct ftrace_profile *b = p2; |
| 569 | |
| 570 | if (a->counter < b->counter) |
| 571 | return -1; |
| 572 | if (a->counter > b->counter) |
| 573 | return 1; |
| 574 | else |
| 575 | return 0; |
| 576 | } |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 577 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 578 | |
| 579 | static int function_stat_headers(struct seq_file *m) |
| 580 | { |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 581 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 582 | seq_puts(m, " Function " |
| 583 | "Hit Time Avg s^2\n" |
| 584 | " -------- " |
| 585 | "--- ---- --- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 586 | #else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 587 | seq_puts(m, " Function Hit\n" |
| 588 | " -------- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 589 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int function_stat_show(struct seq_file *m, void *v) |
| 594 | { |
| 595 | struct ftrace_profile *rec = v; |
| 596 | char str[KSYM_SYMBOL_LEN]; |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 597 | int ret = 0; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 598 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 599 | static struct trace_seq s; |
| 600 | unsigned long long avg; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 601 | unsigned long long stddev; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 602 | #endif |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 603 | mutex_lock(&ftrace_profile_lock); |
| 604 | |
| 605 | /* we raced with function_profile_reset() */ |
| 606 | if (unlikely(rec->counter == 0)) { |
| 607 | ret = -EBUSY; |
| 608 | goto out; |
| 609 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 610 | |
Umesh Tiwari | 8e436ca | 2015-06-22 16:58:08 +0530 | [diff] [blame] | 611 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 612 | avg = rec->time; |
| 613 | do_div(avg, rec->counter); |
| 614 | if (tracing_thresh && (avg < tracing_thresh)) |
| 615 | goto out; |
| 616 | #endif |
| 617 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 618 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 619 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 620 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 621 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 622 | seq_puts(m, " "); |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 623 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 624 | /* Sample standard deviation (s^2) */ |
| 625 | if (rec->counter <= 1) |
| 626 | stddev = 0; |
| 627 | else { |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 628 | /* |
| 629 | * Apply Welford's method: |
| 630 | * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) |
| 631 | */ |
| 632 | stddev = rec->counter * rec->time_squared - |
| 633 | rec->time * rec->time; |
| 634 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 635 | /* |
| 636 | * Divide only 1000 for ns^2 -> us^2 conversion. |
| 637 | * trace_print_graph_duration will divide 1000 again. |
| 638 | */ |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 639 | do_div(stddev, rec->counter * (rec->counter - 1) * 1000); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 640 | } |
| 641 | |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 642 | trace_seq_init(&s); |
| 643 | trace_print_graph_duration(rec->time, &s); |
| 644 | trace_seq_puts(&s, " "); |
| 645 | trace_print_graph_duration(avg, &s); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 646 | trace_seq_puts(&s, " "); |
| 647 | trace_print_graph_duration(stddev, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 648 | trace_print_seq(m, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 649 | #endif |
| 650 | seq_putc(m, '\n'); |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 651 | out: |
| 652 | mutex_unlock(&ftrace_profile_lock); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 653 | |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 654 | return ret; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 657 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 658 | { |
| 659 | struct ftrace_profile_page *pg; |
| 660 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 661 | pg = stat->pages = stat->start; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 662 | |
| 663 | while (pg) { |
| 664 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); |
| 665 | pg->index = 0; |
| 666 | pg = pg->next; |
| 667 | } |
| 668 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 669 | memset(stat->hash, 0, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 670 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
| 671 | } |
| 672 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 673 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 674 | { |
| 675 | struct ftrace_profile_page *pg; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 676 | int functions; |
| 677 | int pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 678 | int i; |
| 679 | |
| 680 | /* If we already allocated, do nothing */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 681 | if (stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 682 | return 0; |
| 683 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 684 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
| 685 | if (!stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 686 | return -ENOMEM; |
| 687 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 688 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 689 | functions = ftrace_update_tot_cnt; |
| 690 | #else |
| 691 | /* |
| 692 | * We do not know the number of functions that exist because |
| 693 | * dynamic tracing is what counts them. With past experience |
| 694 | * we have around 20K functions. That should be more than enough. |
| 695 | * It is highly unlikely we will execute every function in |
| 696 | * the kernel. |
| 697 | */ |
| 698 | functions = 20000; |
| 699 | #endif |
| 700 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 701 | pg = stat->start = stat->pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 702 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 703 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
| 704 | |
Namhyung Kim | 39e30cd | 2013-04-01 21:46:24 +0900 | [diff] [blame] | 705 | for (i = 1; i < pages; i++) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 706 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 707 | if (!pg->next) |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 708 | goto out_free; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 709 | pg = pg->next; |
| 710 | } |
| 711 | |
| 712 | return 0; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 713 | |
| 714 | out_free: |
| 715 | pg = stat->start; |
| 716 | while (pg) { |
| 717 | unsigned long tmp = (unsigned long)pg; |
| 718 | |
| 719 | pg = pg->next; |
| 720 | free_page(tmp); |
| 721 | } |
| 722 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 723 | stat->pages = NULL; |
| 724 | stat->start = NULL; |
| 725 | |
| 726 | return -ENOMEM; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 729 | static int ftrace_profile_init_cpu(int cpu) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 730 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 731 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 732 | int size; |
| 733 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 734 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 735 | |
| 736 | if (stat->hash) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 737 | /* If the profile is already created, simply reset it */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 738 | ftrace_profile_reset(stat); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * We are profiling all functions, but usually only a few thousand |
| 744 | * functions are hit. We'll make a hash of 1024 items. |
| 745 | */ |
| 746 | size = FTRACE_PROFILE_HASH_SIZE; |
| 747 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 748 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 749 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 750 | if (!stat->hash) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 751 | return -ENOMEM; |
| 752 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 753 | /* Preallocate the function profiling pages */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 754 | if (ftrace_profile_pages_init(stat) < 0) { |
| 755 | kfree(stat->hash); |
| 756 | stat->hash = NULL; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 757 | return -ENOMEM; |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 763 | static int ftrace_profile_init(void) |
| 764 | { |
| 765 | int cpu; |
| 766 | int ret = 0; |
| 767 | |
Miao Xie | c4602c1 | 2013-12-16 15:20:01 +0800 | [diff] [blame] | 768 | for_each_possible_cpu(cpu) { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 769 | ret = ftrace_profile_init_cpu(cpu); |
| 770 | if (ret) |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | return ret; |
| 775 | } |
| 776 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 777 | /* interrupts must be disabled */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 778 | static struct ftrace_profile * |
| 779 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 780 | { |
| 781 | struct ftrace_profile *rec; |
| 782 | struct hlist_head *hhd; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 783 | unsigned long key; |
| 784 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 785 | key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 786 | hhd = &stat->hash[key]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 787 | |
| 788 | if (hlist_empty(hhd)) |
| 789 | return NULL; |
| 790 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 791 | hlist_for_each_entry_rcu_notrace(rec, hhd, node) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 792 | if (rec->ip == ip) |
| 793 | return rec; |
| 794 | } |
| 795 | |
| 796 | return NULL; |
| 797 | } |
| 798 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 799 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
| 800 | struct ftrace_profile *rec) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 801 | { |
| 802 | unsigned long key; |
| 803 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 804 | key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 805 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 806 | } |
| 807 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 808 | /* |
| 809 | * The memory is already allocated, this simply finds a new record to use. |
| 810 | */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 811 | static struct ftrace_profile * |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 812 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 813 | { |
| 814 | struct ftrace_profile *rec = NULL; |
| 815 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 816 | /* prevent recursion (from NMIs) */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 817 | if (atomic_inc_return(&stat->disabled) != 1) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 818 | goto out; |
| 819 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 820 | /* |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 821 | * Try to find the function again since an NMI |
| 822 | * could have added it |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 823 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 824 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 825 | if (rec) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 826 | goto out; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 827 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 828 | if (stat->pages->index == PROFILES_PER_PAGE) { |
| 829 | if (!stat->pages->next) |
| 830 | goto out; |
| 831 | stat->pages = stat->pages->next; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 832 | } |
| 833 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 834 | rec = &stat->pages->records[stat->pages->index++]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 835 | rec->ip = ip; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 836 | ftrace_add_profile(stat, rec); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 837 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 838 | out: |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 839 | atomic_dec(&stat->disabled); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 840 | |
| 841 | return rec; |
| 842 | } |
| 843 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 844 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 845 | function_profile_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 846 | struct ftrace_ops *ops, struct pt_regs *regs) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 847 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 848 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 849 | struct ftrace_profile *rec; |
| 850 | unsigned long flags; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 851 | |
| 852 | if (!ftrace_profile_enabled) |
| 853 | return; |
| 854 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 855 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 856 | |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 857 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 858 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 859 | goto out; |
| 860 | |
| 861 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 862 | if (!rec) { |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 863 | rec = ftrace_profile_alloc(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 864 | if (!rec) |
| 865 | goto out; |
| 866 | } |
| 867 | |
| 868 | rec->counter++; |
| 869 | out: |
| 870 | local_irq_restore(flags); |
| 871 | } |
| 872 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 873 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 874 | static int profile_graph_entry(struct ftrace_graph_ent *trace) |
| 875 | { |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 876 | int index = trace->depth; |
| 877 | |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 878 | function_profile_call(trace->func, 0, NULL, NULL); |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 879 | |
Steven Rostedt (VMware) | 741397d | 2017-08-17 16:37:25 -0400 | [diff] [blame] | 880 | /* If function graph is shutting down, ret_stack can be NULL */ |
| 881 | if (!current->ret_stack) |
| 882 | return 0; |
| 883 | |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 884 | if (index >= 0 && index < FTRACE_RETFUNC_DEPTH) |
| 885 | current->ret_stack[index].subtime = 0; |
| 886 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 887 | return 1; |
| 888 | } |
| 889 | |
| 890 | static void profile_graph_return(struct ftrace_graph_ret *trace) |
| 891 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 892 | struct ftrace_profile_stat *stat; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 893 | unsigned long long calltime; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 894 | struct ftrace_profile *rec; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 895 | unsigned long flags; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 896 | |
| 897 | local_irq_save(flags); |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 898 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 899 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 900 | goto out; |
| 901 | |
Steven Rostedt | 37e44bc | 2010-04-27 21:04:24 -0400 | [diff] [blame] | 902 | /* If the calltime was zero'd ignore it */ |
| 903 | if (!trace->calltime) |
| 904 | goto out; |
| 905 | |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 906 | calltime = trace->rettime - trace->calltime; |
| 907 | |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 908 | if (!fgraph_graph_time) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 909 | int index; |
| 910 | |
| 911 | index = trace->depth; |
| 912 | |
| 913 | /* Append this call time to the parent time to subtract */ |
| 914 | if (index) |
| 915 | current->ret_stack[index - 1].subtime += calltime; |
| 916 | |
| 917 | if (current->ret_stack[index].subtime < calltime) |
| 918 | calltime -= current->ret_stack[index].subtime; |
| 919 | else |
| 920 | calltime = 0; |
| 921 | } |
| 922 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 923 | rec = ftrace_find_profiled_func(stat, trace->func); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 924 | if (rec) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 925 | rec->time += calltime; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 926 | rec->time_squared += calltime * calltime; |
| 927 | } |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 928 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 929 | out: |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 930 | local_irq_restore(flags); |
| 931 | } |
| 932 | |
| 933 | static int register_ftrace_profiler(void) |
| 934 | { |
| 935 | return register_ftrace_graph(&profile_graph_return, |
| 936 | &profile_graph_entry); |
| 937 | } |
| 938 | |
| 939 | static void unregister_ftrace_profiler(void) |
| 940 | { |
| 941 | unregister_ftrace_graph(); |
| 942 | } |
| 943 | #else |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 944 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 945 | .func = function_profile_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 946 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 947 | INIT_OPS_HASH(ftrace_profile_ops) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 948 | }; |
| 949 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 950 | static int register_ftrace_profiler(void) |
| 951 | { |
| 952 | return register_ftrace_function(&ftrace_profile_ops); |
| 953 | } |
| 954 | |
| 955 | static void unregister_ftrace_profiler(void) |
| 956 | { |
| 957 | unregister_ftrace_function(&ftrace_profile_ops); |
| 958 | } |
| 959 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 960 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 961 | static ssize_t |
| 962 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 963 | size_t cnt, loff_t *ppos) |
| 964 | { |
| 965 | unsigned long val; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 966 | int ret; |
| 967 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 968 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 969 | if (ret) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 970 | return ret; |
| 971 | |
| 972 | val = !!val; |
| 973 | |
| 974 | mutex_lock(&ftrace_profile_lock); |
| 975 | if (ftrace_profile_enabled ^ val) { |
| 976 | if (val) { |
| 977 | ret = ftrace_profile_init(); |
| 978 | if (ret < 0) { |
| 979 | cnt = ret; |
| 980 | goto out; |
| 981 | } |
| 982 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 983 | ret = register_ftrace_profiler(); |
| 984 | if (ret < 0) { |
| 985 | cnt = ret; |
| 986 | goto out; |
| 987 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 988 | ftrace_profile_enabled = 1; |
| 989 | } else { |
| 990 | ftrace_profile_enabled = 0; |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 991 | /* |
| 992 | * unregister_ftrace_profiler calls stop_machine |
| 993 | * so this acts like an synchronize_sched. |
| 994 | */ |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 995 | unregister_ftrace_profiler(); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | out: |
| 999 | mutex_unlock(&ftrace_profile_lock); |
| 1000 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 1001 | *ppos += cnt; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1002 | |
| 1003 | return cnt; |
| 1004 | } |
| 1005 | |
| 1006 | static ssize_t |
| 1007 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 1008 | size_t cnt, loff_t *ppos) |
| 1009 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 1010 | char buf[64]; /* big enough to hold a number */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1011 | int r; |
| 1012 | |
| 1013 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); |
| 1014 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 1015 | } |
| 1016 | |
| 1017 | static const struct file_operations ftrace_profile_fops = { |
| 1018 | .open = tracing_open_generic, |
| 1019 | .read = ftrace_profile_read, |
| 1020 | .write = ftrace_profile_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1021 | .llseek = default_llseek, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1022 | }; |
| 1023 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1024 | /* used to initialize the real stat files */ |
| 1025 | static struct tracer_stat function_stats __initdata = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 1026 | .name = "functions", |
| 1027 | .stat_start = function_stat_start, |
| 1028 | .stat_next = function_stat_next, |
| 1029 | .stat_cmp = function_stat_cmp, |
| 1030 | .stat_headers = function_stat_headers, |
| 1031 | .stat_show = function_stat_show |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1032 | }; |
| 1033 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1034 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1035 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1036 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1037 | struct dentry *entry; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1038 | char *name; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1039 | int ret; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1040 | int cpu; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1041 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1042 | for_each_possible_cpu(cpu) { |
| 1043 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 1044 | |
Geliang Tang | 6363c6b | 2016-03-15 22:12:34 +0800 | [diff] [blame] | 1045 | name = kasprintf(GFP_KERNEL, "function%d", cpu); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1046 | if (!name) { |
| 1047 | /* |
| 1048 | * The files created are permanent, if something happens |
| 1049 | * we still do not free memory. |
| 1050 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1051 | WARN(1, |
| 1052 | "Could not allocate stat file for cpu %d\n", |
| 1053 | cpu); |
| 1054 | return; |
| 1055 | } |
| 1056 | stat->stat = function_stats; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1057 | stat->stat.name = name; |
| 1058 | ret = register_stat_tracer(&stat->stat); |
| 1059 | if (ret) { |
| 1060 | WARN(1, |
| 1061 | "Could not register function stat for cpu %d\n", |
| 1062 | cpu); |
| 1063 | kfree(name); |
| 1064 | return; |
| 1065 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1066 | } |
| 1067 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1068 | entry = tracefs_create_file("function_profile_enabled", 0644, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1069 | d_tracer, NULL, &ftrace_profile_fops); |
| 1070 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1071 | pr_warn("Could not create tracefs 'function_profile_enabled' entry\n"); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | #else /* CONFIG_FUNCTION_PROFILER */ |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1075 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1076 | { |
| 1077 | } |
| 1078 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 1079 | |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1080 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
| 1081 | |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 1082 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 1083 | static int ftrace_graph_active; |
| 1084 | #else |
| 1085 | # define ftrace_graph_active 0 |
| 1086 | #endif |
| 1087 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1088 | #ifdef CONFIG_DYNAMIC_FTRACE |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1089 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1090 | static struct ftrace_ops *removed_ops; |
| 1091 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 1092 | /* |
| 1093 | * Set when doing a global update, like enabling all recs or disabling them. |
| 1094 | * It is not set when just updating a single ftrace_ops. |
| 1095 | */ |
| 1096 | static bool update_all_ops; |
| 1097 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1098 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1099 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1100 | #endif |
| 1101 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1102 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; |
| 1103 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1104 | struct ftrace_func_probe { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1105 | struct hlist_node node; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1106 | struct ftrace_probe_ops *ops; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1107 | unsigned long flags; |
| 1108 | unsigned long ip; |
| 1109 | void *data; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 1110 | struct list_head free_list; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1111 | }; |
| 1112 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1113 | struct ftrace_func_entry { |
| 1114 | struct hlist_node hlist; |
| 1115 | unsigned long ip; |
| 1116 | }; |
| 1117 | |
| 1118 | struct ftrace_hash { |
| 1119 | unsigned long size_bits; |
| 1120 | struct hlist_head *buckets; |
| 1121 | unsigned long count; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1122 | struct rcu_head rcu; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1123 | }; |
| 1124 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1125 | /* |
| 1126 | * We make these constant because no one should touch them, |
| 1127 | * but they are used as the default "empty hash", to avoid allocating |
| 1128 | * it all the time. These are in a read only section such that if |
| 1129 | * anyone does try to modify it, it will cause an exception. |
| 1130 | */ |
| 1131 | static const struct hlist_head empty_buckets[1]; |
| 1132 | static const struct ftrace_hash empty_hash = { |
| 1133 | .buckets = (struct hlist_head *)empty_buckets, |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1134 | }; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1135 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1136 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 1137 | static struct ftrace_ops global_ops = { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1138 | .func = ftrace_stub, |
| 1139 | .local_hash.notrace_hash = EMPTY_HASH, |
| 1140 | .local_hash.filter_hash = EMPTY_HASH, |
| 1141 | INIT_OPS_HASH(global_ops) |
| 1142 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 1143 | FTRACE_OPS_FL_INITIALIZED | |
| 1144 | FTRACE_OPS_FL_PID, |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1145 | }; |
| 1146 | |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1147 | /* |
| 1148 | * 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] | 1149 | * address is on a dynamically allocated trampoline that would |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1150 | * not return true for either core_kernel_text() or |
| 1151 | * is_module_text_address(). |
| 1152 | */ |
| 1153 | bool is_ftrace_trampoline(unsigned long addr) |
| 1154 | { |
| 1155 | struct ftrace_ops *op; |
| 1156 | bool ret = false; |
| 1157 | |
| 1158 | /* |
| 1159 | * Some of the ops may be dynamically allocated, |
| 1160 | * they are freed after a synchronize_sched(). |
| 1161 | */ |
| 1162 | preempt_disable_notrace(); |
| 1163 | |
| 1164 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1165 | /* |
| 1166 | * This is to check for dynamically allocated trampolines. |
| 1167 | * Trampolines that are in kernel text will have |
| 1168 | * core_kernel_text() return true. |
| 1169 | */ |
| 1170 | if (op->trampoline && op->trampoline_size) |
| 1171 | if (addr >= op->trampoline && |
| 1172 | addr < op->trampoline + op->trampoline_size) { |
| 1173 | ret = true; |
| 1174 | goto out; |
| 1175 | } |
| 1176 | } while_for_each_ftrace_op(op); |
| 1177 | |
| 1178 | out: |
| 1179 | preempt_enable_notrace(); |
| 1180 | |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1184 | struct ftrace_page { |
| 1185 | struct ftrace_page *next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1186 | struct dyn_ftrace *records; |
Steven Rostedt | 431aa3f | 2009-01-06 12:43:01 -0500 | [diff] [blame] | 1187 | int index; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1188 | int size; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 1189 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1190 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1191 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1192 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1193 | |
| 1194 | /* estimate from running different kernels */ |
| 1195 | #define NR_TO_INIT 10000 |
| 1196 | |
| 1197 | static struct ftrace_page *ftrace_pages_start; |
| 1198 | static struct ftrace_page *ftrace_pages; |
| 1199 | |
Steven Rostedt (Red Hat) | 68f4096 | 2014-05-01 12:44:50 -0400 | [diff] [blame] | 1200 | static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash) |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1201 | { |
| 1202 | return !hash || !hash->count; |
| 1203 | } |
| 1204 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1205 | static struct ftrace_func_entry * |
| 1206 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1207 | { |
| 1208 | unsigned long key; |
| 1209 | struct ftrace_func_entry *entry; |
| 1210 | struct hlist_head *hhd; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1211 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1212 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1213 | return NULL; |
| 1214 | |
| 1215 | if (hash->size_bits > 0) |
| 1216 | key = hash_long(ip, hash->size_bits); |
| 1217 | else |
| 1218 | key = 0; |
| 1219 | |
| 1220 | hhd = &hash->buckets[key]; |
| 1221 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 1222 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1223 | if (entry->ip == ip) |
| 1224 | return entry; |
| 1225 | } |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1229 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1230 | struct ftrace_func_entry *entry) |
| 1231 | { |
| 1232 | struct hlist_head *hhd; |
| 1233 | unsigned long key; |
| 1234 | |
| 1235 | if (hash->size_bits) |
| 1236 | key = hash_long(entry->ip, hash->size_bits); |
| 1237 | else |
| 1238 | key = 0; |
| 1239 | |
| 1240 | hhd = &hash->buckets[key]; |
| 1241 | hlist_add_head(&entry->hlist, hhd); |
| 1242 | hash->count++; |
| 1243 | } |
| 1244 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1245 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1246 | { |
| 1247 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1248 | |
| 1249 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1250 | if (!entry) |
| 1251 | return -ENOMEM; |
| 1252 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1253 | entry->ip = ip; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1254 | __add_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1255 | |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | static void |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1260 | free_hash_entry(struct ftrace_hash *hash, |
| 1261 | struct ftrace_func_entry *entry) |
| 1262 | { |
| 1263 | hlist_del(&entry->hlist); |
| 1264 | kfree(entry); |
| 1265 | hash->count--; |
| 1266 | } |
| 1267 | |
| 1268 | static void |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1269 | remove_hash_entry(struct ftrace_hash *hash, |
| 1270 | struct ftrace_func_entry *entry) |
| 1271 | { |
| 1272 | hlist_del(&entry->hlist); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1273 | hash->count--; |
| 1274 | } |
| 1275 | |
| 1276 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1277 | { |
| 1278 | struct hlist_head *hhd; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1279 | struct hlist_node *tn; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1280 | struct ftrace_func_entry *entry; |
| 1281 | int size = 1 << hash->size_bits; |
| 1282 | int i; |
| 1283 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1284 | if (!hash->count) |
| 1285 | return; |
| 1286 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1287 | for (i = 0; i < size; i++) { |
| 1288 | hhd = &hash->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1289 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1290 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1291 | } |
| 1292 | FTRACE_WARN_ON(hash->count); |
| 1293 | } |
| 1294 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1295 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1296 | { |
| 1297 | if (!hash || hash == EMPTY_HASH) |
| 1298 | return; |
| 1299 | ftrace_hash_clear(hash); |
| 1300 | kfree(hash->buckets); |
| 1301 | kfree(hash); |
| 1302 | } |
| 1303 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1304 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1305 | { |
| 1306 | struct ftrace_hash *hash; |
| 1307 | |
| 1308 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1309 | free_ftrace_hash(hash); |
| 1310 | } |
| 1311 | |
| 1312 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1313 | { |
| 1314 | if (!hash || hash == EMPTY_HASH) |
| 1315 | return; |
| 1316 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); |
| 1317 | } |
| 1318 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1319 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1320 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 1321 | ftrace_ops_init(ops); |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1322 | free_ftrace_hash(ops->func_hash->filter_hash); |
| 1323 | free_ftrace_hash(ops->func_hash->notrace_hash); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1326 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1327 | { |
| 1328 | struct ftrace_hash *hash; |
| 1329 | int size; |
| 1330 | |
| 1331 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1332 | if (!hash) |
| 1333 | return NULL; |
| 1334 | |
| 1335 | size = 1 << size_bits; |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1336 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1337 | |
| 1338 | if (!hash->buckets) { |
| 1339 | kfree(hash); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | |
| 1343 | hash->size_bits = size_bits; |
| 1344 | |
| 1345 | return hash; |
| 1346 | } |
| 1347 | |
| 1348 | static struct ftrace_hash * |
| 1349 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1350 | { |
| 1351 | struct ftrace_func_entry *entry; |
| 1352 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1353 | int size; |
| 1354 | int ret; |
| 1355 | int i; |
| 1356 | |
| 1357 | new_hash = alloc_ftrace_hash(size_bits); |
| 1358 | if (!new_hash) |
| 1359 | return NULL; |
| 1360 | |
| 1361 | /* Empty hash? */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1362 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1363 | return new_hash; |
| 1364 | |
| 1365 | size = 1 << hash->size_bits; |
| 1366 | for (i = 0; i < size; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1367 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1368 | ret = add_hash_entry(new_hash, entry->ip); |
| 1369 | if (ret < 0) |
| 1370 | goto free_hash; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1375 | |
| 1376 | return new_hash; |
| 1377 | |
| 1378 | free_hash: |
| 1379 | free_ftrace_hash(new_hash); |
| 1380 | return NULL; |
| 1381 | } |
| 1382 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1383 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1384 | ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1385 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1386 | ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1387 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1388 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1389 | struct ftrace_hash *new_hash); |
| 1390 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1391 | static int |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1392 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1393 | struct ftrace_hash **dst, struct ftrace_hash *src) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1394 | { |
| 1395 | struct ftrace_func_entry *entry; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1396 | struct hlist_node *tn; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1397 | struct hlist_head *hhd; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1398 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1399 | int size = src->count; |
| 1400 | int bits = 0; |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1401 | int ret; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1402 | int i; |
| 1403 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1404 | /* Reject setting notrace hash on IPMODIFY ftrace_ops */ |
| 1405 | if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) |
| 1406 | return -EINVAL; |
| 1407 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1408 | /* |
| 1409 | * If the new source is empty, just free dst and assign it |
| 1410 | * the empty_hash. |
| 1411 | */ |
| 1412 | if (!src->count) { |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1413 | new_hash = EMPTY_HASH; |
| 1414 | goto update; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1415 | } |
| 1416 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1417 | /* |
| 1418 | * Make the hash size about 1/2 the # found |
| 1419 | */ |
| 1420 | for (size /= 2; size; size >>= 1) |
| 1421 | bits++; |
| 1422 | |
| 1423 | /* Don't allocate too much */ |
| 1424 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1425 | bits = FTRACE_HASH_MAX_BITS; |
| 1426 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1427 | new_hash = alloc_ftrace_hash(bits); |
| 1428 | if (!new_hash) |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1429 | return -ENOMEM; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1430 | |
| 1431 | size = 1 << src->size_bits; |
| 1432 | for (i = 0; i < size; i++) { |
| 1433 | hhd = &src->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1434 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1435 | remove_hash_entry(src, entry); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1436 | __add_hash_entry(new_hash, entry); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1440 | update: |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1441 | /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ |
| 1442 | if (enable) { |
| 1443 | /* IPMODIFY should be updated only when filter_hash updating */ |
| 1444 | ret = ftrace_hash_ipmodify_update(ops, new_hash); |
| 1445 | if (ret < 0) { |
| 1446 | free_ftrace_hash(new_hash); |
| 1447 | return ret; |
| 1448 | } |
| 1449 | } |
| 1450 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1451 | /* |
| 1452 | * Remove the current set, update the hash and add |
| 1453 | * them back. |
| 1454 | */ |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1455 | ftrace_hash_rec_disable_modify(ops, enable); |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1456 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1457 | rcu_assign_pointer(*dst, new_hash); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1458 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1459 | ftrace_hash_rec_enable_modify(ops, enable); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1460 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1461 | return 0; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1462 | } |
| 1463 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1464 | static bool hash_contains_ip(unsigned long ip, |
| 1465 | struct ftrace_ops_hash *hash) |
| 1466 | { |
| 1467 | /* |
| 1468 | * The function record is a match if it exists in the filter |
| 1469 | * hash and not in the notrace hash. Note, an emty hash is |
| 1470 | * considered a match for the filter hash, but an empty |
| 1471 | * notrace hash is considered not in the notrace hash. |
| 1472 | */ |
| 1473 | return (ftrace_hash_empty(hash->filter_hash) || |
| 1474 | ftrace_lookup_ip(hash->filter_hash, ip)) && |
| 1475 | (ftrace_hash_empty(hash->notrace_hash) || |
| 1476 | !ftrace_lookup_ip(hash->notrace_hash, ip)); |
| 1477 | } |
| 1478 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1479 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1480 | * Test the hashes for this ops to see if we want to call |
| 1481 | * the ops->func or not. |
| 1482 | * |
| 1483 | * It's a match if the ip is in the ops->filter_hash or |
| 1484 | * the filter_hash does not exist or is empty, |
| 1485 | * AND |
| 1486 | * the ip is not in the ops->notrace_hash. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 1487 | * |
| 1488 | * This needs to be called with preemption disabled as |
| 1489 | * the hashes are freed with call_rcu_sched(). |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1490 | */ |
| 1491 | static int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1492 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1493 | { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1494 | struct ftrace_ops_hash hash; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1495 | int ret; |
| 1496 | |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1497 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1498 | /* |
| 1499 | * There's a small race when adding ops that the ftrace handler |
| 1500 | * that wants regs, may be called without them. We can not |
| 1501 | * allow that handler to be called if regs is NULL. |
| 1502 | */ |
| 1503 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) |
| 1504 | return 0; |
| 1505 | #endif |
| 1506 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1507 | hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash); |
| 1508 | hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1509 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1510 | if (hash_contains_ip(ip, &hash)) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1511 | ret = 1; |
| 1512 | else |
| 1513 | ret = 0; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1514 | |
| 1515 | return ret; |
| 1516 | } |
| 1517 | |
| 1518 | /* |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1519 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1520 | * you must use a goto. |
| 1521 | */ |
| 1522 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1523 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1524 | int _____i; \ |
| 1525 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1526 | rec = &pg->records[_____i]; |
| 1527 | |
| 1528 | #define while_for_each_ftrace_rec() \ |
| 1529 | } \ |
| 1530 | } |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 1531 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1532 | |
| 1533 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1534 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1535 | const struct dyn_ftrace *key = a; |
| 1536 | const struct dyn_ftrace *rec = b; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1537 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1538 | if (key->flags < rec->ip) |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1539 | return -1; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1540 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
| 1541 | return 1; |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
Michael Ellerman | 04cf31a | 2016-03-24 22:04:01 +1100 | [diff] [blame] | 1545 | /** |
| 1546 | * ftrace_location_range - return the first address of a traced location |
| 1547 | * if it touches the given ip range |
| 1548 | * @start: start of range to search. |
| 1549 | * @end: end of range to search (inclusive). @end points to the last byte |
| 1550 | * to check. |
| 1551 | * |
| 1552 | * Returns rec->ip if the related ftrace location is a least partly within |
| 1553 | * the given address range. That is, the first address of the instruction |
| 1554 | * that is either a NOP or call to the function tracer. It checks the ftrace |
| 1555 | * internal tables to determine if the address belongs or not. |
| 1556 | */ |
| 1557 | unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1558 | { |
| 1559 | struct ftrace_page *pg; |
| 1560 | struct dyn_ftrace *rec; |
| 1561 | struct dyn_ftrace key; |
| 1562 | |
| 1563 | key.ip = start; |
| 1564 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 1565 | |
| 1566 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1567 | if (end < pg->records[0].ip || |
| 1568 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 1569 | continue; |
| 1570 | rec = bsearch(&key, pg->records, pg->index, |
| 1571 | sizeof(struct dyn_ftrace), |
| 1572 | ftrace_cmp_recs); |
| 1573 | if (rec) |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1574 | return rec->ip; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1580 | /** |
| 1581 | * ftrace_location - return true if the ip giving is a traced location |
| 1582 | * @ip: the instruction pointer to check |
| 1583 | * |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1584 | * 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] | 1585 | * That is, the instruction that is either a NOP or call to |
| 1586 | * the function tracer. It checks the ftrace internal tables to |
| 1587 | * determine if the address belongs or not. |
| 1588 | */ |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1589 | unsigned long ftrace_location(unsigned long ip) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1590 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1591 | return ftrace_location_range(ip, ip); |
| 1592 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1593 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1594 | /** |
| 1595 | * ftrace_text_reserved - return true if range contains an ftrace location |
| 1596 | * @start: start of range to search |
| 1597 | * @end: end of range to search (inclusive). @end points to the last byte to check. |
| 1598 | * |
| 1599 | * Returns 1 if @start and @end contains a ftrace location. |
| 1600 | * That is, the instruction that is either a NOP or call to |
| 1601 | * the function tracer. It checks the ftrace internal tables to |
| 1602 | * determine if the address belongs or not. |
| 1603 | */ |
Sasha Levin | d88471c | 2013-01-09 18:09:20 -0500 | [diff] [blame] | 1604 | int ftrace_text_reserved(const void *start, const void *end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1605 | { |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1606 | unsigned long ret; |
| 1607 | |
| 1608 | ret = ftrace_location_range((unsigned long)start, |
| 1609 | (unsigned long)end); |
| 1610 | |
| 1611 | return (int)!!ret; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1612 | } |
| 1613 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1614 | /* Test if ops registered to this rec needs regs */ |
| 1615 | static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) |
| 1616 | { |
| 1617 | struct ftrace_ops *ops; |
| 1618 | bool keep_regs = false; |
| 1619 | |
| 1620 | for (ops = ftrace_ops_list; |
| 1621 | ops != &ftrace_list_end; ops = ops->next) { |
| 1622 | /* pass rec in as regs to have non-NULL val */ |
| 1623 | if (ftrace_ops_test(ops, rec->ip, rec)) { |
| 1624 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1625 | keep_regs = true; |
| 1626 | break; |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | return keep_regs; |
| 1632 | } |
| 1633 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1634 | static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1635 | int filter_hash, |
| 1636 | bool inc) |
| 1637 | { |
| 1638 | struct ftrace_hash *hash; |
| 1639 | struct ftrace_hash *other_hash; |
| 1640 | struct ftrace_page *pg; |
| 1641 | struct dyn_ftrace *rec; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1642 | bool update = false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1643 | int count = 0; |
| 1644 | int all = 0; |
| 1645 | |
| 1646 | /* Only update if the ops has been registered */ |
| 1647 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1648 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1649 | |
| 1650 | /* |
| 1651 | * In the filter_hash case: |
| 1652 | * If the count is zero, we update all records. |
| 1653 | * Otherwise we just update the items in the hash. |
| 1654 | * |
| 1655 | * In the notrace_hash case: |
| 1656 | * We enable the update in the hash. |
| 1657 | * As disabling notrace means enabling the tracing, |
| 1658 | * and enabling notrace means disabling, the inc variable |
| 1659 | * gets inversed. |
| 1660 | */ |
| 1661 | if (filter_hash) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1662 | hash = ops->func_hash->filter_hash; |
| 1663 | other_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1664 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1665 | all = 1; |
| 1666 | } else { |
| 1667 | inc = !inc; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1668 | hash = ops->func_hash->notrace_hash; |
| 1669 | other_hash = ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1670 | /* |
| 1671 | * If the notrace hash has no items, |
| 1672 | * then there's nothing to do. |
| 1673 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1674 | if (ftrace_hash_empty(hash)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1675 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | do_for_each_ftrace_rec(pg, rec) { |
| 1679 | int in_other_hash = 0; |
| 1680 | int in_hash = 0; |
| 1681 | int match = 0; |
| 1682 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 1683 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1684 | continue; |
| 1685 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1686 | if (all) { |
| 1687 | /* |
| 1688 | * Only the filter_hash affects all records. |
| 1689 | * Update if the record is not in the notrace hash. |
| 1690 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1691 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1692 | match = 1; |
| 1693 | } else { |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1694 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
| 1695 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1696 | |
| 1697 | /* |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1698 | * If filter_hash is set, we want to match all functions |
| 1699 | * that are in the hash but not in the other hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1700 | * |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1701 | * If filter_hash is not set, then we are decrementing. |
| 1702 | * That means we match anything that is in the hash |
| 1703 | * and also in the other_hash. That is, we need to turn |
| 1704 | * off functions in the other hash because they are disabled |
| 1705 | * by this hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1706 | */ |
| 1707 | if (filter_hash && in_hash && !in_other_hash) |
| 1708 | match = 1; |
| 1709 | else if (!filter_hash && in_hash && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1710 | (in_other_hash || ftrace_hash_empty(other_hash))) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1711 | match = 1; |
| 1712 | } |
| 1713 | if (!match) |
| 1714 | continue; |
| 1715 | |
| 1716 | if (inc) { |
| 1717 | rec->flags++; |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1718 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1719 | return false; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1720 | |
| 1721 | /* |
| 1722 | * If there's only a single callback registered to a |
| 1723 | * function, and the ops has a trampoline registered |
| 1724 | * for it, then we can call it directly. |
| 1725 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1726 | if (ftrace_rec_count(rec) == 1 && ops->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1727 | rec->flags |= FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1728 | else |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1729 | /* |
| 1730 | * If we are adding another function callback |
| 1731 | * to this function, and the previous had a |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 1732 | * custom trampoline in use, then we need to go |
| 1733 | * back to the default trampoline. |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1734 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1735 | rec->flags &= ~FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1736 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1737 | /* |
| 1738 | * If any ops wants regs saved for this function |
| 1739 | * then all ops will get saved regs. |
| 1740 | */ |
| 1741 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 1742 | rec->flags |= FTRACE_FL_REGS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1743 | } else { |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1744 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1745 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1746 | rec->flags--; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1747 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1748 | /* |
| 1749 | * If the rec had REGS enabled and the ops that is |
| 1750 | * being removed had REGS set, then see if there is |
| 1751 | * still any ops for this record that wants regs. |
| 1752 | * If not, we can stop recording them. |
| 1753 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1754 | if (ftrace_rec_count(rec) > 0 && |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1755 | rec->flags & FTRACE_FL_REGS && |
| 1756 | ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1757 | if (!test_rec_ops_needs_regs(rec)) |
| 1758 | rec->flags &= ~FTRACE_FL_REGS; |
| 1759 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1760 | |
| 1761 | /* |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1762 | * If the rec had TRAMP enabled, then it needs to |
| 1763 | * be cleared. As TRAMP can only be enabled iff |
| 1764 | * there is only a single ops attached to it. |
| 1765 | * In otherwords, always disable it on decrementing. |
| 1766 | * In the future, we may set it if rec count is |
| 1767 | * decremented to one, and the ops that is left |
| 1768 | * has a trampoline. |
| 1769 | */ |
| 1770 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 1771 | |
| 1772 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1773 | * flags will be cleared in ftrace_check_record() |
| 1774 | * if rec count is zero. |
| 1775 | */ |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1776 | } |
| 1777 | count++; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */ |
| 1780 | update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE; |
| 1781 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1782 | /* Shortcut, if we handled all records, we are done. */ |
| 1783 | if (!all && count == hash->count) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1784 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1785 | } while_for_each_ftrace_rec(); |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1786 | |
| 1787 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1788 | } |
| 1789 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1790 | static bool ftrace_hash_rec_disable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1791 | int filter_hash) |
| 1792 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1793 | return __ftrace_hash_rec_update(ops, filter_hash, 0); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1794 | } |
| 1795 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1796 | static bool ftrace_hash_rec_enable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1797 | int filter_hash) |
| 1798 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1799 | return __ftrace_hash_rec_update(ops, filter_hash, 1); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1800 | } |
| 1801 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1802 | static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, |
| 1803 | int filter_hash, int inc) |
| 1804 | { |
| 1805 | struct ftrace_ops *op; |
| 1806 | |
| 1807 | __ftrace_hash_rec_update(ops, filter_hash, inc); |
| 1808 | |
| 1809 | if (ops->func_hash != &global_ops.local_hash) |
| 1810 | return; |
| 1811 | |
| 1812 | /* |
| 1813 | * If the ops shares the global_ops hash, then we need to update |
| 1814 | * all ops that are enabled and use this hash. |
| 1815 | */ |
| 1816 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1817 | /* Already done */ |
| 1818 | if (op == ops) |
| 1819 | continue; |
| 1820 | if (op->func_hash == &global_ops.local_hash) |
| 1821 | __ftrace_hash_rec_update(op, filter_hash, inc); |
| 1822 | } while_for_each_ftrace_op(op); |
| 1823 | } |
| 1824 | |
| 1825 | static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, |
| 1826 | int filter_hash) |
| 1827 | { |
| 1828 | ftrace_hash_rec_update_modify(ops, filter_hash, 0); |
| 1829 | } |
| 1830 | |
| 1831 | static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, |
| 1832 | int filter_hash) |
| 1833 | { |
| 1834 | ftrace_hash_rec_update_modify(ops, filter_hash, 1); |
| 1835 | } |
| 1836 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1837 | /* |
| 1838 | * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK |
| 1839 | * or no-needed to update, -EBUSY if it detects a conflict of the flag |
| 1840 | * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. |
| 1841 | * Note that old_hash and new_hash has below meanings |
| 1842 | * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) |
| 1843 | * - If the hash is EMPTY_HASH, it hits nothing |
| 1844 | * - Anything else hits the recs which match the hash entries. |
| 1845 | */ |
| 1846 | static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, |
| 1847 | struct ftrace_hash *old_hash, |
| 1848 | struct ftrace_hash *new_hash) |
| 1849 | { |
| 1850 | struct ftrace_page *pg; |
| 1851 | struct dyn_ftrace *rec, *end = NULL; |
| 1852 | int in_old, in_new; |
| 1853 | |
| 1854 | /* Only update if the ops has been registered */ |
| 1855 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1856 | return 0; |
| 1857 | |
| 1858 | if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 1859 | return 0; |
| 1860 | |
| 1861 | /* |
| 1862 | * Since the IPMODIFY is a very address sensitive action, we do not |
| 1863 | * allow ftrace_ops to set all functions to new hash. |
| 1864 | */ |
| 1865 | if (!new_hash || !old_hash) |
| 1866 | return -EINVAL; |
| 1867 | |
| 1868 | /* Update rec->flags */ |
| 1869 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1870 | |
| 1871 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1872 | continue; |
| 1873 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1874 | /* We need to update only differences of filter_hash */ |
| 1875 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1876 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1877 | if (in_old == in_new) |
| 1878 | continue; |
| 1879 | |
| 1880 | if (in_new) { |
| 1881 | /* New entries must ensure no others are using it */ |
| 1882 | if (rec->flags & FTRACE_FL_IPMODIFY) |
| 1883 | goto rollback; |
| 1884 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1885 | } else /* Removed entry */ |
| 1886 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1887 | } while_for_each_ftrace_rec(); |
| 1888 | |
| 1889 | return 0; |
| 1890 | |
| 1891 | rollback: |
| 1892 | end = rec; |
| 1893 | |
| 1894 | /* Roll back what we did above */ |
| 1895 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1896 | |
| 1897 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1898 | continue; |
| 1899 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1900 | if (rec == end) |
| 1901 | goto err_out; |
| 1902 | |
| 1903 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1904 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1905 | if (in_old == in_new) |
| 1906 | continue; |
| 1907 | |
| 1908 | if (in_new) |
| 1909 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1910 | else |
| 1911 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1912 | } while_for_each_ftrace_rec(); |
| 1913 | |
| 1914 | err_out: |
| 1915 | return -EBUSY; |
| 1916 | } |
| 1917 | |
| 1918 | static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) |
| 1919 | { |
| 1920 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1921 | |
| 1922 | if (ftrace_hash_empty(hash)) |
| 1923 | hash = NULL; |
| 1924 | |
| 1925 | return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash); |
| 1926 | } |
| 1927 | |
| 1928 | /* Disabling always succeeds */ |
| 1929 | static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) |
| 1930 | { |
| 1931 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1932 | |
| 1933 | if (ftrace_hash_empty(hash)) |
| 1934 | hash = NULL; |
| 1935 | |
| 1936 | __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH); |
| 1937 | } |
| 1938 | |
| 1939 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1940 | struct ftrace_hash *new_hash) |
| 1941 | { |
| 1942 | struct ftrace_hash *old_hash = ops->func_hash->filter_hash; |
| 1943 | |
| 1944 | if (ftrace_hash_empty(old_hash)) |
| 1945 | old_hash = NULL; |
| 1946 | |
| 1947 | if (ftrace_hash_empty(new_hash)) |
| 1948 | new_hash = NULL; |
| 1949 | |
| 1950 | return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash); |
| 1951 | } |
| 1952 | |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1953 | static void print_ip_ins(const char *fmt, const unsigned char *p) |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 1954 | { |
| 1955 | int i; |
| 1956 | |
| 1957 | printk(KERN_CONT "%s", fmt); |
| 1958 | |
| 1959 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 1960 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 1961 | } |
| 1962 | |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1963 | static struct ftrace_ops * |
| 1964 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 1965 | static struct ftrace_ops * |
| 1966 | ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1967 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1968 | enum ftrace_bug_type ftrace_bug_type; |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1969 | const void *ftrace_expected; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1970 | |
| 1971 | static void print_bug_type(void) |
| 1972 | { |
| 1973 | switch (ftrace_bug_type) { |
| 1974 | case FTRACE_BUG_UNKNOWN: |
| 1975 | break; |
| 1976 | case FTRACE_BUG_INIT: |
| 1977 | pr_info("Initializing ftrace call sites\n"); |
| 1978 | break; |
| 1979 | case FTRACE_BUG_NOP: |
| 1980 | pr_info("Setting ftrace call site to NOP\n"); |
| 1981 | break; |
| 1982 | case FTRACE_BUG_CALL: |
| 1983 | pr_info("Setting ftrace call site to call ftrace function\n"); |
| 1984 | break; |
| 1985 | case FTRACE_BUG_UPDATE: |
| 1986 | pr_info("Updating ftrace call site to call a different ftrace function\n"); |
| 1987 | break; |
| 1988 | } |
| 1989 | } |
| 1990 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1991 | /** |
| 1992 | * ftrace_bug - report and shutdown function tracer |
| 1993 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1994 | * @rec: The record that failed |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1995 | * |
| 1996 | * The arch code that enables or disables the function tracing |
| 1997 | * can call ftrace_bug() when it has detected a problem in |
| 1998 | * modifying the code. @failed should be one of either: |
| 1999 | * EFAULT - if the problem happens on reading the @ip address |
| 2000 | * EINVAL - if what is read at @ip is not what was expected |
| 2001 | * EPERM - if the problem happens on writting to the @ip address |
| 2002 | */ |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2003 | void ftrace_bug(int failed, struct dyn_ftrace *rec) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2004 | { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2005 | unsigned long ip = rec ? rec->ip : 0; |
| 2006 | |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2007 | switch (failed) { |
| 2008 | case -EFAULT: |
| 2009 | FTRACE_WARN_ON_ONCE(1); |
| 2010 | pr_info("ftrace faulted on modifying "); |
| 2011 | print_ip_sym(ip); |
| 2012 | break; |
| 2013 | case -EINVAL: |
| 2014 | FTRACE_WARN_ON_ONCE(1); |
| 2015 | pr_info("ftrace failed to modify "); |
| 2016 | print_ip_sym(ip); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2017 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2018 | pr_cont("\n"); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2019 | if (ftrace_expected) { |
| 2020 | print_ip_ins(" expected: ", ftrace_expected); |
| 2021 | pr_cont("\n"); |
| 2022 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2023 | break; |
| 2024 | case -EPERM: |
| 2025 | FTRACE_WARN_ON_ONCE(1); |
| 2026 | pr_info("ftrace faulted on writing "); |
| 2027 | print_ip_sym(ip); |
| 2028 | break; |
| 2029 | default: |
| 2030 | FTRACE_WARN_ON_ONCE(1); |
| 2031 | pr_info("ftrace faulted on unknown error "); |
| 2032 | print_ip_sym(ip); |
| 2033 | } |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2034 | print_bug_type(); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2035 | if (rec) { |
| 2036 | struct ftrace_ops *ops = NULL; |
| 2037 | |
| 2038 | pr_info("ftrace record flags: %lx\n", rec->flags); |
| 2039 | pr_cont(" (%ld)%s", ftrace_rec_count(rec), |
| 2040 | rec->flags & FTRACE_FL_REGS ? " R" : " "); |
| 2041 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2042 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2043 | if (ops) { |
| 2044 | do { |
| 2045 | pr_cont("\ttramp: %pS (%pS)", |
| 2046 | (void *)ops->trampoline, |
| 2047 | (void *)ops->func); |
| 2048 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 2049 | } while (ops); |
| 2050 | } else |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2051 | pr_cont("\ttramp: ERROR!"); |
| 2052 | |
| 2053 | } |
| 2054 | ip = ftrace_get_addr_curr(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2055 | pr_cont("\n expected tramp: %lx\n", ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2056 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2059 | 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] | 2060 | { |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2061 | unsigned long flag = 0UL; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 2062 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2063 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2064 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2065 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2066 | return FTRACE_UPDATE_IGNORE; |
| 2067 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2068 | /* |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2069 | * If we are updating calls: |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2070 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2071 | * If the record has a ref count, then we need to enable it |
| 2072 | * because someone is using it. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2073 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2074 | * Otherwise we make sure its disabled. |
| 2075 | * |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2076 | * If we are disabling calls, then disable all records that |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2077 | * are enabled. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2078 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2079 | if (enable && ftrace_rec_count(rec)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2080 | flag = FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2081 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2082 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2083 | * If enabling and the REGS flag does not match the REGS_EN, or |
| 2084 | * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore |
| 2085 | * this record. Set flags to fail the compare against ENABLED. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2086 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2087 | if (flag) { |
| 2088 | if (!(rec->flags & FTRACE_FL_REGS) != |
| 2089 | !(rec->flags & FTRACE_FL_REGS_EN)) |
| 2090 | flag |= FTRACE_FL_REGS; |
| 2091 | |
| 2092 | if (!(rec->flags & FTRACE_FL_TRAMP) != |
| 2093 | !(rec->flags & FTRACE_FL_TRAMP_EN)) |
| 2094 | flag |= FTRACE_FL_TRAMP; |
| 2095 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2096 | |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2097 | /* If the state of this record hasn't changed, then do nothing */ |
| 2098 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2099 | return FTRACE_UPDATE_IGNORE; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2100 | |
| 2101 | if (flag) { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2102 | /* Save off if rec is being enabled (for return value) */ |
| 2103 | flag ^= rec->flags & FTRACE_FL_ENABLED; |
| 2104 | |
| 2105 | if (update) { |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2106 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2107 | if (flag & FTRACE_FL_REGS) { |
| 2108 | if (rec->flags & FTRACE_FL_REGS) |
| 2109 | rec->flags |= FTRACE_FL_REGS_EN; |
| 2110 | else |
| 2111 | rec->flags &= ~FTRACE_FL_REGS_EN; |
| 2112 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2113 | if (flag & FTRACE_FL_TRAMP) { |
| 2114 | if (rec->flags & FTRACE_FL_TRAMP) |
| 2115 | rec->flags |= FTRACE_FL_TRAMP_EN; |
| 2116 | else |
| 2117 | rec->flags &= ~FTRACE_FL_TRAMP_EN; |
| 2118 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * If this record is being updated from a nop, then |
| 2123 | * return UPDATE_MAKE_CALL. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2124 | * Otherwise, |
| 2125 | * return UPDATE_MODIFY_CALL to tell the caller to convert |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2126 | * 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] | 2127 | * vice versa, or from a trampoline call. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2128 | */ |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2129 | if (flag & FTRACE_FL_ENABLED) { |
| 2130 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2131 | return FTRACE_UPDATE_MAKE_CALL; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2132 | } |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2133 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2134 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2135 | return FTRACE_UPDATE_MODIFY_CALL; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2136 | } |
| 2137 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2138 | if (update) { |
| 2139 | /* If there's no more users, clear all flags */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2140 | if (!ftrace_rec_count(rec)) |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2141 | rec->flags = 0; |
| 2142 | else |
Steven Rostedt (Red Hat) | b24d443 | 2015-03-04 23:10:28 -0500 | [diff] [blame] | 2143 | /* |
| 2144 | * Just disable the record, but keep the ops TRAMP |
| 2145 | * and REGS states. The _EN flags must be disabled though. |
| 2146 | */ |
| 2147 | rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN | |
| 2148 | FTRACE_FL_REGS_EN); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2149 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2150 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2151 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2152 | return FTRACE_UPDATE_MAKE_NOP; |
| 2153 | } |
| 2154 | |
| 2155 | /** |
| 2156 | * ftrace_update_record, set a record that now is tracing or not |
| 2157 | * @rec: the record to update |
| 2158 | * @enable: set to 1 if the record is tracing, zero to force disable |
| 2159 | * |
| 2160 | * The records that represent all functions that can be traced need |
| 2161 | * to be updated when tracing has been enabled. |
| 2162 | */ |
| 2163 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) |
| 2164 | { |
| 2165 | return ftrace_check_record(rec, enable, 1); |
| 2166 | } |
| 2167 | |
| 2168 | /** |
| 2169 | * ftrace_test_record, check if the record has been enabled or not |
| 2170 | * @rec: the record to test |
| 2171 | * @enable: set to 1 to check if enabled, 0 if it is disabled |
| 2172 | * |
| 2173 | * The arch code may need to test if a record is already set to |
| 2174 | * tracing to determine how to modify the function code that it |
| 2175 | * represents. |
| 2176 | */ |
| 2177 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) |
| 2178 | { |
| 2179 | return ftrace_check_record(rec, enable, 0); |
| 2180 | } |
| 2181 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2182 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2183 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) |
| 2184 | { |
| 2185 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2186 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2187 | |
| 2188 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2189 | |
| 2190 | if (!op->trampoline) |
| 2191 | continue; |
| 2192 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2193 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2194 | return op; |
| 2195 | } while_for_each_ftrace_op(op); |
| 2196 | |
| 2197 | return NULL; |
| 2198 | } |
| 2199 | |
| 2200 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2201 | ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, |
| 2202 | struct ftrace_ops *op) |
| 2203 | { |
| 2204 | unsigned long ip = rec->ip; |
| 2205 | |
| 2206 | while_for_each_ftrace_op(op) { |
| 2207 | |
| 2208 | if (!op->trampoline) |
| 2209 | continue; |
| 2210 | |
| 2211 | if (hash_contains_ip(ip, op->func_hash)) |
| 2212 | return op; |
| 2213 | } |
| 2214 | |
| 2215 | return NULL; |
| 2216 | } |
| 2217 | |
| 2218 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2219 | ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) |
| 2220 | { |
| 2221 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2222 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2223 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2224 | /* |
| 2225 | * Need to check removed ops first. |
| 2226 | * If they are being removed, and this rec has a tramp, |
| 2227 | * and this rec is in the ops list, then it would be the |
| 2228 | * one with the tramp. |
| 2229 | */ |
| 2230 | if (removed_ops) { |
| 2231 | if (hash_contains_ip(ip, &removed_ops->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2232 | return removed_ops; |
| 2233 | } |
| 2234 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2235 | /* |
| 2236 | * Need to find the current trampoline for a rec. |
| 2237 | * Now, a trampoline is only attached to a rec if there |
| 2238 | * was a single 'ops' attached to it. But this can be called |
| 2239 | * when we are adding another op to the rec or removing the |
| 2240 | * current one. Thus, if the op is being added, we can |
| 2241 | * 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] | 2242 | * yet. |
| 2243 | * |
| 2244 | * If an ops is being modified (hooking to different functions) |
| 2245 | * then we don't care about the new functions that are being |
| 2246 | * added, just the old ones (that are probably being removed). |
| 2247 | * |
| 2248 | * If we are adding an ops to a function that already is using |
| 2249 | * a trampoline, it needs to be removed (trampolines are only |
| 2250 | * for single ops connected), then an ops that is not being |
| 2251 | * modified also needs to be checked. |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2252 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2253 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2254 | |
| 2255 | if (!op->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2256 | continue; |
| 2257 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2258 | /* |
| 2259 | * If the ops is being added, it hasn't gotten to |
| 2260 | * the point to be removed from this tree yet. |
| 2261 | */ |
| 2262 | if (op->flags & FTRACE_OPS_FL_ADDING) |
| 2263 | continue; |
| 2264 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2265 | |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2266 | /* |
| 2267 | * If the ops is being modified and is in the old |
| 2268 | * hash, then it is probably being removed from this |
| 2269 | * function. |
| 2270 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2271 | if ((op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2272 | hash_contains_ip(ip, &op->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2273 | return op; |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2274 | /* |
| 2275 | * If the ops is not being added or modified, and it's |
| 2276 | * in its normal filter hash, then this must be the one |
| 2277 | * we want! |
| 2278 | */ |
| 2279 | if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2280 | hash_contains_ip(ip, op->func_hash)) |
| 2281 | return op; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2282 | |
| 2283 | } while_for_each_ftrace_op(op); |
| 2284 | |
| 2285 | return NULL; |
| 2286 | } |
| 2287 | |
| 2288 | static struct ftrace_ops * |
| 2289 | ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) |
| 2290 | { |
| 2291 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2292 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2293 | |
| 2294 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2295 | /* pass rec in as regs to have non-NULL val */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2296 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2297 | return op; |
| 2298 | } while_for_each_ftrace_op(op); |
| 2299 | |
| 2300 | return NULL; |
| 2301 | } |
| 2302 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2303 | /** |
| 2304 | * ftrace_get_addr_new - Get the call address to set to |
| 2305 | * @rec: The ftrace record descriptor |
| 2306 | * |
| 2307 | * If the record has the FTRACE_FL_REGS set, that means that it |
| 2308 | * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS |
| 2309 | * is not not set, then it wants to convert to the normal callback. |
| 2310 | * |
| 2311 | * Returns the address of the trampoline to set to |
| 2312 | */ |
| 2313 | unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) |
| 2314 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2315 | struct ftrace_ops *ops; |
| 2316 | |
| 2317 | /* Trampolines take precedence over regs */ |
| 2318 | if (rec->flags & FTRACE_FL_TRAMP) { |
| 2319 | ops = ftrace_find_tramp_ops_new(rec); |
| 2320 | if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 2321 | pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n", |
| 2322 | (void *)rec->ip, (void *)rec->ip, rec->flags); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2323 | /* Ftrace is shutting down, return anything */ |
| 2324 | return (unsigned long)FTRACE_ADDR; |
| 2325 | } |
| 2326 | return ops->trampoline; |
| 2327 | } |
| 2328 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2329 | if (rec->flags & FTRACE_FL_REGS) |
| 2330 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2331 | else |
| 2332 | return (unsigned long)FTRACE_ADDR; |
| 2333 | } |
| 2334 | |
| 2335 | /** |
| 2336 | * ftrace_get_addr_curr - Get the call address that is already there |
| 2337 | * @rec: The ftrace record descriptor |
| 2338 | * |
| 2339 | * The FTRACE_FL_REGS_EN is set when the record already points to |
| 2340 | * a function that saves all the regs. Basically the '_EN' version |
| 2341 | * represents the current state of the function. |
| 2342 | * |
| 2343 | * Returns the address of the trampoline that is currently being called |
| 2344 | */ |
| 2345 | unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) |
| 2346 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2347 | struct ftrace_ops *ops; |
| 2348 | |
| 2349 | /* Trampolines take precedence over regs */ |
| 2350 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2351 | ops = ftrace_find_tramp_ops_curr(rec); |
| 2352 | if (FTRACE_WARN_ON(!ops)) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 2353 | pr_warn("Bad trampoline accounting at: %p (%pS)\n", |
| 2354 | (void *)rec->ip, (void *)rec->ip); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2355 | /* Ftrace is shutting down, return anything */ |
| 2356 | return (unsigned long)FTRACE_ADDR; |
| 2357 | } |
| 2358 | return ops->trampoline; |
| 2359 | } |
| 2360 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2361 | if (rec->flags & FTRACE_FL_REGS_EN) |
| 2362 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2363 | else |
| 2364 | return (unsigned long)FTRACE_ADDR; |
| 2365 | } |
| 2366 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2367 | static int |
| 2368 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
| 2369 | { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2370 | unsigned long ftrace_old_addr; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2371 | unsigned long ftrace_addr; |
| 2372 | int ret; |
| 2373 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2374 | ftrace_addr = ftrace_get_addr_new(rec); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2375 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2376 | /* This needs to be done before we call ftrace_update_record */ |
| 2377 | ftrace_old_addr = ftrace_get_addr_curr(rec); |
| 2378 | |
| 2379 | ret = ftrace_update_record(rec, enable); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2380 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2381 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2382 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2383 | switch (ret) { |
| 2384 | case FTRACE_UPDATE_IGNORE: |
| 2385 | return 0; |
| 2386 | |
| 2387 | case FTRACE_UPDATE_MAKE_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2388 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2389 | return ftrace_make_call(rec, ftrace_addr); |
| 2390 | |
| 2391 | case FTRACE_UPDATE_MAKE_NOP: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2392 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt (Red Hat) | 39b5552 | 2014-08-17 20:59:10 -0400 | [diff] [blame] | 2393 | return ftrace_make_nop(NULL, rec, ftrace_old_addr); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2394 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2395 | case FTRACE_UPDATE_MODIFY_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2396 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2397 | return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | return -1; /* unknow ftrace bug */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2403 | void __weak ftrace_replace_code(int enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2404 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2405 | struct dyn_ftrace *rec; |
| 2406 | struct ftrace_page *pg; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2407 | int failed; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2408 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2409 | if (unlikely(ftrace_disabled)) |
| 2410 | return; |
| 2411 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2412 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 2413 | |
| 2414 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2415 | continue; |
| 2416 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2417 | failed = __ftrace_replace_code(rec, enable); |
Zhaolei | fa9d13c | 2009-03-13 17:16:34 +0800 | [diff] [blame] | 2418 | if (failed) { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2419 | ftrace_bug(failed, rec); |
Steven Rostedt | 3279ba3 | 2009-10-07 16:57:56 -0400 | [diff] [blame] | 2420 | /* Stop processing */ |
| 2421 | return; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2422 | } |
| 2423 | } while_for_each_ftrace_rec(); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2424 | } |
| 2425 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2426 | struct ftrace_rec_iter { |
| 2427 | struct ftrace_page *pg; |
| 2428 | int index; |
| 2429 | }; |
| 2430 | |
| 2431 | /** |
| 2432 | * ftrace_rec_iter_start, start up iterating over traced functions |
| 2433 | * |
| 2434 | * Returns an iterator handle that is used to iterate over all |
| 2435 | * the records that represent address locations where functions |
| 2436 | * are traced. |
| 2437 | * |
| 2438 | * May return NULL if no records are available. |
| 2439 | */ |
| 2440 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 2441 | { |
| 2442 | /* |
| 2443 | * We only use a single iterator. |
| 2444 | * Protected by the ftrace_lock mutex. |
| 2445 | */ |
| 2446 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 2447 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 2448 | |
| 2449 | iter->pg = ftrace_pages_start; |
| 2450 | iter->index = 0; |
| 2451 | |
| 2452 | /* Could have empty pages */ |
| 2453 | while (iter->pg && !iter->pg->index) |
| 2454 | iter->pg = iter->pg->next; |
| 2455 | |
| 2456 | if (!iter->pg) |
| 2457 | return NULL; |
| 2458 | |
| 2459 | return iter; |
| 2460 | } |
| 2461 | |
| 2462 | /** |
| 2463 | * ftrace_rec_iter_next, get the next record to process. |
| 2464 | * @iter: The handle to the iterator. |
| 2465 | * |
| 2466 | * Returns the next iterator after the given iterator @iter. |
| 2467 | */ |
| 2468 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 2469 | { |
| 2470 | iter->index++; |
| 2471 | |
| 2472 | if (iter->index >= iter->pg->index) { |
| 2473 | iter->pg = iter->pg->next; |
| 2474 | iter->index = 0; |
| 2475 | |
| 2476 | /* Could have empty pages */ |
| 2477 | while (iter->pg && !iter->pg->index) |
| 2478 | iter->pg = iter->pg->next; |
| 2479 | } |
| 2480 | |
| 2481 | if (!iter->pg) |
| 2482 | return NULL; |
| 2483 | |
| 2484 | return iter; |
| 2485 | } |
| 2486 | |
| 2487 | /** |
| 2488 | * ftrace_rec_iter_record, get the record at the iterator location |
| 2489 | * @iter: The current iterator location |
| 2490 | * |
| 2491 | * Returns the record that the current @iter is at. |
| 2492 | */ |
| 2493 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 2494 | { |
| 2495 | return &iter->pg->records[iter->index]; |
| 2496 | } |
| 2497 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2498 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2499 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2500 | { |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2501 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2502 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2503 | if (unlikely(ftrace_disabled)) |
| 2504 | return 0; |
| 2505 | |
Shaohua Li | 25aac9d | 2009-01-09 11:29:40 +0800 | [diff] [blame] | 2506 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2507 | if (ret) { |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2508 | ftrace_bug_type = FTRACE_BUG_INIT; |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2509 | ftrace_bug(ret, rec); |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2510 | return 0; |
Steven Rostedt | 37ad508 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2511 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2512 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2515 | /* |
| 2516 | * archs can override this function if they must do something |
| 2517 | * before the modifying code is performed. |
| 2518 | */ |
| 2519 | int __weak ftrace_arch_code_modify_prepare(void) |
| 2520 | { |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
| 2524 | /* |
| 2525 | * archs can override this function if they must do something |
| 2526 | * after the modifying code is performed. |
| 2527 | */ |
| 2528 | int __weak ftrace_arch_code_modify_post_process(void) |
| 2529 | { |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2533 | void ftrace_modify_all_code(int command) |
| 2534 | { |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2535 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2536 | int err = 0; |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2537 | |
| 2538 | /* |
| 2539 | * If the ftrace_caller calls a ftrace_ops func directly, |
| 2540 | * we need to make sure that it only traces functions it |
| 2541 | * expects to trace. When doing the switch of functions, |
| 2542 | * we need to update to the ftrace_ops_list_func first |
| 2543 | * before the transition between old and new calls are set, |
| 2544 | * as the ftrace_ops_list_func will check the ops hashes |
| 2545 | * to make sure the ops are having the right functions |
| 2546 | * traced. |
| 2547 | */ |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2548 | if (update) { |
| 2549 | err = ftrace_update_ftrace_func(ftrace_ops_list_func); |
| 2550 | if (FTRACE_WARN_ON(err)) |
| 2551 | return; |
| 2552 | } |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2553 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2554 | if (command & FTRACE_UPDATE_CALLS) |
| 2555 | ftrace_replace_code(1); |
| 2556 | else if (command & FTRACE_DISABLE_CALLS) |
| 2557 | ftrace_replace_code(0); |
| 2558 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2559 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
| 2560 | function_trace_op = set_function_trace_op; |
| 2561 | smp_wmb(); |
| 2562 | /* If irqs are disabled, we are in stop machine */ |
| 2563 | if (!irqs_disabled()) |
| 2564 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2565 | err = ftrace_update_ftrace_func(ftrace_trace_function); |
| 2566 | if (FTRACE_WARN_ON(err)) |
| 2567 | return; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2568 | } |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2569 | |
| 2570 | if (command & FTRACE_START_FUNC_RET) |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2571 | err = ftrace_enable_ftrace_graph_caller(); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2572 | else if (command & FTRACE_STOP_FUNC_RET) |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2573 | err = ftrace_disable_ftrace_graph_caller(); |
| 2574 | FTRACE_WARN_ON(err); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2575 | } |
| 2576 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2577 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2578 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2579 | int *command = data; |
| 2580 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2581 | ftrace_modify_all_code(*command); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 2582 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2583 | return 0; |
| 2584 | } |
| 2585 | |
| 2586 | /** |
| 2587 | * ftrace_run_stop_machine, go back to the stop machine method |
| 2588 | * @command: The command to tell ftrace what to do |
| 2589 | * |
| 2590 | * If an arch needs to fall back to the stop machine method, the |
| 2591 | * it can call this function. |
| 2592 | */ |
| 2593 | void ftrace_run_stop_machine(int command) |
| 2594 | { |
| 2595 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 2596 | } |
| 2597 | |
| 2598 | /** |
| 2599 | * arch_ftrace_update_code, modify the code to trace or not trace |
| 2600 | * @command: The command that needs to be done |
| 2601 | * |
| 2602 | * Archs can override this function if it does not need to |
| 2603 | * run stop_machine() to modify code. |
| 2604 | */ |
| 2605 | void __weak arch_ftrace_update_code(int command) |
| 2606 | { |
| 2607 | ftrace_run_stop_machine(command); |
| 2608 | } |
| 2609 | |
| 2610 | static void ftrace_run_update_code(int command) |
| 2611 | { |
| 2612 | int ret; |
| 2613 | |
| 2614 | ret = ftrace_arch_code_modify_prepare(); |
| 2615 | FTRACE_WARN_ON(ret); |
| 2616 | if (ret) |
| 2617 | return; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2618 | |
| 2619 | /* |
| 2620 | * By default we use stop_machine() to modify the code. |
| 2621 | * But archs can do what ever they want as long as it |
| 2622 | * is safe. The stop_machine() is the safest, but also |
| 2623 | * produces the most overhead. |
| 2624 | */ |
| 2625 | arch_ftrace_update_code(command); |
| 2626 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2627 | ret = ftrace_arch_code_modify_post_process(); |
| 2628 | FTRACE_WARN_ON(ret); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2629 | } |
| 2630 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2631 | static void ftrace_run_modify_code(struct ftrace_ops *ops, int command, |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2632 | struct ftrace_ops_hash *old_hash) |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2633 | { |
| 2634 | ops->flags |= FTRACE_OPS_FL_MODIFYING; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2635 | ops->old_hash.filter_hash = old_hash->filter_hash; |
| 2636 | ops->old_hash.notrace_hash = old_hash->notrace_hash; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2637 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2638 | ops->old_hash.filter_hash = NULL; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2639 | ops->old_hash.notrace_hash = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2640 | ops->flags &= ~FTRACE_OPS_FL_MODIFYING; |
| 2641 | } |
| 2642 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2643 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2644 | static int ftrace_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2645 | |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2646 | void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) |
| 2647 | { |
| 2648 | } |
| 2649 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2650 | static void per_cpu_ops_free(struct ftrace_ops *ops) |
Jiri Slaby | db0fbad | 2014-03-10 21:42:11 +0100 | [diff] [blame] | 2651 | { |
| 2652 | free_percpu(ops->disabled); |
| 2653 | } |
| 2654 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2655 | static void ftrace_startup_enable(int command) |
| 2656 | { |
| 2657 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2658 | saved_ftrace_func = ftrace_trace_function; |
| 2659 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2660 | } |
| 2661 | |
| 2662 | if (!command || !ftrace_enabled) |
| 2663 | return; |
| 2664 | |
| 2665 | ftrace_run_update_code(command); |
| 2666 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2667 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2668 | static void ftrace_startup_all(int command) |
| 2669 | { |
| 2670 | update_all_ops = true; |
| 2671 | ftrace_startup_enable(command); |
| 2672 | update_all_ops = false; |
| 2673 | } |
| 2674 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2675 | static int ftrace_startup(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2676 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2677 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2678 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2679 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2680 | return -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2681 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2682 | ret = __register_ftrace_function(ops); |
| 2683 | if (ret) |
| 2684 | return ret; |
| 2685 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2686 | ftrace_start_up++; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2687 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2688 | /* |
| 2689 | * Note that ftrace probes uses this to start up |
| 2690 | * and modify functions it will probe. But we still |
| 2691 | * set the ADDING flag for modification, as probes |
| 2692 | * do not have trampolines. If they add them in the |
| 2693 | * future, then the probes will need to distinguish |
| 2694 | * between adding and updating probes. |
| 2695 | */ |
| 2696 | ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; |
Steven Rostedt (Red Hat) | 66209a5 | 2014-05-06 21:57:49 -0400 | [diff] [blame] | 2697 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2698 | ret = ftrace_hash_ipmodify_enable(ops); |
| 2699 | if (ret < 0) { |
| 2700 | /* Rollback registration process */ |
| 2701 | __unregister_ftrace_function(ops); |
| 2702 | ftrace_start_up--; |
| 2703 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 2704 | return ret; |
| 2705 | } |
| 2706 | |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2707 | if (ftrace_hash_rec_enable(ops, 1)) |
| 2708 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2709 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2710 | ftrace_startup_enable(command); |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2711 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2712 | ops->flags &= ~FTRACE_OPS_FL_ADDING; |
| 2713 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2714 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2715 | } |
| 2716 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2717 | static int ftrace_shutdown(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2718 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2719 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2720 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2721 | if (unlikely(ftrace_disabled)) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2722 | return -ENODEV; |
| 2723 | |
| 2724 | ret = __unregister_ftrace_function(ops); |
| 2725 | if (ret) |
| 2726 | return ret; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2727 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2728 | ftrace_start_up--; |
Frederic Weisbecker | 9ea1a15 | 2009-06-20 06:52:21 +0200 | [diff] [blame] | 2729 | /* |
| 2730 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 2731 | * critical but the ftrace_call callers may be never nopped again after |
| 2732 | * further ftrace uses. |
| 2733 | */ |
| 2734 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 2735 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2736 | /* Disabling ipmodify never fails */ |
| 2737 | ftrace_hash_ipmodify_disable(ops); |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2738 | |
| 2739 | if (ftrace_hash_rec_disable(ops, 1)) |
| 2740 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2741 | |
Namhyung Kim | a737e6d | 2014-06-12 23:56:12 +0900 | [diff] [blame] | 2742 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2743 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2744 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2745 | saved_ftrace_func = ftrace_trace_function; |
| 2746 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2747 | } |
| 2748 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2749 | if (!command || !ftrace_enabled) { |
| 2750 | /* |
Steven Rostedt (VMware) | 100553e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2751 | * If these are dynamic or per_cpu ops, they still |
| 2752 | * need their data freed. Since, function tracing is |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2753 | * not currently active, we can just free them |
| 2754 | * without synchronizing all CPUs. |
| 2755 | */ |
Steven Rostedt (VMware) | 100553e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2756 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) |
| 2757 | goto free_ops; |
| 2758 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2759 | return 0; |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2760 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2761 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2762 | /* |
| 2763 | * If the ops uses a trampoline, then it needs to be |
| 2764 | * tested first on update. |
| 2765 | */ |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2766 | ops->flags |= FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2767 | removed_ops = ops; |
| 2768 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2769 | /* The trampoline logic checks the old hashes */ |
| 2770 | ops->old_hash.filter_hash = ops->func_hash->filter_hash; |
| 2771 | ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; |
| 2772 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2773 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2774 | |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2775 | /* |
| 2776 | * If there's no more ops registered with ftrace, run a |
| 2777 | * sanity check to make sure all rec flags are cleared. |
| 2778 | */ |
| 2779 | if (ftrace_ops_list == &ftrace_list_end) { |
| 2780 | struct ftrace_page *pg; |
| 2781 | struct dyn_ftrace *rec; |
| 2782 | |
| 2783 | do_for_each_ftrace_rec(pg, rec) { |
Alexei Starovoitov | 977c1f9 | 2016-11-07 15:14:20 -0800 | [diff] [blame] | 2784 | if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED)) |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2785 | pr_warn(" %pS flags:%lx\n", |
| 2786 | (void *)rec->ip, rec->flags); |
| 2787 | } while_for_each_ftrace_rec(); |
| 2788 | } |
| 2789 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2790 | ops->old_hash.filter_hash = NULL; |
| 2791 | ops->old_hash.notrace_hash = NULL; |
| 2792 | |
| 2793 | removed_ops = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2794 | ops->flags &= ~FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2795 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2796 | /* |
| 2797 | * Dynamic ops may be freed, we must make sure that all |
| 2798 | * callers are done before leaving this function. |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2799 | * The same goes for freeing the per_cpu data of the per_cpu |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2800 | * ops. |
| 2801 | * |
| 2802 | * Again, normal synchronize_sched() is not good enough. |
| 2803 | * We need to do a hard force of sched synchronization. |
| 2804 | * This is because we use preempt_disable() to do RCU, but |
| 2805 | * the function tracers can be called where RCU is not watching |
| 2806 | * (like before user_exit()). We can not rely on the RCU |
| 2807 | * infrastructure to do the synchronization, thus we must do it |
| 2808 | * ourselves. |
| 2809 | */ |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2810 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) { |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2811 | schedule_on_each_cpu(ftrace_sync); |
| 2812 | |
Steven Rostedt (VMware) | 100553e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2813 | free_ops: |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2814 | arch_ftrace_trampoline_free(ops); |
| 2815 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2816 | if (ops->flags & FTRACE_OPS_FL_PER_CPU) |
| 2817 | per_cpu_ops_free(ops); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2818 | } |
| 2819 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2820 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2823 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2824 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2825 | int command; |
| 2826 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2827 | if (unlikely(ftrace_disabled)) |
| 2828 | return; |
| 2829 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2830 | /* Force update next time */ |
| 2831 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2832 | /* ftrace_start_up is true if we want ftrace running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2833 | if (ftrace_start_up) { |
| 2834 | command = FTRACE_UPDATE_CALLS; |
| 2835 | if (ftrace_graph_active) |
| 2836 | command |= FTRACE_START_FUNC_RET; |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 2837 | ftrace_startup_enable(command); |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2838 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2839 | } |
| 2840 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2841 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2842 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2843 | int command; |
| 2844 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2845 | if (unlikely(ftrace_disabled)) |
| 2846 | return; |
| 2847 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2848 | /* ftrace_start_up is true if ftrace is running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2849 | if (ftrace_start_up) { |
| 2850 | command = FTRACE_DISABLE_CALLS; |
| 2851 | if (ftrace_graph_active) |
| 2852 | command |= FTRACE_STOP_FUNC_RET; |
| 2853 | ftrace_run_update_code(command); |
| 2854 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2857 | static cycle_t ftrace_update_time; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2858 | unsigned long ftrace_update_tot_cnt; |
| 2859 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2860 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2861 | { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2862 | /* |
| 2863 | * Filter_hash being empty will default to trace module. |
| 2864 | * But notrace hash requires a test of individual module functions. |
| 2865 | */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2866 | return ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2867 | ftrace_hash_empty(ops->func_hash->notrace_hash); |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2868 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2869 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2870 | /* |
| 2871 | * Check if the current ops references the record. |
| 2872 | * |
| 2873 | * If the ops traces all functions, then it was already accounted for. |
| 2874 | * If the ops does not trace the current record function, skip it. |
| 2875 | * If the ops ignores the function via notrace filter, skip it. |
| 2876 | */ |
| 2877 | static inline bool |
| 2878 | ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 2879 | { |
| 2880 | /* If ops isn't enabled, ignore it */ |
| 2881 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 2882 | return 0; |
| 2883 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2884 | /* If ops traces all then it includes this function */ |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2885 | if (ops_traces_mod(ops)) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2886 | return 1; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2887 | |
| 2888 | /* The function must be in the filter */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2889 | if (!ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2890 | !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2891 | return 0; |
| 2892 | |
| 2893 | /* If in notrace hash, we ignore it too */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2894 | 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] | 2895 | return 0; |
| 2896 | |
| 2897 | return 1; |
| 2898 | } |
| 2899 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2900 | 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] | 2901 | { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2902 | struct ftrace_page *pg; |
Lai Jiangshan | e94142a | 2009-03-13 17:51:27 +0800 | [diff] [blame] | 2903 | struct dyn_ftrace *p; |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2904 | cycle_t start, stop; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2905 | unsigned long update_cnt = 0; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2906 | unsigned long rec_flags = 0; |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2907 | int i; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2908 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2909 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2910 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2911 | /* |
| 2912 | * When a module is loaded, this function is called to convert |
| 2913 | * the calls to mcount in its text to nops, and also to create |
| 2914 | * an entry in the ftrace data. Now, if ftrace is activated |
| 2915 | * after this call, but before the module sets its text to |
| 2916 | * read-only, the modification of enabling ftrace can fail if |
| 2917 | * the read-only is done while ftrace is converting the calls. |
| 2918 | * To prevent this, the module's records are set as disabled |
| 2919 | * and will be enabled after the call to set the module's text |
| 2920 | * to read-only. |
| 2921 | */ |
| 2922 | if (mod) |
| 2923 | rec_flags |= FTRACE_FL_DISABLED; |
| 2924 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2925 | for (pg = new_pgs; pg; pg = pg->next) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2926 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2927 | for (i = 0; i < pg->index; i++) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2928 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2929 | /* If something went wrong, bail without enabling anything */ |
| 2930 | if (unlikely(ftrace_disabled)) |
| 2931 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2932 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2933 | p = &pg->records[i]; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2934 | p->flags = rec_flags; |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 2935 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2936 | /* |
| 2937 | * Do the initial record conversion from mcount jump |
| 2938 | * to the NOP instructions. |
| 2939 | */ |
| 2940 | if (!ftrace_code_disable(mod, p)) |
| 2941 | break; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2942 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2943 | update_cnt++; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2944 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2947 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2948 | ftrace_update_time = stop - start; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2949 | ftrace_update_tot_cnt += update_cnt; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2950 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2951 | return 0; |
| 2952 | } |
| 2953 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2954 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2955 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2956 | int order; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2957 | int cnt; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2958 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2959 | if (WARN_ON(!count)) |
| 2960 | return -EINVAL; |
| 2961 | |
| 2962 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2963 | |
| 2964 | /* |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2965 | * We want to fill as much as possible. No more than a page |
| 2966 | * may be empty. |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2967 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2968 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
| 2969 | order--; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2970 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2971 | again: |
| 2972 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 2973 | |
| 2974 | if (!pg->records) { |
| 2975 | /* if we can't allocate this size, try something smaller */ |
| 2976 | if (!order) |
| 2977 | return -ENOMEM; |
| 2978 | order >>= 1; |
| 2979 | goto again; |
| 2980 | } |
| 2981 | |
| 2982 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 2983 | pg->size = cnt; |
| 2984 | |
| 2985 | if (cnt > count) |
| 2986 | cnt = count; |
| 2987 | |
| 2988 | return cnt; |
| 2989 | } |
| 2990 | |
| 2991 | static struct ftrace_page * |
| 2992 | ftrace_allocate_pages(unsigned long num_to_init) |
| 2993 | { |
| 2994 | struct ftrace_page *start_pg; |
| 2995 | struct ftrace_page *pg; |
| 2996 | int order; |
| 2997 | int cnt; |
| 2998 | |
| 2999 | if (!num_to_init) |
| 3000 | return 0; |
| 3001 | |
| 3002 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3003 | if (!pg) |
| 3004 | return NULL; |
| 3005 | |
| 3006 | /* |
| 3007 | * Try to allocate as much as possible in one continues |
| 3008 | * location that fills in all of the space. We want to |
| 3009 | * waste as little space as possible. |
| 3010 | */ |
| 3011 | for (;;) { |
| 3012 | cnt = ftrace_allocate_records(pg, num_to_init); |
| 3013 | if (cnt < 0) |
| 3014 | goto free_pages; |
| 3015 | |
| 3016 | num_to_init -= cnt; |
| 3017 | if (!num_to_init) |
| 3018 | break; |
| 3019 | |
| 3020 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3021 | if (!pg->next) |
| 3022 | goto free_pages; |
| 3023 | |
| 3024 | pg = pg->next; |
| 3025 | } |
| 3026 | |
| 3027 | return start_pg; |
| 3028 | |
| 3029 | free_pages: |
Namhyung Kim | 1f61be00 | 2014-06-11 17:06:53 +0900 | [diff] [blame] | 3030 | pg = start_pg; |
| 3031 | while (pg) { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3032 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 3033 | free_pages((unsigned long)pg->records, order); |
| 3034 | start_pg = pg->next; |
| 3035 | kfree(pg); |
| 3036 | pg = start_pg; |
| 3037 | } |
| 3038 | pr_info("ftrace: FAILED to allocate memory for functions\n"); |
| 3039 | return NULL; |
| 3040 | } |
| 3041 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3042 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 3043 | |
| 3044 | struct ftrace_iterator { |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3045 | loff_t pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3046 | loff_t func_pos; |
| 3047 | struct ftrace_page *pg; |
| 3048 | struct dyn_ftrace *func; |
| 3049 | struct ftrace_func_probe *probe; |
| 3050 | struct trace_parser parser; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3051 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3052 | struct ftrace_ops *ops; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3053 | int hidx; |
| 3054 | int idx; |
| 3055 | unsigned flags; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3056 | }; |
| 3057 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3058 | static void * |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3059 | t_hash_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3060 | { |
| 3061 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3062 | struct hlist_node *hnd = NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3063 | struct hlist_head *hhd; |
| 3064 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3065 | (*pos)++; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3066 | iter->pos = *pos; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3067 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3068 | if (iter->probe) |
| 3069 | hnd = &iter->probe->node; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3070 | retry: |
| 3071 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) |
| 3072 | return NULL; |
| 3073 | |
| 3074 | hhd = &ftrace_func_hash[iter->hidx]; |
| 3075 | |
| 3076 | if (hlist_empty(hhd)) { |
| 3077 | iter->hidx++; |
| 3078 | hnd = NULL; |
| 3079 | goto retry; |
| 3080 | } |
| 3081 | |
| 3082 | if (!hnd) |
| 3083 | hnd = hhd->first; |
| 3084 | else { |
| 3085 | hnd = hnd->next; |
| 3086 | if (!hnd) { |
| 3087 | iter->hidx++; |
| 3088 | goto retry; |
| 3089 | } |
| 3090 | } |
| 3091 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3092 | if (WARN_ON_ONCE(!hnd)) |
| 3093 | return NULL; |
| 3094 | |
| 3095 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); |
| 3096 | |
| 3097 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | static void *t_hash_start(struct seq_file *m, loff_t *pos) |
| 3101 | { |
| 3102 | struct ftrace_iterator *iter = m->private; |
| 3103 | void *p = NULL; |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3104 | loff_t l; |
| 3105 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3106 | if (!(iter->flags & FTRACE_ITER_DO_HASH)) |
| 3107 | return NULL; |
| 3108 | |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3109 | if (iter->func_pos > *pos) |
| 3110 | return NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3111 | |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3112 | iter->hidx = 0; |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3113 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3114 | p = t_hash_next(m, &l); |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3115 | if (!p) |
| 3116 | break; |
| 3117 | } |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3118 | if (!p) |
| 3119 | return NULL; |
| 3120 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3121 | /* Only set this if we have an item */ |
| 3122 | iter->flags |= FTRACE_ITER_HASH; |
| 3123 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3124 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3125 | } |
| 3126 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3127 | static int |
| 3128 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3129 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3130 | struct ftrace_func_probe *rec; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3131 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3132 | rec = iter->probe; |
| 3133 | if (WARN_ON_ONCE(!rec)) |
| 3134 | return -EIO; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3135 | |
Steven Rostedt | 809dcf2 | 2009-02-16 23:06:01 -0500 | [diff] [blame] | 3136 | if (rec->ops->print) |
| 3137 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); |
| 3138 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 3139 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3140 | |
| 3141 | if (rec->data) |
| 3142 | seq_printf(m, ":%p", rec->data); |
| 3143 | seq_putc(m, '\n'); |
| 3144 | |
| 3145 | return 0; |
| 3146 | } |
| 3147 | |
| 3148 | static void * |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3149 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 3150 | { |
| 3151 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3152 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3153 | struct dyn_ftrace *rec = NULL; |
| 3154 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3155 | if (unlikely(ftrace_disabled)) |
| 3156 | return NULL; |
| 3157 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3158 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3159 | return t_hash_next(m, pos); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3160 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3161 | (*pos)++; |
Jiri Olsa | 1106b69 | 2011-02-16 17:35:34 +0100 | [diff] [blame] | 3162 | iter->pos = iter->func_pos = *pos; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3163 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3164 | if (iter->flags & FTRACE_ITER_PRINTALL) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3165 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3166 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3167 | retry: |
| 3168 | if (iter->idx >= iter->pg->index) { |
| 3169 | if (iter->pg->next) { |
| 3170 | iter->pg = iter->pg->next; |
| 3171 | iter->idx = 0; |
| 3172 | goto retry; |
| 3173 | } |
| 3174 | } else { |
| 3175 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3176 | if (((iter->flags & FTRACE_ITER_FILTER) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3177 | !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) || |
Steven Rostedt | 0183fb1 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3178 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3179 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3180 | !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) || |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3181 | |
| 3182 | ((iter->flags & FTRACE_ITER_ENABLED) && |
Steven Rostedt (Red Hat) | 23ea9c4 | 2013-05-09 19:31:48 -0400 | [diff] [blame] | 3183 | !(rec->flags & FTRACE_FL_ENABLED))) { |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3184 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3185 | rec = NULL; |
| 3186 | goto retry; |
| 3187 | } |
| 3188 | } |
| 3189 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3190 | if (!rec) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3191 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3192 | |
| 3193 | iter->func = rec; |
| 3194 | |
| 3195 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3196 | } |
| 3197 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3198 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 3199 | { |
| 3200 | iter->pos = 0; |
| 3201 | iter->func_pos = 0; |
Dan Carpenter | 70f77b3 | 2012-06-09 19:10:27 +0300 | [diff] [blame] | 3202 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 3206 | { |
| 3207 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3208 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3209 | void *p = NULL; |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3210 | loff_t l; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3211 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3212 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3213 | |
| 3214 | if (unlikely(ftrace_disabled)) |
| 3215 | return NULL; |
| 3216 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3217 | /* |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3218 | * If an lseek was done, then reset and start from beginning. |
| 3219 | */ |
| 3220 | if (*pos < iter->pos) |
| 3221 | reset_iter_read(iter); |
| 3222 | |
| 3223 | /* |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3224 | * For set_ftrace_filter reading, if we have the filter |
| 3225 | * off, we can short cut and just print out that all |
| 3226 | * functions are enabled. |
| 3227 | */ |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3228 | if ((iter->flags & FTRACE_ITER_FILTER && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3229 | ftrace_hash_empty(ops->func_hash->filter_hash)) || |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3230 | (iter->flags & FTRACE_ITER_NOTRACE && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3231 | ftrace_hash_empty(ops->func_hash->notrace_hash))) { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3232 | if (*pos > 0) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3233 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3234 | iter->flags |= FTRACE_ITER_PRINTALL; |
Chris Wright | df09162 | 2010-09-09 16:34:59 -0700 | [diff] [blame] | 3235 | /* reset in case of seek/pread */ |
| 3236 | iter->flags &= ~FTRACE_ITER_HASH; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3237 | return iter; |
| 3238 | } |
| 3239 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3240 | if (iter->flags & FTRACE_ITER_HASH) |
| 3241 | return t_hash_start(m, pos); |
| 3242 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3243 | /* |
| 3244 | * Unfortunately, we need to restart at ftrace_pages_start |
| 3245 | * every time we let go of the ftrace_mutex. This is because |
| 3246 | * those pointers can change without the lock. |
| 3247 | */ |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3248 | iter->pg = ftrace_pages_start; |
| 3249 | iter->idx = 0; |
| 3250 | for (l = 0; l <= *pos; ) { |
| 3251 | p = t_next(m, p, &l); |
| 3252 | if (!p) |
| 3253 | break; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 3254 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 3255 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3256 | if (!p) |
| 3257 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3258 | |
| 3259 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | static void t_stop(struct seq_file *m, void *p) |
| 3263 | { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3264 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3265 | } |
| 3266 | |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3267 | void * __weak |
| 3268 | arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 3269 | { |
| 3270 | return NULL; |
| 3271 | } |
| 3272 | |
| 3273 | static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, |
| 3274 | struct dyn_ftrace *rec) |
| 3275 | { |
| 3276 | void *ptr; |
| 3277 | |
| 3278 | ptr = arch_ftrace_trampoline_func(ops, rec); |
| 3279 | if (ptr) |
| 3280 | seq_printf(m, " ->%pS", ptr); |
| 3281 | } |
| 3282 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3283 | static int t_show(struct seq_file *m, void *v) |
| 3284 | { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3285 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3286 | struct dyn_ftrace *rec; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3287 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3288 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3289 | return t_hash_show(m, iter); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3290 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3291 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3292 | if (iter->flags & FTRACE_ITER_NOTRACE) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3293 | seq_puts(m, "#### no functions disabled ####\n"); |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3294 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3295 | seq_puts(m, "#### all functions enabled ####\n"); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3296 | return 0; |
| 3297 | } |
| 3298 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3299 | rec = iter->func; |
| 3300 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3301 | if (!rec) |
| 3302 | return 0; |
| 3303 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3304 | seq_printf(m, "%ps", (void *)rec->ip); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3305 | if (iter->flags & FTRACE_ITER_ENABLED) { |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3306 | struct ftrace_ops *ops; |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3307 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3308 | seq_printf(m, " (%ld)%s%s", |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 3309 | ftrace_rec_count(rec), |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3310 | rec->flags & FTRACE_FL_REGS ? " R" : " ", |
| 3311 | rec->flags & FTRACE_FL_IPMODIFY ? " I" : " "); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3312 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 3313 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3314 | if (ops) { |
| 3315 | do { |
| 3316 | seq_printf(m, "\ttramp: %pS (%pS)", |
| 3317 | (void *)ops->trampoline, |
| 3318 | (void *)ops->func); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3319 | add_trampoline_func(m, ops, rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3320 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 3321 | } while (ops); |
| 3322 | } else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3323 | seq_puts(m, "\ttramp: ERROR!"); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3324 | } else { |
| 3325 | add_trampoline_func(m, NULL, rec); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3329 | seq_putc(m, '\n'); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3330 | |
| 3331 | return 0; |
| 3332 | } |
| 3333 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3334 | static const struct seq_operations show_ftrace_seq_ops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3335 | .start = t_start, |
| 3336 | .next = t_next, |
| 3337 | .stop = t_stop, |
| 3338 | .show = t_show, |
| 3339 | }; |
| 3340 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3341 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3342 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 3343 | { |
| 3344 | struct ftrace_iterator *iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3345 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3346 | if (unlikely(ftrace_disabled)) |
| 3347 | return -ENODEV; |
| 3348 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3349 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3350 | if (iter) { |
| 3351 | iter->pg = ftrace_pages_start; |
| 3352 | iter->ops = &global_ops; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3353 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3354 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3355 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3356 | } |
| 3357 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3358 | static int |
| 3359 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 3360 | { |
| 3361 | struct ftrace_iterator *iter; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3362 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3363 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3364 | if (iter) { |
| 3365 | iter->pg = ftrace_pages_start; |
| 3366 | iter->flags = FTRACE_ITER_ENABLED; |
| 3367 | iter->ops = &global_ops; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3368 | } |
| 3369 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3370 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3371 | } |
| 3372 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3373 | /** |
| 3374 | * ftrace_regex_open - initialize function tracer filter files |
| 3375 | * @ops: The ftrace_ops that hold the hash filters |
| 3376 | * @flag: The type of filter to process |
| 3377 | * @inode: The inode, usually passed in to your open routine |
| 3378 | * @file: The file, usually passed in to your open routine |
| 3379 | * |
| 3380 | * ftrace_regex_open() initializes the filter files for the |
| 3381 | * @ops. Depending on @flag it may process the filter hash or |
| 3382 | * the notrace hash of @ops. With this called from the open |
| 3383 | * routine, you can use ftrace_filter_write() for the write |
| 3384 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 3385 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3386 | * tracing_lseek() should be used as the lseek routine, and |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3387 | * release must call ftrace_regex_release(). |
| 3388 | */ |
| 3389 | int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3390 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3391 | struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3392 | { |
| 3393 | struct ftrace_iterator *iter; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3394 | struct ftrace_hash *hash; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3395 | int ret = 0; |
| 3396 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3397 | ftrace_ops_init(ops); |
| 3398 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3399 | if (unlikely(ftrace_disabled)) |
| 3400 | return -ENODEV; |
| 3401 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3402 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 3403 | if (!iter) |
| 3404 | return -ENOMEM; |
| 3405 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3406 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
| 3407 | kfree(iter); |
| 3408 | return -ENOMEM; |
| 3409 | } |
| 3410 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3411 | iter->ops = ops; |
| 3412 | iter->flags = flag; |
| 3413 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3414 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3415 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3416 | if (flag & FTRACE_ITER_NOTRACE) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3417 | hash = ops->func_hash->notrace_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3418 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3419 | hash = ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3420 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3421 | if (file->f_mode & FMODE_WRITE) { |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3422 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 3423 | |
| 3424 | if (file->f_flags & O_TRUNC) |
| 3425 | iter->hash = alloc_ftrace_hash(size_bits); |
| 3426 | else |
| 3427 | iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); |
| 3428 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3429 | if (!iter->hash) { |
| 3430 | trace_parser_put(&iter->parser); |
| 3431 | kfree(iter); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3432 | ret = -ENOMEM; |
| 3433 | goto out_unlock; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3434 | } |
| 3435 | } |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3436 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3437 | if (file->f_mode & FMODE_READ) { |
| 3438 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3439 | |
| 3440 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 3441 | if (!ret) { |
| 3442 | struct seq_file *m = file->private_data; |
| 3443 | m->private = iter; |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3444 | } else { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3445 | /* Failed */ |
| 3446 | free_ftrace_hash(iter->hash); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3447 | trace_parser_put(&iter->parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3448 | kfree(iter); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3449 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3450 | } else |
| 3451 | file->private_data = iter; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3452 | |
| 3453 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3454 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3455 | |
| 3456 | return ret; |
| 3457 | } |
| 3458 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3459 | static int |
| 3460 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 3461 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3462 | struct ftrace_ops *ops = inode->i_private; |
| 3463 | |
| 3464 | return ftrace_regex_open(ops, |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3465 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, |
| 3466 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | static int |
| 3470 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 3471 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3472 | struct ftrace_ops *ops = inode->i_private; |
| 3473 | |
| 3474 | return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3475 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3476 | } |
| 3477 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3478 | /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */ |
| 3479 | struct ftrace_glob { |
| 3480 | char *search; |
| 3481 | unsigned len; |
| 3482 | int type; |
| 3483 | }; |
| 3484 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3485 | /* |
| 3486 | * If symbols in an architecture don't correspond exactly to the user-visible |
| 3487 | * name of what they represent, it is possible to define this function to |
| 3488 | * perform the necessary adjustments. |
| 3489 | */ |
| 3490 | char * __weak arch_ftrace_match_adjust(char *str, const char *search) |
| 3491 | { |
| 3492 | return str; |
| 3493 | } |
| 3494 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3495 | static int ftrace_match(char *str, struct ftrace_glob *g) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3496 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3497 | int matched = 0; |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3498 | int slen; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3499 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3500 | str = arch_ftrace_match_adjust(str, g->search); |
| 3501 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3502 | switch (g->type) { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3503 | case MATCH_FULL: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3504 | if (strcmp(str, g->search) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3505 | matched = 1; |
| 3506 | break; |
| 3507 | case MATCH_FRONT_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3508 | if (strncmp(str, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3509 | matched = 1; |
| 3510 | break; |
| 3511 | case MATCH_MIDDLE_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3512 | if (strstr(str, g->search)) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3513 | matched = 1; |
| 3514 | break; |
| 3515 | case MATCH_END_ONLY: |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3516 | slen = strlen(str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3517 | if (slen >= g->len && |
| 3518 | memcmp(str + slen - g->len, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3519 | matched = 1; |
| 3520 | break; |
| 3521 | } |
| 3522 | |
| 3523 | return matched; |
| 3524 | } |
| 3525 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3526 | static int |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3527 | enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter) |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 3528 | { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3529 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3530 | int ret = 0; |
| 3531 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3532 | entry = ftrace_lookup_ip(hash, rec->ip); |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3533 | if (clear_filter) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3534 | /* Do nothing if it doesn't exist */ |
| 3535 | if (!entry) |
| 3536 | return 0; |
| 3537 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3538 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3539 | } else { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3540 | /* Do nothing if it exists */ |
| 3541 | if (entry) |
| 3542 | return 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3543 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3544 | ret = add_hash_entry(hash, rec->ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3545 | } |
| 3546 | return ret; |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 3547 | } |
| 3548 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3549 | static int |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3550 | ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g, |
| 3551 | struct ftrace_glob *mod_g, int exclude_mod) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3552 | { |
| 3553 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3554 | char *modname; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3555 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3556 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); |
| 3557 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3558 | if (mod_g) { |
| 3559 | int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0; |
| 3560 | |
| 3561 | /* blank module name to match all modules */ |
| 3562 | if (!mod_g->len) { |
| 3563 | /* blank module globbing: modname xor exclude_mod */ |
| 3564 | if ((!exclude_mod) != (!modname)) |
| 3565 | goto func_match; |
| 3566 | return 0; |
| 3567 | } |
| 3568 | |
| 3569 | /* not matching the module */ |
| 3570 | if (!modname || !mod_matches) { |
| 3571 | if (exclude_mod) |
| 3572 | goto func_match; |
| 3573 | else |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
| 3577 | if (mod_matches && exclude_mod) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3578 | return 0; |
| 3579 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3580 | func_match: |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3581 | /* blank search means to match all funcs in the mod */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3582 | if (!func_g->len) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3583 | return 1; |
| 3584 | } |
| 3585 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3586 | return ftrace_match(str, func_g); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3587 | } |
| 3588 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3589 | static int |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3590 | match_records(struct ftrace_hash *hash, char *func, int len, char *mod) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3591 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3592 | struct ftrace_page *pg; |
| 3593 | struct dyn_ftrace *rec; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3594 | struct ftrace_glob func_g = { .type = MATCH_FULL }; |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3595 | struct ftrace_glob mod_g = { .type = MATCH_FULL }; |
| 3596 | struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL; |
| 3597 | int exclude_mod = 0; |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3598 | int found = 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3599 | int ret; |
Dan Carpenter | 198bd49 | 2017-07-12 10:35:57 +0300 | [diff] [blame] | 3600 | int clear_filter = 0; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3601 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3602 | if (func) { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3603 | func_g.type = filter_parse_regex(func, len, &func_g.search, |
| 3604 | &clear_filter); |
| 3605 | func_g.len = strlen(func_g.search); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3606 | } |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3607 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3608 | if (mod) { |
| 3609 | mod_g.type = filter_parse_regex(mod, strlen(mod), |
| 3610 | &mod_g.search, &exclude_mod); |
| 3611 | mod_g.len = strlen(mod_g.search); |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3612 | } |
| 3613 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3614 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3615 | |
| 3616 | if (unlikely(ftrace_disabled)) |
| 3617 | goto out_unlock; |
| 3618 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3619 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 3620 | |
| 3621 | if (rec->flags & FTRACE_FL_DISABLED) |
| 3622 | continue; |
| 3623 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3624 | if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3625 | ret = enter_record(hash, rec, clear_filter); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3626 | if (ret < 0) { |
| 3627 | found = ret; |
| 3628 | goto out_unlock; |
| 3629 | } |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3630 | found = 1; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3631 | } |
| 3632 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3633 | out_unlock: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3634 | mutex_unlock(&ftrace_lock); |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3635 | |
| 3636 | return found; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3637 | } |
| 3638 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3639 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3640 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3641 | { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3642 | return match_records(hash, buff, len, NULL); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3643 | } |
| 3644 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3645 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3646 | /* |
| 3647 | * We register the module command as a template to show others how |
| 3648 | * to register the a command as well. |
| 3649 | */ |
| 3650 | |
| 3651 | static int |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 3652 | ftrace_mod_callback(struct ftrace_hash *hash, |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3653 | char *func, char *cmd, char *module, int enable) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3654 | { |
Dmitry Safonov | 5e3949f | 2015-09-29 19:46:12 +0300 | [diff] [blame] | 3655 | int ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3656 | |
| 3657 | /* |
| 3658 | * cmd == 'mod' because we only registered this func |
| 3659 | * for the 'mod' ftrace_func_command. |
| 3660 | * But if you register one func with multiple commands, |
| 3661 | * you can tell which command was used by the cmd |
| 3662 | * parameter. |
| 3663 | */ |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3664 | ret = match_records(hash, func, strlen(func), module); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3665 | if (!ret) |
Dmitry Safonov | 5e3949f | 2015-09-29 19:46:12 +0300 | [diff] [blame] | 3666 | return -EINVAL; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3667 | if (ret < 0) |
| 3668 | return ret; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3669 | return 0; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 3673 | .name = "mod", |
| 3674 | .func = ftrace_mod_callback, |
| 3675 | }; |
| 3676 | |
| 3677 | static int __init ftrace_mod_cmd_init(void) |
| 3678 | { |
| 3679 | return register_ftrace_command(&ftrace_mod_cmd); |
| 3680 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 3681 | core_initcall(ftrace_mod_cmd_init); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3682 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 3683 | 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] | 3684 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3685 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3686 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3687 | struct hlist_head *hhd; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3688 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3689 | |
| 3690 | key = hash_long(ip, FTRACE_HASH_BITS); |
| 3691 | |
| 3692 | hhd = &ftrace_func_hash[key]; |
| 3693 | |
| 3694 | if (hlist_empty(hhd)) |
| 3695 | return; |
| 3696 | |
| 3697 | /* |
| 3698 | * Disable preemption for these calls to prevent a RCU grace |
| 3699 | * period. This syncs the hash iteration and freeing of items |
| 3700 | * on the hash. rcu_read_lock is too dangerous here. |
| 3701 | */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3702 | preempt_disable_notrace(); |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 3703 | hlist_for_each_entry_rcu_notrace(entry, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3704 | if (entry->ip == ip) |
| 3705 | entry->ops->func(ip, parent_ip, &entry->data); |
| 3706 | } |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3707 | preempt_enable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3708 | } |
| 3709 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3710 | static struct ftrace_ops trace_probe_ops __read_mostly = |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3711 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 3712 | .func = function_trace_probe_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3713 | .flags = FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3714 | INIT_OPS_HASH(trace_probe_ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3715 | }; |
| 3716 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3717 | static int ftrace_probe_registered; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3718 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3719 | static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3720 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3721 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3722 | int i; |
| 3723 | |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3724 | if (ftrace_probe_registered) { |
| 3725 | /* still need to update the function call sites */ |
| 3726 | if (ftrace_enabled) |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3727 | ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, |
| 3728 | old_hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3729 | return; |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3730 | } |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3731 | |
| 3732 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3733 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3734 | if (hhd->first) |
| 3735 | break; |
| 3736 | } |
| 3737 | /* Nothing registered? */ |
| 3738 | if (i == FTRACE_FUNC_HASHSIZE) |
| 3739 | return; |
| 3740 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3741 | ret = ftrace_startup(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3742 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3743 | ftrace_probe_registered = 1; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3744 | } |
| 3745 | |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3746 | static bool __disable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3747 | { |
| 3748 | int i; |
| 3749 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3750 | if (!ftrace_probe_registered) |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3751 | return false; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3752 | |
| 3753 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3754 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3755 | if (hhd->first) |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3756 | return false; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | /* no more funcs left */ |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3760 | ftrace_shutdown(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3761 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3762 | ftrace_probe_registered = 0; |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3763 | return true; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3767 | static void ftrace_free_entry(struct ftrace_func_probe *entry) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3768 | { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3769 | if (entry->ops->free) |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3770 | entry->ops->free(entry->ops, entry->ip, &entry->data); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3771 | kfree(entry); |
| 3772 | } |
| 3773 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3774 | int |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3775 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3776 | void *data) |
| 3777 | { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3778 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3779 | struct ftrace_func_probe *entry; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3780 | struct ftrace_glob func_g; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3781 | 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] | 3782 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3783 | struct ftrace_hash *hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3784 | struct ftrace_page *pg; |
| 3785 | struct dyn_ftrace *rec; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3786 | int not; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3787 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3788 | int count = 0; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3789 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3790 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3791 | func_g.type = filter_parse_regex(glob, strlen(glob), |
| 3792 | &func_g.search, ¬); |
| 3793 | func_g.len = strlen(func_g.search); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3794 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3795 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3796 | if (WARN_ON(not)) |
| 3797 | return -EINVAL; |
| 3798 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3799 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3800 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3801 | old_hash_ops.filter_hash = old_hash; |
| 3802 | /* Probes only have filters */ |
| 3803 | old_hash_ops.notrace_hash = NULL; |
| 3804 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3805 | 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] | 3806 | if (!hash) { |
| 3807 | count = -ENOMEM; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3808 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | if (unlikely(ftrace_disabled)) { |
| 3812 | count = -ENODEV; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3813 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3814 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3815 | |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3816 | mutex_lock(&ftrace_lock); |
| 3817 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3818 | do_for_each_ftrace_rec(pg, rec) { |
| 3819 | |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 3820 | if (rec->flags & FTRACE_FL_DISABLED) |
| 3821 | continue; |
| 3822 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3823 | if (!ftrace_match_record(rec, &func_g, NULL, 0)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3824 | continue; |
| 3825 | |
| 3826 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 3827 | if (!entry) { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3828 | /* If we did not process any, then return error */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3829 | if (!count) |
| 3830 | count = -ENOMEM; |
| 3831 | goto out_unlock; |
| 3832 | } |
| 3833 | |
| 3834 | count++; |
| 3835 | |
| 3836 | entry->data = data; |
| 3837 | |
| 3838 | /* |
| 3839 | * The caller might want to do something special |
| 3840 | * for each function we find. We call the callback |
| 3841 | * to give the caller an opportunity to do so. |
| 3842 | */ |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3843 | if (ops->init) { |
| 3844 | if (ops->init(ops, rec->ip, &entry->data) < 0) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3845 | /* caller does not like this func */ |
| 3846 | kfree(entry); |
| 3847 | continue; |
| 3848 | } |
| 3849 | } |
| 3850 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3851 | ret = enter_record(hash, rec, 0); |
| 3852 | if (ret < 0) { |
| 3853 | kfree(entry); |
| 3854 | count = ret; |
| 3855 | goto out_unlock; |
| 3856 | } |
| 3857 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3858 | entry->ops = ops; |
| 3859 | entry->ip = rec->ip; |
| 3860 | |
| 3861 | key = hash_long(entry->ip, FTRACE_HASH_BITS); |
| 3862 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); |
| 3863 | |
| 3864 | } while_for_each_ftrace_rec(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3865 | |
| 3866 | 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] | 3867 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3868 | __enable_ftrace_function_probe(&old_hash_ops); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3869 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3870 | if (!ret) |
| 3871 | free_ftrace_hash_rcu(old_hash); |
| 3872 | else |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3873 | count = ret; |
| 3874 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3875 | out_unlock: |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3876 | mutex_unlock(&ftrace_lock); |
| 3877 | out: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3878 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3879 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3880 | |
| 3881 | return count; |
| 3882 | } |
| 3883 | |
| 3884 | enum { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3885 | PROBE_TEST_FUNC = 1, |
| 3886 | PROBE_TEST_DATA = 2 |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3887 | }; |
| 3888 | |
| 3889 | static void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3890 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3891 | void *data, int flags) |
| 3892 | { |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3893 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3894 | struct ftrace_func_entry *rec_entry; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3895 | struct ftrace_func_probe *entry; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3896 | struct ftrace_func_probe *p; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3897 | struct ftrace_glob func_g; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3898 | 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] | 3899 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3900 | struct list_head free_list; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3901 | struct ftrace_hash *hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3902 | struct hlist_node *tmp; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3903 | char str[KSYM_SYMBOL_LEN]; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3904 | int i, ret; |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3905 | bool disabled; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3906 | |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3907 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3908 | func_g.search = NULL; |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3909 | else if (glob) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3910 | int not; |
| 3911 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3912 | func_g.type = filter_parse_regex(glob, strlen(glob), |
| 3913 | &func_g.search, ¬); |
| 3914 | func_g.len = strlen(func_g.search); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3915 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3916 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3917 | if (WARN_ON(not)) |
| 3918 | return; |
| 3919 | } |
| 3920 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3921 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3922 | |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3923 | old_hash_ops.filter_hash = old_hash; |
| 3924 | /* Probes only have filters */ |
| 3925 | old_hash_ops.notrace_hash = NULL; |
| 3926 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3927 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 3928 | if (!hash) |
| 3929 | /* Hmm, should report this somehow */ |
| 3930 | goto out_unlock; |
| 3931 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3932 | INIT_LIST_HEAD(&free_list); |
| 3933 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3934 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3935 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3936 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3937 | hlist_for_each_entry_safe(entry, tmp, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3938 | |
| 3939 | /* break up if statements for readability */ |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3940 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3941 | continue; |
| 3942 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3943 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3944 | continue; |
| 3945 | |
| 3946 | /* do this last, since it is the most expensive */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3947 | if (func_g.search) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3948 | kallsyms_lookup(entry->ip, NULL, NULL, |
| 3949 | NULL, str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3950 | if (!ftrace_match(str, &func_g)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3951 | continue; |
| 3952 | } |
| 3953 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3954 | rec_entry = ftrace_lookup_ip(hash, entry->ip); |
| 3955 | /* It is possible more than one entry had this ip */ |
| 3956 | if (rec_entry) |
| 3957 | free_hash_entry(hash, rec_entry); |
| 3958 | |
Steven Rostedt (Red Hat) | 740466b | 2013-03-13 11:15:19 -0400 | [diff] [blame] | 3959 | hlist_del_rcu(&entry->node); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3960 | list_add(&entry->free_list, &free_list); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3961 | } |
| 3962 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3963 | mutex_lock(&ftrace_lock); |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3964 | disabled = __disable_ftrace_function_probe(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3965 | /* |
| 3966 | * Remove after the disable is called. Otherwise, if the last |
| 3967 | * probe is removed, a null hash means *all enabled*. |
| 3968 | */ |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3969 | ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3970 | |
| 3971 | /* still need to update the function call sites */ |
| 3972 | if (ftrace_enabled && !disabled) |
| 3973 | ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, |
| 3974 | &old_hash_ops); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3975 | synchronize_sched(); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3976 | if (!ret) |
| 3977 | free_ftrace_hash_rcu(old_hash); |
| 3978 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3979 | list_for_each_entry_safe(entry, p, &free_list, free_list) { |
| 3980 | list_del(&entry->free_list); |
| 3981 | ftrace_free_entry(entry); |
| 3982 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3983 | mutex_unlock(&ftrace_lock); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3984 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3985 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3986 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3987 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3991 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3992 | void *data) |
| 3993 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3994 | __unregister_ftrace_function_probe(glob, ops, data, |
| 3995 | PROBE_TEST_FUNC | PROBE_TEST_DATA); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3999 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4000 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4001 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4002 | } |
| 4003 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4004 | void unregister_ftrace_function_probe_all(char *glob) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4005 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4006 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4007 | } |
| 4008 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4009 | static LIST_HEAD(ftrace_commands); |
| 4010 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 4011 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4012 | /* |
| 4013 | * Currently we only register ftrace commands from __init, so mark this |
| 4014 | * __init too. |
| 4015 | */ |
| 4016 | __init int register_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4017 | { |
| 4018 | struct ftrace_func_command *p; |
| 4019 | int ret = 0; |
| 4020 | |
| 4021 | mutex_lock(&ftrace_cmd_mutex); |
| 4022 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4023 | if (strcmp(cmd->name, p->name) == 0) { |
| 4024 | ret = -EBUSY; |
| 4025 | goto out_unlock; |
| 4026 | } |
| 4027 | } |
| 4028 | list_add(&cmd->list, &ftrace_commands); |
| 4029 | out_unlock: |
| 4030 | mutex_unlock(&ftrace_cmd_mutex); |
| 4031 | |
| 4032 | return ret; |
| 4033 | } |
| 4034 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4035 | /* |
| 4036 | * Currently we only unregister ftrace commands from __init, so mark |
| 4037 | * this __init too. |
| 4038 | */ |
| 4039 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4040 | { |
| 4041 | struct ftrace_func_command *p, *n; |
| 4042 | int ret = -ENODEV; |
| 4043 | |
| 4044 | mutex_lock(&ftrace_cmd_mutex); |
| 4045 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 4046 | if (strcmp(cmd->name, p->name) == 0) { |
| 4047 | ret = 0; |
| 4048 | list_del_init(&p->list); |
| 4049 | goto out_unlock; |
| 4050 | } |
| 4051 | } |
| 4052 | out_unlock: |
| 4053 | mutex_unlock(&ftrace_cmd_mutex); |
| 4054 | |
| 4055 | return ret; |
| 4056 | } |
| 4057 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4058 | static int ftrace_process_regex(struct ftrace_hash *hash, |
| 4059 | char *buff, int len, int enable) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4060 | { |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4061 | char *func, *command, *next = buff; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 4062 | struct ftrace_func_command *p; |
GuoWen Li | 0aff1c0 | 2011-06-01 19:18:47 +0800 | [diff] [blame] | 4063 | int ret = -EINVAL; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4064 | |
| 4065 | func = strsep(&next, ":"); |
| 4066 | |
| 4067 | if (!next) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4068 | ret = ftrace_match_records(hash, func, len); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 4069 | if (!ret) |
| 4070 | ret = -EINVAL; |
| 4071 | if (ret < 0) |
| 4072 | return ret; |
| 4073 | return 0; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4074 | } |
| 4075 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4076 | /* command found */ |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4077 | |
| 4078 | command = strsep(&next, ":"); |
| 4079 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4080 | mutex_lock(&ftrace_cmd_mutex); |
| 4081 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4082 | if (strcmp(p->name, command) == 0) { |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 4083 | ret = p->func(hash, func, command, next, enable); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4084 | goto out_unlock; |
| 4085 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4086 | } |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4087 | out_unlock: |
| 4088 | mutex_unlock(&ftrace_cmd_mutex); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4089 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4090 | return ret; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4091 | } |
| 4092 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4093 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4094 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 4095 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4096 | { |
| 4097 | struct ftrace_iterator *iter; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4098 | struct trace_parser *parser; |
| 4099 | ssize_t ret, read; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4100 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4101 | if (!cnt) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4102 | return 0; |
| 4103 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4104 | if (file->f_mode & FMODE_READ) { |
| 4105 | struct seq_file *m = file->private_data; |
| 4106 | iter = m->private; |
| 4107 | } else |
| 4108 | iter = file->private_data; |
| 4109 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4110 | if (unlikely(ftrace_disabled)) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4111 | return -ENODEV; |
| 4112 | |
| 4113 | /* 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] | 4114 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4115 | parser = &iter->parser; |
| 4116 | read = trace_get_user(parser, ubuf, cnt, ppos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4117 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4118 | if (read >= 0 && trace_parser_loaded(parser) && |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4119 | !trace_parser_cont(parser)) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4120 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4121 | parser->idx, enable); |
Li Zefan | 313254a | 2009-12-08 11:15:30 +0800 | [diff] [blame] | 4122 | trace_parser_clear(parser); |
Steven Rostedt (Red Hat) | 7c088b5 | 2013-05-09 11:35:12 -0400 | [diff] [blame] | 4123 | if (ret < 0) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4124 | goto out; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4125 | } |
| 4126 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4127 | ret = read; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4128 | out: |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4129 | return ret; |
| 4130 | } |
| 4131 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4132 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4133 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 4134 | size_t cnt, loff_t *ppos) |
| 4135 | { |
| 4136 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 4137 | } |
| 4138 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4139 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4140 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 4141 | size_t cnt, loff_t *ppos) |
| 4142 | { |
| 4143 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 4144 | } |
| 4145 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4146 | static int |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4147 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
| 4148 | { |
| 4149 | struct ftrace_func_entry *entry; |
| 4150 | |
| 4151 | if (!ftrace_location(ip)) |
| 4152 | return -EINVAL; |
| 4153 | |
| 4154 | if (remove) { |
| 4155 | entry = ftrace_lookup_ip(hash, ip); |
| 4156 | if (!entry) |
| 4157 | return -ENOENT; |
| 4158 | free_hash_entry(hash, entry); |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
| 4162 | return add_hash_entry(hash, ip); |
| 4163 | } |
| 4164 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4165 | static void ftrace_ops_update_code(struct ftrace_ops *ops, |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4166 | struct ftrace_ops_hash *old_hash) |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4167 | { |
Steven Rostedt (Red Hat) | 8f86f83 | 2015-01-13 11:20:43 -0500 | [diff] [blame] | 4168 | struct ftrace_ops *op; |
| 4169 | |
| 4170 | if (!ftrace_enabled) |
| 4171 | return; |
| 4172 | |
| 4173 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4174 | ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); |
Steven Rostedt (Red Hat) | 8f86f83 | 2015-01-13 11:20:43 -0500 | [diff] [blame] | 4175 | return; |
| 4176 | } |
| 4177 | |
| 4178 | /* |
| 4179 | * If this is the shared global_ops filter, then we need to |
| 4180 | * check if there is another ops that shares it, is enabled. |
| 4181 | * If so, we still need to run the modify code. |
| 4182 | */ |
| 4183 | if (ops->func_hash != &global_ops.local_hash) |
| 4184 | return; |
| 4185 | |
| 4186 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 4187 | if (op->func_hash == &global_ops.local_hash && |
| 4188 | op->flags & FTRACE_OPS_FL_ENABLED) { |
| 4189 | ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash); |
| 4190 | /* Only need to do this once */ |
| 4191 | return; |
| 4192 | } |
| 4193 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4194 | } |
| 4195 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4196 | static int |
| 4197 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4198 | unsigned long ip, int remove, int reset, int enable) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4199 | { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4200 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4201 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4202 | struct ftrace_hash *old_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4203 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4204 | int ret; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4205 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4206 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4207 | return -ENODEV; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4208 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4209 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4210 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4211 | if (enable) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4212 | orig_hash = &ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4213 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4214 | orig_hash = &ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4215 | |
Wang Nan | b972cc5 | 2014-07-15 08:40:20 +0800 | [diff] [blame] | 4216 | if (reset) |
| 4217 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4218 | else |
| 4219 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 4220 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4221 | if (!hash) { |
| 4222 | ret = -ENOMEM; |
| 4223 | goto out_regex_unlock; |
| 4224 | } |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4225 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4226 | if (buf && !ftrace_match_records(hash, buf, len)) { |
| 4227 | ret = -EINVAL; |
| 4228 | goto out_regex_unlock; |
| 4229 | } |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4230 | if (ip) { |
| 4231 | ret = ftrace_match_addr(hash, ip, remove); |
| 4232 | if (ret < 0) |
| 4233 | goto out_regex_unlock; |
| 4234 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4235 | |
| 4236 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4237 | old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4238 | old_hash_ops.filter_hash = ops->func_hash->filter_hash; |
| 4239 | old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4240 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4241 | if (!ret) { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4242 | ftrace_ops_update_code(ops, &old_hash_ops); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4243 | free_ftrace_hash_rcu(old_hash); |
| 4244 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4245 | mutex_unlock(&ftrace_lock); |
| 4246 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4247 | out_regex_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4248 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4249 | |
| 4250 | free_ftrace_hash(hash); |
| 4251 | return ret; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4252 | } |
| 4253 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4254 | static int |
| 4255 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, |
| 4256 | int reset, int enable) |
| 4257 | { |
| 4258 | return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); |
| 4259 | } |
| 4260 | |
| 4261 | /** |
| 4262 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address |
| 4263 | * @ops - the ops to set the filter with |
| 4264 | * @ip - the address to add to or remove from the filter. |
| 4265 | * @remove - non zero to remove the ip from the filter |
| 4266 | * @reset - non zero to reset all filters before applying this filter. |
| 4267 | * |
| 4268 | * Filters denote which functions should be enabled when tracing is enabled |
| 4269 | * If @ip is NULL, it failes to update filter. |
| 4270 | */ |
| 4271 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, |
| 4272 | int remove, int reset) |
| 4273 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4274 | ftrace_ops_init(ops); |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4275 | return ftrace_set_addr(ops, ip, remove, reset, 1); |
| 4276 | } |
| 4277 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); |
| 4278 | |
| 4279 | static int |
| 4280 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4281 | int reset, int enable) |
| 4282 | { |
| 4283 | return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); |
| 4284 | } |
| 4285 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4286 | /** |
| 4287 | * ftrace_set_filter - set a function to filter on in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4288 | * @ops - the ops to set the filter with |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4289 | * @buf - the string that holds the function filter text. |
| 4290 | * @len - the length of the string. |
| 4291 | * @reset - non zero to reset all filters before applying this filter. |
| 4292 | * |
| 4293 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4294 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4295 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4296 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4297 | int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4298 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4299 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4300 | return ftrace_set_regex(ops, buf, len, reset, 1); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4301 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4302 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4303 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4304 | /** |
| 4305 | * ftrace_set_notrace - set a function to not trace in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4306 | * @ops - the ops to set the notrace filter with |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4307 | * @buf - the string that holds the function notrace text. |
| 4308 | * @len - the length of the string. |
| 4309 | * @reset - non zero to reset all filters before applying this filter. |
| 4310 | * |
| 4311 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4312 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4313 | * for tracing. |
| 4314 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4315 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4316 | int len, int reset) |
| 4317 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4318 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4319 | return ftrace_set_regex(ops, buf, len, reset, 0); |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4320 | } |
| 4321 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 4322 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4323 | * 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] | 4324 | * @buf - the string that holds the function filter text. |
| 4325 | * @len - the length of the string. |
| 4326 | * @reset - non zero to reset all filters before applying this filter. |
| 4327 | * |
| 4328 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4329 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4330 | */ |
| 4331 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 4332 | { |
| 4333 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
| 4334 | } |
| 4335 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 4336 | |
| 4337 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4338 | * 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] | 4339 | * @buf - the string that holds the function notrace text. |
| 4340 | * @len - the length of the string. |
| 4341 | * @reset - non zero to reset all filters before applying this filter. |
| 4342 | * |
| 4343 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4344 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4345 | * for tracing. |
| 4346 | */ |
| 4347 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4348 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4349 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4350 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4351 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4352 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4353 | /* |
| 4354 | * command line interface to allow users to set filters on boot up. |
| 4355 | */ |
| 4356 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 4357 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4358 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4359 | |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4360 | /* Used by function selftest to not test if filter is set */ |
| 4361 | bool ftrace_filter_param __initdata; |
| 4362 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4363 | static int __init set_ftrace_notrace(char *str) |
| 4364 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4365 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4366 | strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4367 | return 1; |
| 4368 | } |
| 4369 | __setup("ftrace_notrace=", set_ftrace_notrace); |
| 4370 | |
| 4371 | static int __init set_ftrace_filter(char *str) |
| 4372 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4373 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4374 | strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4375 | return 1; |
| 4376 | } |
| 4377 | __setup("ftrace_filter=", set_ftrace_filter); |
| 4378 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4379 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Lai Jiangshan | f6060f4 | 2009-11-05 11:16:17 +0800 | [diff] [blame] | 4380 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4381 | static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4382 | 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] | 4383 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4384 | static int __init set_graph_function(char *str) |
| 4385 | { |
Frederic Weisbecker | 06f43d6 | 2009-10-14 20:43:39 +0200 | [diff] [blame] | 4386 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4387 | return 1; |
| 4388 | } |
| 4389 | __setup("ftrace_graph_filter=", set_graph_function); |
| 4390 | |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4391 | static int __init set_graph_notrace_function(char *str) |
| 4392 | { |
| 4393 | strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 4394 | return 1; |
| 4395 | } |
| 4396 | __setup("ftrace_graph_notrace=", set_graph_notrace_function); |
| 4397 | |
| 4398 | static void __init set_ftrace_early_graph(char *buf, int enable) |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4399 | { |
| 4400 | int ret; |
| 4401 | char *func; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4402 | unsigned long *table = ftrace_graph_funcs; |
| 4403 | int *count = &ftrace_graph_count; |
| 4404 | |
| 4405 | if (!enable) { |
| 4406 | table = ftrace_graph_notrace_funcs; |
| 4407 | count = &ftrace_graph_notrace_count; |
| 4408 | } |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4409 | |
| 4410 | while (buf) { |
| 4411 | func = strsep(&buf, ","); |
| 4412 | /* we allow only one expression at a time */ |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4413 | ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4414 | if (ret) |
| 4415 | printk(KERN_DEBUG "ftrace: function %s not " |
| 4416 | "traceable\n", func); |
| 4417 | } |
| 4418 | } |
| 4419 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4420 | |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4421 | void __init |
| 4422 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4423 | { |
| 4424 | char *func; |
| 4425 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4426 | ftrace_ops_init(ops); |
| 4427 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4428 | while (buf) { |
| 4429 | func = strsep(&buf, ","); |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4430 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | static void __init set_ftrace_early_filters(void) |
| 4435 | { |
| 4436 | if (ftrace_filter_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4437 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4438 | if (ftrace_notrace_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4439 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4440 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4441 | if (ftrace_graph_buf[0]) |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4442 | set_ftrace_early_graph(ftrace_graph_buf, 1); |
| 4443 | if (ftrace_graph_notrace_buf[0]) |
| 4444 | set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4445 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4446 | } |
| 4447 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4448 | int ftrace_regex_release(struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4449 | { |
| 4450 | struct seq_file *m = (struct seq_file *)file->private_data; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4451 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4452 | struct ftrace_iterator *iter; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4453 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4454 | struct ftrace_hash *old_hash; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4455 | struct trace_parser *parser; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4456 | int filter_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4457 | int ret; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4458 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4459 | if (file->f_mode & FMODE_READ) { |
| 4460 | iter = m->private; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4461 | seq_release(inode, file); |
| 4462 | } else |
| 4463 | iter = file->private_data; |
| 4464 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4465 | parser = &iter->parser; |
| 4466 | if (trace_parser_loaded(parser)) { |
| 4467 | parser->buffer[parser->idx] = 0; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4468 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4469 | } |
| 4470 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4471 | trace_parser_put(parser); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4472 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4473 | mutex_lock(&iter->ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4474 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4475 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4476 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 4477 | |
| 4478 | if (filter_hash) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4479 | orig_hash = &iter->ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4480 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4481 | orig_hash = &iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4482 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4483 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4484 | old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4485 | old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash; |
| 4486 | old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4487 | ret = ftrace_hash_move(iter->ops, filter_hash, |
| 4488 | orig_hash, iter->hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4489 | if (!ret) { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4490 | ftrace_ops_update_code(iter->ops, &old_hash_ops); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4491 | free_ftrace_hash_rcu(old_hash); |
| 4492 | } |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4493 | mutex_unlock(&ftrace_lock); |
| 4494 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4495 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4496 | mutex_unlock(&iter->ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4497 | free_ftrace_hash(iter->hash); |
| 4498 | kfree(iter); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4499 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4500 | return 0; |
| 4501 | } |
| 4502 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4503 | static const struct file_operations ftrace_avail_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4504 | .open = ftrace_avail_open, |
| 4505 | .read = seq_read, |
| 4506 | .llseek = seq_lseek, |
Li Zefan | 3be04b4 | 2009-08-17 16:54:03 +0800 | [diff] [blame] | 4507 | .release = seq_release_private, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4508 | }; |
| 4509 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4510 | static const struct file_operations ftrace_enabled_fops = { |
| 4511 | .open = ftrace_enabled_open, |
| 4512 | .read = seq_read, |
| 4513 | .llseek = seq_lseek, |
| 4514 | .release = seq_release_private, |
| 4515 | }; |
| 4516 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4517 | static const struct file_operations ftrace_filter_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4518 | .open = ftrace_filter_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4519 | .read = seq_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4520 | .write = ftrace_filter_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4521 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4522 | .release = ftrace_regex_release, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4523 | }; |
| 4524 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4525 | static const struct file_operations ftrace_notrace_fops = { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4526 | .open = ftrace_notrace_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4527 | .read = seq_read, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4528 | .write = ftrace_notrace_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4529 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4530 | .release = ftrace_regex_release, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4531 | }; |
| 4532 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4533 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4534 | |
| 4535 | static DEFINE_MUTEX(graph_lock); |
| 4536 | |
| 4537 | int ftrace_graph_count; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4538 | int ftrace_graph_notrace_count; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4539 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4540 | unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4541 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4542 | struct ftrace_graph_data { |
| 4543 | unsigned long *table; |
| 4544 | size_t size; |
| 4545 | int *count; |
| 4546 | const struct seq_operations *seq_ops; |
| 4547 | }; |
| 4548 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4549 | static void * |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4550 | __g_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4551 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4552 | struct ftrace_graph_data *fgd = m->private; |
| 4553 | |
| 4554 | if (*pos >= *fgd->count) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4555 | return NULL; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4556 | return &fgd->table[*pos]; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4557 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4558 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4559 | static void * |
| 4560 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 4561 | { |
| 4562 | (*pos)++; |
| 4563 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 4567 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4568 | struct ftrace_graph_data *fgd = m->private; |
| 4569 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4570 | mutex_lock(&graph_lock); |
| 4571 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4572 | /* Nothing, tell g_show to print all functions are enabled */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4573 | if (!*fgd->count && !*pos) |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4574 | return (void *)1; |
| 4575 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4576 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | static void g_stop(struct seq_file *m, void *p) |
| 4580 | { |
| 4581 | mutex_unlock(&graph_lock); |
| 4582 | } |
| 4583 | |
| 4584 | static int g_show(struct seq_file *m, void *v) |
| 4585 | { |
| 4586 | unsigned long *ptr = v; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4587 | |
| 4588 | if (!ptr) |
| 4589 | return 0; |
| 4590 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4591 | if (ptr == (unsigned long *)1) { |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4592 | struct ftrace_graph_data *fgd = m->private; |
| 4593 | |
| 4594 | if (fgd->table == ftrace_graph_funcs) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4595 | seq_puts(m, "#### all functions enabled ####\n"); |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4596 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4597 | seq_puts(m, "#### no functions disabled ####\n"); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4598 | return 0; |
| 4599 | } |
| 4600 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 4601 | seq_printf(m, "%ps\n", (void *)*ptr); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4602 | |
| 4603 | return 0; |
| 4604 | } |
| 4605 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 4606 | static const struct seq_operations ftrace_graph_seq_ops = { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4607 | .start = g_start, |
| 4608 | .next = g_next, |
| 4609 | .stop = g_stop, |
| 4610 | .show = g_show, |
| 4611 | }; |
| 4612 | |
| 4613 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4614 | __ftrace_graph_open(struct inode *inode, struct file *file, |
| 4615 | struct ftrace_graph_data *fgd) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4616 | { |
| 4617 | int ret = 0; |
| 4618 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4619 | mutex_lock(&graph_lock); |
| 4620 | if ((file->f_mode & FMODE_WRITE) && |
Steven Rostedt | 8650ae3 | 2009-07-22 23:29:30 -0400 | [diff] [blame] | 4621 | (file->f_flags & O_TRUNC)) { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4622 | *fgd->count = 0; |
| 4623 | memset(fgd->table, 0, fgd->size * sizeof(*fgd->table)); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4624 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4625 | mutex_unlock(&graph_lock); |
| 4626 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4627 | if (file->f_mode & FMODE_READ) { |
| 4628 | ret = seq_open(file, fgd->seq_ops); |
| 4629 | if (!ret) { |
| 4630 | struct seq_file *m = file->private_data; |
| 4631 | m->private = fgd; |
| 4632 | } |
| 4633 | } else |
| 4634 | file->private_data = fgd; |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 4635 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4636 | return ret; |
| 4637 | } |
| 4638 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4639 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4640 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 4641 | { |
| 4642 | struct ftrace_graph_data *fgd; |
| 4643 | |
| 4644 | if (unlikely(ftrace_disabled)) |
| 4645 | return -ENODEV; |
| 4646 | |
| 4647 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4648 | if (fgd == NULL) |
| 4649 | return -ENOMEM; |
| 4650 | |
| 4651 | fgd->table = ftrace_graph_funcs; |
| 4652 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 4653 | fgd->count = &ftrace_graph_count; |
| 4654 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4655 | |
| 4656 | return __ftrace_graph_open(inode, file, fgd); |
| 4657 | } |
| 4658 | |
| 4659 | static int |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4660 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) |
| 4661 | { |
| 4662 | struct ftrace_graph_data *fgd; |
| 4663 | |
| 4664 | if (unlikely(ftrace_disabled)) |
| 4665 | return -ENODEV; |
| 4666 | |
| 4667 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4668 | if (fgd == NULL) |
| 4669 | return -ENOMEM; |
| 4670 | |
| 4671 | fgd->table = ftrace_graph_notrace_funcs; |
| 4672 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 4673 | fgd->count = &ftrace_graph_notrace_count; |
| 4674 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4675 | |
| 4676 | return __ftrace_graph_open(inode, file, fgd); |
| 4677 | } |
| 4678 | |
| 4679 | static int |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4680 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 4681 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4682 | if (file->f_mode & FMODE_READ) { |
| 4683 | struct seq_file *m = file->private_data; |
| 4684 | |
| 4685 | kfree(m->private); |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4686 | seq_release(inode, file); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4687 | } else { |
| 4688 | kfree(file->private_data); |
| 4689 | } |
| 4690 | |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4691 | return 0; |
| 4692 | } |
| 4693 | |
| 4694 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4695 | ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4696 | { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4697 | struct ftrace_glob func_g; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4698 | struct dyn_ftrace *rec; |
| 4699 | struct ftrace_page *pg; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4700 | int fail = 1; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4701 | int not; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4702 | bool exists; |
| 4703 | int i; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4704 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4705 | /* decode regex */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4706 | func_g.type = filter_parse_regex(buffer, strlen(buffer), |
| 4707 | &func_g.search, ¬); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4708 | if (!not && *idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4709 | return -EBUSY; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4710 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4711 | func_g.len = strlen(func_g.search); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4712 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4713 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4714 | |
| 4715 | if (unlikely(ftrace_disabled)) { |
| 4716 | mutex_unlock(&ftrace_lock); |
| 4717 | return -ENODEV; |
| 4718 | } |
| 4719 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4720 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4721 | |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 4722 | if (rec->flags & FTRACE_FL_DISABLED) |
| 4723 | continue; |
| 4724 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 4725 | if (ftrace_match_record(rec, &func_g, NULL, 0)) { |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4726 | /* if it is in the array */ |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4727 | exists = false; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4728 | for (i = 0; i < *idx; i++) { |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4729 | if (array[i] == rec->ip) { |
| 4730 | exists = true; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4731 | break; |
| 4732 | } |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4733 | } |
| 4734 | |
| 4735 | if (!not) { |
| 4736 | fail = 0; |
| 4737 | if (!exists) { |
| 4738 | array[(*idx)++] = rec->ip; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4739 | if (*idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4740 | goto out; |
| 4741 | } |
| 4742 | } else { |
| 4743 | if (exists) { |
| 4744 | array[i] = array[--(*idx)]; |
| 4745 | array[*idx] = 0; |
| 4746 | fail = 0; |
| 4747 | } |
| 4748 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4749 | } |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4750 | } while_for_each_ftrace_rec(); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4751 | out: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4752 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4753 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4754 | if (fail) |
| 4755 | return -EINVAL; |
| 4756 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4757 | return 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | static ssize_t |
| 4761 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 4762 | size_t cnt, loff_t *ppos) |
| 4763 | { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4764 | struct trace_parser parser; |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4765 | ssize_t read, ret = 0; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4766 | struct ftrace_graph_data *fgd = file->private_data; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4767 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4768 | if (!cnt) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4769 | return 0; |
| 4770 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4771 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) |
| 4772 | return -ENOMEM; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4773 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4774 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
| 4775 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4776 | if (read >= 0 && trace_parser_loaded((&parser))) { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4777 | parser.buffer[parser.idx] = 0; |
| 4778 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4779 | mutex_lock(&graph_lock); |
| 4780 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4781 | /* we allow only one expression at a time */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4782 | ret = ftrace_set_func(fgd->table, fgd->count, fgd->size, |
| 4783 | parser.buffer); |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4784 | |
| 4785 | mutex_unlock(&graph_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4786 | } |
| 4787 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4788 | if (!ret) |
| 4789 | ret = read; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 4790 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4791 | trace_parser_put(&parser); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4792 | |
| 4793 | return ret; |
| 4794 | } |
| 4795 | |
| 4796 | static const struct file_operations ftrace_graph_fops = { |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4797 | .open = ftrace_graph_open, |
| 4798 | .read = seq_read, |
| 4799 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4800 | .llseek = tracing_lseek, |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4801 | .release = ftrace_graph_release, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4802 | }; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4803 | |
| 4804 | static const struct file_operations ftrace_graph_notrace_fops = { |
| 4805 | .open = ftrace_graph_notrace_open, |
| 4806 | .read = seq_read, |
| 4807 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4808 | .llseek = tracing_lseek, |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4809 | .release = ftrace_graph_release, |
| 4810 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4811 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4812 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4813 | void ftrace_create_filter_files(struct ftrace_ops *ops, |
| 4814 | struct dentry *parent) |
| 4815 | { |
| 4816 | |
| 4817 | trace_create_file("set_ftrace_filter", 0644, parent, |
| 4818 | ops, &ftrace_filter_fops); |
| 4819 | |
| 4820 | trace_create_file("set_ftrace_notrace", 0644, parent, |
| 4821 | ops, &ftrace_notrace_fops); |
| 4822 | } |
| 4823 | |
| 4824 | /* |
| 4825 | * The name "destroy_filter_files" is really a misnomer. Although |
| 4826 | * in the future, it may actualy delete the files, but this is |
| 4827 | * really intended to make sure the ops passed in are disabled |
| 4828 | * and that when this function returns, the caller is free to |
| 4829 | * free the ops. |
| 4830 | * |
| 4831 | * The "destroy" name is only to match the "create" name that this |
| 4832 | * should be paired with. |
| 4833 | */ |
| 4834 | void ftrace_destroy_filter_files(struct ftrace_ops *ops) |
| 4835 | { |
| 4836 | mutex_lock(&ftrace_lock); |
| 4837 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 4838 | ftrace_shutdown(ops, 0); |
| 4839 | ops->flags |= FTRACE_OPS_FL_DELETED; |
Steven Rostedt (VMware) | 326c9e1 | 2018-12-10 23:58:01 -0500 | [diff] [blame] | 4840 | ftrace_free_filter(ops); |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4841 | mutex_unlock(&ftrace_lock); |
| 4842 | } |
| 4843 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 4844 | static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4845 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4846 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4847 | trace_create_file("available_filter_functions", 0444, |
| 4848 | d_tracer, NULL, &ftrace_avail_fops); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4849 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4850 | trace_create_file("enabled_functions", 0444, |
| 4851 | d_tracer, NULL, &ftrace_enabled_fops); |
| 4852 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4853 | ftrace_create_filter_files(&global_ops, d_tracer); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 4854 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4855 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4856 | trace_create_file("set_graph_function", 0444, d_tracer, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4857 | NULL, |
| 4858 | &ftrace_graph_fops); |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4859 | trace_create_file("set_graph_notrace", 0444, d_tracer, |
| 4860 | NULL, |
| 4861 | &ftrace_graph_notrace_fops); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4862 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4863 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4864 | return 0; |
| 4865 | } |
| 4866 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4867 | static int ftrace_cmp_ips(const void *a, const void *b) |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4868 | { |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4869 | const unsigned long *ipa = a; |
| 4870 | const unsigned long *ipb = b; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4871 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4872 | if (*ipa > *ipb) |
| 4873 | return 1; |
| 4874 | if (*ipa < *ipb) |
| 4875 | return -1; |
| 4876 | return 0; |
| 4877 | } |
| 4878 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 4879 | static int ftrace_process_locs(struct module *mod, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4880 | unsigned long *start, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4881 | unsigned long *end) |
| 4882 | { |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4883 | struct ftrace_page *start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4884 | struct ftrace_page *pg; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4885 | struct dyn_ftrace *rec; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4886 | unsigned long count; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4887 | unsigned long *p; |
| 4888 | unsigned long addr; |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4889 | unsigned long flags = 0; /* Shut up gcc */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4890 | int ret = -ENOMEM; |
| 4891 | |
| 4892 | count = end - start; |
| 4893 | |
| 4894 | if (!count) |
| 4895 | return 0; |
| 4896 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4897 | sort(start, count, sizeof(*start), |
Rasmus Villemoes | 6db0290 | 2015-09-09 23:27:02 +0200 | [diff] [blame] | 4898 | ftrace_cmp_ips, NULL); |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4899 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4900 | start_pg = ftrace_allocate_pages(count); |
| 4901 | if (!start_pg) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4902 | return -ENOMEM; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4903 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4904 | mutex_lock(&ftrace_lock); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4905 | |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4906 | /* |
| 4907 | * Core and each module needs their own pages, as |
| 4908 | * modules will free them when they are removed. |
| 4909 | * Force a new page to be allocated for modules. |
| 4910 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4911 | if (!mod) { |
| 4912 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 4913 | /* First initialization */ |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4914 | ftrace_pages = ftrace_pages_start = start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4915 | } else { |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4916 | if (!ftrace_pages) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4917 | goto out; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4918 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4919 | if (WARN_ON(ftrace_pages->next)) { |
| 4920 | /* Hmm, we have free pages? */ |
| 4921 | while (ftrace_pages->next) |
| 4922 | ftrace_pages = ftrace_pages->next; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4923 | } |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4924 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4925 | ftrace_pages->next = start_pg; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4926 | } |
| 4927 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4928 | p = start; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4929 | pg = start_pg; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4930 | while (p < end) { |
| 4931 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4932 | /* |
| 4933 | * Some architecture linkers will pad between |
| 4934 | * the different mcount_loc sections of different |
| 4935 | * object files to satisfy alignments. |
| 4936 | * Skip any NULL pointers. |
| 4937 | */ |
| 4938 | if (!addr) |
| 4939 | continue; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4940 | |
| 4941 | if (pg->index == pg->size) { |
| 4942 | /* We should have allocated enough */ |
| 4943 | if (WARN_ON(!pg->next)) |
| 4944 | break; |
| 4945 | pg = pg->next; |
| 4946 | } |
| 4947 | |
| 4948 | rec = &pg->records[pg->index++]; |
| 4949 | rec->ip = addr; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4950 | } |
| 4951 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4952 | /* We should have used all pages */ |
| 4953 | WARN_ON(pg->next); |
| 4954 | |
| 4955 | /* Assign the last page to ftrace_pages */ |
| 4956 | ftrace_pages = pg; |
| 4957 | |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4958 | /* |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4959 | * We only need to disable interrupts on start up |
| 4960 | * because we are modifying code that an interrupt |
| 4961 | * may execute, and the modification is not atomic. |
| 4962 | * But for modules, nothing runs the code we modify |
| 4963 | * until we are finished with it, and there's no |
| 4964 | * reason to cause large interrupt latencies while we do it. |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4965 | */ |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4966 | if (!mod) |
| 4967 | local_irq_save(flags); |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 4968 | ftrace_update_code(mod, start_pg); |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4969 | if (!mod) |
| 4970 | local_irq_restore(flags); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4971 | ret = 0; |
| 4972 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4973 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4974 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4975 | return ret; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4976 | } |
| 4977 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4978 | #ifdef CONFIG_MODULES |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4979 | |
| 4980 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 4981 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 4982 | static int referenced_filters(struct dyn_ftrace *rec) |
| 4983 | { |
| 4984 | struct ftrace_ops *ops; |
| 4985 | int cnt = 0; |
| 4986 | |
| 4987 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { |
| 4988 | if (ops_references_rec(ops, rec)) |
| 4989 | cnt++; |
| 4990 | } |
| 4991 | |
| 4992 | return cnt; |
| 4993 | } |
| 4994 | |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4995 | void ftrace_release_mod(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4996 | { |
| 4997 | struct dyn_ftrace *rec; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4998 | struct ftrace_page **last_pg; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4999 | struct ftrace_page *pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5000 | int order; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5001 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5002 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5003 | |
| 5004 | if (ftrace_disabled) |
| 5005 | goto out_unlock; |
| 5006 | |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5007 | /* |
| 5008 | * Each module has its own ftrace_pages, remove |
| 5009 | * them from the list. |
| 5010 | */ |
| 5011 | last_pg = &ftrace_pages_start; |
| 5012 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 5013 | rec = &pg->records[0]; |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 5014 | if (within_module_core(rec->ip, mod)) { |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5015 | /* |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5016 | * As core pages are first, the first |
| 5017 | * page should never be a module page. |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5018 | */ |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5019 | if (WARN_ON(pg == ftrace_pages_start)) |
| 5020 | goto out_unlock; |
| 5021 | |
| 5022 | /* Check if we are deleting the last page */ |
| 5023 | if (pg == ftrace_pages) |
| 5024 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 5025 | |
| 5026 | *last_pg = pg->next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5027 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 5028 | free_pages((unsigned long)pg->records, order); |
| 5029 | kfree(pg); |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5030 | } else |
| 5031 | last_pg = &pg->next; |
| 5032 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5033 | out_unlock: |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5034 | mutex_unlock(&ftrace_lock); |
| 5035 | } |
| 5036 | |
Jessica Yu | 7dcd182 | 2016-02-16 17:32:33 -0500 | [diff] [blame] | 5037 | void ftrace_module_enable(struct module *mod) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5038 | { |
| 5039 | struct dyn_ftrace *rec; |
| 5040 | struct ftrace_page *pg; |
| 5041 | |
| 5042 | mutex_lock(&ftrace_lock); |
| 5043 | |
| 5044 | if (ftrace_disabled) |
| 5045 | goto out_unlock; |
| 5046 | |
| 5047 | /* |
| 5048 | * If the tracing is enabled, go ahead and enable the record. |
| 5049 | * |
| 5050 | * The reason not to enable the record immediatelly is the |
| 5051 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 5052 | * correct previous instructions. Making first the NOP |
| 5053 | * conversion puts the module to the correct state, thus |
| 5054 | * passing the ftrace_make_call check. |
| 5055 | * |
| 5056 | * We also delay this to after the module code already set the |
| 5057 | * text to read-only, as we now need to set it back to read-write |
| 5058 | * so that we can modify the text. |
| 5059 | */ |
| 5060 | if (ftrace_start_up) |
| 5061 | ftrace_arch_code_modify_prepare(); |
| 5062 | |
| 5063 | do_for_each_ftrace_rec(pg, rec) { |
| 5064 | int cnt; |
| 5065 | /* |
| 5066 | * do_for_each_ftrace_rec() is a double loop. |
| 5067 | * module text shares the pg. If a record is |
| 5068 | * not part of this module, then skip this pg, |
| 5069 | * which the "break" will do. |
| 5070 | */ |
| 5071 | if (!within_module_core(rec->ip, mod)) |
| 5072 | break; |
| 5073 | |
| 5074 | cnt = 0; |
| 5075 | |
| 5076 | /* |
| 5077 | * When adding a module, we need to check if tracers are |
| 5078 | * currently enabled and if they are, and can trace this record, |
| 5079 | * we need to enable the module functions as well as update the |
| 5080 | * reference counts for those function records. |
| 5081 | */ |
| 5082 | if (ftrace_start_up) |
| 5083 | cnt += referenced_filters(rec); |
| 5084 | |
| 5085 | /* This clears FTRACE_FL_DISABLED */ |
| 5086 | rec->flags = cnt; |
| 5087 | |
| 5088 | if (ftrace_start_up && cnt) { |
| 5089 | int failed = __ftrace_replace_code(rec, 1); |
| 5090 | if (failed) { |
| 5091 | ftrace_bug(failed, rec); |
| 5092 | goto out_loop; |
| 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | } while_for_each_ftrace_rec(); |
| 5097 | |
| 5098 | out_loop: |
| 5099 | if (ftrace_start_up) |
| 5100 | ftrace_arch_code_modify_post_process(); |
| 5101 | |
| 5102 | out_unlock: |
| 5103 | mutex_unlock(&ftrace_lock); |
| 5104 | } |
| 5105 | |
Steven Rostedt (Red Hat) | a949ae5 | 2014-04-24 10:40:12 -0400 | [diff] [blame] | 5106 | void ftrace_module_init(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5107 | { |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5108 | if (ftrace_disabled || !mod->num_ftrace_callsites) |
Abel Vesa | b6b71f6 | 2015-12-02 15:39:57 +0100 | [diff] [blame] | 5109 | return; |
| 5110 | |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5111 | ftrace_process_locs(mod, mod->ftrace_callsites, |
| 5112 | mod->ftrace_callsites + mod->num_ftrace_callsites); |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 5113 | } |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5114 | #endif /* CONFIG_MODULES */ |
| 5115 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5116 | void __init ftrace_init(void) |
| 5117 | { |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 5118 | extern unsigned long __start_mcount_loc[]; |
| 5119 | extern unsigned long __stop_mcount_loc[]; |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 5120 | unsigned long count, flags; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5121 | int ret; |
| 5122 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5123 | local_irq_save(flags); |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 5124 | ret = ftrace_dyn_arch_init(); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5125 | local_irq_restore(flags); |
Jiri Slaby | af64a7c | 2014-02-24 19:59:58 +0100 | [diff] [blame] | 5126 | if (ret) |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5127 | goto failed; |
| 5128 | |
| 5129 | count = __stop_mcount_loc - __start_mcount_loc; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 5130 | if (!count) { |
| 5131 | pr_info("ftrace: No functions to be traced?\n"); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5132 | goto failed; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | pr_info("ftrace: allocating %ld entries in %ld pages\n", |
| 5136 | count, count / ENTRIES_PER_PAGE + 1); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5137 | |
| 5138 | last_ftrace_enabled = ftrace_enabled = 1; |
| 5139 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 5140 | ret = ftrace_process_locs(NULL, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 5141 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5142 | __stop_mcount_loc); |
| 5143 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 5144 | set_ftrace_early_filters(); |
| 5145 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5146 | return; |
| 5147 | failed: |
| 5148 | ftrace_disabled = 1; |
| 5149 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5150 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5151 | /* Do nothing if arch does not support this */ |
| 5152 | void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5153 | { |
| 5154 | } |
| 5155 | |
| 5156 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5157 | { |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 5158 | |
| 5159 | /* |
| 5160 | * Currently there's no safe way to free a trampoline when the kernel |
| 5161 | * is configured with PREEMPT. That is because a task could be preempted |
| 5162 | * when it jumped to the trampoline, it may be preempted for a long time |
| 5163 | * depending on the system load, and currently there's no way to know |
| 5164 | * when it will be off the trampoline. If the trampoline is freed |
| 5165 | * too early, when the task runs again, it will be executing on freed |
| 5166 | * memory and crash. |
| 5167 | */ |
| 5168 | #ifdef CONFIG_PREEMPT |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5169 | /* Currently, only non dynamic ops can have a trampoline */ |
| 5170 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) |
| 5171 | return; |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 5172 | #endif |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5173 | |
| 5174 | arch_ftrace_update_trampoline(ops); |
| 5175 | } |
| 5176 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5177 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5178 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 5179 | static struct ftrace_ops global_ops = { |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 5180 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 5181 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 5182 | FTRACE_OPS_FL_INITIALIZED | |
| 5183 | FTRACE_OPS_FL_PID, |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 5184 | }; |
| 5185 | |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5186 | static int __init ftrace_nodyn_init(void) |
| 5187 | { |
| 5188 | ftrace_enabled = 1; |
| 5189 | return 0; |
| 5190 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 5191 | core_initcall(ftrace_nodyn_init); |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5192 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 5193 | static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5194 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5195 | static inline void ftrace_startup_all(int command) { } |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 5196 | /* 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] | 5197 | # define ftrace_startup(ops, command) \ |
| 5198 | ({ \ |
| 5199 | int ___ret = __register_ftrace_function(ops); \ |
| 5200 | if (!___ret) \ |
| 5201 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ |
| 5202 | ___ret; \ |
Steven Rostedt | 3b6cfdb | 2011-05-23 15:33:49 -0400 | [diff] [blame] | 5203 | }) |
Steven Rostedt (Red Hat) | 1fcc155 | 2014-02-19 15:12:18 -0500 | [diff] [blame] | 5204 | # define ftrace_shutdown(ops, command) \ |
| 5205 | ({ \ |
| 5206 | int ___ret = __unregister_ftrace_function(ops); \ |
| 5207 | if (!___ret) \ |
| 5208 | (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \ |
| 5209 | ___ret; \ |
| 5210 | }) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5211 | |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 5212 | # define ftrace_startup_sysctl() do { } while (0) |
| 5213 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5214 | |
| 5215 | static inline int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 5216 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5217 | { |
| 5218 | return 1; |
| 5219 | } |
| 5220 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5221 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5222 | { |
| 5223 | } |
| 5224 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5225 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 5226 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5227 | __init void ftrace_init_global_array_ops(struct trace_array *tr) |
| 5228 | { |
| 5229 | tr->ops = &global_ops; |
| 5230 | tr->ops->private = tr; |
| 5231 | } |
| 5232 | |
| 5233 | void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) |
| 5234 | { |
| 5235 | /* If we filter on pids, update to use the pid function */ |
| 5236 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { |
| 5237 | if (WARN_ON(tr->ops->func != ftrace_stub)) |
| 5238 | printk("ftrace ops had %pS for function\n", |
| 5239 | tr->ops->func); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5240 | } |
| 5241 | tr->ops->func = func; |
| 5242 | tr->ops->private = tr; |
| 5243 | } |
| 5244 | |
| 5245 | void ftrace_reset_array_ops(struct trace_array *tr) |
| 5246 | { |
| 5247 | tr->ops->func = ftrace_stub; |
| 5248 | } |
| 5249 | |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5250 | static nokprobe_inline void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5251 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5252 | struct ftrace_ops *ignored, struct pt_regs *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5253 | { |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5254 | struct ftrace_ops *op; |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5255 | int bit; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5256 | |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5257 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5258 | if (bit < 0) |
| 5259 | return; |
Steven Rostedt | c29f122 | 2012-11-02 17:17:59 -0400 | [diff] [blame] | 5260 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5261 | /* |
| 5262 | * Some of the ops may be dynamically allocated, |
| 5263 | * they must be freed after a synchronize_sched(). |
| 5264 | */ |
| 5265 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 5266 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5267 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 5268 | /* |
| 5269 | * Check the following for each ops before calling their func: |
| 5270 | * if RCU flag is set, then rcu_is_watching() must be true |
| 5271 | * if PER_CPU is set, then ftrace_function_local_disable() |
| 5272 | * must be false |
| 5273 | * Otherwise test if the ip matches the ops filter |
| 5274 | * |
| 5275 | * If any of the above fails then the op->func() is not executed. |
| 5276 | */ |
| 5277 | if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) && |
| 5278 | (!(op->flags & FTRACE_OPS_FL_PER_CPU) || |
| 5279 | !ftrace_function_local_disabled(op)) && |
| 5280 | ftrace_ops_test(op, ip, regs)) { |
| 5281 | |
Steven Rostedt (Red Hat) | 1d48d59 | 2014-06-25 11:54:03 -0400 | [diff] [blame] | 5282 | if (FTRACE_WARN_ON(!op->func)) { |
| 5283 | pr_warn("op=%p %pS\n", op, op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5284 | goto out; |
| 5285 | } |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5286 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5287 | } |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5288 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5289 | out: |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5290 | preempt_enable_notrace(); |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5291 | trace_clear_recursion(bit); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5292 | } |
| 5293 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5294 | /* |
| 5295 | * Some archs only support passing ip and parent_ip. Even though |
| 5296 | * the list function ignores the op parameter, we do not want any |
| 5297 | * C side effects, where a function is called without the caller |
| 5298 | * sending a third parameter. |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5299 | * Archs are to support both the regs and ftrace_ops at the same time. |
| 5300 | * If they support ftrace_ops, it is assumed they support regs. |
| 5301 | * 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] | 5302 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
| 5303 | * 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] | 5304 | * An architecture can pass partial regs with ftrace_ops and still |
Li Bin | b8ec330 | 2015-11-30 18:23:36 +0800 | [diff] [blame] | 5305 | * set the ARCH_SUPPORTS_FTRACE_OPS. |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5306 | */ |
| 5307 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 5308 | 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] | 5309 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5310 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5311 | __ftrace_ops_list_func(ip, parent_ip, NULL, regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5312 | } |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5313 | NOKPROBE_SYMBOL(ftrace_ops_list_func); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5314 | #else |
| 5315 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip) |
| 5316 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5317 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5318 | } |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5319 | NOKPROBE_SYMBOL(ftrace_ops_no_ops); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5320 | #endif |
| 5321 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5322 | /* |
| 5323 | * If there's only one function registered but it does not support |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5324 | * recursion, needs RCU protection and/or requires per cpu handling, then |
| 5325 | * this function will be called by the mcount trampoline. |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5326 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5327 | static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5328 | struct ftrace_ops *op, struct pt_regs *regs) |
| 5329 | { |
| 5330 | int bit; |
| 5331 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5332 | if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching()) |
| 5333 | return; |
| 5334 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5335 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5336 | if (bit < 0) |
| 5337 | return; |
| 5338 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5339 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5340 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5341 | if (!(op->flags & FTRACE_OPS_FL_PER_CPU) || |
| 5342 | !ftrace_function_local_disabled(op)) { |
| 5343 | op->func(ip, parent_ip, op, regs); |
| 5344 | } |
| 5345 | |
| 5346 | preempt_enable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5347 | trace_clear_recursion(bit); |
| 5348 | } |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5349 | NOKPROBE_SYMBOL(ftrace_ops_assist_func); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5350 | |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5351 | /** |
| 5352 | * ftrace_ops_get_func - get the function a trampoline should call |
| 5353 | * @ops: the ops to get the function for |
| 5354 | * |
| 5355 | * Normally the mcount trampoline will call the ops->func, but there |
| 5356 | * are times that it should not. For example, if the ops does not |
| 5357 | * have its own recursion protection, then it should call the |
| 5358 | * ftrace_ops_recurs_func() instead. |
| 5359 | * |
| 5360 | * Returns the function that the trampoline should call for @ops. |
| 5361 | */ |
| 5362 | ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) |
| 5363 | { |
| 5364 | /* |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5365 | * If the function does not handle recursion, needs to be RCU safe, |
| 5366 | * or does per cpu logic, then we need to call the assist handler. |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5367 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5368 | if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) || |
| 5369 | ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU)) |
| 5370 | return ftrace_ops_assist_func; |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5371 | |
| 5372 | return ops->func; |
| 5373 | } |
| 5374 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5375 | static void |
| 5376 | ftrace_filter_pid_sched_switch_probe(void *data, bool preempt, |
| 5377 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5378 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5379 | struct trace_array *tr = data; |
| 5380 | struct trace_pid_list *pid_list; |
| 5381 | |
| 5382 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 5383 | |
| 5384 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 5385 | trace_ignore_this_task(pid_list, next)); |
| 5386 | } |
| 5387 | |
| 5388 | static void clear_ftrace_pids(struct trace_array *tr) |
| 5389 | { |
| 5390 | struct trace_pid_list *pid_list; |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5391 | int cpu; |
| 5392 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5393 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 5394 | lockdep_is_held(&ftrace_lock)); |
| 5395 | if (!pid_list) |
| 5396 | return; |
| 5397 | |
| 5398 | unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 5399 | |
| 5400 | for_each_possible_cpu(cpu) |
| 5401 | per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false; |
| 5402 | |
| 5403 | rcu_assign_pointer(tr->function_pids, NULL); |
| 5404 | |
| 5405 | /* Wait till all users are no longer using pid filtering */ |
| 5406 | synchronize_sched(); |
| 5407 | |
| 5408 | trace_free_pid_list(pid_list); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5409 | } |
| 5410 | |
Namhyung Kim | 7da0f8e | 2017-04-17 11:44:27 +0900 | [diff] [blame] | 5411 | void ftrace_clear_pids(struct trace_array *tr) |
| 5412 | { |
| 5413 | mutex_lock(&ftrace_lock); |
| 5414 | |
| 5415 | clear_ftrace_pids(tr); |
| 5416 | |
| 5417 | mutex_unlock(&ftrace_lock); |
| 5418 | } |
| 5419 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5420 | static void ftrace_pid_reset(struct trace_array *tr) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5421 | { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5422 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5423 | clear_ftrace_pids(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5424 | |
| 5425 | ftrace_update_pid_func(); |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5426 | ftrace_startup_all(0); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5427 | |
| 5428 | mutex_unlock(&ftrace_lock); |
| 5429 | } |
| 5430 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5431 | /* Greater than any max PID */ |
| 5432 | #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1) |
| 5433 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5434 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5435 | __acquires(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5436 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5437 | struct trace_pid_list *pid_list; |
| 5438 | struct trace_array *tr = m->private; |
| 5439 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5440 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5441 | rcu_read_lock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5442 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5443 | pid_list = rcu_dereference_sched(tr->function_pids); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5444 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5445 | if (!pid_list) |
| 5446 | return !(*pos) ? FTRACE_NO_PIDS : NULL; |
| 5447 | |
| 5448 | return trace_pid_start(pid_list, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 5452 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5453 | struct trace_array *tr = m->private; |
| 5454 | struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids); |
| 5455 | |
| 5456 | if (v == FTRACE_NO_PIDS) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5457 | return NULL; |
| 5458 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5459 | return trace_pid_next(pid_list, v, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | static void fpid_stop(struct seq_file *m, void *p) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5463 | __releases(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5464 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5465 | rcu_read_unlock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5466 | mutex_unlock(&ftrace_lock); |
| 5467 | } |
| 5468 | |
| 5469 | static int fpid_show(struct seq_file *m, void *v) |
| 5470 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5471 | if (v == FTRACE_NO_PIDS) { |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 5472 | seq_puts(m, "no pid\n"); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5473 | return 0; |
| 5474 | } |
| 5475 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5476 | return trace_pid_show(m, v); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | static const struct seq_operations ftrace_pid_sops = { |
| 5480 | .start = fpid_start, |
| 5481 | .next = fpid_next, |
| 5482 | .stop = fpid_stop, |
| 5483 | .show = fpid_show, |
| 5484 | }; |
| 5485 | |
| 5486 | static int |
| 5487 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 5488 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5489 | struct trace_array *tr = inode->i_private; |
| 5490 | struct seq_file *m; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5491 | int ret = 0; |
| 5492 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5493 | if (trace_array_get(tr) < 0) |
| 5494 | return -ENODEV; |
| 5495 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5496 | if ((file->f_mode & FMODE_WRITE) && |
| 5497 | (file->f_flags & O_TRUNC)) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5498 | ftrace_pid_reset(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5499 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5500 | ret = seq_open(file, &ftrace_pid_sops); |
| 5501 | if (ret < 0) { |
| 5502 | trace_array_put(tr); |
| 5503 | } else { |
| 5504 | m = file->private_data; |
| 5505 | /* copy tr over to seq ops */ |
| 5506 | m->private = tr; |
| 5507 | } |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5508 | |
| 5509 | return ret; |
| 5510 | } |
| 5511 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5512 | static void ignore_task_cpu(void *data) |
| 5513 | { |
| 5514 | struct trace_array *tr = data; |
| 5515 | struct trace_pid_list *pid_list; |
| 5516 | |
| 5517 | /* |
| 5518 | * This function is called by on_each_cpu() while the |
| 5519 | * event_mutex is held. |
| 5520 | */ |
| 5521 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 5522 | mutex_is_locked(&ftrace_lock)); |
| 5523 | |
| 5524 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 5525 | trace_ignore_this_task(pid_list, current)); |
| 5526 | } |
| 5527 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5528 | static ssize_t |
| 5529 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 5530 | size_t cnt, loff_t *ppos) |
| 5531 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5532 | struct seq_file *m = filp->private_data; |
| 5533 | struct trace_array *tr = m->private; |
| 5534 | struct trace_pid_list *filtered_pids = NULL; |
| 5535 | struct trace_pid_list *pid_list; |
| 5536 | ssize_t ret; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5537 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5538 | if (!cnt) |
| 5539 | return 0; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5540 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5541 | mutex_lock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5542 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5543 | filtered_pids = rcu_dereference_protected(tr->function_pids, |
| 5544 | lockdep_is_held(&ftrace_lock)); |
| 5545 | |
| 5546 | ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); |
| 5547 | if (ret < 0) |
| 5548 | goto out; |
| 5549 | |
| 5550 | rcu_assign_pointer(tr->function_pids, pid_list); |
| 5551 | |
| 5552 | if (filtered_pids) { |
| 5553 | synchronize_sched(); |
| 5554 | trace_free_pid_list(filtered_pids); |
| 5555 | } else if (pid_list) { |
| 5556 | /* Register a probe to set whether to ignore the tracing of a task */ |
| 5557 | register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 5558 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5559 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5560 | /* |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5561 | * Ignoring of pids is done at task switch. But we have to |
| 5562 | * check for those tasks that are currently running. |
| 5563 | * Always do this in case a pid was appended or removed. |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5564 | */ |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5565 | on_each_cpu(ignore_task_cpu, tr, 1); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5566 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5567 | ftrace_update_pid_func(); |
| 5568 | ftrace_startup_all(0); |
| 5569 | out: |
| 5570 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5571 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5572 | if (ret > 0) |
| 5573 | *ppos += ret; |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5574 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5575 | return ret; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5576 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5577 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5578 | static int |
| 5579 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 5580 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5581 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5582 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5583 | trace_array_put(tr); |
| 5584 | |
| 5585 | return seq_release(inode, file); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5586 | } |
| 5587 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 5588 | static const struct file_operations ftrace_pid_fops = { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5589 | .open = ftrace_pid_open, |
| 5590 | .write = ftrace_pid_write, |
| 5591 | .read = seq_read, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5592 | .llseek = tracing_lseek, |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5593 | .release = ftrace_pid_release, |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5594 | }; |
| 5595 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5596 | void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5597 | { |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 5598 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5599 | tr, &ftrace_pid_fops); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5600 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5601 | |
Steven Rostedt (Red Hat) | 501c237 | 2016-07-05 10:04:34 -0400 | [diff] [blame] | 5602 | void __init ftrace_init_tracefs_toplevel(struct trace_array *tr, |
| 5603 | struct dentry *d_tracer) |
| 5604 | { |
| 5605 | /* Only the top level directory has the dyn_tracefs and profile */ |
| 5606 | WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); |
| 5607 | |
| 5608 | ftrace_init_dyn_tracefs(d_tracer); |
| 5609 | ftrace_profile_tracefs(d_tracer); |
| 5610 | } |
| 5611 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5612 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5613 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5614 | * |
| 5615 | * This function should be used by panic code. It stops ftrace |
| 5616 | * but in a not so nice way. If you need to simply kill ftrace |
| 5617 | * from a non-atomic section, use ftrace_kill. |
| 5618 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5619 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5620 | { |
| 5621 | ftrace_disabled = 1; |
| 5622 | ftrace_enabled = 0; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5623 | clear_ftrace_function(); |
| 5624 | } |
| 5625 | |
| 5626 | /** |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 5627 | * Test if ftrace is dead or not. |
| 5628 | */ |
| 5629 | int ftrace_is_dead(void) |
| 5630 | { |
| 5631 | return ftrace_disabled; |
| 5632 | } |
| 5633 | |
| 5634 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5635 | * register_ftrace_function - register a function for profiling |
| 5636 | * @ops - ops structure that holds the function for profiling. |
| 5637 | * |
| 5638 | * Register a function to be called by all functions in the |
| 5639 | * kernel. |
| 5640 | * |
| 5641 | * Note: @ops->func and all the functions it calls must be labeled |
| 5642 | * with "notrace", otherwise it will go into a |
| 5643 | * recursive loop. |
| 5644 | */ |
| 5645 | int register_ftrace_function(struct ftrace_ops *ops) |
| 5646 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5647 | int ret = -1; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5648 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 5649 | ftrace_ops_init(ops); |
| 5650 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5651 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5652 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5653 | ret = ftrace_startup(ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5654 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5655 | mutex_unlock(&ftrace_lock); |
Borislav Petkov | 8d240dd | 2012-03-29 19:11:40 +0200 | [diff] [blame] | 5656 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5657 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5658 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5659 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5660 | |
| 5661 | /** |
Uwe Kleine-Koenig | 3263292 | 2009-01-12 23:35:50 +0100 | [diff] [blame] | 5662 | * unregister_ftrace_function - unregister a function for profiling. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5663 | * @ops - ops structure that holds the function to unregister |
| 5664 | * |
| 5665 | * Unregister a function that was added to be called by ftrace profiling. |
| 5666 | */ |
| 5667 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 5668 | { |
| 5669 | int ret; |
| 5670 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5671 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5672 | ret = ftrace_shutdown(ops, 0); |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5673 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5674 | |
| 5675 | return ret; |
| 5676 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5677 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5678 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 5679 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5680 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 5681 | void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5682 | loff_t *ppos) |
| 5683 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5684 | int ret = -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5685 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5686 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5687 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5688 | if (unlikely(ftrace_disabled)) |
| 5689 | goto out; |
| 5690 | |
| 5691 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5692 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5693 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5694 | goto out; |
| 5695 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5696 | last_ftrace_enabled = !!ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5697 | |
| 5698 | if (ftrace_enabled) { |
| 5699 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5700 | /* we are starting ftrace again */ |
Jan Kiszka | 5000c41 | 2013-03-26 17:53:03 +0100 | [diff] [blame] | 5701 | if (ftrace_ops_list != &ftrace_list_end) |
| 5702 | update_ftrace_function(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5703 | |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 5704 | ftrace_startup_sysctl(); |
| 5705 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5706 | } else { |
| 5707 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 5708 | ftrace_trace_function = ftrace_stub; |
| 5709 | |
| 5710 | ftrace_shutdown_sysctl(); |
| 5711 | } |
| 5712 | |
| 5713 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5714 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5715 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5716 | } |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 5717 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5718 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5719 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5720 | static struct ftrace_ops graph_ops = { |
| 5721 | .func = ftrace_stub, |
| 5722 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 5723 | FTRACE_OPS_FL_INITIALIZED | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 5724 | FTRACE_OPS_FL_PID | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5725 | FTRACE_OPS_FL_STUB, |
| 5726 | #ifdef FTRACE_GRAPH_TRAMP_ADDR |
| 5727 | .trampoline = FTRACE_GRAPH_TRAMP_ADDR, |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 5728 | /* trampoline_size is only needed for dynamically allocated tramps */ |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5729 | #endif |
| 5730 | ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash) |
| 5731 | }; |
| 5732 | |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 5733 | void ftrace_graph_sleep_time_control(bool enable) |
| 5734 | { |
| 5735 | fgraph_sleep_time = enable; |
| 5736 | } |
| 5737 | |
| 5738 | void ftrace_graph_graph_time_control(bool enable) |
| 5739 | { |
| 5740 | fgraph_graph_time = enable; |
| 5741 | } |
| 5742 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5743 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 5744 | { |
| 5745 | return 0; |
| 5746 | } |
| 5747 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5748 | /* The callbacks that hook a function */ |
| 5749 | trace_func_graph_ret_t ftrace_graph_return = |
| 5750 | (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5751 | 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] | 5752 | 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] | 5753 | |
| 5754 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 5755 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 5756 | { |
| 5757 | int i; |
| 5758 | int ret = 0; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5759 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 5760 | struct task_struct *g, *t; |
| 5761 | |
| 5762 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 5763 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH |
| 5764 | * sizeof(struct ftrace_ret_stack), |
| 5765 | GFP_KERNEL); |
| 5766 | if (!ret_stack_list[i]) { |
| 5767 | start = 0; |
| 5768 | end = i; |
| 5769 | ret = -ENOMEM; |
| 5770 | goto free; |
| 5771 | } |
| 5772 | } |
| 5773 | |
Soumya PN | 6112a30 | 2016-05-17 21:31:14 +0530 | [diff] [blame] | 5774 | read_lock(&tasklist_lock); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5775 | do_each_thread(g, t) { |
| 5776 | if (start == end) { |
| 5777 | ret = -EAGAIN; |
| 5778 | goto unlock; |
| 5779 | } |
| 5780 | |
| 5781 | if (t->ret_stack == NULL) { |
Frederic Weisbecker | 380c4b1 | 2008-12-06 03:43:41 +0100 | [diff] [blame] | 5782 | atomic_set(&t->tracing_graph_pause, 0); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5783 | atomic_set(&t->trace_overrun, 0); |
Steven Rostedt | 26c0162 | 2009-06-02 14:01:19 -0400 | [diff] [blame] | 5784 | t->curr_ret_stack = -1; |
| 5785 | /* Make sure the tasks see the -1 first: */ |
| 5786 | smp_wmb(); |
| 5787 | t->ret_stack = ret_stack_list[start++]; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5788 | } |
| 5789 | } while_each_thread(g, t); |
| 5790 | |
| 5791 | unlock: |
Soumya PN | 6112a30 | 2016-05-17 21:31:14 +0530 | [diff] [blame] | 5792 | read_unlock(&tasklist_lock); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5793 | free: |
| 5794 | for (i = start; i < end; i++) |
| 5795 | kfree(ret_stack_list[i]); |
| 5796 | return ret; |
| 5797 | } |
| 5798 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5799 | static void |
Peter Zijlstra | c73464b | 2015-09-28 18:06:56 +0200 | [diff] [blame] | 5800 | ftrace_graph_probe_sched_switch(void *ignore, bool preempt, |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5801 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5802 | { |
| 5803 | unsigned long long timestamp; |
| 5804 | int index; |
| 5805 | |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 5806 | /* |
| 5807 | * Does the user want to count the time a function was asleep. |
| 5808 | * If so, do not update the time stamps. |
| 5809 | */ |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 5810 | if (fgraph_sleep_time) |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 5811 | return; |
| 5812 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5813 | timestamp = trace_clock_local(); |
| 5814 | |
| 5815 | prev->ftrace_timestamp = timestamp; |
| 5816 | |
| 5817 | /* only process tasks that we timestamped */ |
| 5818 | if (!next->ftrace_timestamp) |
| 5819 | return; |
| 5820 | |
| 5821 | /* |
| 5822 | * Update all the counters in next to make up for the |
| 5823 | * time next was sleeping. |
| 5824 | */ |
| 5825 | timestamp -= next->ftrace_timestamp; |
| 5826 | |
| 5827 | for (index = next->curr_ret_stack; index >= 0; index--) |
| 5828 | next->ret_stack[index].calltime += timestamp; |
| 5829 | } |
| 5830 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5831 | /* Allocate a return stack for each task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5832 | static int start_graph_tracing(void) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5833 | { |
| 5834 | struct ftrace_ret_stack **ret_stack_list; |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5835 | int ret, cpu; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5836 | |
| 5837 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * |
| 5838 | sizeof(struct ftrace_ret_stack *), |
| 5839 | GFP_KERNEL); |
| 5840 | |
| 5841 | if (!ret_stack_list) |
| 5842 | return -ENOMEM; |
| 5843 | |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5844 | /* The cpu_boot init_task->ret_stack will never be freed */ |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 5845 | for_each_online_cpu(cpu) { |
| 5846 | if (!idle_task(cpu)->ret_stack) |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5847 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 5848 | } |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5849 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5850 | do { |
| 5851 | ret = alloc_retstack_tasklist(ret_stack_list); |
| 5852 | } while (ret == -EAGAIN); |
| 5853 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5854 | if (!ret) { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5855 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5856 | if (ret) |
| 5857 | pr_info("ftrace_graph: Couldn't activate tracepoint" |
| 5858 | " probe to kernel_sched_switch\n"); |
| 5859 | } |
| 5860 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5861 | kfree(ret_stack_list); |
| 5862 | return ret; |
| 5863 | } |
| 5864 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5865 | /* |
| 5866 | * Hibernation protection. |
| 5867 | * The state of the current task is too much unstable during |
| 5868 | * suspend/restore to disk. We want to protect against that. |
| 5869 | */ |
| 5870 | static int |
| 5871 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, |
| 5872 | void *unused) |
| 5873 | { |
| 5874 | switch (state) { |
| 5875 | case PM_HIBERNATION_PREPARE: |
| 5876 | pause_graph_tracing(); |
| 5877 | break; |
| 5878 | |
| 5879 | case PM_POST_HIBERNATION: |
| 5880 | unpause_graph_tracing(); |
| 5881 | break; |
| 5882 | } |
| 5883 | return NOTIFY_DONE; |
| 5884 | } |
| 5885 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5886 | static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) |
| 5887 | { |
| 5888 | if (!ftrace_ops_test(&global_ops, trace->func, NULL)) |
| 5889 | return 0; |
| 5890 | return __ftrace_graph_entry(trace); |
| 5891 | } |
| 5892 | |
| 5893 | /* |
| 5894 | * The function graph tracer should only trace the functions defined |
| 5895 | * by set_ftrace_filter and set_ftrace_notrace. If another function |
| 5896 | * tracer ops is registered, the graph tracer requires testing the |
| 5897 | * function against the global ops, and not just trace any function |
| 5898 | * that any ftrace_ops registered. |
| 5899 | */ |
| 5900 | static void update_function_graph_func(void) |
| 5901 | { |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5902 | struct ftrace_ops *op; |
| 5903 | bool do_test = false; |
| 5904 | |
| 5905 | /* |
| 5906 | * The graph and global ops share the same set of functions |
| 5907 | * to test. If any other ops is on the list, then |
| 5908 | * the graph tracing needs to test if its the function |
| 5909 | * it should call. |
| 5910 | */ |
| 5911 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 5912 | if (op != &global_ops && op != &graph_ops && |
| 5913 | op != &ftrace_list_end) { |
| 5914 | do_test = true; |
| 5915 | /* in double loop, break out with goto */ |
| 5916 | goto out; |
| 5917 | } |
| 5918 | } while_for_each_ftrace_op(op); |
| 5919 | out: |
| 5920 | if (do_test) |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5921 | ftrace_graph_entry = ftrace_graph_entry_test; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5922 | else |
| 5923 | ftrace_graph_entry = __ftrace_graph_entry; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5924 | } |
| 5925 | |
Mathias Krause | 8275f69 | 2014-03-30 15:31:50 +0200 | [diff] [blame] | 5926 | static struct notifier_block ftrace_suspend_notifier = { |
| 5927 | .notifier_call = ftrace_suspend_notifier_call, |
| 5928 | }; |
| 5929 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5930 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 5931 | trace_func_graph_ent_t entryfunc) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5932 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5933 | int ret = 0; |
| 5934 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5935 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5936 | |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5937 | /* we currently allow only one tracer registered at a time */ |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5938 | if (ftrace_graph_active) { |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5939 | ret = -EBUSY; |
| 5940 | goto out; |
| 5941 | } |
| 5942 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5943 | register_pm_notifier(&ftrace_suspend_notifier); |
| 5944 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5945 | ftrace_graph_active++; |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5946 | ret = start_graph_tracing(); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5947 | if (ret) { |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5948 | ftrace_graph_active--; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5949 | goto out; |
| 5950 | } |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5951 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5952 | ftrace_graph_return = retfunc; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5953 | |
| 5954 | /* |
| 5955 | * Update the indirect function to the entryfunc, and the |
| 5956 | * function that gets called to the entry_test first. Then |
| 5957 | * call the update fgraph entry function to determine if |
| 5958 | * the entryfunc should be called directly or not. |
| 5959 | */ |
| 5960 | __ftrace_graph_entry = entryfunc; |
| 5961 | ftrace_graph_entry = ftrace_graph_entry_test; |
| 5962 | update_function_graph_func(); |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5963 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5964 | ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5965 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5966 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5967 | return ret; |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5968 | } |
| 5969 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5970 | void unregister_ftrace_graph(void) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5971 | { |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5972 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5973 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5974 | if (unlikely(!ftrace_graph_active)) |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5975 | goto out; |
| 5976 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5977 | ftrace_graph_active--; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5978 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5979 | ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5980 | __ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5981 | ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET); |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5982 | unregister_pm_notifier(&ftrace_suspend_notifier); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5983 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5984 | |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5985 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5986 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5987 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5988 | |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5989 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
| 5990 | |
| 5991 | static void |
| 5992 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) |
| 5993 | { |
| 5994 | atomic_set(&t->tracing_graph_pause, 0); |
| 5995 | atomic_set(&t->trace_overrun, 0); |
| 5996 | t->ftrace_timestamp = 0; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5997 | /* make curr_ret_stack visible before we add the ret_stack */ |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5998 | smp_wmb(); |
| 5999 | t->ret_stack = ret_stack; |
| 6000 | } |
| 6001 | |
| 6002 | /* |
| 6003 | * Allocate a return stack for the idle task. May be the first |
| 6004 | * time through, or it may be done by CPU hotplug online. |
| 6005 | */ |
| 6006 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) |
| 6007 | { |
| 6008 | t->curr_ret_stack = -1; |
| 6009 | /* |
| 6010 | * The idle task has no parent, it either has its own |
| 6011 | * stack or no stack at all. |
| 6012 | */ |
| 6013 | if (t->ret_stack) |
| 6014 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); |
| 6015 | |
| 6016 | if (ftrace_graph_active) { |
| 6017 | struct ftrace_ret_stack *ret_stack; |
| 6018 | |
| 6019 | ret_stack = per_cpu(idle_ret_stack, cpu); |
| 6020 | if (!ret_stack) { |
| 6021 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
| 6022 | * sizeof(struct ftrace_ret_stack), |
| 6023 | GFP_KERNEL); |
| 6024 | if (!ret_stack) |
| 6025 | return; |
| 6026 | per_cpu(idle_ret_stack, cpu) = ret_stack; |
| 6027 | } |
| 6028 | graph_init_task(t, ret_stack); |
| 6029 | } |
| 6030 | } |
| 6031 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6032 | /* Allocate a return stack for newly created task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6033 | void ftrace_graph_init_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6034 | { |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6035 | /* Make sure we do not use the parent ret_stack */ |
| 6036 | t->ret_stack = NULL; |
Steven Rostedt | ea14eb7 | 2010-03-12 19:41:23 -0500 | [diff] [blame] | 6037 | t->curr_ret_stack = -1; |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6038 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6039 | if (ftrace_graph_active) { |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 6040 | struct ftrace_ret_stack *ret_stack; |
| 6041 | |
| 6042 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6043 | * sizeof(struct ftrace_ret_stack), |
| 6044 | GFP_KERNEL); |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 6045 | if (!ret_stack) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6046 | return; |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 6047 | graph_init_task(t, ret_stack); |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6048 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6049 | } |
| 6050 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6051 | void ftrace_graph_exit_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6052 | { |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 6053 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 6054 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6055 | t->ret_stack = NULL; |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 6056 | /* NULL must become visible to IRQs before we free it: */ |
| 6057 | barrier(); |
| 6058 | |
| 6059 | kfree(ret_stack); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6060 | } |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 6061 | #endif |