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> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 18 | #include <linux/sched/task.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 19 | #include <linux/kallsyms.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 21 | #include <linux/suspend.h> |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 22 | #include <linux/tracefs.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 23 | #include <linux/hardirq.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 26 | #include <linux/bsearch.h> |
Paul Gortmaker | 56d82e0 | 2011-05-26 17:53:52 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 28 | #include <linux/ftrace.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 29 | #include <linux/sysctl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 32 | #include <linux/sort.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 33 | #include <linux/list.h> |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 34 | #include <linux/hash.h> |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 35 | #include <linux/rcupdate.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 | |
| 880 | if (index >= 0 && index < FTRACE_RETFUNC_DEPTH) |
| 881 | current->ret_stack[index].subtime = 0; |
| 882 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 883 | return 1; |
| 884 | } |
| 885 | |
| 886 | static void profile_graph_return(struct ftrace_graph_ret *trace) |
| 887 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 888 | struct ftrace_profile_stat *stat; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 889 | unsigned long long calltime; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 890 | struct ftrace_profile *rec; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 891 | unsigned long flags; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 892 | |
| 893 | local_irq_save(flags); |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 894 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 895 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 896 | goto out; |
| 897 | |
Steven Rostedt | 37e44bc | 2010-04-27 21:04:24 -0400 | [diff] [blame] | 898 | /* If the calltime was zero'd ignore it */ |
| 899 | if (!trace->calltime) |
| 900 | goto out; |
| 901 | |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 902 | calltime = trace->rettime - trace->calltime; |
| 903 | |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 904 | if (!fgraph_graph_time) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 905 | int index; |
| 906 | |
| 907 | index = trace->depth; |
| 908 | |
| 909 | /* Append this call time to the parent time to subtract */ |
| 910 | if (index) |
| 911 | current->ret_stack[index - 1].subtime += calltime; |
| 912 | |
| 913 | if (current->ret_stack[index].subtime < calltime) |
| 914 | calltime -= current->ret_stack[index].subtime; |
| 915 | else |
| 916 | calltime = 0; |
| 917 | } |
| 918 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 919 | rec = ftrace_find_profiled_func(stat, trace->func); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 920 | if (rec) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 921 | rec->time += calltime; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 922 | rec->time_squared += calltime * calltime; |
| 923 | } |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 924 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 925 | out: |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 926 | local_irq_restore(flags); |
| 927 | } |
| 928 | |
| 929 | static int register_ftrace_profiler(void) |
| 930 | { |
| 931 | return register_ftrace_graph(&profile_graph_return, |
| 932 | &profile_graph_entry); |
| 933 | } |
| 934 | |
| 935 | static void unregister_ftrace_profiler(void) |
| 936 | { |
| 937 | unregister_ftrace_graph(); |
| 938 | } |
| 939 | #else |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 940 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 941 | .func = function_profile_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 942 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 943 | INIT_OPS_HASH(ftrace_profile_ops) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 944 | }; |
| 945 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 946 | static int register_ftrace_profiler(void) |
| 947 | { |
| 948 | return register_ftrace_function(&ftrace_profile_ops); |
| 949 | } |
| 950 | |
| 951 | static void unregister_ftrace_profiler(void) |
| 952 | { |
| 953 | unregister_ftrace_function(&ftrace_profile_ops); |
| 954 | } |
| 955 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 956 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 957 | static ssize_t |
| 958 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 959 | size_t cnt, loff_t *ppos) |
| 960 | { |
| 961 | unsigned long val; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 962 | int ret; |
| 963 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 964 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 965 | if (ret) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 966 | return ret; |
| 967 | |
| 968 | val = !!val; |
| 969 | |
| 970 | mutex_lock(&ftrace_profile_lock); |
| 971 | if (ftrace_profile_enabled ^ val) { |
| 972 | if (val) { |
| 973 | ret = ftrace_profile_init(); |
| 974 | if (ret < 0) { |
| 975 | cnt = ret; |
| 976 | goto out; |
| 977 | } |
| 978 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 979 | ret = register_ftrace_profiler(); |
| 980 | if (ret < 0) { |
| 981 | cnt = ret; |
| 982 | goto out; |
| 983 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 984 | ftrace_profile_enabled = 1; |
| 985 | } else { |
| 986 | ftrace_profile_enabled = 0; |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 987 | /* |
| 988 | * unregister_ftrace_profiler calls stop_machine |
| 989 | * so this acts like an synchronize_sched. |
| 990 | */ |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 991 | unregister_ftrace_profiler(); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | out: |
| 995 | mutex_unlock(&ftrace_profile_lock); |
| 996 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 997 | *ppos += cnt; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 998 | |
| 999 | return cnt; |
| 1000 | } |
| 1001 | |
| 1002 | static ssize_t |
| 1003 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 1004 | size_t cnt, loff_t *ppos) |
| 1005 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 1006 | char buf[64]; /* big enough to hold a number */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1007 | int r; |
| 1008 | |
| 1009 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); |
| 1010 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 1011 | } |
| 1012 | |
| 1013 | static const struct file_operations ftrace_profile_fops = { |
| 1014 | .open = tracing_open_generic, |
| 1015 | .read = ftrace_profile_read, |
| 1016 | .write = ftrace_profile_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1017 | .llseek = default_llseek, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1018 | }; |
| 1019 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1020 | /* used to initialize the real stat files */ |
| 1021 | static struct tracer_stat function_stats __initdata = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 1022 | .name = "functions", |
| 1023 | .stat_start = function_stat_start, |
| 1024 | .stat_next = function_stat_next, |
| 1025 | .stat_cmp = function_stat_cmp, |
| 1026 | .stat_headers = function_stat_headers, |
| 1027 | .stat_show = function_stat_show |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1028 | }; |
| 1029 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1030 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1031 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1032 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1033 | struct dentry *entry; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1034 | char *name; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1035 | int ret; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1036 | int cpu; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1037 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1038 | for_each_possible_cpu(cpu) { |
| 1039 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 1040 | |
Geliang Tang | 6363c6b | 2016-03-15 22:12:34 +0800 | [diff] [blame] | 1041 | name = kasprintf(GFP_KERNEL, "function%d", cpu); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1042 | if (!name) { |
| 1043 | /* |
| 1044 | * The files created are permanent, if something happens |
| 1045 | * we still do not free memory. |
| 1046 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1047 | WARN(1, |
| 1048 | "Could not allocate stat file for cpu %d\n", |
| 1049 | cpu); |
| 1050 | return; |
| 1051 | } |
| 1052 | stat->stat = function_stats; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1053 | stat->stat.name = name; |
| 1054 | ret = register_stat_tracer(&stat->stat); |
| 1055 | if (ret) { |
| 1056 | WARN(1, |
| 1057 | "Could not register function stat for cpu %d\n", |
| 1058 | cpu); |
| 1059 | kfree(name); |
| 1060 | return; |
| 1061 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1062 | } |
| 1063 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1064 | entry = tracefs_create_file("function_profile_enabled", 0644, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1065 | d_tracer, NULL, &ftrace_profile_fops); |
| 1066 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1067 | pr_warn("Could not create tracefs 'function_profile_enabled' entry\n"); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | #else /* CONFIG_FUNCTION_PROFILER */ |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1071 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1072 | { |
| 1073 | } |
| 1074 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 1075 | |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1076 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
| 1077 | |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 1078 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 1079 | static int ftrace_graph_active; |
| 1080 | #else |
| 1081 | # define ftrace_graph_active 0 |
| 1082 | #endif |
| 1083 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1084 | #ifdef CONFIG_DYNAMIC_FTRACE |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1085 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1086 | static struct ftrace_ops *removed_ops; |
| 1087 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 1088 | /* |
| 1089 | * Set when doing a global update, like enabling all recs or disabling them. |
| 1090 | * It is not set when just updating a single ftrace_ops. |
| 1091 | */ |
| 1092 | static bool update_all_ops; |
| 1093 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1094 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1095 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1096 | #endif |
| 1097 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1098 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; |
| 1099 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1100 | struct ftrace_func_probe { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1101 | struct hlist_node node; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1102 | struct ftrace_probe_ops *ops; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1103 | unsigned long flags; |
| 1104 | unsigned long ip; |
| 1105 | void *data; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 1106 | struct list_head free_list; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1107 | }; |
| 1108 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1109 | struct ftrace_func_entry { |
| 1110 | struct hlist_node hlist; |
| 1111 | unsigned long ip; |
| 1112 | }; |
| 1113 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1114 | /* |
| 1115 | * We make these constant because no one should touch them, |
| 1116 | * but they are used as the default "empty hash", to avoid allocating |
| 1117 | * it all the time. These are in a read only section such that if |
| 1118 | * anyone does try to modify it, it will cause an exception. |
| 1119 | */ |
| 1120 | static const struct hlist_head empty_buckets[1]; |
| 1121 | static const struct ftrace_hash empty_hash = { |
| 1122 | .buckets = (struct hlist_head *)empty_buckets, |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1123 | }; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1124 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1125 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 1126 | static struct ftrace_ops global_ops = { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1127 | .func = ftrace_stub, |
| 1128 | .local_hash.notrace_hash = EMPTY_HASH, |
| 1129 | .local_hash.filter_hash = EMPTY_HASH, |
| 1130 | INIT_OPS_HASH(global_ops) |
| 1131 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 1132 | FTRACE_OPS_FL_INITIALIZED | |
| 1133 | FTRACE_OPS_FL_PID, |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1134 | }; |
| 1135 | |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1136 | /* |
| 1137 | * 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] | 1138 | * address is on a dynamically allocated trampoline that would |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1139 | * not return true for either core_kernel_text() or |
| 1140 | * is_module_text_address(). |
| 1141 | */ |
| 1142 | bool is_ftrace_trampoline(unsigned long addr) |
| 1143 | { |
| 1144 | struct ftrace_ops *op; |
| 1145 | bool ret = false; |
| 1146 | |
| 1147 | /* |
| 1148 | * Some of the ops may be dynamically allocated, |
| 1149 | * they are freed after a synchronize_sched(). |
| 1150 | */ |
| 1151 | preempt_disable_notrace(); |
| 1152 | |
| 1153 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1154 | /* |
| 1155 | * This is to check for dynamically allocated trampolines. |
| 1156 | * Trampolines that are in kernel text will have |
| 1157 | * core_kernel_text() return true. |
| 1158 | */ |
| 1159 | if (op->trampoline && op->trampoline_size) |
| 1160 | if (addr >= op->trampoline && |
| 1161 | addr < op->trampoline + op->trampoline_size) { |
| 1162 | ret = true; |
| 1163 | goto out; |
| 1164 | } |
| 1165 | } while_for_each_ftrace_op(op); |
| 1166 | |
| 1167 | out: |
| 1168 | preempt_enable_notrace(); |
| 1169 | |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1173 | struct ftrace_page { |
| 1174 | struct ftrace_page *next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1175 | struct dyn_ftrace *records; |
Steven Rostedt | 431aa3f | 2009-01-06 12:43:01 -0500 | [diff] [blame] | 1176 | int index; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1177 | int size; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 1178 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1179 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1180 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1181 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1182 | |
| 1183 | /* estimate from running different kernels */ |
| 1184 | #define NR_TO_INIT 10000 |
| 1185 | |
| 1186 | static struct ftrace_page *ftrace_pages_start; |
| 1187 | static struct ftrace_page *ftrace_pages; |
| 1188 | |
Steven Rostedt (VMware) | 2b0cce0 | 2017-02-01 12:19:33 -0500 | [diff] [blame] | 1189 | static __always_inline unsigned long |
| 1190 | ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip) |
| 1191 | { |
| 1192 | if (hash->size_bits > 0) |
| 1193 | return hash_long(ip, hash->size_bits); |
| 1194 | |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1198 | /* Only use this function if ftrace_hash_empty() has already been tested */ |
| 1199 | static __always_inline struct ftrace_func_entry * |
| 1200 | __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1201 | { |
| 1202 | unsigned long key; |
| 1203 | struct ftrace_func_entry *entry; |
| 1204 | struct hlist_head *hhd; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1205 | |
Steven Rostedt (VMware) | 2b0cce0 | 2017-02-01 12:19:33 -0500 | [diff] [blame] | 1206 | key = ftrace_hash_key(hash, ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1207 | hhd = &hash->buckets[key]; |
| 1208 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 1209 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1210 | if (entry->ip == ip) |
| 1211 | return entry; |
| 1212 | } |
| 1213 | return NULL; |
| 1214 | } |
| 1215 | |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1216 | /** |
| 1217 | * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash |
| 1218 | * @hash: The hash to look at |
| 1219 | * @ip: The instruction pointer to test |
| 1220 | * |
| 1221 | * Search a given @hash to see if a given instruction pointer (@ip) |
| 1222 | * exists in it. |
| 1223 | * |
| 1224 | * Returns the entry that holds the @ip if found. NULL otherwise. |
| 1225 | */ |
| 1226 | struct ftrace_func_entry * |
| 1227 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1228 | { |
| 1229 | if (ftrace_hash_empty(hash)) |
| 1230 | return NULL; |
| 1231 | |
| 1232 | return __ftrace_lookup_ip(hash, ip); |
| 1233 | } |
| 1234 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1235 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1236 | struct ftrace_func_entry *entry) |
| 1237 | { |
| 1238 | struct hlist_head *hhd; |
| 1239 | unsigned long key; |
| 1240 | |
Steven Rostedt (VMware) | 2b0cce0 | 2017-02-01 12:19:33 -0500 | [diff] [blame] | 1241 | key = ftrace_hash_key(hash, entry->ip); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1242 | hhd = &hash->buckets[key]; |
| 1243 | hlist_add_head(&entry->hlist, hhd); |
| 1244 | hash->count++; |
| 1245 | } |
| 1246 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1247 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1248 | { |
| 1249 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1250 | |
| 1251 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1252 | if (!entry) |
| 1253 | return -ENOMEM; |
| 1254 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1255 | entry->ip = ip; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1256 | __add_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static void |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1262 | free_hash_entry(struct ftrace_hash *hash, |
| 1263 | struct ftrace_func_entry *entry) |
| 1264 | { |
| 1265 | hlist_del(&entry->hlist); |
| 1266 | kfree(entry); |
| 1267 | hash->count--; |
| 1268 | } |
| 1269 | |
| 1270 | static void |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1271 | remove_hash_entry(struct ftrace_hash *hash, |
| 1272 | struct ftrace_func_entry *entry) |
| 1273 | { |
| 1274 | hlist_del(&entry->hlist); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1275 | hash->count--; |
| 1276 | } |
| 1277 | |
| 1278 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1279 | { |
| 1280 | struct hlist_head *hhd; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1281 | struct hlist_node *tn; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1282 | struct ftrace_func_entry *entry; |
| 1283 | int size = 1 << hash->size_bits; |
| 1284 | int i; |
| 1285 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1286 | if (!hash->count) |
| 1287 | return; |
| 1288 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1289 | for (i = 0; i < size; i++) { |
| 1290 | hhd = &hash->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1291 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1292 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1293 | } |
| 1294 | FTRACE_WARN_ON(hash->count); |
| 1295 | } |
| 1296 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1297 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1298 | { |
| 1299 | if (!hash || hash == EMPTY_HASH) |
| 1300 | return; |
| 1301 | ftrace_hash_clear(hash); |
| 1302 | kfree(hash->buckets); |
| 1303 | kfree(hash); |
| 1304 | } |
| 1305 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1306 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1307 | { |
| 1308 | struct ftrace_hash *hash; |
| 1309 | |
| 1310 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1311 | free_ftrace_hash(hash); |
| 1312 | } |
| 1313 | |
| 1314 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1315 | { |
| 1316 | if (!hash || hash == EMPTY_HASH) |
| 1317 | return; |
| 1318 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); |
| 1319 | } |
| 1320 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1321 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1322 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 1323 | ftrace_ops_init(ops); |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1324 | free_ftrace_hash(ops->func_hash->filter_hash); |
| 1325 | free_ftrace_hash(ops->func_hash->notrace_hash); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1326 | } |
| 1327 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1328 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1329 | { |
| 1330 | struct ftrace_hash *hash; |
| 1331 | int size; |
| 1332 | |
| 1333 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1334 | if (!hash) |
| 1335 | return NULL; |
| 1336 | |
| 1337 | size = 1 << size_bits; |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1338 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1339 | |
| 1340 | if (!hash->buckets) { |
| 1341 | kfree(hash); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | |
| 1345 | hash->size_bits = size_bits; |
| 1346 | |
| 1347 | return hash; |
| 1348 | } |
| 1349 | |
| 1350 | static struct ftrace_hash * |
| 1351 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1352 | { |
| 1353 | struct ftrace_func_entry *entry; |
| 1354 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1355 | int size; |
| 1356 | int ret; |
| 1357 | int i; |
| 1358 | |
| 1359 | new_hash = alloc_ftrace_hash(size_bits); |
| 1360 | if (!new_hash) |
| 1361 | return NULL; |
| 1362 | |
| 1363 | /* Empty hash? */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1364 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1365 | return new_hash; |
| 1366 | |
| 1367 | size = 1 << hash->size_bits; |
| 1368 | for (i = 0; i < size; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1369 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1370 | ret = add_hash_entry(new_hash, entry->ip); |
| 1371 | if (ret < 0) |
| 1372 | goto free_hash; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1377 | |
| 1378 | return new_hash; |
| 1379 | |
| 1380 | free_hash: |
| 1381 | free_ftrace_hash(new_hash); |
| 1382 | return NULL; |
| 1383 | } |
| 1384 | |
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_disable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1387 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1388 | ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1389 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1390 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1391 | struct ftrace_hash *new_hash); |
| 1392 | |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1393 | static struct ftrace_hash * |
| 1394 | __ftrace_hash_move(struct ftrace_hash *src) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1395 | { |
| 1396 | struct ftrace_func_entry *entry; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1397 | struct hlist_node *tn; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1398 | struct hlist_head *hhd; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1399 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1400 | int size = src->count; |
| 1401 | int bits = 0; |
| 1402 | int i; |
| 1403 | |
| 1404 | /* |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1405 | * If the new source is empty, just return the empty_hash. |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1406 | */ |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1407 | if (!src->count) |
| 1408 | return EMPTY_HASH; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1409 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1410 | /* |
| 1411 | * Make the hash size about 1/2 the # found |
| 1412 | */ |
| 1413 | for (size /= 2; size; size >>= 1) |
| 1414 | bits++; |
| 1415 | |
| 1416 | /* Don't allocate too much */ |
| 1417 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1418 | bits = FTRACE_HASH_MAX_BITS; |
| 1419 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1420 | new_hash = alloc_ftrace_hash(bits); |
| 1421 | if (!new_hash) |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1422 | return NULL; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1423 | |
| 1424 | size = 1 << src->size_bits; |
| 1425 | for (i = 0; i < size; i++) { |
| 1426 | hhd = &src->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1427 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1428 | remove_hash_entry(src, entry); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1429 | __add_hash_entry(new_hash, entry); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1433 | return new_hash; |
| 1434 | } |
| 1435 | |
| 1436 | static int |
| 1437 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1438 | struct ftrace_hash **dst, struct ftrace_hash *src) |
| 1439 | { |
| 1440 | struct ftrace_hash *new_hash; |
| 1441 | int ret; |
| 1442 | |
| 1443 | /* Reject setting notrace hash on IPMODIFY ftrace_ops */ |
| 1444 | if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) |
| 1445 | return -EINVAL; |
| 1446 | |
| 1447 | new_hash = __ftrace_hash_move(src); |
| 1448 | if (!new_hash) |
| 1449 | return -ENOMEM; |
| 1450 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1451 | /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ |
| 1452 | if (enable) { |
| 1453 | /* IPMODIFY should be updated only when filter_hash updating */ |
| 1454 | ret = ftrace_hash_ipmodify_update(ops, new_hash); |
| 1455 | if (ret < 0) { |
| 1456 | free_ftrace_hash(new_hash); |
| 1457 | return ret; |
| 1458 | } |
| 1459 | } |
| 1460 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1461 | /* |
| 1462 | * Remove the current set, update the hash and add |
| 1463 | * them back. |
| 1464 | */ |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1465 | ftrace_hash_rec_disable_modify(ops, enable); |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1466 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1467 | rcu_assign_pointer(*dst, new_hash); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1468 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1469 | ftrace_hash_rec_enable_modify(ops, enable); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1470 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1471 | return 0; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1472 | } |
| 1473 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1474 | static bool hash_contains_ip(unsigned long ip, |
| 1475 | struct ftrace_ops_hash *hash) |
| 1476 | { |
| 1477 | /* |
| 1478 | * The function record is a match if it exists in the filter |
| 1479 | * hash and not in the notrace hash. Note, an emty hash is |
| 1480 | * considered a match for the filter hash, but an empty |
| 1481 | * notrace hash is considered not in the notrace hash. |
| 1482 | */ |
| 1483 | return (ftrace_hash_empty(hash->filter_hash) || |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1484 | __ftrace_lookup_ip(hash->filter_hash, ip)) && |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1485 | (ftrace_hash_empty(hash->notrace_hash) || |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1486 | !__ftrace_lookup_ip(hash->notrace_hash, ip)); |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1487 | } |
| 1488 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1489 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1490 | * Test the hashes for this ops to see if we want to call |
| 1491 | * the ops->func or not. |
| 1492 | * |
| 1493 | * It's a match if the ip is in the ops->filter_hash or |
| 1494 | * the filter_hash does not exist or is empty, |
| 1495 | * AND |
| 1496 | * the ip is not in the ops->notrace_hash. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 1497 | * |
| 1498 | * This needs to be called with preemption disabled as |
| 1499 | * the hashes are freed with call_rcu_sched(). |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1500 | */ |
| 1501 | static int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1502 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1503 | { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1504 | struct ftrace_ops_hash hash; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1505 | int ret; |
| 1506 | |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1507 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1508 | /* |
| 1509 | * There's a small race when adding ops that the ftrace handler |
| 1510 | * that wants regs, may be called without them. We can not |
| 1511 | * allow that handler to be called if regs is NULL. |
| 1512 | */ |
| 1513 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) |
| 1514 | return 0; |
| 1515 | #endif |
| 1516 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1517 | hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash); |
| 1518 | hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1519 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1520 | if (hash_contains_ip(ip, &hash)) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1521 | ret = 1; |
| 1522 | else |
| 1523 | ret = 0; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1524 | |
| 1525 | return ret; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1529 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1530 | * you must use a goto. |
| 1531 | */ |
| 1532 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1533 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1534 | int _____i; \ |
| 1535 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1536 | rec = &pg->records[_____i]; |
| 1537 | |
| 1538 | #define while_for_each_ftrace_rec() \ |
| 1539 | } \ |
| 1540 | } |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 1541 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1542 | |
| 1543 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1544 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1545 | const struct dyn_ftrace *key = a; |
| 1546 | const struct dyn_ftrace *rec = b; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1547 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1548 | if (key->flags < rec->ip) |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1549 | return -1; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1550 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
| 1551 | return 1; |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
Michael Ellerman | 04cf31a | 2016-03-24 22:04:01 +1100 | [diff] [blame] | 1555 | /** |
| 1556 | * ftrace_location_range - return the first address of a traced location |
| 1557 | * if it touches the given ip range |
| 1558 | * @start: start of range to search. |
| 1559 | * @end: end of range to search (inclusive). @end points to the last byte |
| 1560 | * to check. |
| 1561 | * |
| 1562 | * Returns rec->ip if the related ftrace location is a least partly within |
| 1563 | * the given address range. That is, the first address of the instruction |
| 1564 | * that is either a NOP or call to the function tracer. It checks the ftrace |
| 1565 | * internal tables to determine if the address belongs or not. |
| 1566 | */ |
| 1567 | unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1568 | { |
| 1569 | struct ftrace_page *pg; |
| 1570 | struct dyn_ftrace *rec; |
| 1571 | struct dyn_ftrace key; |
| 1572 | |
| 1573 | key.ip = start; |
| 1574 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 1575 | |
| 1576 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1577 | if (end < pg->records[0].ip || |
| 1578 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 1579 | continue; |
| 1580 | rec = bsearch(&key, pg->records, pg->index, |
| 1581 | sizeof(struct dyn_ftrace), |
| 1582 | ftrace_cmp_recs); |
| 1583 | if (rec) |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1584 | return rec->ip; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1585 | } |
| 1586 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1587 | return 0; |
| 1588 | } |
| 1589 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1590 | /** |
| 1591 | * ftrace_location - return true if the ip giving is a traced location |
| 1592 | * @ip: the instruction pointer to check |
| 1593 | * |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1594 | * 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] | 1595 | * That is, the instruction that is either a NOP or call to |
| 1596 | * the function tracer. It checks the ftrace internal tables to |
| 1597 | * determine if the address belongs or not. |
| 1598 | */ |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1599 | unsigned long ftrace_location(unsigned long ip) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1600 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1601 | return ftrace_location_range(ip, ip); |
| 1602 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1603 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1604 | /** |
| 1605 | * ftrace_text_reserved - return true if range contains an ftrace location |
| 1606 | * @start: start of range to search |
| 1607 | * @end: end of range to search (inclusive). @end points to the last byte to check. |
| 1608 | * |
| 1609 | * Returns 1 if @start and @end contains a ftrace location. |
| 1610 | * That is, the instruction that is either a NOP or call to |
| 1611 | * the function tracer. It checks the ftrace internal tables to |
| 1612 | * determine if the address belongs or not. |
| 1613 | */ |
Sasha Levin | d88471c | 2013-01-09 18:09:20 -0500 | [diff] [blame] | 1614 | int ftrace_text_reserved(const void *start, const void *end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1615 | { |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1616 | unsigned long ret; |
| 1617 | |
| 1618 | ret = ftrace_location_range((unsigned long)start, |
| 1619 | (unsigned long)end); |
| 1620 | |
| 1621 | return (int)!!ret; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1622 | } |
| 1623 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1624 | /* Test if ops registered to this rec needs regs */ |
| 1625 | static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) |
| 1626 | { |
| 1627 | struct ftrace_ops *ops; |
| 1628 | bool keep_regs = false; |
| 1629 | |
| 1630 | for (ops = ftrace_ops_list; |
| 1631 | ops != &ftrace_list_end; ops = ops->next) { |
| 1632 | /* pass rec in as regs to have non-NULL val */ |
| 1633 | if (ftrace_ops_test(ops, rec->ip, rec)) { |
| 1634 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1635 | keep_regs = true; |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | return keep_regs; |
| 1642 | } |
| 1643 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1644 | static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1645 | int filter_hash, |
| 1646 | bool inc) |
| 1647 | { |
| 1648 | struct ftrace_hash *hash; |
| 1649 | struct ftrace_hash *other_hash; |
| 1650 | struct ftrace_page *pg; |
| 1651 | struct dyn_ftrace *rec; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1652 | bool update = false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1653 | int count = 0; |
| 1654 | int all = 0; |
| 1655 | |
| 1656 | /* Only update if the ops has been registered */ |
| 1657 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1658 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1659 | |
| 1660 | /* |
| 1661 | * In the filter_hash case: |
| 1662 | * If the count is zero, we update all records. |
| 1663 | * Otherwise we just update the items in the hash. |
| 1664 | * |
| 1665 | * In the notrace_hash case: |
| 1666 | * We enable the update in the hash. |
| 1667 | * As disabling notrace means enabling the tracing, |
| 1668 | * and enabling notrace means disabling, the inc variable |
| 1669 | * gets inversed. |
| 1670 | */ |
| 1671 | if (filter_hash) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1672 | hash = ops->func_hash->filter_hash; |
| 1673 | other_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1674 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1675 | all = 1; |
| 1676 | } else { |
| 1677 | inc = !inc; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1678 | hash = ops->func_hash->notrace_hash; |
| 1679 | other_hash = ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1680 | /* |
| 1681 | * If the notrace hash has no items, |
| 1682 | * then there's nothing to do. |
| 1683 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1684 | if (ftrace_hash_empty(hash)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1685 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | do_for_each_ftrace_rec(pg, rec) { |
| 1689 | int in_other_hash = 0; |
| 1690 | int in_hash = 0; |
| 1691 | int match = 0; |
| 1692 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 1693 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1694 | continue; |
| 1695 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1696 | if (all) { |
| 1697 | /* |
| 1698 | * Only the filter_hash affects all records. |
| 1699 | * Update if the record is not in the notrace hash. |
| 1700 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1701 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1702 | match = 1; |
| 1703 | } else { |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1704 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
| 1705 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1706 | |
| 1707 | /* |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1708 | * If filter_hash is set, we want to match all functions |
| 1709 | * that are in the hash but not in the other hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1710 | * |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1711 | * If filter_hash is not set, then we are decrementing. |
| 1712 | * That means we match anything that is in the hash |
| 1713 | * and also in the other_hash. That is, we need to turn |
| 1714 | * off functions in the other hash because they are disabled |
| 1715 | * by this hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1716 | */ |
| 1717 | if (filter_hash && in_hash && !in_other_hash) |
| 1718 | match = 1; |
| 1719 | else if (!filter_hash && in_hash && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1720 | (in_other_hash || ftrace_hash_empty(other_hash))) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1721 | match = 1; |
| 1722 | } |
| 1723 | if (!match) |
| 1724 | continue; |
| 1725 | |
| 1726 | if (inc) { |
| 1727 | rec->flags++; |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1728 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1729 | return false; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1730 | |
| 1731 | /* |
| 1732 | * If there's only a single callback registered to a |
| 1733 | * function, and the ops has a trampoline registered |
| 1734 | * for it, then we can call it directly. |
| 1735 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1736 | if (ftrace_rec_count(rec) == 1 && ops->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1737 | rec->flags |= FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1738 | else |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1739 | /* |
| 1740 | * If we are adding another function callback |
| 1741 | * to this function, and the previous had a |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 1742 | * custom trampoline in use, then we need to go |
| 1743 | * back to the default trampoline. |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1744 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1745 | rec->flags &= ~FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1746 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1747 | /* |
| 1748 | * If any ops wants regs saved for this function |
| 1749 | * then all ops will get saved regs. |
| 1750 | */ |
| 1751 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 1752 | rec->flags |= FTRACE_FL_REGS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1753 | } else { |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1754 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1755 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1756 | rec->flags--; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1757 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1758 | /* |
| 1759 | * If the rec had REGS enabled and the ops that is |
| 1760 | * being removed had REGS set, then see if there is |
| 1761 | * still any ops for this record that wants regs. |
| 1762 | * If not, we can stop recording them. |
| 1763 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1764 | if (ftrace_rec_count(rec) > 0 && |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1765 | rec->flags & FTRACE_FL_REGS && |
| 1766 | ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1767 | if (!test_rec_ops_needs_regs(rec)) |
| 1768 | rec->flags &= ~FTRACE_FL_REGS; |
| 1769 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1770 | |
| 1771 | /* |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1772 | * If the rec had TRAMP enabled, then it needs to |
| 1773 | * be cleared. As TRAMP can only be enabled iff |
| 1774 | * there is only a single ops attached to it. |
| 1775 | * In otherwords, always disable it on decrementing. |
| 1776 | * In the future, we may set it if rec count is |
| 1777 | * decremented to one, and the ops that is left |
| 1778 | * has a trampoline. |
| 1779 | */ |
| 1780 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 1781 | |
| 1782 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1783 | * flags will be cleared in ftrace_check_record() |
| 1784 | * if rec count is zero. |
| 1785 | */ |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1786 | } |
| 1787 | count++; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1788 | |
| 1789 | /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */ |
| 1790 | update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE; |
| 1791 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1792 | /* Shortcut, if we handled all records, we are done. */ |
| 1793 | if (!all && count == hash->count) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1794 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1795 | } while_for_each_ftrace_rec(); |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1796 | |
| 1797 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1798 | } |
| 1799 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1800 | static bool ftrace_hash_rec_disable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1801 | int filter_hash) |
| 1802 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1803 | return __ftrace_hash_rec_update(ops, filter_hash, 0); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1804 | } |
| 1805 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1806 | static bool ftrace_hash_rec_enable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1807 | int filter_hash) |
| 1808 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1809 | return __ftrace_hash_rec_update(ops, filter_hash, 1); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1810 | } |
| 1811 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1812 | static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, |
| 1813 | int filter_hash, int inc) |
| 1814 | { |
| 1815 | struct ftrace_ops *op; |
| 1816 | |
| 1817 | __ftrace_hash_rec_update(ops, filter_hash, inc); |
| 1818 | |
| 1819 | if (ops->func_hash != &global_ops.local_hash) |
| 1820 | return; |
| 1821 | |
| 1822 | /* |
| 1823 | * If the ops shares the global_ops hash, then we need to update |
| 1824 | * all ops that are enabled and use this hash. |
| 1825 | */ |
| 1826 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1827 | /* Already done */ |
| 1828 | if (op == ops) |
| 1829 | continue; |
| 1830 | if (op->func_hash == &global_ops.local_hash) |
| 1831 | __ftrace_hash_rec_update(op, filter_hash, inc); |
| 1832 | } while_for_each_ftrace_op(op); |
| 1833 | } |
| 1834 | |
| 1835 | static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, |
| 1836 | int filter_hash) |
| 1837 | { |
| 1838 | ftrace_hash_rec_update_modify(ops, filter_hash, 0); |
| 1839 | } |
| 1840 | |
| 1841 | static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, |
| 1842 | int filter_hash) |
| 1843 | { |
| 1844 | ftrace_hash_rec_update_modify(ops, filter_hash, 1); |
| 1845 | } |
| 1846 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1847 | /* |
| 1848 | * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK |
| 1849 | * or no-needed to update, -EBUSY if it detects a conflict of the flag |
| 1850 | * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. |
| 1851 | * Note that old_hash and new_hash has below meanings |
| 1852 | * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) |
| 1853 | * - If the hash is EMPTY_HASH, it hits nothing |
| 1854 | * - Anything else hits the recs which match the hash entries. |
| 1855 | */ |
| 1856 | static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, |
| 1857 | struct ftrace_hash *old_hash, |
| 1858 | struct ftrace_hash *new_hash) |
| 1859 | { |
| 1860 | struct ftrace_page *pg; |
| 1861 | struct dyn_ftrace *rec, *end = NULL; |
| 1862 | int in_old, in_new; |
| 1863 | |
| 1864 | /* Only update if the ops has been registered */ |
| 1865 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1866 | return 0; |
| 1867 | |
| 1868 | if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 1869 | return 0; |
| 1870 | |
| 1871 | /* |
| 1872 | * Since the IPMODIFY is a very address sensitive action, we do not |
| 1873 | * allow ftrace_ops to set all functions to new hash. |
| 1874 | */ |
| 1875 | if (!new_hash || !old_hash) |
| 1876 | return -EINVAL; |
| 1877 | |
| 1878 | /* Update rec->flags */ |
| 1879 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1880 | |
| 1881 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1882 | continue; |
| 1883 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1884 | /* We need to update only differences of filter_hash */ |
| 1885 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1886 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1887 | if (in_old == in_new) |
| 1888 | continue; |
| 1889 | |
| 1890 | if (in_new) { |
| 1891 | /* New entries must ensure no others are using it */ |
| 1892 | if (rec->flags & FTRACE_FL_IPMODIFY) |
| 1893 | goto rollback; |
| 1894 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1895 | } else /* Removed entry */ |
| 1896 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1897 | } while_for_each_ftrace_rec(); |
| 1898 | |
| 1899 | return 0; |
| 1900 | |
| 1901 | rollback: |
| 1902 | end = rec; |
| 1903 | |
| 1904 | /* Roll back what we did above */ |
| 1905 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1906 | |
| 1907 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1908 | continue; |
| 1909 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1910 | if (rec == end) |
| 1911 | goto err_out; |
| 1912 | |
| 1913 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1914 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1915 | if (in_old == in_new) |
| 1916 | continue; |
| 1917 | |
| 1918 | if (in_new) |
| 1919 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1920 | else |
| 1921 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1922 | } while_for_each_ftrace_rec(); |
| 1923 | |
| 1924 | err_out: |
| 1925 | return -EBUSY; |
| 1926 | } |
| 1927 | |
| 1928 | static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) |
| 1929 | { |
| 1930 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1931 | |
| 1932 | if (ftrace_hash_empty(hash)) |
| 1933 | hash = NULL; |
| 1934 | |
| 1935 | return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash); |
| 1936 | } |
| 1937 | |
| 1938 | /* Disabling always succeeds */ |
| 1939 | static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) |
| 1940 | { |
| 1941 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1942 | |
| 1943 | if (ftrace_hash_empty(hash)) |
| 1944 | hash = NULL; |
| 1945 | |
| 1946 | __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH); |
| 1947 | } |
| 1948 | |
| 1949 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1950 | struct ftrace_hash *new_hash) |
| 1951 | { |
| 1952 | struct ftrace_hash *old_hash = ops->func_hash->filter_hash; |
| 1953 | |
| 1954 | if (ftrace_hash_empty(old_hash)) |
| 1955 | old_hash = NULL; |
| 1956 | |
| 1957 | if (ftrace_hash_empty(new_hash)) |
| 1958 | new_hash = NULL; |
| 1959 | |
| 1960 | return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash); |
| 1961 | } |
| 1962 | |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1963 | static void print_ip_ins(const char *fmt, const unsigned char *p) |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 1964 | { |
| 1965 | int i; |
| 1966 | |
| 1967 | printk(KERN_CONT "%s", fmt); |
| 1968 | |
| 1969 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 1970 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 1971 | } |
| 1972 | |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1973 | static struct ftrace_ops * |
| 1974 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 1975 | static struct ftrace_ops * |
| 1976 | 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] | 1977 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1978 | enum ftrace_bug_type ftrace_bug_type; |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1979 | const void *ftrace_expected; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1980 | |
| 1981 | static void print_bug_type(void) |
| 1982 | { |
| 1983 | switch (ftrace_bug_type) { |
| 1984 | case FTRACE_BUG_UNKNOWN: |
| 1985 | break; |
| 1986 | case FTRACE_BUG_INIT: |
| 1987 | pr_info("Initializing ftrace call sites\n"); |
| 1988 | break; |
| 1989 | case FTRACE_BUG_NOP: |
| 1990 | pr_info("Setting ftrace call site to NOP\n"); |
| 1991 | break; |
| 1992 | case FTRACE_BUG_CALL: |
| 1993 | pr_info("Setting ftrace call site to call ftrace function\n"); |
| 1994 | break; |
| 1995 | case FTRACE_BUG_UPDATE: |
| 1996 | pr_info("Updating ftrace call site to call a different ftrace function\n"); |
| 1997 | break; |
| 1998 | } |
| 1999 | } |
| 2000 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2001 | /** |
| 2002 | * ftrace_bug - report and shutdown function tracer |
| 2003 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2004 | * @rec: The record that failed |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2005 | * |
| 2006 | * The arch code that enables or disables the function tracing |
| 2007 | * can call ftrace_bug() when it has detected a problem in |
| 2008 | * modifying the code. @failed should be one of either: |
| 2009 | * EFAULT - if the problem happens on reading the @ip address |
| 2010 | * EINVAL - if what is read at @ip is not what was expected |
| 2011 | * EPERM - if the problem happens on writting to the @ip address |
| 2012 | */ |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2013 | void ftrace_bug(int failed, struct dyn_ftrace *rec) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2014 | { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2015 | unsigned long ip = rec ? rec->ip : 0; |
| 2016 | |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2017 | switch (failed) { |
| 2018 | case -EFAULT: |
| 2019 | FTRACE_WARN_ON_ONCE(1); |
| 2020 | pr_info("ftrace faulted on modifying "); |
| 2021 | print_ip_sym(ip); |
| 2022 | break; |
| 2023 | case -EINVAL: |
| 2024 | FTRACE_WARN_ON_ONCE(1); |
| 2025 | pr_info("ftrace failed to modify "); |
| 2026 | print_ip_sym(ip); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2027 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2028 | pr_cont("\n"); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2029 | if (ftrace_expected) { |
| 2030 | print_ip_ins(" expected: ", ftrace_expected); |
| 2031 | pr_cont("\n"); |
| 2032 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2033 | break; |
| 2034 | case -EPERM: |
| 2035 | FTRACE_WARN_ON_ONCE(1); |
| 2036 | pr_info("ftrace faulted on writing "); |
| 2037 | print_ip_sym(ip); |
| 2038 | break; |
| 2039 | default: |
| 2040 | FTRACE_WARN_ON_ONCE(1); |
| 2041 | pr_info("ftrace faulted on unknown error "); |
| 2042 | print_ip_sym(ip); |
| 2043 | } |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2044 | print_bug_type(); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2045 | if (rec) { |
| 2046 | struct ftrace_ops *ops = NULL; |
| 2047 | |
| 2048 | pr_info("ftrace record flags: %lx\n", rec->flags); |
| 2049 | pr_cont(" (%ld)%s", ftrace_rec_count(rec), |
| 2050 | rec->flags & FTRACE_FL_REGS ? " R" : " "); |
| 2051 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2052 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2053 | if (ops) { |
| 2054 | do { |
| 2055 | pr_cont("\ttramp: %pS (%pS)", |
| 2056 | (void *)ops->trampoline, |
| 2057 | (void *)ops->func); |
| 2058 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 2059 | } while (ops); |
| 2060 | } else |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2061 | pr_cont("\ttramp: ERROR!"); |
| 2062 | |
| 2063 | } |
| 2064 | ip = ftrace_get_addr_curr(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2065 | pr_cont("\n expected tramp: %lx\n", ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2066 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2069 | 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] | 2070 | { |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2071 | unsigned long flag = 0UL; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 2072 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2073 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2074 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2075 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2076 | return FTRACE_UPDATE_IGNORE; |
| 2077 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2078 | /* |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2079 | * If we are updating calls: |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2080 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2081 | * If the record has a ref count, then we need to enable it |
| 2082 | * because someone is using it. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2083 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2084 | * Otherwise we make sure its disabled. |
| 2085 | * |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2086 | * If we are disabling calls, then disable all records that |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2087 | * are enabled. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2088 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2089 | if (enable && ftrace_rec_count(rec)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2090 | flag = FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2091 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2092 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2093 | * If enabling and the REGS flag does not match the REGS_EN, or |
| 2094 | * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore |
| 2095 | * this record. Set flags to fail the compare against ENABLED. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2096 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2097 | if (flag) { |
| 2098 | if (!(rec->flags & FTRACE_FL_REGS) != |
| 2099 | !(rec->flags & FTRACE_FL_REGS_EN)) |
| 2100 | flag |= FTRACE_FL_REGS; |
| 2101 | |
| 2102 | if (!(rec->flags & FTRACE_FL_TRAMP) != |
| 2103 | !(rec->flags & FTRACE_FL_TRAMP_EN)) |
| 2104 | flag |= FTRACE_FL_TRAMP; |
| 2105 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2106 | |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2107 | /* If the state of this record hasn't changed, then do nothing */ |
| 2108 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2109 | return FTRACE_UPDATE_IGNORE; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2110 | |
| 2111 | if (flag) { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2112 | /* Save off if rec is being enabled (for return value) */ |
| 2113 | flag ^= rec->flags & FTRACE_FL_ENABLED; |
| 2114 | |
| 2115 | if (update) { |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2116 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2117 | if (flag & FTRACE_FL_REGS) { |
| 2118 | if (rec->flags & FTRACE_FL_REGS) |
| 2119 | rec->flags |= FTRACE_FL_REGS_EN; |
| 2120 | else |
| 2121 | rec->flags &= ~FTRACE_FL_REGS_EN; |
| 2122 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2123 | if (flag & FTRACE_FL_TRAMP) { |
| 2124 | if (rec->flags & FTRACE_FL_TRAMP) |
| 2125 | rec->flags |= FTRACE_FL_TRAMP_EN; |
| 2126 | else |
| 2127 | rec->flags &= ~FTRACE_FL_TRAMP_EN; |
| 2128 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /* |
| 2132 | * If this record is being updated from a nop, then |
| 2133 | * return UPDATE_MAKE_CALL. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2134 | * Otherwise, |
| 2135 | * return UPDATE_MODIFY_CALL to tell the caller to convert |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2136 | * 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] | 2137 | * vice versa, or from a trampoline call. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2138 | */ |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2139 | if (flag & FTRACE_FL_ENABLED) { |
| 2140 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2141 | return FTRACE_UPDATE_MAKE_CALL; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2142 | } |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2143 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2144 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2145 | return FTRACE_UPDATE_MODIFY_CALL; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2148 | if (update) { |
| 2149 | /* If there's no more users, clear all flags */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2150 | if (!ftrace_rec_count(rec)) |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2151 | rec->flags = 0; |
| 2152 | else |
Steven Rostedt (Red Hat) | b24d443 | 2015-03-04 23:10:28 -0500 | [diff] [blame] | 2153 | /* |
| 2154 | * Just disable the record, but keep the ops TRAMP |
| 2155 | * and REGS states. The _EN flags must be disabled though. |
| 2156 | */ |
| 2157 | rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN | |
| 2158 | FTRACE_FL_REGS_EN); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2159 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2160 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2161 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2162 | return FTRACE_UPDATE_MAKE_NOP; |
| 2163 | } |
| 2164 | |
| 2165 | /** |
| 2166 | * ftrace_update_record, set a record that now is tracing or not |
| 2167 | * @rec: the record to update |
| 2168 | * @enable: set to 1 if the record is tracing, zero to force disable |
| 2169 | * |
| 2170 | * The records that represent all functions that can be traced need |
| 2171 | * to be updated when tracing has been enabled. |
| 2172 | */ |
| 2173 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) |
| 2174 | { |
| 2175 | return ftrace_check_record(rec, enable, 1); |
| 2176 | } |
| 2177 | |
| 2178 | /** |
| 2179 | * ftrace_test_record, check if the record has been enabled or not |
| 2180 | * @rec: the record to test |
| 2181 | * @enable: set to 1 to check if enabled, 0 if it is disabled |
| 2182 | * |
| 2183 | * The arch code may need to test if a record is already set to |
| 2184 | * tracing to determine how to modify the function code that it |
| 2185 | * represents. |
| 2186 | */ |
| 2187 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) |
| 2188 | { |
| 2189 | return ftrace_check_record(rec, enable, 0); |
| 2190 | } |
| 2191 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2192 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2193 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) |
| 2194 | { |
| 2195 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2196 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2197 | |
| 2198 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2199 | |
| 2200 | if (!op->trampoline) |
| 2201 | continue; |
| 2202 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2203 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2204 | return op; |
| 2205 | } while_for_each_ftrace_op(op); |
| 2206 | |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | |
| 2210 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2211 | ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, |
| 2212 | struct ftrace_ops *op) |
| 2213 | { |
| 2214 | unsigned long ip = rec->ip; |
| 2215 | |
| 2216 | while_for_each_ftrace_op(op) { |
| 2217 | |
| 2218 | if (!op->trampoline) |
| 2219 | continue; |
| 2220 | |
| 2221 | if (hash_contains_ip(ip, op->func_hash)) |
| 2222 | return op; |
| 2223 | } |
| 2224 | |
| 2225 | return NULL; |
| 2226 | } |
| 2227 | |
| 2228 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2229 | ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) |
| 2230 | { |
| 2231 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2232 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2233 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2234 | /* |
| 2235 | * Need to check removed ops first. |
| 2236 | * If they are being removed, and this rec has a tramp, |
| 2237 | * and this rec is in the ops list, then it would be the |
| 2238 | * one with the tramp. |
| 2239 | */ |
| 2240 | if (removed_ops) { |
| 2241 | if (hash_contains_ip(ip, &removed_ops->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2242 | return removed_ops; |
| 2243 | } |
| 2244 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2245 | /* |
| 2246 | * Need to find the current trampoline for a rec. |
| 2247 | * Now, a trampoline is only attached to a rec if there |
| 2248 | * was a single 'ops' attached to it. But this can be called |
| 2249 | * when we are adding another op to the rec or removing the |
| 2250 | * current one. Thus, if the op is being added, we can |
| 2251 | * 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] | 2252 | * yet. |
| 2253 | * |
| 2254 | * If an ops is being modified (hooking to different functions) |
| 2255 | * then we don't care about the new functions that are being |
| 2256 | * added, just the old ones (that are probably being removed). |
| 2257 | * |
| 2258 | * If we are adding an ops to a function that already is using |
| 2259 | * a trampoline, it needs to be removed (trampolines are only |
| 2260 | * for single ops connected), then an ops that is not being |
| 2261 | * modified also needs to be checked. |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2262 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2263 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2264 | |
| 2265 | if (!op->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2266 | continue; |
| 2267 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2268 | /* |
| 2269 | * If the ops is being added, it hasn't gotten to |
| 2270 | * the point to be removed from this tree yet. |
| 2271 | */ |
| 2272 | if (op->flags & FTRACE_OPS_FL_ADDING) |
| 2273 | continue; |
| 2274 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2275 | |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2276 | /* |
| 2277 | * If the ops is being modified and is in the old |
| 2278 | * hash, then it is probably being removed from this |
| 2279 | * function. |
| 2280 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2281 | if ((op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2282 | hash_contains_ip(ip, &op->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2283 | return op; |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2284 | /* |
| 2285 | * If the ops is not being added or modified, and it's |
| 2286 | * in its normal filter hash, then this must be the one |
| 2287 | * we want! |
| 2288 | */ |
| 2289 | if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2290 | hash_contains_ip(ip, op->func_hash)) |
| 2291 | return op; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2292 | |
| 2293 | } while_for_each_ftrace_op(op); |
| 2294 | |
| 2295 | return NULL; |
| 2296 | } |
| 2297 | |
| 2298 | static struct ftrace_ops * |
| 2299 | ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) |
| 2300 | { |
| 2301 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2302 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2303 | |
| 2304 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2305 | /* pass rec in as regs to have non-NULL val */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2306 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2307 | return op; |
| 2308 | } while_for_each_ftrace_op(op); |
| 2309 | |
| 2310 | return NULL; |
| 2311 | } |
| 2312 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2313 | /** |
| 2314 | * ftrace_get_addr_new - Get the call address to set to |
| 2315 | * @rec: The ftrace record descriptor |
| 2316 | * |
| 2317 | * If the record has the FTRACE_FL_REGS set, that means that it |
| 2318 | * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS |
| 2319 | * is not not set, then it wants to convert to the normal callback. |
| 2320 | * |
| 2321 | * Returns the address of the trampoline to set to |
| 2322 | */ |
| 2323 | unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) |
| 2324 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2325 | struct ftrace_ops *ops; |
| 2326 | |
| 2327 | /* Trampolines take precedence over regs */ |
| 2328 | if (rec->flags & FTRACE_FL_TRAMP) { |
| 2329 | ops = ftrace_find_tramp_ops_new(rec); |
| 2330 | if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 2331 | pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n", |
| 2332 | (void *)rec->ip, (void *)rec->ip, rec->flags); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2333 | /* Ftrace is shutting down, return anything */ |
| 2334 | return (unsigned long)FTRACE_ADDR; |
| 2335 | } |
| 2336 | return ops->trampoline; |
| 2337 | } |
| 2338 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2339 | if (rec->flags & FTRACE_FL_REGS) |
| 2340 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2341 | else |
| 2342 | return (unsigned long)FTRACE_ADDR; |
| 2343 | } |
| 2344 | |
| 2345 | /** |
| 2346 | * ftrace_get_addr_curr - Get the call address that is already there |
| 2347 | * @rec: The ftrace record descriptor |
| 2348 | * |
| 2349 | * The FTRACE_FL_REGS_EN is set when the record already points to |
| 2350 | * a function that saves all the regs. Basically the '_EN' version |
| 2351 | * represents the current state of the function. |
| 2352 | * |
| 2353 | * Returns the address of the trampoline that is currently being called |
| 2354 | */ |
| 2355 | unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) |
| 2356 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2357 | struct ftrace_ops *ops; |
| 2358 | |
| 2359 | /* Trampolines take precedence over regs */ |
| 2360 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2361 | ops = ftrace_find_tramp_ops_curr(rec); |
| 2362 | if (FTRACE_WARN_ON(!ops)) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 2363 | pr_warn("Bad trampoline accounting at: %p (%pS)\n", |
| 2364 | (void *)rec->ip, (void *)rec->ip); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2365 | /* Ftrace is shutting down, return anything */ |
| 2366 | return (unsigned long)FTRACE_ADDR; |
| 2367 | } |
| 2368 | return ops->trampoline; |
| 2369 | } |
| 2370 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2371 | if (rec->flags & FTRACE_FL_REGS_EN) |
| 2372 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2373 | else |
| 2374 | return (unsigned long)FTRACE_ADDR; |
| 2375 | } |
| 2376 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2377 | static int |
| 2378 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
| 2379 | { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2380 | unsigned long ftrace_old_addr; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2381 | unsigned long ftrace_addr; |
| 2382 | int ret; |
| 2383 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2384 | ftrace_addr = ftrace_get_addr_new(rec); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2385 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2386 | /* This needs to be done before we call ftrace_update_record */ |
| 2387 | ftrace_old_addr = ftrace_get_addr_curr(rec); |
| 2388 | |
| 2389 | ret = ftrace_update_record(rec, enable); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2390 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2391 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2392 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2393 | switch (ret) { |
| 2394 | case FTRACE_UPDATE_IGNORE: |
| 2395 | return 0; |
| 2396 | |
| 2397 | case FTRACE_UPDATE_MAKE_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2398 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2399 | return ftrace_make_call(rec, ftrace_addr); |
| 2400 | |
| 2401 | case FTRACE_UPDATE_MAKE_NOP: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2402 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt (Red Hat) | 39b5552 | 2014-08-17 20:59:10 -0400 | [diff] [blame] | 2403 | return ftrace_make_nop(NULL, rec, ftrace_old_addr); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2404 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2405 | case FTRACE_UPDATE_MODIFY_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2406 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2407 | return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | return -1; /* unknow ftrace bug */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2413 | void __weak ftrace_replace_code(int enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2414 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2415 | struct dyn_ftrace *rec; |
| 2416 | struct ftrace_page *pg; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2417 | int failed; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2418 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2419 | if (unlikely(ftrace_disabled)) |
| 2420 | return; |
| 2421 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2422 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 2423 | |
| 2424 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2425 | continue; |
| 2426 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2427 | failed = __ftrace_replace_code(rec, enable); |
Zhaolei | fa9d13c | 2009-03-13 17:16:34 +0800 | [diff] [blame] | 2428 | if (failed) { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2429 | ftrace_bug(failed, rec); |
Steven Rostedt | 3279ba3 | 2009-10-07 16:57:56 -0400 | [diff] [blame] | 2430 | /* Stop processing */ |
| 2431 | return; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2432 | } |
| 2433 | } while_for_each_ftrace_rec(); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2436 | struct ftrace_rec_iter { |
| 2437 | struct ftrace_page *pg; |
| 2438 | int index; |
| 2439 | }; |
| 2440 | |
| 2441 | /** |
| 2442 | * ftrace_rec_iter_start, start up iterating over traced functions |
| 2443 | * |
| 2444 | * Returns an iterator handle that is used to iterate over all |
| 2445 | * the records that represent address locations where functions |
| 2446 | * are traced. |
| 2447 | * |
| 2448 | * May return NULL if no records are available. |
| 2449 | */ |
| 2450 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 2451 | { |
| 2452 | /* |
| 2453 | * We only use a single iterator. |
| 2454 | * Protected by the ftrace_lock mutex. |
| 2455 | */ |
| 2456 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 2457 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 2458 | |
| 2459 | iter->pg = ftrace_pages_start; |
| 2460 | iter->index = 0; |
| 2461 | |
| 2462 | /* Could have empty pages */ |
| 2463 | while (iter->pg && !iter->pg->index) |
| 2464 | iter->pg = iter->pg->next; |
| 2465 | |
| 2466 | if (!iter->pg) |
| 2467 | return NULL; |
| 2468 | |
| 2469 | return iter; |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * ftrace_rec_iter_next, get the next record to process. |
| 2474 | * @iter: The handle to the iterator. |
| 2475 | * |
| 2476 | * Returns the next iterator after the given iterator @iter. |
| 2477 | */ |
| 2478 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 2479 | { |
| 2480 | iter->index++; |
| 2481 | |
| 2482 | if (iter->index >= iter->pg->index) { |
| 2483 | iter->pg = iter->pg->next; |
| 2484 | iter->index = 0; |
| 2485 | |
| 2486 | /* Could have empty pages */ |
| 2487 | while (iter->pg && !iter->pg->index) |
| 2488 | iter->pg = iter->pg->next; |
| 2489 | } |
| 2490 | |
| 2491 | if (!iter->pg) |
| 2492 | return NULL; |
| 2493 | |
| 2494 | return iter; |
| 2495 | } |
| 2496 | |
| 2497 | /** |
| 2498 | * ftrace_rec_iter_record, get the record at the iterator location |
| 2499 | * @iter: The current iterator location |
| 2500 | * |
| 2501 | * Returns the record that the current @iter is at. |
| 2502 | */ |
| 2503 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 2504 | { |
| 2505 | return &iter->pg->records[iter->index]; |
| 2506 | } |
| 2507 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2508 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2509 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2510 | { |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2511 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2512 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2513 | if (unlikely(ftrace_disabled)) |
| 2514 | return 0; |
| 2515 | |
Shaohua Li | 25aac9d | 2009-01-09 11:29:40 +0800 | [diff] [blame] | 2516 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2517 | if (ret) { |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2518 | ftrace_bug_type = FTRACE_BUG_INIT; |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2519 | ftrace_bug(ret, rec); |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2520 | return 0; |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2521 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2522 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2523 | } |
| 2524 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2525 | /* |
| 2526 | * archs can override this function if they must do something |
| 2527 | * before the modifying code is performed. |
| 2528 | */ |
| 2529 | int __weak ftrace_arch_code_modify_prepare(void) |
| 2530 | { |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
| 2534 | /* |
| 2535 | * archs can override this function if they must do something |
| 2536 | * after the modifying code is performed. |
| 2537 | */ |
| 2538 | int __weak ftrace_arch_code_modify_post_process(void) |
| 2539 | { |
| 2540 | return 0; |
| 2541 | } |
| 2542 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2543 | void ftrace_modify_all_code(int command) |
| 2544 | { |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2545 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2546 | int err = 0; |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2547 | |
| 2548 | /* |
| 2549 | * If the ftrace_caller calls a ftrace_ops func directly, |
| 2550 | * we need to make sure that it only traces functions it |
| 2551 | * expects to trace. When doing the switch of functions, |
| 2552 | * we need to update to the ftrace_ops_list_func first |
| 2553 | * before the transition between old and new calls are set, |
| 2554 | * as the ftrace_ops_list_func will check the ops hashes |
| 2555 | * to make sure the ops are having the right functions |
| 2556 | * traced. |
| 2557 | */ |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2558 | if (update) { |
| 2559 | err = ftrace_update_ftrace_func(ftrace_ops_list_func); |
| 2560 | if (FTRACE_WARN_ON(err)) |
| 2561 | return; |
| 2562 | } |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2563 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2564 | if (command & FTRACE_UPDATE_CALLS) |
| 2565 | ftrace_replace_code(1); |
| 2566 | else if (command & FTRACE_DISABLE_CALLS) |
| 2567 | ftrace_replace_code(0); |
| 2568 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2569 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
| 2570 | function_trace_op = set_function_trace_op; |
| 2571 | smp_wmb(); |
| 2572 | /* If irqs are disabled, we are in stop machine */ |
| 2573 | if (!irqs_disabled()) |
| 2574 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2575 | err = ftrace_update_ftrace_func(ftrace_trace_function); |
| 2576 | if (FTRACE_WARN_ON(err)) |
| 2577 | return; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2578 | } |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2579 | |
| 2580 | if (command & FTRACE_START_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2581 | err = ftrace_enable_ftrace_graph_caller(); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2582 | else if (command & FTRACE_STOP_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2583 | err = ftrace_disable_ftrace_graph_caller(); |
| 2584 | FTRACE_WARN_ON(err); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2585 | } |
| 2586 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2587 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2588 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2589 | int *command = data; |
| 2590 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2591 | ftrace_modify_all_code(*command); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 2592 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2593 | return 0; |
| 2594 | } |
| 2595 | |
| 2596 | /** |
| 2597 | * ftrace_run_stop_machine, go back to the stop machine method |
| 2598 | * @command: The command to tell ftrace what to do |
| 2599 | * |
| 2600 | * If an arch needs to fall back to the stop machine method, the |
| 2601 | * it can call this function. |
| 2602 | */ |
| 2603 | void ftrace_run_stop_machine(int command) |
| 2604 | { |
| 2605 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 2606 | } |
| 2607 | |
| 2608 | /** |
| 2609 | * arch_ftrace_update_code, modify the code to trace or not trace |
| 2610 | * @command: The command that needs to be done |
| 2611 | * |
| 2612 | * Archs can override this function if it does not need to |
| 2613 | * run stop_machine() to modify code. |
| 2614 | */ |
| 2615 | void __weak arch_ftrace_update_code(int command) |
| 2616 | { |
| 2617 | ftrace_run_stop_machine(command); |
| 2618 | } |
| 2619 | |
| 2620 | static void ftrace_run_update_code(int command) |
| 2621 | { |
| 2622 | int ret; |
| 2623 | |
| 2624 | ret = ftrace_arch_code_modify_prepare(); |
| 2625 | FTRACE_WARN_ON(ret); |
| 2626 | if (ret) |
| 2627 | return; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2628 | |
| 2629 | /* |
| 2630 | * By default we use stop_machine() to modify the code. |
| 2631 | * But archs can do what ever they want as long as it |
| 2632 | * is safe. The stop_machine() is the safest, but also |
| 2633 | * produces the most overhead. |
| 2634 | */ |
| 2635 | arch_ftrace_update_code(command); |
| 2636 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2637 | ret = ftrace_arch_code_modify_post_process(); |
| 2638 | FTRACE_WARN_ON(ret); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2639 | } |
| 2640 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2641 | 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] | 2642 | struct ftrace_ops_hash *old_hash) |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2643 | { |
| 2644 | ops->flags |= FTRACE_OPS_FL_MODIFYING; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2645 | ops->old_hash.filter_hash = old_hash->filter_hash; |
| 2646 | ops->old_hash.notrace_hash = old_hash->notrace_hash; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2647 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2648 | ops->old_hash.filter_hash = NULL; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2649 | ops->old_hash.notrace_hash = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2650 | ops->flags &= ~FTRACE_OPS_FL_MODIFYING; |
| 2651 | } |
| 2652 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2653 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2654 | static int ftrace_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2655 | |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2656 | void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) |
| 2657 | { |
| 2658 | } |
| 2659 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2660 | static void per_cpu_ops_free(struct ftrace_ops *ops) |
Jiri Slaby | db0fbad | 2014-03-10 21:42:11 +0100 | [diff] [blame] | 2661 | { |
| 2662 | free_percpu(ops->disabled); |
| 2663 | } |
| 2664 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2665 | static void ftrace_startup_enable(int command) |
| 2666 | { |
| 2667 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2668 | saved_ftrace_func = ftrace_trace_function; |
| 2669 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2670 | } |
| 2671 | |
| 2672 | if (!command || !ftrace_enabled) |
| 2673 | return; |
| 2674 | |
| 2675 | ftrace_run_update_code(command); |
| 2676 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2677 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2678 | static void ftrace_startup_all(int command) |
| 2679 | { |
| 2680 | update_all_ops = true; |
| 2681 | ftrace_startup_enable(command); |
| 2682 | update_all_ops = false; |
| 2683 | } |
| 2684 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2685 | static int ftrace_startup(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2686 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2687 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2688 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2689 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2690 | return -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2691 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2692 | ret = __register_ftrace_function(ops); |
| 2693 | if (ret) |
| 2694 | return ret; |
| 2695 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2696 | ftrace_start_up++; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2697 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2698 | /* |
| 2699 | * Note that ftrace probes uses this to start up |
| 2700 | * and modify functions it will probe. But we still |
| 2701 | * set the ADDING flag for modification, as probes |
| 2702 | * do not have trampolines. If they add them in the |
| 2703 | * future, then the probes will need to distinguish |
| 2704 | * between adding and updating probes. |
| 2705 | */ |
| 2706 | ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; |
Steven Rostedt (Red Hat) | 66209a5 | 2014-05-06 21:57:49 -0400 | [diff] [blame] | 2707 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2708 | ret = ftrace_hash_ipmodify_enable(ops); |
| 2709 | if (ret < 0) { |
| 2710 | /* Rollback registration process */ |
| 2711 | __unregister_ftrace_function(ops); |
| 2712 | ftrace_start_up--; |
| 2713 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 2714 | return ret; |
| 2715 | } |
| 2716 | |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2717 | if (ftrace_hash_rec_enable(ops, 1)) |
| 2718 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2719 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2720 | ftrace_startup_enable(command); |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2721 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2722 | ops->flags &= ~FTRACE_OPS_FL_ADDING; |
| 2723 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2724 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2725 | } |
| 2726 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2727 | static int ftrace_shutdown(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2728 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2729 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2730 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2731 | if (unlikely(ftrace_disabled)) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2732 | return -ENODEV; |
| 2733 | |
| 2734 | ret = __unregister_ftrace_function(ops); |
| 2735 | if (ret) |
| 2736 | return ret; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2737 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2738 | ftrace_start_up--; |
Frederic Weisbecker | 9ea1a15 | 2009-06-20 06:52:21 +0200 | [diff] [blame] | 2739 | /* |
| 2740 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 2741 | * critical but the ftrace_call callers may be never nopped again after |
| 2742 | * further ftrace uses. |
| 2743 | */ |
| 2744 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 2745 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2746 | /* Disabling ipmodify never fails */ |
| 2747 | ftrace_hash_ipmodify_disable(ops); |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2748 | |
| 2749 | if (ftrace_hash_rec_disable(ops, 1)) |
| 2750 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2751 | |
Namhyung Kim | a737e6d | 2014-06-12 23:56:12 +0900 | [diff] [blame] | 2752 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2753 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2754 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2755 | saved_ftrace_func = ftrace_trace_function; |
| 2756 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2757 | } |
| 2758 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2759 | if (!command || !ftrace_enabled) { |
| 2760 | /* |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2761 | * If these are per_cpu ops, they still need their |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2762 | * per_cpu field freed. Since, function tracing is |
| 2763 | * not currently active, we can just free them |
| 2764 | * without synchronizing all CPUs. |
| 2765 | */ |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2766 | if (ops->flags & FTRACE_OPS_FL_PER_CPU) |
| 2767 | per_cpu_ops_free(ops); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2768 | return 0; |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2769 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2770 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2771 | /* |
| 2772 | * If the ops uses a trampoline, then it needs to be |
| 2773 | * tested first on update. |
| 2774 | */ |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2775 | ops->flags |= FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2776 | removed_ops = ops; |
| 2777 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2778 | /* The trampoline logic checks the old hashes */ |
| 2779 | ops->old_hash.filter_hash = ops->func_hash->filter_hash; |
| 2780 | ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; |
| 2781 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2782 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2783 | |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2784 | /* |
| 2785 | * If there's no more ops registered with ftrace, run a |
| 2786 | * sanity check to make sure all rec flags are cleared. |
| 2787 | */ |
| 2788 | if (ftrace_ops_list == &ftrace_list_end) { |
| 2789 | struct ftrace_page *pg; |
| 2790 | struct dyn_ftrace *rec; |
| 2791 | |
| 2792 | do_for_each_ftrace_rec(pg, rec) { |
Alexei Starovoitov | 977c1f9 | 2016-11-07 15:14:20 -0800 | [diff] [blame] | 2793 | if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED)) |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2794 | pr_warn(" %pS flags:%lx\n", |
| 2795 | (void *)rec->ip, rec->flags); |
| 2796 | } while_for_each_ftrace_rec(); |
| 2797 | } |
| 2798 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2799 | ops->old_hash.filter_hash = NULL; |
| 2800 | ops->old_hash.notrace_hash = NULL; |
| 2801 | |
| 2802 | removed_ops = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2803 | ops->flags &= ~FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2804 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2805 | /* |
| 2806 | * Dynamic ops may be freed, we must make sure that all |
| 2807 | * callers are done before leaving this function. |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2808 | * 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] | 2809 | * ops. |
| 2810 | * |
| 2811 | * Again, normal synchronize_sched() is not good enough. |
| 2812 | * We need to do a hard force of sched synchronization. |
| 2813 | * This is because we use preempt_disable() to do RCU, but |
| 2814 | * the function tracers can be called where RCU is not watching |
| 2815 | * (like before user_exit()). We can not rely on the RCU |
| 2816 | * infrastructure to do the synchronization, thus we must do it |
| 2817 | * ourselves. |
| 2818 | */ |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2819 | 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] | 2820 | schedule_on_each_cpu(ftrace_sync); |
| 2821 | |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2822 | arch_ftrace_trampoline_free(ops); |
| 2823 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2824 | if (ops->flags & FTRACE_OPS_FL_PER_CPU) |
| 2825 | per_cpu_ops_free(ops); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2826 | } |
| 2827 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2828 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2829 | } |
| 2830 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2831 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2832 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2833 | int command; |
| 2834 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2835 | if (unlikely(ftrace_disabled)) |
| 2836 | return; |
| 2837 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2838 | /* Force update next time */ |
| 2839 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2840 | /* ftrace_start_up is true if we want ftrace running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2841 | if (ftrace_start_up) { |
| 2842 | command = FTRACE_UPDATE_CALLS; |
| 2843 | if (ftrace_graph_active) |
| 2844 | command |= FTRACE_START_FUNC_RET; |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 2845 | ftrace_startup_enable(command); |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2846 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2849 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2850 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2851 | int command; |
| 2852 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2853 | if (unlikely(ftrace_disabled)) |
| 2854 | return; |
| 2855 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2856 | /* ftrace_start_up is true if ftrace is running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2857 | if (ftrace_start_up) { |
| 2858 | command = FTRACE_DISABLE_CALLS; |
| 2859 | if (ftrace_graph_active) |
| 2860 | command |= FTRACE_STOP_FUNC_RET; |
| 2861 | ftrace_run_update_code(command); |
| 2862 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2863 | } |
| 2864 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2865 | static u64 ftrace_update_time; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2866 | unsigned long ftrace_update_tot_cnt; |
| 2867 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2868 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
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 | * Filter_hash being empty will default to trace module. |
| 2872 | * But notrace hash requires a test of individual module functions. |
| 2873 | */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2874 | return ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2875 | ftrace_hash_empty(ops->func_hash->notrace_hash); |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2876 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2877 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2878 | /* |
| 2879 | * Check if the current ops references the record. |
| 2880 | * |
| 2881 | * If the ops traces all functions, then it was already accounted for. |
| 2882 | * If the ops does not trace the current record function, skip it. |
| 2883 | * If the ops ignores the function via notrace filter, skip it. |
| 2884 | */ |
| 2885 | static inline bool |
| 2886 | ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 2887 | { |
| 2888 | /* If ops isn't enabled, ignore it */ |
| 2889 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 2890 | return 0; |
| 2891 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2892 | /* If ops traces all then it includes this function */ |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2893 | if (ops_traces_mod(ops)) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2894 | return 1; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2895 | |
| 2896 | /* The function must be in the filter */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2897 | if (!ftrace_hash_empty(ops->func_hash->filter_hash) && |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 2898 | !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2899 | return 0; |
| 2900 | |
| 2901 | /* If in notrace hash, we ignore it too */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2902 | 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] | 2903 | return 0; |
| 2904 | |
| 2905 | return 1; |
| 2906 | } |
| 2907 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2908 | 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] | 2909 | { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2910 | struct ftrace_page *pg; |
Lai Jiangshan | e94142a | 2009-03-13 17:51:27 +0800 | [diff] [blame] | 2911 | struct dyn_ftrace *p; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2912 | u64 start, stop; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2913 | unsigned long update_cnt = 0; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2914 | unsigned long rec_flags = 0; |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2915 | int i; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2916 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2917 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2918 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2919 | /* |
| 2920 | * When a module is loaded, this function is called to convert |
| 2921 | * the calls to mcount in its text to nops, and also to create |
| 2922 | * an entry in the ftrace data. Now, if ftrace is activated |
| 2923 | * after this call, but before the module sets its text to |
| 2924 | * read-only, the modification of enabling ftrace can fail if |
| 2925 | * the read-only is done while ftrace is converting the calls. |
| 2926 | * To prevent this, the module's records are set as disabled |
| 2927 | * and will be enabled after the call to set the module's text |
| 2928 | * to read-only. |
| 2929 | */ |
| 2930 | if (mod) |
| 2931 | rec_flags |= FTRACE_FL_DISABLED; |
| 2932 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2933 | for (pg = new_pgs; pg; pg = pg->next) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2934 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2935 | for (i = 0; i < pg->index; i++) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2936 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2937 | /* If something went wrong, bail without enabling anything */ |
| 2938 | if (unlikely(ftrace_disabled)) |
| 2939 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2940 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2941 | p = &pg->records[i]; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2942 | p->flags = rec_flags; |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 2943 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2944 | /* |
| 2945 | * Do the initial record conversion from mcount jump |
| 2946 | * to the NOP instructions. |
| 2947 | */ |
| 2948 | if (!ftrace_code_disable(mod, p)) |
| 2949 | break; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2950 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2951 | update_cnt++; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2952 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2953 | } |
| 2954 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2955 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2956 | ftrace_update_time = stop - start; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2957 | ftrace_update_tot_cnt += update_cnt; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2958 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2959 | return 0; |
| 2960 | } |
| 2961 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2962 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2963 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2964 | int order; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2965 | int cnt; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2966 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2967 | if (WARN_ON(!count)) |
| 2968 | return -EINVAL; |
| 2969 | |
| 2970 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2971 | |
| 2972 | /* |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2973 | * We want to fill as much as possible. No more than a page |
| 2974 | * may be empty. |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2975 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2976 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
| 2977 | order--; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2978 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2979 | again: |
| 2980 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 2981 | |
| 2982 | if (!pg->records) { |
| 2983 | /* if we can't allocate this size, try something smaller */ |
| 2984 | if (!order) |
| 2985 | return -ENOMEM; |
| 2986 | order >>= 1; |
| 2987 | goto again; |
| 2988 | } |
| 2989 | |
| 2990 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 2991 | pg->size = cnt; |
| 2992 | |
| 2993 | if (cnt > count) |
| 2994 | cnt = count; |
| 2995 | |
| 2996 | return cnt; |
| 2997 | } |
| 2998 | |
| 2999 | static struct ftrace_page * |
| 3000 | ftrace_allocate_pages(unsigned long num_to_init) |
| 3001 | { |
| 3002 | struct ftrace_page *start_pg; |
| 3003 | struct ftrace_page *pg; |
| 3004 | int order; |
| 3005 | int cnt; |
| 3006 | |
| 3007 | if (!num_to_init) |
| 3008 | return 0; |
| 3009 | |
| 3010 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3011 | if (!pg) |
| 3012 | return NULL; |
| 3013 | |
| 3014 | /* |
| 3015 | * Try to allocate as much as possible in one continues |
| 3016 | * location that fills in all of the space. We want to |
| 3017 | * waste as little space as possible. |
| 3018 | */ |
| 3019 | for (;;) { |
| 3020 | cnt = ftrace_allocate_records(pg, num_to_init); |
| 3021 | if (cnt < 0) |
| 3022 | goto free_pages; |
| 3023 | |
| 3024 | num_to_init -= cnt; |
| 3025 | if (!num_to_init) |
| 3026 | break; |
| 3027 | |
| 3028 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3029 | if (!pg->next) |
| 3030 | goto free_pages; |
| 3031 | |
| 3032 | pg = pg->next; |
| 3033 | } |
| 3034 | |
| 3035 | return start_pg; |
| 3036 | |
| 3037 | free_pages: |
Namhyung Kim | 1f61be00 | 2014-06-11 17:06:53 +0900 | [diff] [blame] | 3038 | pg = start_pg; |
| 3039 | while (pg) { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3040 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 3041 | free_pages((unsigned long)pg->records, order); |
| 3042 | start_pg = pg->next; |
| 3043 | kfree(pg); |
| 3044 | pg = start_pg; |
| 3045 | } |
| 3046 | pr_info("ftrace: FAILED to allocate memory for functions\n"); |
| 3047 | return NULL; |
| 3048 | } |
| 3049 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3050 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 3051 | |
| 3052 | struct ftrace_iterator { |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3053 | loff_t pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3054 | loff_t func_pos; |
| 3055 | struct ftrace_page *pg; |
| 3056 | struct dyn_ftrace *func; |
| 3057 | struct ftrace_func_probe *probe; |
| 3058 | struct trace_parser parser; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3059 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3060 | struct ftrace_ops *ops; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3061 | int hidx; |
| 3062 | int idx; |
| 3063 | unsigned flags; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3064 | }; |
| 3065 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3066 | static void * |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3067 | t_hash_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3068 | { |
| 3069 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3070 | struct hlist_node *hnd = NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3071 | struct hlist_head *hhd; |
| 3072 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3073 | (*pos)++; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3074 | iter->pos = *pos; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3075 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3076 | if (iter->probe) |
| 3077 | hnd = &iter->probe->node; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3078 | retry: |
| 3079 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) |
| 3080 | return NULL; |
| 3081 | |
| 3082 | hhd = &ftrace_func_hash[iter->hidx]; |
| 3083 | |
| 3084 | if (hlist_empty(hhd)) { |
| 3085 | iter->hidx++; |
| 3086 | hnd = NULL; |
| 3087 | goto retry; |
| 3088 | } |
| 3089 | |
| 3090 | if (!hnd) |
| 3091 | hnd = hhd->first; |
| 3092 | else { |
| 3093 | hnd = hnd->next; |
| 3094 | if (!hnd) { |
| 3095 | iter->hidx++; |
| 3096 | goto retry; |
| 3097 | } |
| 3098 | } |
| 3099 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3100 | if (WARN_ON_ONCE(!hnd)) |
| 3101 | return NULL; |
| 3102 | |
| 3103 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); |
| 3104 | |
| 3105 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | static void *t_hash_start(struct seq_file *m, loff_t *pos) |
| 3109 | { |
| 3110 | struct ftrace_iterator *iter = m->private; |
| 3111 | void *p = NULL; |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3112 | loff_t l; |
| 3113 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3114 | if (!(iter->flags & FTRACE_ITER_DO_HASH)) |
| 3115 | return NULL; |
| 3116 | |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3117 | if (iter->func_pos > *pos) |
| 3118 | return NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3119 | |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3120 | iter->hidx = 0; |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3121 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3122 | p = t_hash_next(m, &l); |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3123 | if (!p) |
| 3124 | break; |
| 3125 | } |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3126 | if (!p) |
| 3127 | return NULL; |
| 3128 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3129 | /* Only set this if we have an item */ |
| 3130 | iter->flags |= FTRACE_ITER_HASH; |
| 3131 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3132 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3133 | } |
| 3134 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3135 | static int |
| 3136 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3137 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3138 | struct ftrace_func_probe *rec; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3139 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3140 | rec = iter->probe; |
| 3141 | if (WARN_ON_ONCE(!rec)) |
| 3142 | return -EIO; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3143 | |
Steven Rostedt | 809dcf2 | 2009-02-16 23:06:01 -0500 | [diff] [blame] | 3144 | if (rec->ops->print) |
| 3145 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); |
| 3146 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 3147 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3148 | |
| 3149 | if (rec->data) |
| 3150 | seq_printf(m, ":%p", rec->data); |
| 3151 | seq_putc(m, '\n'); |
| 3152 | |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | static void * |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3157 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 3158 | { |
| 3159 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3160 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3161 | struct dyn_ftrace *rec = NULL; |
| 3162 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3163 | if (unlikely(ftrace_disabled)) |
| 3164 | return NULL; |
| 3165 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3166 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3167 | return t_hash_next(m, pos); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3168 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3169 | (*pos)++; |
Jiri Olsa | 1106b69 | 2011-02-16 17:35:34 +0100 | [diff] [blame] | 3170 | iter->pos = iter->func_pos = *pos; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3171 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3172 | if (iter->flags & FTRACE_ITER_PRINTALL) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3173 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3174 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3175 | retry: |
| 3176 | if (iter->idx >= iter->pg->index) { |
| 3177 | if (iter->pg->next) { |
| 3178 | iter->pg = iter->pg->next; |
| 3179 | iter->idx = 0; |
| 3180 | goto retry; |
| 3181 | } |
| 3182 | } else { |
| 3183 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3184 | if (((iter->flags & FTRACE_ITER_FILTER) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3185 | !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) || |
Steven Rostedt | 0183fb1c | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3186 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3187 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3188 | !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) || |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3189 | |
| 3190 | ((iter->flags & FTRACE_ITER_ENABLED) && |
Steven Rostedt (Red Hat) | 23ea9c4 | 2013-05-09 19:31:48 -0400 | [diff] [blame] | 3191 | !(rec->flags & FTRACE_FL_ENABLED))) { |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3192 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3193 | rec = NULL; |
| 3194 | goto retry; |
| 3195 | } |
| 3196 | } |
| 3197 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3198 | if (!rec) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3199 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3200 | |
| 3201 | iter->func = rec; |
| 3202 | |
| 3203 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3204 | } |
| 3205 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3206 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 3207 | { |
| 3208 | iter->pos = 0; |
| 3209 | iter->func_pos = 0; |
Dan Carpenter | 70f77b3f | 2012-06-09 19:10:27 +0300 | [diff] [blame] | 3210 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 3214 | { |
| 3215 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3216 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3217 | void *p = NULL; |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3218 | loff_t l; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3219 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3220 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3221 | |
| 3222 | if (unlikely(ftrace_disabled)) |
| 3223 | return NULL; |
| 3224 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3225 | /* |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3226 | * If an lseek was done, then reset and start from beginning. |
| 3227 | */ |
| 3228 | if (*pos < iter->pos) |
| 3229 | reset_iter_read(iter); |
| 3230 | |
| 3231 | /* |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3232 | * For set_ftrace_filter reading, if we have the filter |
| 3233 | * off, we can short cut and just print out that all |
| 3234 | * functions are enabled. |
| 3235 | */ |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3236 | if ((iter->flags & FTRACE_ITER_FILTER && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3237 | ftrace_hash_empty(ops->func_hash->filter_hash)) || |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3238 | (iter->flags & FTRACE_ITER_NOTRACE && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3239 | ftrace_hash_empty(ops->func_hash->notrace_hash))) { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3240 | if (*pos > 0) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3241 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3242 | iter->flags |= FTRACE_ITER_PRINTALL; |
Chris Wright | df09162 | 2010-09-09 16:34:59 -0700 | [diff] [blame] | 3243 | /* reset in case of seek/pread */ |
| 3244 | iter->flags &= ~FTRACE_ITER_HASH; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3245 | return iter; |
| 3246 | } |
| 3247 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3248 | if (iter->flags & FTRACE_ITER_HASH) |
| 3249 | return t_hash_start(m, pos); |
| 3250 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3251 | /* |
| 3252 | * Unfortunately, we need to restart at ftrace_pages_start |
| 3253 | * every time we let go of the ftrace_mutex. This is because |
| 3254 | * those pointers can change without the lock. |
| 3255 | */ |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3256 | iter->pg = ftrace_pages_start; |
| 3257 | iter->idx = 0; |
| 3258 | for (l = 0; l <= *pos; ) { |
| 3259 | p = t_next(m, p, &l); |
| 3260 | if (!p) |
| 3261 | break; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 3262 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 3263 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3264 | if (!p) |
| 3265 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3266 | |
| 3267 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | static void t_stop(struct seq_file *m, void *p) |
| 3271 | { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3272 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3273 | } |
| 3274 | |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3275 | void * __weak |
| 3276 | arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 3277 | { |
| 3278 | return NULL; |
| 3279 | } |
| 3280 | |
| 3281 | static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, |
| 3282 | struct dyn_ftrace *rec) |
| 3283 | { |
| 3284 | void *ptr; |
| 3285 | |
| 3286 | ptr = arch_ftrace_trampoline_func(ops, rec); |
| 3287 | if (ptr) |
| 3288 | seq_printf(m, " ->%pS", ptr); |
| 3289 | } |
| 3290 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3291 | static int t_show(struct seq_file *m, void *v) |
| 3292 | { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3293 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3294 | struct dyn_ftrace *rec; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3295 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3296 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3297 | return t_hash_show(m, iter); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3298 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3299 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3300 | if (iter->flags & FTRACE_ITER_NOTRACE) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3301 | seq_puts(m, "#### no functions disabled ####\n"); |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3302 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3303 | seq_puts(m, "#### all functions enabled ####\n"); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3304 | return 0; |
| 3305 | } |
| 3306 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3307 | rec = iter->func; |
| 3308 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3309 | if (!rec) |
| 3310 | return 0; |
| 3311 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3312 | seq_printf(m, "%ps", (void *)rec->ip); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3313 | if (iter->flags & FTRACE_ITER_ENABLED) { |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3314 | struct ftrace_ops *ops; |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3315 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3316 | seq_printf(m, " (%ld)%s%s", |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 3317 | ftrace_rec_count(rec), |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3318 | rec->flags & FTRACE_FL_REGS ? " R" : " ", |
| 3319 | rec->flags & FTRACE_FL_IPMODIFY ? " I" : " "); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3320 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 3321 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3322 | if (ops) { |
| 3323 | do { |
| 3324 | seq_printf(m, "\ttramp: %pS (%pS)", |
| 3325 | (void *)ops->trampoline, |
| 3326 | (void *)ops->func); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3327 | add_trampoline_func(m, ops, rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3328 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 3329 | } while (ops); |
| 3330 | } else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3331 | seq_puts(m, "\ttramp: ERROR!"); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3332 | } else { |
| 3333 | add_trampoline_func(m, NULL, rec); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3334 | } |
| 3335 | } |
| 3336 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3337 | seq_putc(m, '\n'); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3338 | |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3342 | static const struct seq_operations show_ftrace_seq_ops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3343 | .start = t_start, |
| 3344 | .next = t_next, |
| 3345 | .stop = t_stop, |
| 3346 | .show = t_show, |
| 3347 | }; |
| 3348 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3349 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3350 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 3351 | { |
| 3352 | struct ftrace_iterator *iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3353 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3354 | if (unlikely(ftrace_disabled)) |
| 3355 | return -ENODEV; |
| 3356 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3357 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3358 | if (iter) { |
| 3359 | iter->pg = ftrace_pages_start; |
| 3360 | iter->ops = &global_ops; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3361 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3362 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3363 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3364 | } |
| 3365 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3366 | static int |
| 3367 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 3368 | { |
| 3369 | struct ftrace_iterator *iter; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3370 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3371 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3372 | if (iter) { |
| 3373 | iter->pg = ftrace_pages_start; |
| 3374 | iter->flags = FTRACE_ITER_ENABLED; |
| 3375 | iter->ops = &global_ops; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3376 | } |
| 3377 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3378 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3379 | } |
| 3380 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3381 | /** |
| 3382 | * ftrace_regex_open - initialize function tracer filter files |
| 3383 | * @ops: The ftrace_ops that hold the hash filters |
| 3384 | * @flag: The type of filter to process |
| 3385 | * @inode: The inode, usually passed in to your open routine |
| 3386 | * @file: The file, usually passed in to your open routine |
| 3387 | * |
| 3388 | * ftrace_regex_open() initializes the filter files for the |
| 3389 | * @ops. Depending on @flag it may process the filter hash or |
| 3390 | * the notrace hash of @ops. With this called from the open |
| 3391 | * routine, you can use ftrace_filter_write() for the write |
| 3392 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 3393 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3394 | * tracing_lseek() should be used as the lseek routine, and |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3395 | * release must call ftrace_regex_release(). |
| 3396 | */ |
| 3397 | int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3398 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3399 | struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3400 | { |
| 3401 | struct ftrace_iterator *iter; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3402 | struct ftrace_hash *hash; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3403 | int ret = 0; |
| 3404 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3405 | ftrace_ops_init(ops); |
| 3406 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3407 | if (unlikely(ftrace_disabled)) |
| 3408 | return -ENODEV; |
| 3409 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3410 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 3411 | if (!iter) |
| 3412 | return -ENOMEM; |
| 3413 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3414 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
| 3415 | kfree(iter); |
| 3416 | return -ENOMEM; |
| 3417 | } |
| 3418 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3419 | iter->ops = ops; |
| 3420 | iter->flags = flag; |
| 3421 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3422 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3423 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3424 | if (flag & FTRACE_ITER_NOTRACE) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3425 | hash = ops->func_hash->notrace_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3426 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3427 | hash = ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3428 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3429 | if (file->f_mode & FMODE_WRITE) { |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3430 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 3431 | |
| 3432 | if (file->f_flags & O_TRUNC) |
| 3433 | iter->hash = alloc_ftrace_hash(size_bits); |
| 3434 | else |
| 3435 | iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); |
| 3436 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3437 | if (!iter->hash) { |
| 3438 | trace_parser_put(&iter->parser); |
| 3439 | kfree(iter); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3440 | ret = -ENOMEM; |
| 3441 | goto out_unlock; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3442 | } |
| 3443 | } |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3444 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3445 | if (file->f_mode & FMODE_READ) { |
| 3446 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3447 | |
| 3448 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 3449 | if (!ret) { |
| 3450 | struct seq_file *m = file->private_data; |
| 3451 | m->private = iter; |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3452 | } else { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3453 | /* Failed */ |
| 3454 | free_ftrace_hash(iter->hash); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3455 | trace_parser_put(&iter->parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3456 | kfree(iter); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3457 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3458 | } else |
| 3459 | file->private_data = iter; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3460 | |
| 3461 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3462 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3463 | |
| 3464 | return ret; |
| 3465 | } |
| 3466 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3467 | static int |
| 3468 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 3469 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3470 | struct ftrace_ops *ops = inode->i_private; |
| 3471 | |
| 3472 | return ftrace_regex_open(ops, |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3473 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, |
| 3474 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | static int |
| 3478 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 3479 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3480 | struct ftrace_ops *ops = inode->i_private; |
| 3481 | |
| 3482 | return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3483 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3484 | } |
| 3485 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3486 | /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */ |
| 3487 | struct ftrace_glob { |
| 3488 | char *search; |
| 3489 | unsigned len; |
| 3490 | int type; |
| 3491 | }; |
| 3492 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3493 | /* |
| 3494 | * If symbols in an architecture don't correspond exactly to the user-visible |
| 3495 | * name of what they represent, it is possible to define this function to |
| 3496 | * perform the necessary adjustments. |
| 3497 | */ |
| 3498 | char * __weak arch_ftrace_match_adjust(char *str, const char *search) |
| 3499 | { |
| 3500 | return str; |
| 3501 | } |
| 3502 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3503 | static int ftrace_match(char *str, struct ftrace_glob *g) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3504 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3505 | int matched = 0; |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3506 | int slen; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3507 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3508 | str = arch_ftrace_match_adjust(str, g->search); |
| 3509 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3510 | switch (g->type) { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3511 | case MATCH_FULL: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3512 | if (strcmp(str, g->search) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3513 | matched = 1; |
| 3514 | break; |
| 3515 | case MATCH_FRONT_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3516 | if (strncmp(str, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3517 | matched = 1; |
| 3518 | break; |
| 3519 | case MATCH_MIDDLE_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3520 | if (strstr(str, g->search)) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3521 | matched = 1; |
| 3522 | break; |
| 3523 | case MATCH_END_ONLY: |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3524 | slen = strlen(str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3525 | if (slen >= g->len && |
| 3526 | memcmp(str + slen - g->len, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3527 | matched = 1; |
| 3528 | break; |
Masami Hiramatsu | 60f1d5e | 2016-10-05 20:58:15 +0900 | [diff] [blame] | 3529 | case MATCH_GLOB: |
| 3530 | if (glob_match(g->search, str)) |
| 3531 | matched = 1; |
| 3532 | break; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | return matched; |
| 3536 | } |
| 3537 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3538 | static int |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3539 | 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] | 3540 | { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3541 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3542 | int ret = 0; |
| 3543 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3544 | entry = ftrace_lookup_ip(hash, rec->ip); |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3545 | if (clear_filter) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3546 | /* Do nothing if it doesn't exist */ |
| 3547 | if (!entry) |
| 3548 | return 0; |
| 3549 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3550 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3551 | } else { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3552 | /* Do nothing if it exists */ |
| 3553 | if (entry) |
| 3554 | return 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3555 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3556 | ret = add_hash_entry(hash, rec->ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3557 | } |
| 3558 | return ret; |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 3559 | } |
| 3560 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3561 | static int |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3562 | ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g, |
| 3563 | struct ftrace_glob *mod_g, int exclude_mod) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3564 | { |
| 3565 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3566 | char *modname; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3567 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3568 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); |
| 3569 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3570 | if (mod_g) { |
| 3571 | int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0; |
| 3572 | |
| 3573 | /* blank module name to match all modules */ |
| 3574 | if (!mod_g->len) { |
| 3575 | /* blank module globbing: modname xor exclude_mod */ |
| 3576 | if ((!exclude_mod) != (!modname)) |
| 3577 | goto func_match; |
| 3578 | return 0; |
| 3579 | } |
| 3580 | |
| 3581 | /* not matching the module */ |
| 3582 | if (!modname || !mod_matches) { |
| 3583 | if (exclude_mod) |
| 3584 | goto func_match; |
| 3585 | else |
| 3586 | return 0; |
| 3587 | } |
| 3588 | |
| 3589 | if (mod_matches && exclude_mod) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3590 | return 0; |
| 3591 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3592 | func_match: |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3593 | /* blank search means to match all funcs in the mod */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3594 | if (!func_g->len) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3595 | return 1; |
| 3596 | } |
| 3597 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3598 | return ftrace_match(str, func_g); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3599 | } |
| 3600 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3601 | static int |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3602 | match_records(struct ftrace_hash *hash, char *func, int len, char *mod) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3603 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3604 | struct ftrace_page *pg; |
| 3605 | struct dyn_ftrace *rec; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3606 | struct ftrace_glob func_g = { .type = MATCH_FULL }; |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3607 | struct ftrace_glob mod_g = { .type = MATCH_FULL }; |
| 3608 | struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL; |
| 3609 | int exclude_mod = 0; |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3610 | int found = 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3611 | int ret; |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3612 | int clear_filter; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3613 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3614 | if (func) { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3615 | func_g.type = filter_parse_regex(func, len, &func_g.search, |
| 3616 | &clear_filter); |
| 3617 | func_g.len = strlen(func_g.search); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3618 | } |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3619 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3620 | if (mod) { |
| 3621 | mod_g.type = filter_parse_regex(mod, strlen(mod), |
| 3622 | &mod_g.search, &exclude_mod); |
| 3623 | mod_g.len = strlen(mod_g.search); |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3624 | } |
| 3625 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3626 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3627 | |
| 3628 | if (unlikely(ftrace_disabled)) |
| 3629 | goto out_unlock; |
| 3630 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3631 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 3632 | |
| 3633 | if (rec->flags & FTRACE_FL_DISABLED) |
| 3634 | continue; |
| 3635 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3636 | if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3637 | ret = enter_record(hash, rec, clear_filter); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3638 | if (ret < 0) { |
| 3639 | found = ret; |
| 3640 | goto out_unlock; |
| 3641 | } |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3642 | found = 1; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3643 | } |
| 3644 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3645 | out_unlock: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3646 | mutex_unlock(&ftrace_lock); |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3647 | |
| 3648 | return found; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3649 | } |
| 3650 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3651 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3652 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3653 | { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3654 | return match_records(hash, buff, len, NULL); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3655 | } |
| 3656 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3657 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3658 | /* |
| 3659 | * We register the module command as a template to show others how |
| 3660 | * to register the a command as well. |
| 3661 | */ |
| 3662 | |
| 3663 | static int |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 3664 | ftrace_mod_callback(struct ftrace_hash *hash, |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3665 | char *func, char *cmd, char *module, int enable) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3666 | { |
Dmitry Safonov | 5e3949f | 2015-09-29 19:46:12 +0300 | [diff] [blame] | 3667 | int ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3668 | |
| 3669 | /* |
| 3670 | * cmd == 'mod' because we only registered this func |
| 3671 | * for the 'mod' ftrace_func_command. |
| 3672 | * But if you register one func with multiple commands, |
| 3673 | * you can tell which command was used by the cmd |
| 3674 | * parameter. |
| 3675 | */ |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3676 | ret = match_records(hash, func, strlen(func), module); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3677 | if (!ret) |
Dmitry Safonov | 5e3949f | 2015-09-29 19:46:12 +0300 | [diff] [blame] | 3678 | return -EINVAL; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3679 | if (ret < 0) |
| 3680 | return ret; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3681 | return 0; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 3685 | .name = "mod", |
| 3686 | .func = ftrace_mod_callback, |
| 3687 | }; |
| 3688 | |
| 3689 | static int __init ftrace_mod_cmd_init(void) |
| 3690 | { |
| 3691 | return register_ftrace_command(&ftrace_mod_cmd); |
| 3692 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 3693 | core_initcall(ftrace_mod_cmd_init); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3694 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 3695 | 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] | 3696 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3697 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3698 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3699 | struct hlist_head *hhd; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3700 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3701 | |
| 3702 | key = hash_long(ip, FTRACE_HASH_BITS); |
| 3703 | |
| 3704 | hhd = &ftrace_func_hash[key]; |
| 3705 | |
| 3706 | if (hlist_empty(hhd)) |
| 3707 | return; |
| 3708 | |
| 3709 | /* |
| 3710 | * Disable preemption for these calls to prevent a RCU grace |
| 3711 | * period. This syncs the hash iteration and freeing of items |
| 3712 | * on the hash. rcu_read_lock is too dangerous here. |
| 3713 | */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3714 | preempt_disable_notrace(); |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 3715 | hlist_for_each_entry_rcu_notrace(entry, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3716 | if (entry->ip == ip) |
| 3717 | entry->ops->func(ip, parent_ip, &entry->data); |
| 3718 | } |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3719 | preempt_enable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3720 | } |
| 3721 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3722 | static struct ftrace_ops trace_probe_ops __read_mostly = |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3723 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 3724 | .func = function_trace_probe_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3725 | .flags = FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3726 | INIT_OPS_HASH(trace_probe_ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3727 | }; |
| 3728 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3729 | static int ftrace_probe_registered; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3730 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3731 | static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3732 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3733 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3734 | int i; |
| 3735 | |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3736 | if (ftrace_probe_registered) { |
| 3737 | /* still need to update the function call sites */ |
| 3738 | if (ftrace_enabled) |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3739 | ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, |
| 3740 | old_hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3741 | return; |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3742 | } |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3743 | |
| 3744 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3745 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3746 | if (hhd->first) |
| 3747 | break; |
| 3748 | } |
| 3749 | /* Nothing registered? */ |
| 3750 | if (i == FTRACE_FUNC_HASHSIZE) |
| 3751 | return; |
| 3752 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3753 | ret = ftrace_startup(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3754 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3755 | ftrace_probe_registered = 1; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3756 | } |
| 3757 | |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3758 | static bool __disable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3759 | { |
| 3760 | int i; |
| 3761 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3762 | if (!ftrace_probe_registered) |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3763 | return false; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3764 | |
| 3765 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3766 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3767 | if (hhd->first) |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3768 | return false; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | /* no more funcs left */ |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3772 | ftrace_shutdown(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3773 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3774 | ftrace_probe_registered = 0; |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3775 | return true; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3779 | static void ftrace_free_entry(struct ftrace_func_probe *entry) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3780 | { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3781 | if (entry->ops->free) |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3782 | entry->ops->free(entry->ops, entry->ip, &entry->data); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3783 | kfree(entry); |
| 3784 | } |
| 3785 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3786 | int |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3787 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3788 | void *data) |
| 3789 | { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3790 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3791 | struct ftrace_func_probe *entry; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3792 | struct ftrace_glob func_g; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3793 | 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] | 3794 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3795 | struct ftrace_hash *hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3796 | struct ftrace_page *pg; |
| 3797 | struct dyn_ftrace *rec; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3798 | int not; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3799 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3800 | int count = 0; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3801 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3802 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3803 | func_g.type = filter_parse_regex(glob, strlen(glob), |
| 3804 | &func_g.search, ¬); |
| 3805 | func_g.len = strlen(func_g.search); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3806 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3807 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3808 | if (WARN_ON(not)) |
| 3809 | return -EINVAL; |
| 3810 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3811 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3812 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3813 | old_hash_ops.filter_hash = old_hash; |
| 3814 | /* Probes only have filters */ |
| 3815 | old_hash_ops.notrace_hash = NULL; |
| 3816 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3817 | 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] | 3818 | if (!hash) { |
| 3819 | count = -ENOMEM; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3820 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | if (unlikely(ftrace_disabled)) { |
| 3824 | count = -ENODEV; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3825 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3826 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3827 | |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3828 | mutex_lock(&ftrace_lock); |
| 3829 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3830 | do_for_each_ftrace_rec(pg, rec) { |
| 3831 | |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 3832 | if (rec->flags & FTRACE_FL_DISABLED) |
| 3833 | continue; |
| 3834 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3835 | if (!ftrace_match_record(rec, &func_g, NULL, 0)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3836 | continue; |
| 3837 | |
| 3838 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 3839 | if (!entry) { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3840 | /* If we did not process any, then return error */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3841 | if (!count) |
| 3842 | count = -ENOMEM; |
| 3843 | goto out_unlock; |
| 3844 | } |
| 3845 | |
| 3846 | count++; |
| 3847 | |
| 3848 | entry->data = data; |
| 3849 | |
| 3850 | /* |
| 3851 | * The caller might want to do something special |
| 3852 | * for each function we find. We call the callback |
| 3853 | * to give the caller an opportunity to do so. |
| 3854 | */ |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3855 | if (ops->init) { |
| 3856 | if (ops->init(ops, rec->ip, &entry->data) < 0) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3857 | /* caller does not like this func */ |
| 3858 | kfree(entry); |
| 3859 | continue; |
| 3860 | } |
| 3861 | } |
| 3862 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3863 | ret = enter_record(hash, rec, 0); |
| 3864 | if (ret < 0) { |
| 3865 | kfree(entry); |
| 3866 | count = ret; |
| 3867 | goto out_unlock; |
| 3868 | } |
| 3869 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3870 | entry->ops = ops; |
| 3871 | entry->ip = rec->ip; |
| 3872 | |
| 3873 | key = hash_long(entry->ip, FTRACE_HASH_BITS); |
| 3874 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); |
| 3875 | |
| 3876 | } while_for_each_ftrace_rec(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3877 | |
| 3878 | 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] | 3879 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3880 | __enable_ftrace_function_probe(&old_hash_ops); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3881 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3882 | if (!ret) |
| 3883 | free_ftrace_hash_rcu(old_hash); |
| 3884 | else |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3885 | count = ret; |
| 3886 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3887 | out_unlock: |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3888 | mutex_unlock(&ftrace_lock); |
| 3889 | out: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3890 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3891 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3892 | |
| 3893 | return count; |
| 3894 | } |
| 3895 | |
| 3896 | enum { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3897 | PROBE_TEST_FUNC = 1, |
| 3898 | PROBE_TEST_DATA = 2 |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3899 | }; |
| 3900 | |
| 3901 | static void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3902 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3903 | void *data, int flags) |
| 3904 | { |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3905 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3906 | struct ftrace_func_entry *rec_entry; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3907 | struct ftrace_func_probe *entry; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3908 | struct ftrace_func_probe *p; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3909 | struct ftrace_glob func_g; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3910 | 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] | 3911 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3912 | struct list_head free_list; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3913 | struct ftrace_hash *hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3914 | struct hlist_node *tmp; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3915 | char str[KSYM_SYMBOL_LEN]; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3916 | int i, ret; |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3917 | bool disabled; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3918 | |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3919 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3920 | func_g.search = NULL; |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3921 | else if (glob) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3922 | int not; |
| 3923 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3924 | func_g.type = filter_parse_regex(glob, strlen(glob), |
| 3925 | &func_g.search, ¬); |
| 3926 | func_g.len = strlen(func_g.search); |
| 3927 | func_g.search = glob; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3928 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3929 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3930 | if (WARN_ON(not)) |
| 3931 | return; |
| 3932 | } |
| 3933 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3934 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3935 | |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3936 | old_hash_ops.filter_hash = old_hash; |
| 3937 | /* Probes only have filters */ |
| 3938 | old_hash_ops.notrace_hash = NULL; |
| 3939 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3940 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 3941 | if (!hash) |
| 3942 | /* Hmm, should report this somehow */ |
| 3943 | goto out_unlock; |
| 3944 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3945 | INIT_LIST_HEAD(&free_list); |
| 3946 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3947 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3948 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3949 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3950 | hlist_for_each_entry_safe(entry, tmp, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3951 | |
| 3952 | /* break up if statements for readability */ |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3953 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3954 | continue; |
| 3955 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3956 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3957 | continue; |
| 3958 | |
| 3959 | /* do this last, since it is the most expensive */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3960 | if (func_g.search) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3961 | kallsyms_lookup(entry->ip, NULL, NULL, |
| 3962 | NULL, str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3963 | if (!ftrace_match(str, &func_g)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3964 | continue; |
| 3965 | } |
| 3966 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3967 | rec_entry = ftrace_lookup_ip(hash, entry->ip); |
| 3968 | /* It is possible more than one entry had this ip */ |
| 3969 | if (rec_entry) |
| 3970 | free_hash_entry(hash, rec_entry); |
| 3971 | |
Steven Rostedt (Red Hat) | 740466b | 2013-03-13 11:15:19 -0400 | [diff] [blame] | 3972 | hlist_del_rcu(&entry->node); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3973 | list_add(&entry->free_list, &free_list); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3974 | } |
| 3975 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3976 | mutex_lock(&ftrace_lock); |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3977 | disabled = __disable_ftrace_function_probe(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3978 | /* |
| 3979 | * Remove after the disable is called. Otherwise, if the last |
| 3980 | * probe is removed, a null hash means *all enabled*. |
| 3981 | */ |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3982 | ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3983 | |
| 3984 | /* still need to update the function call sites */ |
| 3985 | if (ftrace_enabled && !disabled) |
| 3986 | ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, |
| 3987 | &old_hash_ops); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3988 | synchronize_sched(); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3989 | if (!ret) |
| 3990 | free_ftrace_hash_rcu(old_hash); |
| 3991 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3992 | list_for_each_entry_safe(entry, p, &free_list, free_list) { |
| 3993 | list_del(&entry->free_list); |
| 3994 | ftrace_free_entry(entry); |
| 3995 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3996 | mutex_unlock(&ftrace_lock); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3997 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3998 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3999 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4000 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4004 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4005 | void *data) |
| 4006 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4007 | __unregister_ftrace_function_probe(glob, ops, data, |
| 4008 | PROBE_TEST_FUNC | PROBE_TEST_DATA); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4012 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4013 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4014 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4015 | } |
| 4016 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4017 | void unregister_ftrace_function_probe_all(char *glob) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4018 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4019 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4020 | } |
| 4021 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4022 | static LIST_HEAD(ftrace_commands); |
| 4023 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 4024 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4025 | /* |
| 4026 | * Currently we only register ftrace commands from __init, so mark this |
| 4027 | * __init too. |
| 4028 | */ |
| 4029 | __init int register_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4030 | { |
| 4031 | struct ftrace_func_command *p; |
| 4032 | int ret = 0; |
| 4033 | |
| 4034 | mutex_lock(&ftrace_cmd_mutex); |
| 4035 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4036 | if (strcmp(cmd->name, p->name) == 0) { |
| 4037 | ret = -EBUSY; |
| 4038 | goto out_unlock; |
| 4039 | } |
| 4040 | } |
| 4041 | list_add(&cmd->list, &ftrace_commands); |
| 4042 | out_unlock: |
| 4043 | mutex_unlock(&ftrace_cmd_mutex); |
| 4044 | |
| 4045 | return ret; |
| 4046 | } |
| 4047 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4048 | /* |
| 4049 | * Currently we only unregister ftrace commands from __init, so mark |
| 4050 | * this __init too. |
| 4051 | */ |
| 4052 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4053 | { |
| 4054 | struct ftrace_func_command *p, *n; |
| 4055 | int ret = -ENODEV; |
| 4056 | |
| 4057 | mutex_lock(&ftrace_cmd_mutex); |
| 4058 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 4059 | if (strcmp(cmd->name, p->name) == 0) { |
| 4060 | ret = 0; |
| 4061 | list_del_init(&p->list); |
| 4062 | goto out_unlock; |
| 4063 | } |
| 4064 | } |
| 4065 | out_unlock: |
| 4066 | mutex_unlock(&ftrace_cmd_mutex); |
| 4067 | |
| 4068 | return ret; |
| 4069 | } |
| 4070 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4071 | static int ftrace_process_regex(struct ftrace_hash *hash, |
| 4072 | char *buff, int len, int enable) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4073 | { |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4074 | char *func, *command, *next = buff; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 4075 | struct ftrace_func_command *p; |
GuoWen Li | 0aff1c0 | 2011-06-01 19:18:47 +0800 | [diff] [blame] | 4076 | int ret = -EINVAL; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4077 | |
| 4078 | func = strsep(&next, ":"); |
| 4079 | |
| 4080 | if (!next) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4081 | ret = ftrace_match_records(hash, func, len); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 4082 | if (!ret) |
| 4083 | ret = -EINVAL; |
| 4084 | if (ret < 0) |
| 4085 | return ret; |
| 4086 | return 0; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4087 | } |
| 4088 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4089 | /* command found */ |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4090 | |
| 4091 | command = strsep(&next, ":"); |
| 4092 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4093 | mutex_lock(&ftrace_cmd_mutex); |
| 4094 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4095 | if (strcmp(p->name, command) == 0) { |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 4096 | ret = p->func(hash, func, command, next, enable); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4097 | goto out_unlock; |
| 4098 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4099 | } |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4100 | out_unlock: |
| 4101 | mutex_unlock(&ftrace_cmd_mutex); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4102 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4103 | return ret; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4104 | } |
| 4105 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4106 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4107 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 4108 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4109 | { |
| 4110 | struct ftrace_iterator *iter; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4111 | struct trace_parser *parser; |
| 4112 | ssize_t ret, read; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4113 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4114 | if (!cnt) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4115 | return 0; |
| 4116 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4117 | if (file->f_mode & FMODE_READ) { |
| 4118 | struct seq_file *m = file->private_data; |
| 4119 | iter = m->private; |
| 4120 | } else |
| 4121 | iter = file->private_data; |
| 4122 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4123 | if (unlikely(ftrace_disabled)) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4124 | return -ENODEV; |
| 4125 | |
| 4126 | /* 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] | 4127 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4128 | parser = &iter->parser; |
| 4129 | read = trace_get_user(parser, ubuf, cnt, ppos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4130 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4131 | if (read >= 0 && trace_parser_loaded(parser) && |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4132 | !trace_parser_cont(parser)) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4133 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4134 | parser->idx, enable); |
Li Zefan | 313254a | 2009-12-08 11:15:30 +0800 | [diff] [blame] | 4135 | trace_parser_clear(parser); |
Steven Rostedt (Red Hat) | 7c088b5 | 2013-05-09 11:35:12 -0400 | [diff] [blame] | 4136 | if (ret < 0) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4137 | goto out; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4140 | ret = read; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4141 | out: |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4142 | return ret; |
| 4143 | } |
| 4144 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4145 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4146 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 4147 | size_t cnt, loff_t *ppos) |
| 4148 | { |
| 4149 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 4150 | } |
| 4151 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4152 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4153 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 4154 | size_t cnt, loff_t *ppos) |
| 4155 | { |
| 4156 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 4157 | } |
| 4158 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4159 | static int |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4160 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
| 4161 | { |
| 4162 | struct ftrace_func_entry *entry; |
| 4163 | |
| 4164 | if (!ftrace_location(ip)) |
| 4165 | return -EINVAL; |
| 4166 | |
| 4167 | if (remove) { |
| 4168 | entry = ftrace_lookup_ip(hash, ip); |
| 4169 | if (!entry) |
| 4170 | return -ENOENT; |
| 4171 | free_hash_entry(hash, entry); |
| 4172 | return 0; |
| 4173 | } |
| 4174 | |
| 4175 | return add_hash_entry(hash, ip); |
| 4176 | } |
| 4177 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4178 | static void ftrace_ops_update_code(struct ftrace_ops *ops, |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4179 | struct ftrace_ops_hash *old_hash) |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4180 | { |
Steven Rostedt (Red Hat) | 8f86f83 | 2015-01-13 11:20:43 -0500 | [diff] [blame] | 4181 | struct ftrace_ops *op; |
| 4182 | |
| 4183 | if (!ftrace_enabled) |
| 4184 | return; |
| 4185 | |
| 4186 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4187 | ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); |
Steven Rostedt (Red Hat) | 8f86f83 | 2015-01-13 11:20:43 -0500 | [diff] [blame] | 4188 | return; |
| 4189 | } |
| 4190 | |
| 4191 | /* |
| 4192 | * If this is the shared global_ops filter, then we need to |
| 4193 | * check if there is another ops that shares it, is enabled. |
| 4194 | * If so, we still need to run the modify code. |
| 4195 | */ |
| 4196 | if (ops->func_hash != &global_ops.local_hash) |
| 4197 | return; |
| 4198 | |
| 4199 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 4200 | if (op->func_hash == &global_ops.local_hash && |
| 4201 | op->flags & FTRACE_OPS_FL_ENABLED) { |
| 4202 | ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash); |
| 4203 | /* Only need to do this once */ |
| 4204 | return; |
| 4205 | } |
| 4206 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4207 | } |
| 4208 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4209 | static int |
| 4210 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4211 | unsigned long ip, int remove, int reset, int enable) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4212 | { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4213 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4214 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4215 | struct ftrace_hash *old_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4216 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4217 | int ret; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4218 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4219 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4220 | return -ENODEV; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4221 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4222 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4223 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4224 | if (enable) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4225 | orig_hash = &ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4226 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4227 | orig_hash = &ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4228 | |
Wang Nan | b972cc5 | 2014-07-15 08:40:20 +0800 | [diff] [blame] | 4229 | if (reset) |
| 4230 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4231 | else |
| 4232 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 4233 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4234 | if (!hash) { |
| 4235 | ret = -ENOMEM; |
| 4236 | goto out_regex_unlock; |
| 4237 | } |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4238 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4239 | if (buf && !ftrace_match_records(hash, buf, len)) { |
| 4240 | ret = -EINVAL; |
| 4241 | goto out_regex_unlock; |
| 4242 | } |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4243 | if (ip) { |
| 4244 | ret = ftrace_match_addr(hash, ip, remove); |
| 4245 | if (ret < 0) |
| 4246 | goto out_regex_unlock; |
| 4247 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4248 | |
| 4249 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4250 | old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4251 | old_hash_ops.filter_hash = ops->func_hash->filter_hash; |
| 4252 | old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4253 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4254 | if (!ret) { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4255 | ftrace_ops_update_code(ops, &old_hash_ops); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4256 | free_ftrace_hash_rcu(old_hash); |
| 4257 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4258 | mutex_unlock(&ftrace_lock); |
| 4259 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4260 | out_regex_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4261 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4262 | |
| 4263 | free_ftrace_hash(hash); |
| 4264 | return ret; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4265 | } |
| 4266 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4267 | static int |
| 4268 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, |
| 4269 | int reset, int enable) |
| 4270 | { |
| 4271 | return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); |
| 4272 | } |
| 4273 | |
| 4274 | /** |
| 4275 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address |
| 4276 | * @ops - the ops to set the filter with |
| 4277 | * @ip - the address to add to or remove from the filter. |
| 4278 | * @remove - non zero to remove the ip from the filter |
| 4279 | * @reset - non zero to reset all filters before applying this filter. |
| 4280 | * |
| 4281 | * Filters denote which functions should be enabled when tracing is enabled |
| 4282 | * If @ip is NULL, it failes to update filter. |
| 4283 | */ |
| 4284 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, |
| 4285 | int remove, int reset) |
| 4286 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4287 | ftrace_ops_init(ops); |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4288 | return ftrace_set_addr(ops, ip, remove, reset, 1); |
| 4289 | } |
| 4290 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); |
| 4291 | |
Joel Fernandes | d032ae8 | 2016-11-15 12:31:20 -0800 | [diff] [blame] | 4292 | /** |
| 4293 | * ftrace_ops_set_global_filter - setup ops to use global filters |
| 4294 | * @ops - the ops which will use the global filters |
| 4295 | * |
| 4296 | * ftrace users who need global function trace filtering should call this. |
| 4297 | * It can set the global filter only if ops were not initialized before. |
| 4298 | */ |
| 4299 | void ftrace_ops_set_global_filter(struct ftrace_ops *ops) |
| 4300 | { |
| 4301 | if (ops->flags & FTRACE_OPS_FL_INITIALIZED) |
| 4302 | return; |
| 4303 | |
| 4304 | ftrace_ops_init(ops); |
| 4305 | ops->func_hash = &global_ops.local_hash; |
| 4306 | } |
| 4307 | EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter); |
| 4308 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4309 | static int |
| 4310 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4311 | int reset, int enable) |
| 4312 | { |
| 4313 | return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); |
| 4314 | } |
| 4315 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4316 | /** |
| 4317 | * ftrace_set_filter - set a function to filter on in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4318 | * @ops - the ops to set the filter with |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4319 | * @buf - the string that holds the function filter text. |
| 4320 | * @len - the length of the string. |
| 4321 | * @reset - non zero to reset all filters before applying this filter. |
| 4322 | * |
| 4323 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4324 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4325 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4326 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4327 | int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4328 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4329 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4330 | return ftrace_set_regex(ops, buf, len, reset, 1); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4331 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4332 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4333 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4334 | /** |
| 4335 | * ftrace_set_notrace - set a function to not trace in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4336 | * @ops - the ops to set the notrace filter with |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4337 | * @buf - the string that holds the function notrace text. |
| 4338 | * @len - the length of the string. |
| 4339 | * @reset - non zero to reset all filters before applying this filter. |
| 4340 | * |
| 4341 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4342 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4343 | * for tracing. |
| 4344 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4345 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4346 | int len, int reset) |
| 4347 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4348 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4349 | return ftrace_set_regex(ops, buf, len, reset, 0); |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4350 | } |
| 4351 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 4352 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4353 | * 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] | 4354 | * @buf - the string that holds the function filter text. |
| 4355 | * @len - the length of the string. |
| 4356 | * @reset - non zero to reset all filters before applying this filter. |
| 4357 | * |
| 4358 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4359 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4360 | */ |
| 4361 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 4362 | { |
| 4363 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
| 4364 | } |
| 4365 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 4366 | |
| 4367 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4368 | * 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] | 4369 | * @buf - the string that holds the function notrace text. |
| 4370 | * @len - the length of the string. |
| 4371 | * @reset - non zero to reset all filters before applying this filter. |
| 4372 | * |
| 4373 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4374 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4375 | * for tracing. |
| 4376 | */ |
| 4377 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4378 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4379 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4380 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4381 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4382 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4383 | /* |
| 4384 | * command line interface to allow users to set filters on boot up. |
| 4385 | */ |
| 4386 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 4387 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4388 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4389 | |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4390 | /* Used by function selftest to not test if filter is set */ |
| 4391 | bool ftrace_filter_param __initdata; |
| 4392 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4393 | static int __init set_ftrace_notrace(char *str) |
| 4394 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4395 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4396 | strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4397 | return 1; |
| 4398 | } |
| 4399 | __setup("ftrace_notrace=", set_ftrace_notrace); |
| 4400 | |
| 4401 | static int __init set_ftrace_filter(char *str) |
| 4402 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4403 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4404 | strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4405 | return 1; |
| 4406 | } |
| 4407 | __setup("ftrace_filter=", set_ftrace_filter); |
| 4408 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4409 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Lai Jiangshan | f6060f4 | 2009-11-05 11:16:17 +0800 | [diff] [blame] | 4410 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4411 | static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4412 | static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer); |
Steven Rostedt | 801c29f | 2010-03-05 20:02:19 -0500 | [diff] [blame] | 4413 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 4414 | static unsigned long save_global_trampoline; |
| 4415 | static unsigned long save_global_flags; |
| 4416 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4417 | static int __init set_graph_function(char *str) |
| 4418 | { |
Frederic Weisbecker | 06f43d6 | 2009-10-14 20:43:39 +0200 | [diff] [blame] | 4419 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4420 | return 1; |
| 4421 | } |
| 4422 | __setup("ftrace_graph_filter=", set_graph_function); |
| 4423 | |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4424 | static int __init set_graph_notrace_function(char *str) |
| 4425 | { |
| 4426 | strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 4427 | return 1; |
| 4428 | } |
| 4429 | __setup("ftrace_graph_notrace=", set_graph_notrace_function); |
| 4430 | |
Todd Brandt | 65a50c6 | 2017-03-02 16:12:15 -0800 | [diff] [blame] | 4431 | static int __init set_graph_max_depth_function(char *str) |
| 4432 | { |
| 4433 | if (!str) |
| 4434 | return 0; |
| 4435 | fgraph_max_depth = simple_strtoul(str, NULL, 0); |
| 4436 | return 1; |
| 4437 | } |
| 4438 | __setup("ftrace_graph_max_depth=", set_graph_max_depth_function); |
| 4439 | |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4440 | static void __init set_ftrace_early_graph(char *buf, int enable) |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4441 | { |
| 4442 | int ret; |
| 4443 | char *func; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4444 | struct ftrace_hash *hash; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4445 | |
Steven Rostedt (VMware) | 92ad18e | 2017-03-02 12:53:26 -0500 | [diff] [blame] | 4446 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4447 | if (WARN_ON(!hash)) |
| 4448 | return; |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4449 | |
| 4450 | while (buf) { |
| 4451 | func = strsep(&buf, ","); |
| 4452 | /* we allow only one expression at a time */ |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4453 | ret = ftrace_graph_set_hash(hash, func); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4454 | if (ret) |
| 4455 | printk(KERN_DEBUG "ftrace: function %s not " |
| 4456 | "traceable\n", func); |
| 4457 | } |
Steven Rostedt (VMware) | 92ad18e | 2017-03-02 12:53:26 -0500 | [diff] [blame] | 4458 | |
| 4459 | if (enable) |
| 4460 | ftrace_graph_hash = hash; |
| 4461 | else |
| 4462 | ftrace_graph_notrace_hash = hash; |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4463 | } |
| 4464 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4465 | |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4466 | void __init |
| 4467 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4468 | { |
| 4469 | char *func; |
| 4470 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4471 | ftrace_ops_init(ops); |
| 4472 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4473 | while (buf) { |
| 4474 | func = strsep(&buf, ","); |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4475 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4476 | } |
| 4477 | } |
| 4478 | |
| 4479 | static void __init set_ftrace_early_filters(void) |
| 4480 | { |
| 4481 | if (ftrace_filter_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4482 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4483 | if (ftrace_notrace_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4484 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4485 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4486 | if (ftrace_graph_buf[0]) |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4487 | set_ftrace_early_graph(ftrace_graph_buf, 1); |
| 4488 | if (ftrace_graph_notrace_buf[0]) |
| 4489 | set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4490 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4491 | } |
| 4492 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4493 | int ftrace_regex_release(struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4494 | { |
| 4495 | struct seq_file *m = (struct seq_file *)file->private_data; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4496 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4497 | struct ftrace_iterator *iter; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4498 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4499 | struct ftrace_hash *old_hash; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4500 | struct trace_parser *parser; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4501 | int filter_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4502 | int ret; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4503 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4504 | if (file->f_mode & FMODE_READ) { |
| 4505 | iter = m->private; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4506 | seq_release(inode, file); |
| 4507 | } else |
| 4508 | iter = file->private_data; |
| 4509 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4510 | parser = &iter->parser; |
| 4511 | if (trace_parser_loaded(parser)) { |
| 4512 | parser->buffer[parser->idx] = 0; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4513 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4516 | trace_parser_put(parser); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4517 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4518 | mutex_lock(&iter->ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4519 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4520 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4521 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 4522 | |
| 4523 | if (filter_hash) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4524 | orig_hash = &iter->ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4525 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4526 | orig_hash = &iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4527 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4528 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4529 | old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4530 | old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash; |
| 4531 | old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4532 | ret = ftrace_hash_move(iter->ops, filter_hash, |
| 4533 | orig_hash, iter->hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4534 | if (!ret) { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4535 | ftrace_ops_update_code(iter->ops, &old_hash_ops); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4536 | free_ftrace_hash_rcu(old_hash); |
| 4537 | } |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4538 | mutex_unlock(&ftrace_lock); |
| 4539 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4540 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4541 | mutex_unlock(&iter->ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4542 | free_ftrace_hash(iter->hash); |
| 4543 | kfree(iter); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4544 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4545 | return 0; |
| 4546 | } |
| 4547 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4548 | static const struct file_operations ftrace_avail_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4549 | .open = ftrace_avail_open, |
| 4550 | .read = seq_read, |
| 4551 | .llseek = seq_lseek, |
Li Zefan | 3be04b4 | 2009-08-17 16:54:03 +0800 | [diff] [blame] | 4552 | .release = seq_release_private, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4553 | }; |
| 4554 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4555 | static const struct file_operations ftrace_enabled_fops = { |
| 4556 | .open = ftrace_enabled_open, |
| 4557 | .read = seq_read, |
| 4558 | .llseek = seq_lseek, |
| 4559 | .release = seq_release_private, |
| 4560 | }; |
| 4561 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4562 | static const struct file_operations ftrace_filter_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4563 | .open = ftrace_filter_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4564 | .read = seq_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4565 | .write = ftrace_filter_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4566 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4567 | .release = ftrace_regex_release, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4568 | }; |
| 4569 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4570 | static const struct file_operations ftrace_notrace_fops = { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4571 | .open = ftrace_notrace_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4572 | .read = seq_read, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4573 | .write = ftrace_notrace_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4574 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4575 | .release = ftrace_regex_release, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4576 | }; |
| 4577 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4578 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4579 | |
| 4580 | static DEFINE_MUTEX(graph_lock); |
| 4581 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4582 | struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH; |
| 4583 | struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH; |
| 4584 | |
| 4585 | enum graph_filter_type { |
| 4586 | GRAPH_FILTER_NOTRACE = 0, |
| 4587 | GRAPH_FILTER_FUNCTION, |
| 4588 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4589 | |
Steven Rostedt (VMware) | 555fc78 | 2017-02-02 10:15:22 -0500 | [diff] [blame] | 4590 | #define FTRACE_GRAPH_EMPTY ((void *)1) |
| 4591 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4592 | struct ftrace_graph_data { |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4593 | struct ftrace_hash *hash; |
| 4594 | struct ftrace_func_entry *entry; |
| 4595 | int idx; /* for hash table iteration */ |
| 4596 | enum graph_filter_type type; |
| 4597 | struct ftrace_hash *new_hash; |
| 4598 | const struct seq_operations *seq_ops; |
| 4599 | struct trace_parser parser; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4600 | }; |
| 4601 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4602 | static void * |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4603 | __g_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4604 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4605 | struct ftrace_graph_data *fgd = m->private; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4606 | struct ftrace_func_entry *entry = fgd->entry; |
| 4607 | struct hlist_head *head; |
| 4608 | int i, idx = fgd->idx; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4609 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4610 | if (*pos >= fgd->hash->count) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4611 | return NULL; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4612 | |
| 4613 | if (entry) { |
| 4614 | hlist_for_each_entry_continue(entry, hlist) { |
| 4615 | fgd->entry = entry; |
| 4616 | return entry; |
| 4617 | } |
| 4618 | |
| 4619 | idx++; |
| 4620 | } |
| 4621 | |
| 4622 | for (i = idx; i < 1 << fgd->hash->size_bits; i++) { |
| 4623 | head = &fgd->hash->buckets[i]; |
| 4624 | hlist_for_each_entry(entry, head, hlist) { |
| 4625 | fgd->entry = entry; |
| 4626 | fgd->idx = i; |
| 4627 | return entry; |
| 4628 | } |
| 4629 | } |
| 4630 | return NULL; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4631 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4632 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4633 | static void * |
| 4634 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 4635 | { |
| 4636 | (*pos)++; |
| 4637 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 4641 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4642 | struct ftrace_graph_data *fgd = m->private; |
| 4643 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4644 | mutex_lock(&graph_lock); |
| 4645 | |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 4646 | if (fgd->type == GRAPH_FILTER_FUNCTION) |
| 4647 | fgd->hash = rcu_dereference_protected(ftrace_graph_hash, |
| 4648 | lockdep_is_held(&graph_lock)); |
| 4649 | else |
| 4650 | fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 4651 | lockdep_is_held(&graph_lock)); |
| 4652 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4653 | /* Nothing, tell g_show to print all functions are enabled */ |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4654 | if (ftrace_hash_empty(fgd->hash) && !*pos) |
Steven Rostedt (VMware) | 555fc78 | 2017-02-02 10:15:22 -0500 | [diff] [blame] | 4655 | return FTRACE_GRAPH_EMPTY; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4656 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4657 | fgd->idx = 0; |
| 4658 | fgd->entry = NULL; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4659 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | static void g_stop(struct seq_file *m, void *p) |
| 4663 | { |
| 4664 | mutex_unlock(&graph_lock); |
| 4665 | } |
| 4666 | |
| 4667 | static int g_show(struct seq_file *m, void *v) |
| 4668 | { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4669 | struct ftrace_func_entry *entry = v; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4670 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4671 | if (!entry) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4672 | return 0; |
| 4673 | |
Steven Rostedt (VMware) | 555fc78 | 2017-02-02 10:15:22 -0500 | [diff] [blame] | 4674 | if (entry == FTRACE_GRAPH_EMPTY) { |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4675 | struct ftrace_graph_data *fgd = m->private; |
| 4676 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4677 | if (fgd->type == GRAPH_FILTER_FUNCTION) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4678 | seq_puts(m, "#### all functions enabled ####\n"); |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4679 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4680 | seq_puts(m, "#### no functions disabled ####\n"); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4681 | return 0; |
| 4682 | } |
| 4683 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4684 | seq_printf(m, "%ps\n", (void *)entry->ip); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4685 | |
| 4686 | return 0; |
| 4687 | } |
| 4688 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 4689 | static const struct seq_operations ftrace_graph_seq_ops = { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4690 | .start = g_start, |
| 4691 | .next = g_next, |
| 4692 | .stop = g_stop, |
| 4693 | .show = g_show, |
| 4694 | }; |
| 4695 | |
| 4696 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4697 | __ftrace_graph_open(struct inode *inode, struct file *file, |
| 4698 | struct ftrace_graph_data *fgd) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4699 | { |
| 4700 | int ret = 0; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4701 | struct ftrace_hash *new_hash = NULL; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4702 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4703 | if (file->f_mode & FMODE_WRITE) { |
| 4704 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 4705 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4706 | if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX)) |
| 4707 | return -ENOMEM; |
| 4708 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4709 | if (file->f_flags & O_TRUNC) |
| 4710 | new_hash = alloc_ftrace_hash(size_bits); |
| 4711 | else |
| 4712 | new_hash = alloc_and_copy_ftrace_hash(size_bits, |
| 4713 | fgd->hash); |
| 4714 | if (!new_hash) { |
| 4715 | ret = -ENOMEM; |
| 4716 | goto out; |
| 4717 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4718 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4719 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4720 | if (file->f_mode & FMODE_READ) { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4721 | ret = seq_open(file, &ftrace_graph_seq_ops); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4722 | if (!ret) { |
| 4723 | struct seq_file *m = file->private_data; |
| 4724 | m->private = fgd; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4725 | } else { |
| 4726 | /* Failed */ |
| 4727 | free_ftrace_hash(new_hash); |
| 4728 | new_hash = NULL; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4729 | } |
| 4730 | } else |
| 4731 | file->private_data = fgd; |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 4732 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4733 | out: |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4734 | if (ret < 0 && file->f_mode & FMODE_WRITE) |
| 4735 | trace_parser_put(&fgd->parser); |
| 4736 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4737 | fgd->new_hash = new_hash; |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 4738 | |
| 4739 | /* |
| 4740 | * All uses of fgd->hash must be taken with the graph_lock |
| 4741 | * held. The graph_lock is going to be released, so force |
| 4742 | * fgd->hash to be reinitialized when it is taken again. |
| 4743 | */ |
| 4744 | fgd->hash = NULL; |
| 4745 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4746 | return ret; |
| 4747 | } |
| 4748 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4749 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4750 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 4751 | { |
| 4752 | struct ftrace_graph_data *fgd; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4753 | int ret; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4754 | |
| 4755 | if (unlikely(ftrace_disabled)) |
| 4756 | return -ENODEV; |
| 4757 | |
| 4758 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4759 | if (fgd == NULL) |
| 4760 | return -ENOMEM; |
| 4761 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4762 | mutex_lock(&graph_lock); |
| 4763 | |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 4764 | fgd->hash = rcu_dereference_protected(ftrace_graph_hash, |
| 4765 | lockdep_is_held(&graph_lock)); |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4766 | fgd->type = GRAPH_FILTER_FUNCTION; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4767 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4768 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4769 | ret = __ftrace_graph_open(inode, file, fgd); |
| 4770 | if (ret < 0) |
| 4771 | kfree(fgd); |
| 4772 | |
| 4773 | mutex_unlock(&graph_lock); |
| 4774 | return ret; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | static int |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4778 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) |
| 4779 | { |
| 4780 | struct ftrace_graph_data *fgd; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4781 | int ret; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4782 | |
| 4783 | if (unlikely(ftrace_disabled)) |
| 4784 | return -ENODEV; |
| 4785 | |
| 4786 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4787 | if (fgd == NULL) |
| 4788 | return -ENOMEM; |
| 4789 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4790 | mutex_lock(&graph_lock); |
| 4791 | |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 4792 | fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 4793 | lockdep_is_held(&graph_lock)); |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4794 | fgd->type = GRAPH_FILTER_NOTRACE; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4795 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4796 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4797 | ret = __ftrace_graph_open(inode, file, fgd); |
| 4798 | if (ret < 0) |
| 4799 | kfree(fgd); |
| 4800 | |
| 4801 | mutex_unlock(&graph_lock); |
| 4802 | return ret; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | static int |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4806 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 4807 | { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4808 | struct ftrace_graph_data *fgd; |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4809 | struct ftrace_hash *old_hash, *new_hash; |
| 4810 | struct trace_parser *parser; |
| 4811 | int ret = 0; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4812 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4813 | if (file->f_mode & FMODE_READ) { |
| 4814 | struct seq_file *m = file->private_data; |
| 4815 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4816 | fgd = m->private; |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4817 | seq_release(inode, file); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4818 | } else { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4819 | fgd = file->private_data; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4820 | } |
| 4821 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4822 | |
| 4823 | if (file->f_mode & FMODE_WRITE) { |
| 4824 | |
| 4825 | parser = &fgd->parser; |
| 4826 | |
| 4827 | if (trace_parser_loaded((parser))) { |
| 4828 | parser->buffer[parser->idx] = 0; |
| 4829 | ret = ftrace_graph_set_hash(fgd->new_hash, |
| 4830 | parser->buffer); |
| 4831 | } |
| 4832 | |
| 4833 | trace_parser_put(parser); |
| 4834 | |
| 4835 | new_hash = __ftrace_hash_move(fgd->new_hash); |
| 4836 | if (!new_hash) { |
| 4837 | ret = -ENOMEM; |
| 4838 | goto out; |
| 4839 | } |
| 4840 | |
| 4841 | mutex_lock(&graph_lock); |
| 4842 | |
| 4843 | if (fgd->type == GRAPH_FILTER_FUNCTION) { |
| 4844 | old_hash = rcu_dereference_protected(ftrace_graph_hash, |
| 4845 | lockdep_is_held(&graph_lock)); |
| 4846 | rcu_assign_pointer(ftrace_graph_hash, new_hash); |
| 4847 | } else { |
| 4848 | old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 4849 | lockdep_is_held(&graph_lock)); |
| 4850 | rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash); |
| 4851 | } |
| 4852 | |
| 4853 | mutex_unlock(&graph_lock); |
| 4854 | |
| 4855 | /* Wait till all users are no longer using the old hash */ |
| 4856 | synchronize_sched(); |
| 4857 | |
| 4858 | free_ftrace_hash(old_hash); |
| 4859 | } |
| 4860 | |
| 4861 | out: |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4862 | kfree(fgd->new_hash); |
| 4863 | kfree(fgd); |
| 4864 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4865 | return ret; |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | static int |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4869 | ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4870 | { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4871 | struct ftrace_glob func_g; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4872 | struct dyn_ftrace *rec; |
| 4873 | struct ftrace_page *pg; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4874 | struct ftrace_func_entry *entry; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4875 | int fail = 1; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4876 | int not; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4877 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4878 | /* decode regex */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4879 | func_g.type = filter_parse_regex(buffer, strlen(buffer), |
| 4880 | &func_g.search, ¬); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4881 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4882 | func_g.len = strlen(func_g.search); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4883 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4884 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4885 | |
| 4886 | if (unlikely(ftrace_disabled)) { |
| 4887 | mutex_unlock(&ftrace_lock); |
| 4888 | return -ENODEV; |
| 4889 | } |
| 4890 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4891 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4892 | |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 4893 | if (rec->flags & FTRACE_FL_DISABLED) |
| 4894 | continue; |
| 4895 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 4896 | if (ftrace_match_record(rec, &func_g, NULL, 0)) { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4897 | entry = ftrace_lookup_ip(hash, rec->ip); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4898 | |
| 4899 | if (!not) { |
| 4900 | fail = 0; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4901 | |
| 4902 | if (entry) |
| 4903 | continue; |
| 4904 | if (add_hash_entry(hash, rec->ip) < 0) |
| 4905 | goto out; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4906 | } else { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4907 | if (entry) { |
| 4908 | free_hash_entry(hash, entry); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4909 | fail = 0; |
| 4910 | } |
| 4911 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4912 | } |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4913 | } while_for_each_ftrace_rec(); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4914 | out: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4915 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4916 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4917 | if (fail) |
| 4918 | return -EINVAL; |
| 4919 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4920 | return 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | static ssize_t |
| 4924 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 4925 | size_t cnt, loff_t *ppos) |
| 4926 | { |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4927 | ssize_t read, ret = 0; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4928 | struct ftrace_graph_data *fgd = file->private_data; |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4929 | struct trace_parser *parser; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4930 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4931 | if (!cnt) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4932 | return 0; |
| 4933 | |
Steven Rostedt (VMware) | ae98d27 | 2017-02-02 16:59:06 -0500 | [diff] [blame] | 4934 | /* Read mode uses seq functions */ |
| 4935 | if (file->f_mode & FMODE_READ) { |
| 4936 | struct seq_file *m = file->private_data; |
| 4937 | fgd = m->private; |
| 4938 | } |
| 4939 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4940 | parser = &fgd->parser; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4941 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4942 | read = trace_get_user(parser, ubuf, cnt, ppos); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4943 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4944 | if (read >= 0 && trace_parser_loaded(parser) && |
| 4945 | !trace_parser_cont(parser)) { |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4946 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4947 | ret = ftrace_graph_set_hash(fgd->new_hash, |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 4948 | parser->buffer); |
| 4949 | trace_parser_clear(parser); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4950 | } |
| 4951 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4952 | if (!ret) |
| 4953 | ret = read; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 4954 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4955 | return ret; |
| 4956 | } |
| 4957 | |
| 4958 | static const struct file_operations ftrace_graph_fops = { |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4959 | .open = ftrace_graph_open, |
| 4960 | .read = seq_read, |
| 4961 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4962 | .llseek = tracing_lseek, |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4963 | .release = ftrace_graph_release, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4964 | }; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4965 | |
| 4966 | static const struct file_operations ftrace_graph_notrace_fops = { |
| 4967 | .open = ftrace_graph_notrace_open, |
| 4968 | .read = seq_read, |
| 4969 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4970 | .llseek = tracing_lseek, |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4971 | .release = ftrace_graph_release, |
| 4972 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4973 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4974 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4975 | void ftrace_create_filter_files(struct ftrace_ops *ops, |
| 4976 | struct dentry *parent) |
| 4977 | { |
| 4978 | |
| 4979 | trace_create_file("set_ftrace_filter", 0644, parent, |
| 4980 | ops, &ftrace_filter_fops); |
| 4981 | |
| 4982 | trace_create_file("set_ftrace_notrace", 0644, parent, |
| 4983 | ops, &ftrace_notrace_fops); |
| 4984 | } |
| 4985 | |
| 4986 | /* |
| 4987 | * The name "destroy_filter_files" is really a misnomer. Although |
| 4988 | * in the future, it may actualy delete the files, but this is |
| 4989 | * really intended to make sure the ops passed in are disabled |
| 4990 | * and that when this function returns, the caller is free to |
| 4991 | * free the ops. |
| 4992 | * |
| 4993 | * The "destroy" name is only to match the "create" name that this |
| 4994 | * should be paired with. |
| 4995 | */ |
| 4996 | void ftrace_destroy_filter_files(struct ftrace_ops *ops) |
| 4997 | { |
| 4998 | mutex_lock(&ftrace_lock); |
| 4999 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 5000 | ftrace_shutdown(ops, 0); |
| 5001 | ops->flags |= FTRACE_OPS_FL_DELETED; |
| 5002 | mutex_unlock(&ftrace_lock); |
| 5003 | } |
| 5004 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 5005 | static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5006 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5007 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 5008 | trace_create_file("available_filter_functions", 0444, |
| 5009 | d_tracer, NULL, &ftrace_avail_fops); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5010 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 5011 | trace_create_file("enabled_functions", 0444, |
| 5012 | d_tracer, NULL, &ftrace_enabled_fops); |
| 5013 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 5014 | ftrace_create_filter_files(&global_ops, d_tracer); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 5015 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5016 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 5017 | trace_create_file("set_graph_function", 0444, d_tracer, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5018 | NULL, |
| 5019 | &ftrace_graph_fops); |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5020 | trace_create_file("set_graph_notrace", 0444, d_tracer, |
| 5021 | NULL, |
| 5022 | &ftrace_graph_notrace_fops); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5023 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 5024 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5025 | return 0; |
| 5026 | } |
| 5027 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5028 | static int ftrace_cmp_ips(const void *a, const void *b) |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 5029 | { |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5030 | const unsigned long *ipa = a; |
| 5031 | const unsigned long *ipb = b; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 5032 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5033 | if (*ipa > *ipb) |
| 5034 | return 1; |
| 5035 | if (*ipa < *ipb) |
| 5036 | return -1; |
| 5037 | return 0; |
| 5038 | } |
| 5039 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 5040 | static int ftrace_process_locs(struct module *mod, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 5041 | unsigned long *start, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5042 | unsigned long *end) |
| 5043 | { |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5044 | struct ftrace_page *start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5045 | struct ftrace_page *pg; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5046 | struct dyn_ftrace *rec; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5047 | unsigned long count; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5048 | unsigned long *p; |
| 5049 | unsigned long addr; |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5050 | unsigned long flags = 0; /* Shut up gcc */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5051 | int ret = -ENOMEM; |
| 5052 | |
| 5053 | count = end - start; |
| 5054 | |
| 5055 | if (!count) |
| 5056 | return 0; |
| 5057 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5058 | sort(start, count, sizeof(*start), |
Rasmus Villemoes | 6db0290 | 2015-09-09 23:27:02 +0200 | [diff] [blame] | 5059 | ftrace_cmp_ips, NULL); |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5060 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5061 | start_pg = ftrace_allocate_pages(count); |
| 5062 | if (!start_pg) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5063 | return -ENOMEM; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5064 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5065 | mutex_lock(&ftrace_lock); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5066 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5067 | /* |
| 5068 | * Core and each module needs their own pages, as |
| 5069 | * modules will free them when they are removed. |
| 5070 | * Force a new page to be allocated for modules. |
| 5071 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5072 | if (!mod) { |
| 5073 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 5074 | /* First initialization */ |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5075 | ftrace_pages = ftrace_pages_start = start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5076 | } else { |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5077 | if (!ftrace_pages) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5078 | goto out; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5079 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5080 | if (WARN_ON(ftrace_pages->next)) { |
| 5081 | /* Hmm, we have free pages? */ |
| 5082 | while (ftrace_pages->next) |
| 5083 | ftrace_pages = ftrace_pages->next; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5084 | } |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5085 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5086 | ftrace_pages->next = start_pg; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5087 | } |
| 5088 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5089 | p = start; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5090 | pg = start_pg; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5091 | while (p < end) { |
| 5092 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 5093 | /* |
| 5094 | * Some architecture linkers will pad between |
| 5095 | * the different mcount_loc sections of different |
| 5096 | * object files to satisfy alignments. |
| 5097 | * Skip any NULL pointers. |
| 5098 | */ |
| 5099 | if (!addr) |
| 5100 | continue; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5101 | |
| 5102 | if (pg->index == pg->size) { |
| 5103 | /* We should have allocated enough */ |
| 5104 | if (WARN_ON(!pg->next)) |
| 5105 | break; |
| 5106 | pg = pg->next; |
| 5107 | } |
| 5108 | |
| 5109 | rec = &pg->records[pg->index++]; |
| 5110 | rec->ip = addr; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5111 | } |
| 5112 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5113 | /* We should have used all pages */ |
| 5114 | WARN_ON(pg->next); |
| 5115 | |
| 5116 | /* Assign the last page to ftrace_pages */ |
| 5117 | ftrace_pages = pg; |
| 5118 | |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 5119 | /* |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5120 | * We only need to disable interrupts on start up |
| 5121 | * because we are modifying code that an interrupt |
| 5122 | * may execute, and the modification is not atomic. |
| 5123 | * But for modules, nothing runs the code we modify |
| 5124 | * until we are finished with it, and there's no |
| 5125 | * reason to cause large interrupt latencies while we do it. |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 5126 | */ |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5127 | if (!mod) |
| 5128 | local_irq_save(flags); |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 5129 | ftrace_update_code(mod, start_pg); |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5130 | if (!mod) |
| 5131 | local_irq_restore(flags); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5132 | ret = 0; |
| 5133 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5134 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5135 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5136 | return ret; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5137 | } |
| 5138 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5139 | #ifdef CONFIG_MODULES |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5140 | |
| 5141 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 5142 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5143 | static int referenced_filters(struct dyn_ftrace *rec) |
| 5144 | { |
| 5145 | struct ftrace_ops *ops; |
| 5146 | int cnt = 0; |
| 5147 | |
| 5148 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { |
| 5149 | if (ops_references_rec(ops, rec)) |
| 5150 | cnt++; |
| 5151 | } |
| 5152 | |
| 5153 | return cnt; |
| 5154 | } |
| 5155 | |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 5156 | void ftrace_release_mod(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5157 | { |
| 5158 | struct dyn_ftrace *rec; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5159 | struct ftrace_page **last_pg; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5160 | struct ftrace_page *pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5161 | int order; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5162 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5163 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5164 | |
| 5165 | if (ftrace_disabled) |
| 5166 | goto out_unlock; |
| 5167 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5168 | /* |
| 5169 | * Each module has its own ftrace_pages, remove |
| 5170 | * them from the list. |
| 5171 | */ |
| 5172 | last_pg = &ftrace_pages_start; |
| 5173 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 5174 | rec = &pg->records[0]; |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 5175 | if (within_module_core(rec->ip, mod)) { |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5176 | /* |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5177 | * As core pages are first, the first |
| 5178 | * page should never be a module page. |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5179 | */ |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5180 | if (WARN_ON(pg == ftrace_pages_start)) |
| 5181 | goto out_unlock; |
| 5182 | |
| 5183 | /* Check if we are deleting the last page */ |
| 5184 | if (pg == ftrace_pages) |
| 5185 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 5186 | |
| 5187 | *last_pg = pg->next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5188 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 5189 | free_pages((unsigned long)pg->records, order); |
| 5190 | kfree(pg); |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5191 | } else |
| 5192 | last_pg = &pg->next; |
| 5193 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5194 | out_unlock: |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5195 | mutex_unlock(&ftrace_lock); |
| 5196 | } |
| 5197 | |
Jessica Yu | 7dcd182 | 2016-02-16 17:32:33 -0500 | [diff] [blame] | 5198 | void ftrace_module_enable(struct module *mod) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5199 | { |
| 5200 | struct dyn_ftrace *rec; |
| 5201 | struct ftrace_page *pg; |
| 5202 | |
| 5203 | mutex_lock(&ftrace_lock); |
| 5204 | |
| 5205 | if (ftrace_disabled) |
| 5206 | goto out_unlock; |
| 5207 | |
| 5208 | /* |
| 5209 | * If the tracing is enabled, go ahead and enable the record. |
| 5210 | * |
| 5211 | * The reason not to enable the record immediatelly is the |
| 5212 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 5213 | * correct previous instructions. Making first the NOP |
| 5214 | * conversion puts the module to the correct state, thus |
| 5215 | * passing the ftrace_make_call check. |
| 5216 | * |
| 5217 | * We also delay this to after the module code already set the |
| 5218 | * text to read-only, as we now need to set it back to read-write |
| 5219 | * so that we can modify the text. |
| 5220 | */ |
| 5221 | if (ftrace_start_up) |
| 5222 | ftrace_arch_code_modify_prepare(); |
| 5223 | |
| 5224 | do_for_each_ftrace_rec(pg, rec) { |
| 5225 | int cnt; |
| 5226 | /* |
| 5227 | * do_for_each_ftrace_rec() is a double loop. |
| 5228 | * module text shares the pg. If a record is |
| 5229 | * not part of this module, then skip this pg, |
| 5230 | * which the "break" will do. |
| 5231 | */ |
| 5232 | if (!within_module_core(rec->ip, mod)) |
| 5233 | break; |
| 5234 | |
| 5235 | cnt = 0; |
| 5236 | |
| 5237 | /* |
| 5238 | * When adding a module, we need to check if tracers are |
| 5239 | * currently enabled and if they are, and can trace this record, |
| 5240 | * we need to enable the module functions as well as update the |
| 5241 | * reference counts for those function records. |
| 5242 | */ |
| 5243 | if (ftrace_start_up) |
| 5244 | cnt += referenced_filters(rec); |
| 5245 | |
| 5246 | /* This clears FTRACE_FL_DISABLED */ |
| 5247 | rec->flags = cnt; |
| 5248 | |
| 5249 | if (ftrace_start_up && cnt) { |
| 5250 | int failed = __ftrace_replace_code(rec, 1); |
| 5251 | if (failed) { |
| 5252 | ftrace_bug(failed, rec); |
| 5253 | goto out_loop; |
| 5254 | } |
| 5255 | } |
| 5256 | |
| 5257 | } while_for_each_ftrace_rec(); |
| 5258 | |
| 5259 | out_loop: |
| 5260 | if (ftrace_start_up) |
| 5261 | ftrace_arch_code_modify_post_process(); |
| 5262 | |
| 5263 | out_unlock: |
| 5264 | mutex_unlock(&ftrace_lock); |
| 5265 | } |
| 5266 | |
Steven Rostedt (Red Hat) | a949ae5 | 2014-04-24 10:40:12 -0400 | [diff] [blame] | 5267 | void ftrace_module_init(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5268 | { |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5269 | if (ftrace_disabled || !mod->num_ftrace_callsites) |
Abel Vesa | b6b71f6 | 2015-12-02 15:39:57 +0100 | [diff] [blame] | 5270 | return; |
| 5271 | |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5272 | ftrace_process_locs(mod, mod->ftrace_callsites, |
| 5273 | mod->ftrace_callsites + mod->num_ftrace_callsites); |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 5274 | } |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5275 | #endif /* CONFIG_MODULES */ |
| 5276 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5277 | void __init ftrace_init(void) |
| 5278 | { |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 5279 | extern unsigned long __start_mcount_loc[]; |
| 5280 | extern unsigned long __stop_mcount_loc[]; |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 5281 | unsigned long count, flags; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5282 | int ret; |
| 5283 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5284 | local_irq_save(flags); |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 5285 | ret = ftrace_dyn_arch_init(); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5286 | local_irq_restore(flags); |
Jiri Slaby | af64a7c | 2014-02-24 19:59:58 +0100 | [diff] [blame] | 5287 | if (ret) |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5288 | goto failed; |
| 5289 | |
| 5290 | count = __stop_mcount_loc - __start_mcount_loc; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 5291 | if (!count) { |
| 5292 | pr_info("ftrace: No functions to be traced?\n"); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5293 | goto failed; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | pr_info("ftrace: allocating %ld entries in %ld pages\n", |
| 5297 | count, count / ENTRIES_PER_PAGE + 1); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5298 | |
| 5299 | last_ftrace_enabled = ftrace_enabled = 1; |
| 5300 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 5301 | ret = ftrace_process_locs(NULL, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 5302 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5303 | __stop_mcount_loc); |
| 5304 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 5305 | set_ftrace_early_filters(); |
| 5306 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5307 | return; |
| 5308 | failed: |
| 5309 | ftrace_disabled = 1; |
| 5310 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5311 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5312 | /* Do nothing if arch does not support this */ |
| 5313 | void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5314 | { |
| 5315 | } |
| 5316 | |
| 5317 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5318 | { |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 5319 | |
| 5320 | /* |
| 5321 | * Currently there's no safe way to free a trampoline when the kernel |
| 5322 | * is configured with PREEMPT. That is because a task could be preempted |
| 5323 | * when it jumped to the trampoline, it may be preempted for a long time |
| 5324 | * depending on the system load, and currently there's no way to know |
| 5325 | * when it will be off the trampoline. If the trampoline is freed |
| 5326 | * too early, when the task runs again, it will be executing on freed |
| 5327 | * memory and crash. |
| 5328 | */ |
| 5329 | #ifdef CONFIG_PREEMPT |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5330 | /* Currently, only non dynamic ops can have a trampoline */ |
| 5331 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) |
| 5332 | return; |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 5333 | #endif |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5334 | |
| 5335 | arch_ftrace_update_trampoline(ops); |
| 5336 | } |
| 5337 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5338 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5339 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 5340 | static struct ftrace_ops global_ops = { |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 5341 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 5342 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 5343 | FTRACE_OPS_FL_INITIALIZED | |
| 5344 | FTRACE_OPS_FL_PID, |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 5345 | }; |
| 5346 | |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5347 | static int __init ftrace_nodyn_init(void) |
| 5348 | { |
| 5349 | ftrace_enabled = 1; |
| 5350 | return 0; |
| 5351 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 5352 | core_initcall(ftrace_nodyn_init); |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5353 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 5354 | 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] | 5355 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5356 | static inline void ftrace_startup_all(int command) { } |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 5357 | /* 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] | 5358 | # define ftrace_startup(ops, command) \ |
| 5359 | ({ \ |
| 5360 | int ___ret = __register_ftrace_function(ops); \ |
| 5361 | if (!___ret) \ |
| 5362 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ |
| 5363 | ___ret; \ |
Steven Rostedt | 3b6cfdb | 2011-05-23 15:33:49 -0400 | [diff] [blame] | 5364 | }) |
Steven Rostedt (Red Hat) | 1fcc155 | 2014-02-19 15:12:18 -0500 | [diff] [blame] | 5365 | # define ftrace_shutdown(ops, command) \ |
| 5366 | ({ \ |
| 5367 | int ___ret = __unregister_ftrace_function(ops); \ |
| 5368 | if (!___ret) \ |
| 5369 | (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \ |
| 5370 | ___ret; \ |
| 5371 | }) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5372 | |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 5373 | # define ftrace_startup_sysctl() do { } while (0) |
| 5374 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5375 | |
| 5376 | static inline int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 5377 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5378 | { |
| 5379 | return 1; |
| 5380 | } |
| 5381 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5382 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5383 | { |
| 5384 | } |
| 5385 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5386 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 5387 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5388 | __init void ftrace_init_global_array_ops(struct trace_array *tr) |
| 5389 | { |
| 5390 | tr->ops = &global_ops; |
| 5391 | tr->ops->private = tr; |
| 5392 | } |
| 5393 | |
| 5394 | void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) |
| 5395 | { |
| 5396 | /* If we filter on pids, update to use the pid function */ |
| 5397 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { |
| 5398 | if (WARN_ON(tr->ops->func != ftrace_stub)) |
| 5399 | printk("ftrace ops had %pS for function\n", |
| 5400 | tr->ops->func); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5401 | } |
| 5402 | tr->ops->func = func; |
| 5403 | tr->ops->private = tr; |
| 5404 | } |
| 5405 | |
| 5406 | void ftrace_reset_array_ops(struct trace_array *tr) |
| 5407 | { |
| 5408 | tr->ops->func = ftrace_stub; |
| 5409 | } |
| 5410 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5411 | static inline void |
| 5412 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5413 | struct ftrace_ops *ignored, struct pt_regs *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5414 | { |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5415 | struct ftrace_ops *op; |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5416 | int bit; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5417 | |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5418 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5419 | if (bit < 0) |
| 5420 | return; |
Steven Rostedt | c29f122 | 2012-11-02 17:17:59 -0400 | [diff] [blame] | 5421 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5422 | /* |
| 5423 | * Some of the ops may be dynamically allocated, |
| 5424 | * they must be freed after a synchronize_sched(). |
| 5425 | */ |
| 5426 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 5427 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5428 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 5429 | /* |
| 5430 | * Check the following for each ops before calling their func: |
| 5431 | * if RCU flag is set, then rcu_is_watching() must be true |
| 5432 | * if PER_CPU is set, then ftrace_function_local_disable() |
| 5433 | * must be false |
| 5434 | * Otherwise test if the ip matches the ops filter |
| 5435 | * |
| 5436 | * If any of the above fails then the op->func() is not executed. |
| 5437 | */ |
| 5438 | if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) && |
| 5439 | (!(op->flags & FTRACE_OPS_FL_PER_CPU) || |
| 5440 | !ftrace_function_local_disabled(op)) && |
| 5441 | ftrace_ops_test(op, ip, regs)) { |
| 5442 | |
Steven Rostedt (Red Hat) | 1d48d59 | 2014-06-25 11:54:03 -0400 | [diff] [blame] | 5443 | if (FTRACE_WARN_ON(!op->func)) { |
| 5444 | pr_warn("op=%p %pS\n", op, op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5445 | goto out; |
| 5446 | } |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5447 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5448 | } |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5449 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5450 | out: |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5451 | preempt_enable_notrace(); |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5452 | trace_clear_recursion(bit); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5453 | } |
| 5454 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5455 | /* |
| 5456 | * Some archs only support passing ip and parent_ip. Even though |
| 5457 | * the list function ignores the op parameter, we do not want any |
| 5458 | * C side effects, where a function is called without the caller |
| 5459 | * sending a third parameter. |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5460 | * Archs are to support both the regs and ftrace_ops at the same time. |
| 5461 | * If they support ftrace_ops, it is assumed they support regs. |
| 5462 | * 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] | 5463 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
| 5464 | * 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] | 5465 | * An architecture can pass partial regs with ftrace_ops and still |
Li Bin | b8ec330 | 2015-11-30 18:23:36 +0800 | [diff] [blame] | 5466 | * set the ARCH_SUPPORTS_FTRACE_OPS. |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5467 | */ |
| 5468 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 5469 | 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] | 5470 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5471 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5472 | __ftrace_ops_list_func(ip, parent_ip, NULL, regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5473 | } |
| 5474 | #else |
| 5475 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip) |
| 5476 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5477 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5478 | } |
| 5479 | #endif |
| 5480 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5481 | /* |
| 5482 | * 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] | 5483 | * recursion, needs RCU protection and/or requires per cpu handling, then |
| 5484 | * this function will be called by the mcount trampoline. |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5485 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5486 | 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] | 5487 | struct ftrace_ops *op, struct pt_regs *regs) |
| 5488 | { |
| 5489 | int bit; |
| 5490 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5491 | if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching()) |
| 5492 | return; |
| 5493 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5494 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5495 | if (bit < 0) |
| 5496 | return; |
| 5497 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5498 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5499 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5500 | if (!(op->flags & FTRACE_OPS_FL_PER_CPU) || |
| 5501 | !ftrace_function_local_disabled(op)) { |
| 5502 | op->func(ip, parent_ip, op, regs); |
| 5503 | } |
| 5504 | |
| 5505 | preempt_enable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5506 | trace_clear_recursion(bit); |
| 5507 | } |
| 5508 | |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5509 | /** |
| 5510 | * ftrace_ops_get_func - get the function a trampoline should call |
| 5511 | * @ops: the ops to get the function for |
| 5512 | * |
| 5513 | * Normally the mcount trampoline will call the ops->func, but there |
| 5514 | * are times that it should not. For example, if the ops does not |
| 5515 | * have its own recursion protection, then it should call the |
Chunyu Hu | 3a150df | 2017-02-22 08:29:26 +0800 | [diff] [blame] | 5516 | * ftrace_ops_assist_func() instead. |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5517 | * |
| 5518 | * Returns the function that the trampoline should call for @ops. |
| 5519 | */ |
| 5520 | ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) |
| 5521 | { |
| 5522 | /* |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5523 | * If the function does not handle recursion, needs to be RCU safe, |
| 5524 | * 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] | 5525 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5526 | if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) || |
| 5527 | ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU)) |
| 5528 | return ftrace_ops_assist_func; |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5529 | |
| 5530 | return ops->func; |
| 5531 | } |
| 5532 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5533 | static void |
| 5534 | ftrace_filter_pid_sched_switch_probe(void *data, bool preempt, |
| 5535 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5536 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5537 | struct trace_array *tr = data; |
| 5538 | struct trace_pid_list *pid_list; |
| 5539 | |
| 5540 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 5541 | |
| 5542 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 5543 | trace_ignore_this_task(pid_list, next)); |
| 5544 | } |
| 5545 | |
| 5546 | static void clear_ftrace_pids(struct trace_array *tr) |
| 5547 | { |
| 5548 | struct trace_pid_list *pid_list; |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5549 | int cpu; |
| 5550 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5551 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 5552 | lockdep_is_held(&ftrace_lock)); |
| 5553 | if (!pid_list) |
| 5554 | return; |
| 5555 | |
| 5556 | unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 5557 | |
| 5558 | for_each_possible_cpu(cpu) |
| 5559 | per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false; |
| 5560 | |
| 5561 | rcu_assign_pointer(tr->function_pids, NULL); |
| 5562 | |
| 5563 | /* Wait till all users are no longer using pid filtering */ |
| 5564 | synchronize_sched(); |
| 5565 | |
| 5566 | trace_free_pid_list(pid_list); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5567 | } |
| 5568 | |
Namhyung Kim | d879d0b | 2017-04-17 11:44:27 +0900 | [diff] [blame] | 5569 | void ftrace_clear_pids(struct trace_array *tr) |
| 5570 | { |
| 5571 | mutex_lock(&ftrace_lock); |
| 5572 | |
| 5573 | clear_ftrace_pids(tr); |
| 5574 | |
| 5575 | mutex_unlock(&ftrace_lock); |
| 5576 | } |
| 5577 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5578 | static void ftrace_pid_reset(struct trace_array *tr) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5579 | { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5580 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5581 | clear_ftrace_pids(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5582 | |
| 5583 | ftrace_update_pid_func(); |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5584 | ftrace_startup_all(0); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5585 | |
| 5586 | mutex_unlock(&ftrace_lock); |
| 5587 | } |
| 5588 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5589 | /* Greater than any max PID */ |
| 5590 | #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1) |
| 5591 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5592 | 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] | 5593 | __acquires(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5594 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5595 | struct trace_pid_list *pid_list; |
| 5596 | struct trace_array *tr = m->private; |
| 5597 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5598 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5599 | rcu_read_lock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5600 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5601 | pid_list = rcu_dereference_sched(tr->function_pids); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5602 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5603 | if (!pid_list) |
| 5604 | return !(*pos) ? FTRACE_NO_PIDS : NULL; |
| 5605 | |
| 5606 | return trace_pid_start(pid_list, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 5610 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5611 | struct trace_array *tr = m->private; |
| 5612 | struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids); |
| 5613 | |
| 5614 | if (v == FTRACE_NO_PIDS) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5615 | return NULL; |
| 5616 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5617 | return trace_pid_next(pid_list, v, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | static void fpid_stop(struct seq_file *m, void *p) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5621 | __releases(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5622 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5623 | rcu_read_unlock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5624 | mutex_unlock(&ftrace_lock); |
| 5625 | } |
| 5626 | |
| 5627 | static int fpid_show(struct seq_file *m, void *v) |
| 5628 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5629 | if (v == FTRACE_NO_PIDS) { |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 5630 | seq_puts(m, "no pid\n"); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5631 | return 0; |
| 5632 | } |
| 5633 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5634 | return trace_pid_show(m, v); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | static const struct seq_operations ftrace_pid_sops = { |
| 5638 | .start = fpid_start, |
| 5639 | .next = fpid_next, |
| 5640 | .stop = fpid_stop, |
| 5641 | .show = fpid_show, |
| 5642 | }; |
| 5643 | |
| 5644 | static int |
| 5645 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 5646 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5647 | struct trace_array *tr = inode->i_private; |
| 5648 | struct seq_file *m; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5649 | int ret = 0; |
| 5650 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5651 | if (trace_array_get(tr) < 0) |
| 5652 | return -ENODEV; |
| 5653 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5654 | if ((file->f_mode & FMODE_WRITE) && |
| 5655 | (file->f_flags & O_TRUNC)) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5656 | ftrace_pid_reset(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5657 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5658 | ret = seq_open(file, &ftrace_pid_sops); |
| 5659 | if (ret < 0) { |
| 5660 | trace_array_put(tr); |
| 5661 | } else { |
| 5662 | m = file->private_data; |
| 5663 | /* copy tr over to seq ops */ |
| 5664 | m->private = tr; |
| 5665 | } |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5666 | |
| 5667 | return ret; |
| 5668 | } |
| 5669 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5670 | static void ignore_task_cpu(void *data) |
| 5671 | { |
| 5672 | struct trace_array *tr = data; |
| 5673 | struct trace_pid_list *pid_list; |
| 5674 | |
| 5675 | /* |
| 5676 | * This function is called by on_each_cpu() while the |
| 5677 | * event_mutex is held. |
| 5678 | */ |
| 5679 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 5680 | mutex_is_locked(&ftrace_lock)); |
| 5681 | |
| 5682 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 5683 | trace_ignore_this_task(pid_list, current)); |
| 5684 | } |
| 5685 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5686 | static ssize_t |
| 5687 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 5688 | size_t cnt, loff_t *ppos) |
| 5689 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5690 | struct seq_file *m = filp->private_data; |
| 5691 | struct trace_array *tr = m->private; |
| 5692 | struct trace_pid_list *filtered_pids = NULL; |
| 5693 | struct trace_pid_list *pid_list; |
| 5694 | ssize_t ret; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5695 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5696 | if (!cnt) |
| 5697 | return 0; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5698 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5699 | mutex_lock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5700 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5701 | filtered_pids = rcu_dereference_protected(tr->function_pids, |
| 5702 | lockdep_is_held(&ftrace_lock)); |
| 5703 | |
| 5704 | ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); |
| 5705 | if (ret < 0) |
| 5706 | goto out; |
| 5707 | |
| 5708 | rcu_assign_pointer(tr->function_pids, pid_list); |
| 5709 | |
| 5710 | if (filtered_pids) { |
| 5711 | synchronize_sched(); |
| 5712 | trace_free_pid_list(filtered_pids); |
| 5713 | } else if (pid_list) { |
| 5714 | /* Register a probe to set whether to ignore the tracing of a task */ |
| 5715 | register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 5716 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5717 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5718 | /* |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5719 | * Ignoring of pids is done at task switch. But we have to |
| 5720 | * check for those tasks that are currently running. |
| 5721 | * Always do this in case a pid was appended or removed. |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5722 | */ |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5723 | on_each_cpu(ignore_task_cpu, tr, 1); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5724 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5725 | ftrace_update_pid_func(); |
| 5726 | ftrace_startup_all(0); |
| 5727 | out: |
| 5728 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5729 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5730 | if (ret > 0) |
| 5731 | *ppos += ret; |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5732 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5733 | return ret; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5734 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5735 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5736 | static int |
| 5737 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 5738 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5739 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5740 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5741 | trace_array_put(tr); |
| 5742 | |
| 5743 | return seq_release(inode, file); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5744 | } |
| 5745 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 5746 | static const struct file_operations ftrace_pid_fops = { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5747 | .open = ftrace_pid_open, |
| 5748 | .write = ftrace_pid_write, |
| 5749 | .read = seq_read, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5750 | .llseek = tracing_lseek, |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5751 | .release = ftrace_pid_release, |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5752 | }; |
| 5753 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5754 | void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5755 | { |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 5756 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5757 | tr, &ftrace_pid_fops); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5758 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5759 | |
Steven Rostedt (Red Hat) | 501c237 | 2016-07-05 10:04:34 -0400 | [diff] [blame] | 5760 | void __init ftrace_init_tracefs_toplevel(struct trace_array *tr, |
| 5761 | struct dentry *d_tracer) |
| 5762 | { |
| 5763 | /* Only the top level directory has the dyn_tracefs and profile */ |
| 5764 | WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); |
| 5765 | |
| 5766 | ftrace_init_dyn_tracefs(d_tracer); |
| 5767 | ftrace_profile_tracefs(d_tracer); |
| 5768 | } |
| 5769 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5770 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5771 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5772 | * |
| 5773 | * This function should be used by panic code. It stops ftrace |
| 5774 | * but in a not so nice way. If you need to simply kill ftrace |
| 5775 | * from a non-atomic section, use ftrace_kill. |
| 5776 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5777 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5778 | { |
| 5779 | ftrace_disabled = 1; |
| 5780 | ftrace_enabled = 0; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5781 | clear_ftrace_function(); |
| 5782 | } |
| 5783 | |
| 5784 | /** |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 5785 | * Test if ftrace is dead or not. |
| 5786 | */ |
| 5787 | int ftrace_is_dead(void) |
| 5788 | { |
| 5789 | return ftrace_disabled; |
| 5790 | } |
| 5791 | |
| 5792 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5793 | * register_ftrace_function - register a function for profiling |
| 5794 | * @ops - ops structure that holds the function for profiling. |
| 5795 | * |
| 5796 | * Register a function to be called by all functions in the |
| 5797 | * kernel. |
| 5798 | * |
| 5799 | * Note: @ops->func and all the functions it calls must be labeled |
| 5800 | * with "notrace", otherwise it will go into a |
| 5801 | * recursive loop. |
| 5802 | */ |
| 5803 | int register_ftrace_function(struct ftrace_ops *ops) |
| 5804 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5805 | int ret = -1; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5806 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 5807 | ftrace_ops_init(ops); |
| 5808 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5809 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5810 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5811 | ret = ftrace_startup(ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5812 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5813 | mutex_unlock(&ftrace_lock); |
Borislav Petkov | 8d240dd | 2012-03-29 19:11:40 +0200 | [diff] [blame] | 5814 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5815 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5816 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5817 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5818 | |
| 5819 | /** |
Uwe Kleine-Koenig | 3263292 | 2009-01-12 23:35:50 +0100 | [diff] [blame] | 5820 | * unregister_ftrace_function - unregister a function for profiling. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5821 | * @ops - ops structure that holds the function to unregister |
| 5822 | * |
| 5823 | * Unregister a function that was added to be called by ftrace profiling. |
| 5824 | */ |
| 5825 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 5826 | { |
| 5827 | int ret; |
| 5828 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5829 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5830 | ret = ftrace_shutdown(ops, 0); |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5831 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5832 | |
| 5833 | return ret; |
| 5834 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5835 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5836 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 5837 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5838 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 5839 | void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5840 | loff_t *ppos) |
| 5841 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5842 | int ret = -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5843 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5844 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5845 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5846 | if (unlikely(ftrace_disabled)) |
| 5847 | goto out; |
| 5848 | |
| 5849 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5850 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5851 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5852 | goto out; |
| 5853 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5854 | last_ftrace_enabled = !!ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5855 | |
| 5856 | if (ftrace_enabled) { |
| 5857 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5858 | /* we are starting ftrace again */ |
Jan Kiszka | 5000c41 | 2013-03-26 17:53:03 +0100 | [diff] [blame] | 5859 | if (ftrace_ops_list != &ftrace_list_end) |
| 5860 | update_ftrace_function(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5861 | |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 5862 | ftrace_startup_sysctl(); |
| 5863 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5864 | } else { |
| 5865 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 5866 | ftrace_trace_function = ftrace_stub; |
| 5867 | |
| 5868 | ftrace_shutdown_sysctl(); |
| 5869 | } |
| 5870 | |
| 5871 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5872 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5873 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5874 | } |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 5875 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5876 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5877 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5878 | static struct ftrace_ops graph_ops = { |
| 5879 | .func = ftrace_stub, |
| 5880 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 5881 | FTRACE_OPS_FL_INITIALIZED | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 5882 | FTRACE_OPS_FL_PID | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5883 | FTRACE_OPS_FL_STUB, |
| 5884 | #ifdef FTRACE_GRAPH_TRAMP_ADDR |
| 5885 | .trampoline = FTRACE_GRAPH_TRAMP_ADDR, |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 5886 | /* trampoline_size is only needed for dynamically allocated tramps */ |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5887 | #endif |
| 5888 | ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash) |
| 5889 | }; |
| 5890 | |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 5891 | void ftrace_graph_sleep_time_control(bool enable) |
| 5892 | { |
| 5893 | fgraph_sleep_time = enable; |
| 5894 | } |
| 5895 | |
| 5896 | void ftrace_graph_graph_time_control(bool enable) |
| 5897 | { |
| 5898 | fgraph_graph_time = enable; |
| 5899 | } |
| 5900 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5901 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 5902 | { |
| 5903 | return 0; |
| 5904 | } |
| 5905 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5906 | /* The callbacks that hook a function */ |
| 5907 | trace_func_graph_ret_t ftrace_graph_return = |
| 5908 | (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5909 | 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] | 5910 | 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] | 5911 | |
| 5912 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 5913 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 5914 | { |
| 5915 | int i; |
| 5916 | int ret = 0; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5917 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 5918 | struct task_struct *g, *t; |
| 5919 | |
| 5920 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 5921 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH |
| 5922 | * sizeof(struct ftrace_ret_stack), |
| 5923 | GFP_KERNEL); |
| 5924 | if (!ret_stack_list[i]) { |
| 5925 | start = 0; |
| 5926 | end = i; |
| 5927 | ret = -ENOMEM; |
| 5928 | goto free; |
| 5929 | } |
| 5930 | } |
| 5931 | |
Soumya PN | 6112a30 | 2016-05-17 21:31:14 +0530 | [diff] [blame] | 5932 | read_lock(&tasklist_lock); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5933 | do_each_thread(g, t) { |
| 5934 | if (start == end) { |
| 5935 | ret = -EAGAIN; |
| 5936 | goto unlock; |
| 5937 | } |
| 5938 | |
| 5939 | if (t->ret_stack == NULL) { |
Frederic Weisbecker | 380c4b1 | 2008-12-06 03:43:41 +0100 | [diff] [blame] | 5940 | atomic_set(&t->tracing_graph_pause, 0); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5941 | atomic_set(&t->trace_overrun, 0); |
Steven Rostedt | 26c0162 | 2009-06-02 14:01:19 -0400 | [diff] [blame] | 5942 | t->curr_ret_stack = -1; |
| 5943 | /* Make sure the tasks see the -1 first: */ |
| 5944 | smp_wmb(); |
| 5945 | t->ret_stack = ret_stack_list[start++]; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5946 | } |
| 5947 | } while_each_thread(g, t); |
| 5948 | |
| 5949 | unlock: |
Soumya PN | 6112a30 | 2016-05-17 21:31:14 +0530 | [diff] [blame] | 5950 | read_unlock(&tasklist_lock); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5951 | free: |
| 5952 | for (i = start; i < end; i++) |
| 5953 | kfree(ret_stack_list[i]); |
| 5954 | return ret; |
| 5955 | } |
| 5956 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5957 | static void |
Peter Zijlstra | c73464b | 2015-09-28 18:06:56 +0200 | [diff] [blame] | 5958 | ftrace_graph_probe_sched_switch(void *ignore, bool preempt, |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5959 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5960 | { |
| 5961 | unsigned long long timestamp; |
| 5962 | int index; |
| 5963 | |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 5964 | /* |
| 5965 | * Does the user want to count the time a function was asleep. |
| 5966 | * If so, do not update the time stamps. |
| 5967 | */ |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 5968 | if (fgraph_sleep_time) |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 5969 | return; |
| 5970 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5971 | timestamp = trace_clock_local(); |
| 5972 | |
| 5973 | prev->ftrace_timestamp = timestamp; |
| 5974 | |
| 5975 | /* only process tasks that we timestamped */ |
| 5976 | if (!next->ftrace_timestamp) |
| 5977 | return; |
| 5978 | |
| 5979 | /* |
| 5980 | * Update all the counters in next to make up for the |
| 5981 | * time next was sleeping. |
| 5982 | */ |
| 5983 | timestamp -= next->ftrace_timestamp; |
| 5984 | |
| 5985 | for (index = next->curr_ret_stack; index >= 0; index--) |
| 5986 | next->ret_stack[index].calltime += timestamp; |
| 5987 | } |
| 5988 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5989 | /* Allocate a return stack for each task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5990 | static int start_graph_tracing(void) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5991 | { |
| 5992 | struct ftrace_ret_stack **ret_stack_list; |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5993 | int ret, cpu; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5994 | |
| 5995 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * |
| 5996 | sizeof(struct ftrace_ret_stack *), |
| 5997 | GFP_KERNEL); |
| 5998 | |
| 5999 | if (!ret_stack_list) |
| 6000 | return -ENOMEM; |
| 6001 | |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 6002 | /* The cpu_boot init_task->ret_stack will never be freed */ |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 6003 | for_each_online_cpu(cpu) { |
| 6004 | if (!idle_task(cpu)->ret_stack) |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 6005 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 6006 | } |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 6007 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6008 | do { |
| 6009 | ret = alloc_retstack_tasklist(ret_stack_list); |
| 6010 | } while (ret == -EAGAIN); |
| 6011 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 6012 | if (!ret) { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 6013 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 6014 | if (ret) |
| 6015 | pr_info("ftrace_graph: Couldn't activate tracepoint" |
| 6016 | " probe to kernel_sched_switch\n"); |
| 6017 | } |
| 6018 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6019 | kfree(ret_stack_list); |
| 6020 | return ret; |
| 6021 | } |
| 6022 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 6023 | /* |
| 6024 | * Hibernation protection. |
| 6025 | * The state of the current task is too much unstable during |
| 6026 | * suspend/restore to disk. We want to protect against that. |
| 6027 | */ |
| 6028 | static int |
| 6029 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, |
| 6030 | void *unused) |
| 6031 | { |
| 6032 | switch (state) { |
| 6033 | case PM_HIBERNATION_PREPARE: |
| 6034 | pause_graph_tracing(); |
| 6035 | break; |
| 6036 | |
| 6037 | case PM_POST_HIBERNATION: |
| 6038 | unpause_graph_tracing(); |
| 6039 | break; |
| 6040 | } |
| 6041 | return NOTIFY_DONE; |
| 6042 | } |
| 6043 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 6044 | static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) |
| 6045 | { |
| 6046 | if (!ftrace_ops_test(&global_ops, trace->func, NULL)) |
| 6047 | return 0; |
| 6048 | return __ftrace_graph_entry(trace); |
| 6049 | } |
| 6050 | |
| 6051 | /* |
| 6052 | * The function graph tracer should only trace the functions defined |
| 6053 | * by set_ftrace_filter and set_ftrace_notrace. If another function |
| 6054 | * tracer ops is registered, the graph tracer requires testing the |
| 6055 | * function against the global ops, and not just trace any function |
| 6056 | * that any ftrace_ops registered. |
| 6057 | */ |
| 6058 | static void update_function_graph_func(void) |
| 6059 | { |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 6060 | struct ftrace_ops *op; |
| 6061 | bool do_test = false; |
| 6062 | |
| 6063 | /* |
| 6064 | * The graph and global ops share the same set of functions |
| 6065 | * to test. If any other ops is on the list, then |
| 6066 | * the graph tracing needs to test if its the function |
| 6067 | * it should call. |
| 6068 | */ |
| 6069 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 6070 | if (op != &global_ops && op != &graph_ops && |
| 6071 | op != &ftrace_list_end) { |
| 6072 | do_test = true; |
| 6073 | /* in double loop, break out with goto */ |
| 6074 | goto out; |
| 6075 | } |
| 6076 | } while_for_each_ftrace_op(op); |
| 6077 | out: |
| 6078 | if (do_test) |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 6079 | ftrace_graph_entry = ftrace_graph_entry_test; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 6080 | else |
| 6081 | ftrace_graph_entry = __ftrace_graph_entry; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 6082 | } |
| 6083 | |
Mathias Krause | 8275f69 | 2014-03-30 15:31:50 +0200 | [diff] [blame] | 6084 | static struct notifier_block ftrace_suspend_notifier = { |
| 6085 | .notifier_call = ftrace_suspend_notifier_call, |
| 6086 | }; |
| 6087 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 6088 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 6089 | trace_func_graph_ent_t entryfunc) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 6090 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 6091 | int ret = 0; |
| 6092 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6093 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 6094 | |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 6095 | /* we currently allow only one tracer registered at a time */ |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6096 | if (ftrace_graph_active) { |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 6097 | ret = -EBUSY; |
| 6098 | goto out; |
| 6099 | } |
| 6100 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 6101 | register_pm_notifier(&ftrace_suspend_notifier); |
| 6102 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6103 | ftrace_graph_active++; |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6104 | ret = start_graph_tracing(); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6105 | if (ret) { |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6106 | ftrace_graph_active--; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6107 | goto out; |
| 6108 | } |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 6109 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 6110 | ftrace_graph_return = retfunc; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 6111 | |
| 6112 | /* |
| 6113 | * Update the indirect function to the entryfunc, and the |
| 6114 | * function that gets called to the entry_test first. Then |
| 6115 | * call the update fgraph entry function to determine if |
| 6116 | * the entryfunc should be called directly or not. |
| 6117 | */ |
| 6118 | __ftrace_graph_entry = entryfunc; |
| 6119 | ftrace_graph_entry = ftrace_graph_entry_test; |
| 6120 | update_function_graph_func(); |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 6121 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 6122 | ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 6123 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6124 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 6125 | return ret; |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 6126 | } |
| 6127 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6128 | void unregister_ftrace_graph(void) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 6129 | { |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6130 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 6131 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6132 | if (unlikely(!ftrace_graph_active)) |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 6133 | goto out; |
| 6134 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6135 | ftrace_graph_active--; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 6136 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 6137 | ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 6138 | __ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 6139 | ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET); |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 6140 | unregister_pm_notifier(&ftrace_suspend_notifier); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 6141 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 6142 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 6143 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 6144 | /* |
| 6145 | * Function graph does not allocate the trampoline, but |
| 6146 | * other global_ops do. We need to reset the ALLOC_TRAMP flag |
| 6147 | * if one was used. |
| 6148 | */ |
| 6149 | global_ops.trampoline = save_global_trampoline; |
| 6150 | if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP) |
| 6151 | global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP; |
| 6152 | #endif |
| 6153 | |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 6154 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6155 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 6156 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6157 | |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 6158 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
| 6159 | |
| 6160 | static void |
| 6161 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) |
| 6162 | { |
| 6163 | atomic_set(&t->tracing_graph_pause, 0); |
| 6164 | atomic_set(&t->trace_overrun, 0); |
| 6165 | t->ftrace_timestamp = 0; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6166 | /* make curr_ret_stack visible before we add the ret_stack */ |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 6167 | smp_wmb(); |
| 6168 | t->ret_stack = ret_stack; |
| 6169 | } |
| 6170 | |
| 6171 | /* |
| 6172 | * Allocate a return stack for the idle task. May be the first |
| 6173 | * time through, or it may be done by CPU hotplug online. |
| 6174 | */ |
| 6175 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) |
| 6176 | { |
| 6177 | t->curr_ret_stack = -1; |
| 6178 | /* |
| 6179 | * The idle task has no parent, it either has its own |
| 6180 | * stack or no stack at all. |
| 6181 | */ |
| 6182 | if (t->ret_stack) |
| 6183 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); |
| 6184 | |
| 6185 | if (ftrace_graph_active) { |
| 6186 | struct ftrace_ret_stack *ret_stack; |
| 6187 | |
| 6188 | ret_stack = per_cpu(idle_ret_stack, cpu); |
| 6189 | if (!ret_stack) { |
| 6190 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
| 6191 | * sizeof(struct ftrace_ret_stack), |
| 6192 | GFP_KERNEL); |
| 6193 | if (!ret_stack) |
| 6194 | return; |
| 6195 | per_cpu(idle_ret_stack, cpu) = ret_stack; |
| 6196 | } |
| 6197 | graph_init_task(t, ret_stack); |
| 6198 | } |
| 6199 | } |
| 6200 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6201 | /* Allocate a return stack for newly created task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6202 | void ftrace_graph_init_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6203 | { |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6204 | /* Make sure we do not use the parent ret_stack */ |
| 6205 | t->ret_stack = NULL; |
Steven Rostedt | ea14eb7 | 2010-03-12 19:41:23 -0500 | [diff] [blame] | 6206 | t->curr_ret_stack = -1; |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6207 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6208 | if (ftrace_graph_active) { |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 6209 | struct ftrace_ret_stack *ret_stack; |
| 6210 | |
| 6211 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6212 | * sizeof(struct ftrace_ret_stack), |
| 6213 | GFP_KERNEL); |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 6214 | if (!ret_stack) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6215 | return; |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 6216 | graph_init_task(t, ret_stack); |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6217 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6218 | } |
| 6219 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6220 | void ftrace_graph_exit_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6221 | { |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 6222 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 6223 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6224 | t->ret_stack = NULL; |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 6225 | /* NULL must become visible to IRQs before we free it: */ |
| 6226 | barrier(); |
| 6227 | |
| 6228 | kfree(ret_stack); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6229 | } |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 6230 | #endif |