Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Infrastructure for profiling code inserted by 'gcc -pg'. |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 5 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> |
| 6 | * |
| 7 | * Originally ported from the -rt patch by: |
| 8 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 9 | * |
| 10 | * Based on code in the latency_tracer, that is: |
| 11 | * |
| 12 | * Copyright (C) 2004-2006 Ingo Molnar |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 13 | * Copyright (C) 2004 Nadia Yvette Chambers |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 16 | #include <linux/stop_machine.h> |
| 17 | #include <linux/clocksource.h> |
| 18 | #include <linux/kallsyms.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 20 | #include <linux/suspend.h> |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 21 | #include <linux/tracefs.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 22 | #include <linux/hardirq.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 23 | #include <linux/kthread.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 25 | #include <linux/bsearch.h> |
Paul Gortmaker | 56d82e0 | 2011-05-26 17:53:52 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 27 | #include <linux/ftrace.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 28 | #include <linux/sysctl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 30 | #include <linux/ctype.h> |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 31 | #include <linux/sort.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 32 | #include <linux/list.h> |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 33 | #include <linux/hash.h> |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 34 | #include <linux/rcupdate.h> |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 35 | #include <linux/kprobes.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 36 | |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 37 | #include <trace/events/sched.h> |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 38 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 39 | #include <asm/setup.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 40 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 41 | #include "trace_output.h" |
Steven Rostedt | bac429f | 2009-03-20 12:50:56 -0400 | [diff] [blame] | 42 | #include "trace_stat.h" |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 43 | |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 44 | #define FTRACE_WARN_ON(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 45 | ({ \ |
| 46 | int ___r = cond; \ |
| 47 | if (WARN_ON(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 48 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 49 | ___r; \ |
| 50 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 51 | |
| 52 | #define FTRACE_WARN_ON_ONCE(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 53 | ({ \ |
| 54 | int ___r = cond; \ |
| 55 | if (WARN_ON_ONCE(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 56 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 57 | ___r; \ |
| 58 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 59 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 60 | /* hash bits for specific function selection */ |
| 61 | #define FTRACE_HASH_BITS 7 |
| 62 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 63 | #define FTRACE_HASH_DEFAULT_BITS 10 |
| 64 | #define FTRACE_HASH_MAX_BITS 12 |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 65 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 66 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 67 | #define INIT_OPS_HASH(opsname) \ |
| 68 | .func_hash = &opsname.local_hash, \ |
| 69 | .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 70 | #define ASSIGN_OPS_HASH(opsname, val) \ |
| 71 | .func_hash = val, \ |
| 72 | .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 73 | #else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 74 | #define INIT_OPS_HASH(opsname) |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 75 | #define ASSIGN_OPS_HASH(opsname, val) |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 76 | #endif |
| 77 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 78 | static struct ftrace_ops ftrace_list_end __read_mostly = { |
| 79 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | 395b97a | 2013-03-27 09:31:28 -0400 | [diff] [blame] | 80 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 81 | INIT_OPS_HASH(ftrace_list_end) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 84 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 85 | int ftrace_enabled __read_mostly; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 86 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 87 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 88 | /* Current function tracing op */ |
| 89 | struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 90 | /* What to set function_trace_op to */ |
| 91 | static struct ftrace_ops *set_function_trace_op; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 92 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 93 | static bool ftrace_pids_enabled(struct ftrace_ops *ops) |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 94 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 95 | struct trace_array *tr; |
| 96 | |
| 97 | if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private) |
| 98 | return false; |
| 99 | |
| 100 | tr = ops->private; |
| 101 | |
| 102 | return tr->function_pids != NULL; |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 106 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 107 | /* |
| 108 | * ftrace_disabled is set when an anomaly is discovered. |
| 109 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 110 | */ |
| 111 | static int ftrace_disabled __read_mostly; |
| 112 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 113 | static DEFINE_MUTEX(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 114 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 115 | static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 116 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 117 | static struct ftrace_ops global_ops; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 118 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 119 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 120 | static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 121 | struct ftrace_ops *op, struct pt_regs *regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 122 | #else |
| 123 | /* See comment below, where ftrace_ops_list_func is defined */ |
Sami Tolvanen | c2f9bce | 2018-05-10 14:56:41 -0700 | [diff] [blame] | 124 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip, |
| 125 | struct ftrace_ops *op, struct pt_regs *regs); |
| 126 | #define ftrace_ops_list_func ftrace_ops_no_ops |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 127 | #endif |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 128 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 129 | /* |
| 130 | * Traverse the ftrace_global_list, invoking all entries. The reason that we |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 131 | * 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] | 132 | * 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] | 133 | * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 134 | * concurrent insertions into the ftrace_global_list. |
| 135 | * |
| 136 | * Silly Alpha and silly pointer-speculation compiler optimizations! |
| 137 | */ |
| 138 | #define do_for_each_ftrace_op(op, list) \ |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 139 | op = rcu_dereference_raw_notrace(list); \ |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 140 | do |
| 141 | |
| 142 | /* |
| 143 | * Optimized for just a single item in the list (as that is the normal case). |
| 144 | */ |
| 145 | #define while_for_each_ftrace_op(op) \ |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 146 | while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \ |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 147 | unlikely((op) != &ftrace_list_end)) |
| 148 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 149 | static inline void ftrace_ops_init(struct ftrace_ops *ops) |
| 150 | { |
| 151 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 152 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 153 | mutex_init(&ops->local_hash.regex_lock); |
| 154 | ops->func_hash = &ops->local_hash; |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 155 | ops->flags |= FTRACE_OPS_FL_INITIALIZED; |
| 156 | } |
| 157 | #endif |
| 158 | } |
| 159 | |
Steven Rostedt | ea701f1 | 2012-07-20 13:08:05 -0400 | [diff] [blame] | 160 | /** |
| 161 | * ftrace_nr_registered_ops - return number of ops registered |
| 162 | * |
| 163 | * Returns the number of ftrace_ops registered and tracing functions |
| 164 | */ |
| 165 | int ftrace_nr_registered_ops(void) |
| 166 | { |
| 167 | struct ftrace_ops *ops; |
| 168 | int cnt = 0; |
| 169 | |
| 170 | mutex_lock(&ftrace_lock); |
| 171 | |
| 172 | for (ops = ftrace_ops_list; |
| 173 | ops != &ftrace_list_end; ops = ops->next) |
| 174 | cnt++; |
| 175 | |
| 176 | mutex_unlock(&ftrace_lock); |
| 177 | |
| 178 | return cnt; |
| 179 | } |
| 180 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 181 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 182 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 183 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 184 | struct trace_array *tr = op->private; |
| 185 | |
| 186 | if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid)) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 187 | return; |
| 188 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 189 | op->saved_func(ip, parent_ip, op, regs); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 192 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 193 | * clear_ftrace_function - reset the ftrace function |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 194 | * |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 195 | * This NULLs the ftrace function and in essence stops |
| 196 | * tracing. There may be lag |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 197 | */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 198 | void clear_ftrace_function(void) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 199 | { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 200 | ftrace_trace_function = ftrace_stub; |
| 201 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 202 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 203 | static void per_cpu_ops_disable_all(struct ftrace_ops *ops) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 204 | { |
| 205 | int cpu; |
| 206 | |
| 207 | for_each_possible_cpu(cpu) |
| 208 | *per_cpu_ptr(ops->disabled, cpu) = 1; |
| 209 | } |
| 210 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 211 | static int per_cpu_ops_alloc(struct ftrace_ops *ops) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 212 | { |
| 213 | int __percpu *disabled; |
| 214 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 215 | if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU))) |
| 216 | return -EINVAL; |
| 217 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 218 | disabled = alloc_percpu(int); |
| 219 | if (!disabled) |
| 220 | return -ENOMEM; |
| 221 | |
| 222 | ops->disabled = disabled; |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 223 | per_cpu_ops_disable_all(ops); |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 227 | static void ftrace_sync(struct work_struct *work) |
| 228 | { |
| 229 | /* |
| 230 | * This function is just a stub to implement a hard force |
| 231 | * of synchronize_sched(). This requires synchronizing |
| 232 | * tasks even in userspace and idle. |
| 233 | * |
| 234 | * Yes, function tracing is rude. |
| 235 | */ |
| 236 | } |
| 237 | |
| 238 | static void ftrace_sync_ipi(void *data) |
| 239 | { |
| 240 | /* Probably not needed, but do it anyway */ |
| 241 | smp_rmb(); |
| 242 | } |
| 243 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 244 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 245 | static void update_function_graph_func(void); |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 246 | |
| 247 | /* Both enabled by default (can be cleared by function_graph tracer flags */ |
| 248 | static bool fgraph_sleep_time = true; |
| 249 | static bool fgraph_graph_time = true; |
| 250 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 251 | #else |
| 252 | static inline void update_function_graph_func(void) { } |
| 253 | #endif |
| 254 | |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 255 | |
| 256 | static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops) |
| 257 | { |
| 258 | /* |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 259 | * 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] | 260 | * then it needs to call the list anyway. |
| 261 | */ |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 262 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU | |
| 263 | FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC) |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 264 | return ftrace_ops_list_func; |
| 265 | |
| 266 | return ftrace_ops_get_func(ops); |
| 267 | } |
| 268 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 269 | static void update_ftrace_function(void) |
| 270 | { |
| 271 | ftrace_func_t func; |
| 272 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 273 | /* |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 274 | * Prepare the ftrace_ops that the arch callback will use. |
| 275 | * If there's only one ftrace_ops registered, the ftrace_ops_list |
| 276 | * will point to the ops we want. |
| 277 | */ |
| 278 | set_function_trace_op = ftrace_ops_list; |
| 279 | |
| 280 | /* If there's no ftrace_ops registered, just call the stub function */ |
| 281 | if (ftrace_ops_list == &ftrace_list_end) { |
| 282 | func = ftrace_stub; |
| 283 | |
| 284 | /* |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 285 | * 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] | 286 | * recursion safe and not dynamic and the arch supports passing ops, |
| 287 | * then have the mcount trampoline call the function directly. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 288 | */ |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 289 | } else if (ftrace_ops_list->next == &ftrace_list_end) { |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 290 | func = ftrace_ops_get_list_func(ftrace_ops_list); |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 291 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 292 | } else { |
| 293 | /* Just use the default ftrace_ops */ |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 294 | set_function_trace_op = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 295 | func = ftrace_ops_list_func; |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 296 | } |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 297 | |
Steven Rostedt (Red Hat) | 5f8bf2d2 | 2014-07-15 11:05:12 -0400 | [diff] [blame] | 298 | update_function_graph_func(); |
| 299 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 300 | /* If there's no change, then do nothing more here */ |
| 301 | if (ftrace_trace_function == func) |
| 302 | return; |
| 303 | |
| 304 | /* |
| 305 | * If we are using the list function, it doesn't care |
| 306 | * about the function_trace_ops. |
| 307 | */ |
| 308 | if (func == ftrace_ops_list_func) { |
| 309 | ftrace_trace_function = func; |
| 310 | /* |
| 311 | * Don't even bother setting function_trace_ops, |
| 312 | * it would be racy to do so anyway. |
| 313 | */ |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | #ifndef CONFIG_DYNAMIC_FTRACE |
| 318 | /* |
| 319 | * For static tracing, we need to be a bit more careful. |
| 320 | * The function change takes affect immediately. Thus, |
| 321 | * we need to coorditate the setting of the function_trace_ops |
| 322 | * with the setting of the ftrace_trace_function. |
| 323 | * |
| 324 | * Set the function to the list ops, which will call the |
| 325 | * function we want, albeit indirectly, but it handles the |
| 326 | * ftrace_ops and doesn't depend on function_trace_op. |
| 327 | */ |
| 328 | ftrace_trace_function = ftrace_ops_list_func; |
| 329 | /* |
| 330 | * Make sure all CPUs see this. Yes this is slow, but static |
| 331 | * tracing is slow and nasty to have enabled. |
| 332 | */ |
| 333 | schedule_on_each_cpu(ftrace_sync); |
| 334 | /* Now all cpus are using the list ops. */ |
| 335 | function_trace_op = set_function_trace_op; |
| 336 | /* Make sure the function_trace_op is visible on all CPUs */ |
| 337 | smp_wmb(); |
| 338 | /* Nasty way to force a rmb on all cpus */ |
| 339 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
| 340 | /* OK, we are all set to update the ftrace_trace_function now! */ |
| 341 | #endif /* !CONFIG_DYNAMIC_FTRACE */ |
| 342 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 343 | ftrace_trace_function = func; |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Jiaxing Wang | 7eea4fc | 2014-04-20 23:10:43 +0800 | [diff] [blame] | 346 | int using_ftrace_ops_list_func(void) |
| 347 | { |
| 348 | return ftrace_trace_function == ftrace_ops_list_func; |
| 349 | } |
| 350 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 351 | 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] | 352 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 353 | ops->next = *list; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 354 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 355 | * We are entering ops into the list but another |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 356 | * CPU might be walking that list. We need to make sure |
| 357 | * the ops->next pointer is valid before another CPU sees |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 358 | * the ops pointer included into the list. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 359 | */ |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 360 | rcu_assign_pointer(*list, ops); |
| 361 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 362 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 363 | static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
| 364 | { |
| 365 | struct ftrace_ops **p; |
| 366 | |
| 367 | /* |
| 368 | * If we are removing the last function, then simply point |
| 369 | * to the ftrace_stub. |
| 370 | */ |
| 371 | if (*list == ops && ops->next == &ftrace_list_end) { |
| 372 | *list = &ftrace_list_end; |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
| 377 | if (*p == ops) |
| 378 | break; |
| 379 | |
| 380 | if (*p != ops) |
| 381 | return -1; |
| 382 | |
| 383 | *p = (*p)->next; |
| 384 | return 0; |
| 385 | } |
| 386 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 387 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 388 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 389 | static int __register_ftrace_function(struct ftrace_ops *ops) |
| 390 | { |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 391 | if (ops->flags & FTRACE_OPS_FL_DELETED) |
| 392 | return -EINVAL; |
| 393 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 394 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 395 | return -EBUSY; |
| 396 | |
Masami Hiramatsu | 06aeaae | 2012-09-28 17:15:17 +0900 | [diff] [blame] | 397 | #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 398 | /* |
| 399 | * If the ftrace_ops specifies SAVE_REGS, then it only can be used |
| 400 | * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. |
| 401 | * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. |
| 402 | */ |
| 403 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && |
| 404 | !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) |
| 405 | return -EINVAL; |
| 406 | |
| 407 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) |
| 408 | ops->flags |= FTRACE_OPS_FL_SAVE_REGS; |
| 409 | #endif |
| 410 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 411 | if (!core_kernel_data((unsigned long)ops)) |
| 412 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; |
| 413 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 414 | if (ops->flags & FTRACE_OPS_FL_PER_CPU) { |
| 415 | if (per_cpu_ops_alloc(ops)) |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 416 | return -ENOMEM; |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | add_ftrace_ops(&ftrace_ops_list, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 420 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 421 | /* Always save the function, and reset at unregistering */ |
| 422 | ops->saved_func = ops->func; |
| 423 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 424 | if (ftrace_pids_enabled(ops)) |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 425 | ops->func = ftrace_pid_func; |
| 426 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 427 | ftrace_update_trampoline(ops); |
| 428 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 429 | if (ftrace_enabled) |
| 430 | update_ftrace_function(); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 431 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 435 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 436 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 437 | int ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 438 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 439 | if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) |
| 440 | return -EBUSY; |
| 441 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 442 | ret = remove_ftrace_ops(&ftrace_ops_list, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 443 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 444 | if (ret < 0) |
| 445 | return ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 446 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 447 | if (ftrace_enabled) |
| 448 | update_ftrace_function(); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 449 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 450 | ops->func = ops->saved_func; |
| 451 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 452 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 455 | static void ftrace_update_pid_func(void) |
| 456 | { |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 457 | struct ftrace_ops *op; |
| 458 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 459 | /* Only do something if we are tracing something */ |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 460 | if (ftrace_trace_function == ftrace_stub) |
KOSAKI Motohiro | 10dd3eb | 2009-03-06 15:29:04 +0900 | [diff] [blame] | 461 | return; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 462 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 463 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 464 | if (op->flags & FTRACE_OPS_FL_PID) { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 465 | op->func = ftrace_pids_enabled(op) ? |
| 466 | ftrace_pid_func : op->saved_func; |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 467 | ftrace_update_trampoline(op); |
| 468 | } |
| 469 | } while_for_each_ftrace_op(op); |
| 470 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 471 | update_ftrace_function(); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 474 | #ifdef CONFIG_FUNCTION_PROFILER |
| 475 | struct ftrace_profile { |
| 476 | struct hlist_node node; |
| 477 | unsigned long ip; |
| 478 | unsigned long counter; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 479 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 480 | unsigned long long time; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 481 | unsigned long long time_squared; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 482 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | struct ftrace_profile_page { |
| 486 | struct ftrace_profile_page *next; |
| 487 | unsigned long index; |
| 488 | struct ftrace_profile records[]; |
| 489 | }; |
| 490 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 491 | struct ftrace_profile_stat { |
| 492 | atomic_t disabled; |
| 493 | struct hlist_head *hash; |
| 494 | struct ftrace_profile_page *pages; |
| 495 | struct ftrace_profile_page *start; |
| 496 | struct tracer_stat stat; |
| 497 | }; |
| 498 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 499 | #define PROFILE_RECORDS_SIZE \ |
| 500 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) |
| 501 | |
| 502 | #define PROFILES_PER_PAGE \ |
| 503 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) |
| 504 | |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 505 | static int ftrace_profile_enabled __read_mostly; |
| 506 | |
| 507 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 508 | static DEFINE_MUTEX(ftrace_profile_lock); |
| 509 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 510 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 511 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 512 | #define FTRACE_PROFILE_HASH_BITS 10 |
| 513 | #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 514 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 515 | static void * |
| 516 | function_stat_next(void *v, int idx) |
| 517 | { |
| 518 | struct ftrace_profile *rec = v; |
| 519 | struct ftrace_profile_page *pg; |
| 520 | |
| 521 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
| 522 | |
| 523 | again: |
Li Zefan | 0296e42 | 2009-06-26 11:15:37 +0800 | [diff] [blame] | 524 | if (idx != 0) |
| 525 | rec++; |
| 526 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 527 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
| 528 | pg = pg->next; |
| 529 | if (!pg) |
| 530 | return NULL; |
| 531 | rec = &pg->records[0]; |
| 532 | if (!rec->counter) |
| 533 | goto again; |
| 534 | } |
| 535 | |
| 536 | return rec; |
| 537 | } |
| 538 | |
| 539 | static void *function_stat_start(struct tracer_stat *trace) |
| 540 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 541 | struct ftrace_profile_stat *stat = |
| 542 | container_of(trace, struct ftrace_profile_stat, stat); |
| 543 | |
| 544 | if (!stat || !stat->start) |
| 545 | return NULL; |
| 546 | |
| 547 | return function_stat_next(&stat->start->records[0], 0); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 550 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 551 | /* function graph compares on total time */ |
| 552 | static int function_stat_cmp(void *p1, void *p2) |
| 553 | { |
| 554 | struct ftrace_profile *a = p1; |
| 555 | struct ftrace_profile *b = p2; |
| 556 | |
| 557 | if (a->time < b->time) |
| 558 | return -1; |
| 559 | if (a->time > b->time) |
| 560 | return 1; |
| 561 | else |
| 562 | return 0; |
| 563 | } |
| 564 | #else |
| 565 | /* not function graph compares against hits */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 566 | static int function_stat_cmp(void *p1, void *p2) |
| 567 | { |
| 568 | struct ftrace_profile *a = p1; |
| 569 | struct ftrace_profile *b = p2; |
| 570 | |
| 571 | if (a->counter < b->counter) |
| 572 | return -1; |
| 573 | if (a->counter > b->counter) |
| 574 | return 1; |
| 575 | else |
| 576 | return 0; |
| 577 | } |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 578 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 579 | |
| 580 | static int function_stat_headers(struct seq_file *m) |
| 581 | { |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 582 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 583 | seq_puts(m, " Function " |
| 584 | "Hit Time Avg s^2\n" |
| 585 | " -------- " |
| 586 | "--- ---- --- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 587 | #else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 588 | seq_puts(m, " Function Hit\n" |
| 589 | " -------- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 590 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int function_stat_show(struct seq_file *m, void *v) |
| 595 | { |
| 596 | struct ftrace_profile *rec = v; |
| 597 | char str[KSYM_SYMBOL_LEN]; |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 598 | int ret = 0; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 599 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 600 | static struct trace_seq s; |
| 601 | unsigned long long avg; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 602 | unsigned long long stddev; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 603 | #endif |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 604 | mutex_lock(&ftrace_profile_lock); |
| 605 | |
| 606 | /* we raced with function_profile_reset() */ |
| 607 | if (unlikely(rec->counter == 0)) { |
| 608 | ret = -EBUSY; |
| 609 | goto out; |
| 610 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 611 | |
Umesh Tiwari | 8e436ca | 2015-06-22 16:58:08 +0530 | [diff] [blame] | 612 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 613 | avg = rec->time; |
| 614 | do_div(avg, rec->counter); |
| 615 | if (tracing_thresh && (avg < tracing_thresh)) |
| 616 | goto out; |
| 617 | #endif |
| 618 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 619 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 620 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 621 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 622 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 623 | seq_puts(m, " "); |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 624 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 625 | /* Sample standard deviation (s^2) */ |
| 626 | if (rec->counter <= 1) |
| 627 | stddev = 0; |
| 628 | else { |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 629 | /* |
| 630 | * Apply Welford's method: |
| 631 | * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) |
| 632 | */ |
| 633 | stddev = rec->counter * rec->time_squared - |
| 634 | rec->time * rec->time; |
| 635 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 636 | /* |
| 637 | * Divide only 1000 for ns^2 -> us^2 conversion. |
| 638 | * trace_print_graph_duration will divide 1000 again. |
| 639 | */ |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 640 | do_div(stddev, rec->counter * (rec->counter - 1) * 1000); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 641 | } |
| 642 | |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 643 | trace_seq_init(&s); |
| 644 | trace_print_graph_duration(rec->time, &s); |
| 645 | trace_seq_puts(&s, " "); |
| 646 | trace_print_graph_duration(avg, &s); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 647 | trace_seq_puts(&s, " "); |
| 648 | trace_print_graph_duration(stddev, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 649 | trace_print_seq(m, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 650 | #endif |
| 651 | seq_putc(m, '\n'); |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 652 | out: |
| 653 | mutex_unlock(&ftrace_profile_lock); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 654 | |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 655 | return ret; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 658 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 659 | { |
| 660 | struct ftrace_profile_page *pg; |
| 661 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 662 | pg = stat->pages = stat->start; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 663 | |
| 664 | while (pg) { |
| 665 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); |
| 666 | pg->index = 0; |
| 667 | pg = pg->next; |
| 668 | } |
| 669 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 670 | memset(stat->hash, 0, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 671 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
| 672 | } |
| 673 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 674 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 675 | { |
| 676 | struct ftrace_profile_page *pg; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 677 | int functions; |
| 678 | int pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 679 | int i; |
| 680 | |
| 681 | /* If we already allocated, do nothing */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 682 | if (stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 683 | return 0; |
| 684 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 685 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
| 686 | if (!stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 687 | return -ENOMEM; |
| 688 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 689 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 690 | functions = ftrace_update_tot_cnt; |
| 691 | #else |
| 692 | /* |
| 693 | * We do not know the number of functions that exist because |
| 694 | * dynamic tracing is what counts them. With past experience |
| 695 | * we have around 20K functions. That should be more than enough. |
| 696 | * It is highly unlikely we will execute every function in |
| 697 | * the kernel. |
| 698 | */ |
| 699 | functions = 20000; |
| 700 | #endif |
| 701 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 702 | pg = stat->start = stat->pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 703 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 704 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
| 705 | |
Namhyung Kim | 39e30cd | 2013-04-01 21:46:24 +0900 | [diff] [blame] | 706 | for (i = 1; i < pages; i++) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 707 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 708 | if (!pg->next) |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 709 | goto out_free; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 710 | pg = pg->next; |
| 711 | } |
| 712 | |
| 713 | return 0; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 714 | |
| 715 | out_free: |
| 716 | pg = stat->start; |
| 717 | while (pg) { |
| 718 | unsigned long tmp = (unsigned long)pg; |
| 719 | |
| 720 | pg = pg->next; |
| 721 | free_page(tmp); |
| 722 | } |
| 723 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 724 | stat->pages = NULL; |
| 725 | stat->start = NULL; |
| 726 | |
| 727 | return -ENOMEM; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 728 | } |
| 729 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 730 | static int ftrace_profile_init_cpu(int cpu) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 731 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 732 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 733 | int size; |
| 734 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 735 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 736 | |
| 737 | if (stat->hash) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 738 | /* If the profile is already created, simply reset it */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 739 | ftrace_profile_reset(stat); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * We are profiling all functions, but usually only a few thousand |
| 745 | * functions are hit. We'll make a hash of 1024 items. |
| 746 | */ |
| 747 | size = FTRACE_PROFILE_HASH_SIZE; |
| 748 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 749 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 750 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 751 | if (!stat->hash) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 752 | return -ENOMEM; |
| 753 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 754 | /* Preallocate the function profiling pages */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 755 | if (ftrace_profile_pages_init(stat) < 0) { |
| 756 | kfree(stat->hash); |
| 757 | stat->hash = NULL; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 758 | return -ENOMEM; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 764 | static int ftrace_profile_init(void) |
| 765 | { |
| 766 | int cpu; |
| 767 | int ret = 0; |
| 768 | |
Miao Xie | c4602c1 | 2013-12-16 15:20:01 +0800 | [diff] [blame] | 769 | for_each_possible_cpu(cpu) { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 770 | ret = ftrace_profile_init_cpu(cpu); |
| 771 | if (ret) |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | return ret; |
| 776 | } |
| 777 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 778 | /* interrupts must be disabled */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 779 | static struct ftrace_profile * |
| 780 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 781 | { |
| 782 | struct ftrace_profile *rec; |
| 783 | struct hlist_head *hhd; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 784 | unsigned long key; |
| 785 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 786 | key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 787 | hhd = &stat->hash[key]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 788 | |
| 789 | if (hlist_empty(hhd)) |
| 790 | return NULL; |
| 791 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 792 | hlist_for_each_entry_rcu_notrace(rec, hhd, node) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 793 | if (rec->ip == ip) |
| 794 | return rec; |
| 795 | } |
| 796 | |
| 797 | return NULL; |
| 798 | } |
| 799 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 800 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
| 801 | struct ftrace_profile *rec) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 802 | { |
| 803 | unsigned long key; |
| 804 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 805 | key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 806 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 807 | } |
| 808 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 809 | /* |
| 810 | * The memory is already allocated, this simply finds a new record to use. |
| 811 | */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 812 | static struct ftrace_profile * |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 813 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 814 | { |
| 815 | struct ftrace_profile *rec = NULL; |
| 816 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 817 | /* prevent recursion (from NMIs) */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 818 | if (atomic_inc_return(&stat->disabled) != 1) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 819 | goto out; |
| 820 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 821 | /* |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 822 | * Try to find the function again since an NMI |
| 823 | * could have added it |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 824 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 825 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 826 | if (rec) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 827 | goto out; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 828 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 829 | if (stat->pages->index == PROFILES_PER_PAGE) { |
| 830 | if (!stat->pages->next) |
| 831 | goto out; |
| 832 | stat->pages = stat->pages->next; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 833 | } |
| 834 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 835 | rec = &stat->pages->records[stat->pages->index++]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 836 | rec->ip = ip; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 837 | ftrace_add_profile(stat, rec); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 838 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 839 | out: |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 840 | atomic_dec(&stat->disabled); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 841 | |
| 842 | return rec; |
| 843 | } |
| 844 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 845 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 846 | function_profile_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 847 | struct ftrace_ops *ops, struct pt_regs *regs) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 848 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 849 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 850 | struct ftrace_profile *rec; |
| 851 | unsigned long flags; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 852 | |
| 853 | if (!ftrace_profile_enabled) |
| 854 | return; |
| 855 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 856 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 857 | |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 858 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 859 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 860 | goto out; |
| 861 | |
| 862 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 863 | if (!rec) { |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 864 | rec = ftrace_profile_alloc(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 865 | if (!rec) |
| 866 | goto out; |
| 867 | } |
| 868 | |
| 869 | rec->counter++; |
| 870 | out: |
| 871 | local_irq_restore(flags); |
| 872 | } |
| 873 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 874 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 875 | static int profile_graph_entry(struct ftrace_graph_ent *trace) |
| 876 | { |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 877 | int index = trace->depth; |
| 878 | |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 879 | function_profile_call(trace->func, 0, NULL, NULL); |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 880 | |
Steven Rostedt (VMware) | 741397d | 2017-08-17 16:37:25 -0400 | [diff] [blame] | 881 | /* If function graph is shutting down, ret_stack can be NULL */ |
| 882 | if (!current->ret_stack) |
| 883 | return 0; |
| 884 | |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 885 | if (index >= 0 && index < FTRACE_RETFUNC_DEPTH) |
| 886 | current->ret_stack[index].subtime = 0; |
| 887 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 888 | return 1; |
| 889 | } |
| 890 | |
| 891 | static void profile_graph_return(struct ftrace_graph_ret *trace) |
| 892 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 893 | struct ftrace_profile_stat *stat; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 894 | unsigned long long calltime; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 895 | struct ftrace_profile *rec; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 896 | unsigned long flags; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 897 | |
| 898 | local_irq_save(flags); |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 899 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 900 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 901 | goto out; |
| 902 | |
Steven Rostedt | 37e44bc | 2010-04-27 21:04:24 -0400 | [diff] [blame] | 903 | /* If the calltime was zero'd ignore it */ |
| 904 | if (!trace->calltime) |
| 905 | goto out; |
| 906 | |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 907 | calltime = trace->rettime - trace->calltime; |
| 908 | |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 909 | if (!fgraph_graph_time) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 910 | int index; |
| 911 | |
| 912 | index = trace->depth; |
| 913 | |
| 914 | /* Append this call time to the parent time to subtract */ |
| 915 | if (index) |
| 916 | current->ret_stack[index - 1].subtime += calltime; |
| 917 | |
| 918 | if (current->ret_stack[index].subtime < calltime) |
| 919 | calltime -= current->ret_stack[index].subtime; |
| 920 | else |
| 921 | calltime = 0; |
| 922 | } |
| 923 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 924 | rec = ftrace_find_profiled_func(stat, trace->func); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 925 | if (rec) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 926 | rec->time += calltime; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 927 | rec->time_squared += calltime * calltime; |
| 928 | } |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 929 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 930 | out: |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 931 | local_irq_restore(flags); |
| 932 | } |
| 933 | |
| 934 | static int register_ftrace_profiler(void) |
| 935 | { |
| 936 | return register_ftrace_graph(&profile_graph_return, |
| 937 | &profile_graph_entry); |
| 938 | } |
| 939 | |
| 940 | static void unregister_ftrace_profiler(void) |
| 941 | { |
| 942 | unregister_ftrace_graph(); |
| 943 | } |
| 944 | #else |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 945 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 946 | .func = function_profile_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 947 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 948 | INIT_OPS_HASH(ftrace_profile_ops) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 949 | }; |
| 950 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 951 | static int register_ftrace_profiler(void) |
| 952 | { |
| 953 | return register_ftrace_function(&ftrace_profile_ops); |
| 954 | } |
| 955 | |
| 956 | static void unregister_ftrace_profiler(void) |
| 957 | { |
| 958 | unregister_ftrace_function(&ftrace_profile_ops); |
| 959 | } |
| 960 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 961 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 962 | static ssize_t |
| 963 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 964 | size_t cnt, loff_t *ppos) |
| 965 | { |
| 966 | unsigned long val; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 967 | int ret; |
| 968 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 969 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 970 | if (ret) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 971 | return ret; |
| 972 | |
| 973 | val = !!val; |
| 974 | |
| 975 | mutex_lock(&ftrace_profile_lock); |
| 976 | if (ftrace_profile_enabled ^ val) { |
| 977 | if (val) { |
| 978 | ret = ftrace_profile_init(); |
| 979 | if (ret < 0) { |
| 980 | cnt = ret; |
| 981 | goto out; |
| 982 | } |
| 983 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 984 | ret = register_ftrace_profiler(); |
| 985 | if (ret < 0) { |
| 986 | cnt = ret; |
| 987 | goto out; |
| 988 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 989 | ftrace_profile_enabled = 1; |
| 990 | } else { |
| 991 | ftrace_profile_enabled = 0; |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 992 | /* |
| 993 | * unregister_ftrace_profiler calls stop_machine |
| 994 | * so this acts like an synchronize_sched. |
| 995 | */ |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 996 | unregister_ftrace_profiler(); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | out: |
| 1000 | mutex_unlock(&ftrace_profile_lock); |
| 1001 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 1002 | *ppos += cnt; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1003 | |
| 1004 | return cnt; |
| 1005 | } |
| 1006 | |
| 1007 | static ssize_t |
| 1008 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 1009 | size_t cnt, loff_t *ppos) |
| 1010 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 1011 | char buf[64]; /* big enough to hold a number */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1012 | int r; |
| 1013 | |
| 1014 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); |
| 1015 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 1016 | } |
| 1017 | |
| 1018 | static const struct file_operations ftrace_profile_fops = { |
| 1019 | .open = tracing_open_generic, |
| 1020 | .read = ftrace_profile_read, |
| 1021 | .write = ftrace_profile_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1022 | .llseek = default_llseek, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1023 | }; |
| 1024 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1025 | /* used to initialize the real stat files */ |
| 1026 | static struct tracer_stat function_stats __initdata = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 1027 | .name = "functions", |
| 1028 | .stat_start = function_stat_start, |
| 1029 | .stat_next = function_stat_next, |
| 1030 | .stat_cmp = function_stat_cmp, |
| 1031 | .stat_headers = function_stat_headers, |
| 1032 | .stat_show = function_stat_show |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1033 | }; |
| 1034 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1035 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1036 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1037 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1038 | struct dentry *entry; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1039 | char *name; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1040 | int ret; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1041 | int cpu; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1042 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1043 | for_each_possible_cpu(cpu) { |
| 1044 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 1045 | |
Geliang Tang | 6363c6b | 2016-03-15 22:12:34 +0800 | [diff] [blame] | 1046 | name = kasprintf(GFP_KERNEL, "function%d", cpu); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1047 | if (!name) { |
| 1048 | /* |
| 1049 | * The files created are permanent, if something happens |
| 1050 | * we still do not free memory. |
| 1051 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1052 | WARN(1, |
| 1053 | "Could not allocate stat file for cpu %d\n", |
| 1054 | cpu); |
| 1055 | return; |
| 1056 | } |
| 1057 | stat->stat = function_stats; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 1058 | stat->stat.name = name; |
| 1059 | ret = register_stat_tracer(&stat->stat); |
| 1060 | if (ret) { |
| 1061 | WARN(1, |
| 1062 | "Could not register function stat for cpu %d\n", |
| 1063 | cpu); |
| 1064 | kfree(name); |
| 1065 | return; |
| 1066 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1069 | entry = tracefs_create_file("function_profile_enabled", 0644, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1070 | d_tracer, NULL, &ftrace_profile_fops); |
| 1071 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1072 | pr_warn("Could not create tracefs 'function_profile_enabled' entry\n"); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | #else /* CONFIG_FUNCTION_PROFILER */ |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1076 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1077 | { |
| 1078 | } |
| 1079 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 1080 | |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1081 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
| 1082 | |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 1083 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 1084 | static int ftrace_graph_active; |
| 1085 | #else |
| 1086 | # define ftrace_graph_active 0 |
| 1087 | #endif |
| 1088 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1089 | #ifdef CONFIG_DYNAMIC_FTRACE |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1090 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1091 | static struct ftrace_ops *removed_ops; |
| 1092 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 1093 | /* |
| 1094 | * Set when doing a global update, like enabling all recs or disabling them. |
| 1095 | * It is not set when just updating a single ftrace_ops. |
| 1096 | */ |
| 1097 | static bool update_all_ops; |
| 1098 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1099 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1100 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1101 | #endif |
| 1102 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1103 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; |
| 1104 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1105 | struct ftrace_func_probe { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1106 | struct hlist_node node; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1107 | struct ftrace_probe_ops *ops; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1108 | unsigned long flags; |
| 1109 | unsigned long ip; |
| 1110 | void *data; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 1111 | struct list_head free_list; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1112 | }; |
| 1113 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1114 | struct ftrace_func_entry { |
| 1115 | struct hlist_node hlist; |
| 1116 | unsigned long ip; |
| 1117 | }; |
| 1118 | |
| 1119 | struct ftrace_hash { |
| 1120 | unsigned long size_bits; |
| 1121 | struct hlist_head *buckets; |
| 1122 | unsigned long count; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1123 | struct rcu_head rcu; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1124 | }; |
| 1125 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1126 | /* |
| 1127 | * We make these constant because no one should touch them, |
| 1128 | * but they are used as the default "empty hash", to avoid allocating |
| 1129 | * it all the time. These are in a read only section such that if |
| 1130 | * anyone does try to modify it, it will cause an exception. |
| 1131 | */ |
| 1132 | static const struct hlist_head empty_buckets[1]; |
| 1133 | static const struct ftrace_hash empty_hash = { |
| 1134 | .buckets = (struct hlist_head *)empty_buckets, |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1135 | }; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1136 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1137 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 1138 | static struct ftrace_ops global_ops = { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1139 | .func = ftrace_stub, |
| 1140 | .local_hash.notrace_hash = EMPTY_HASH, |
| 1141 | .local_hash.filter_hash = EMPTY_HASH, |
| 1142 | INIT_OPS_HASH(global_ops) |
| 1143 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 1144 | FTRACE_OPS_FL_INITIALIZED | |
| 1145 | FTRACE_OPS_FL_PID, |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1146 | }; |
| 1147 | |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1148 | /* |
| 1149 | * 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] | 1150 | * address is on a dynamically allocated trampoline that would |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1151 | * not return true for either core_kernel_text() or |
| 1152 | * is_module_text_address(). |
| 1153 | */ |
| 1154 | bool is_ftrace_trampoline(unsigned long addr) |
| 1155 | { |
| 1156 | struct ftrace_ops *op; |
| 1157 | bool ret = false; |
| 1158 | |
| 1159 | /* |
| 1160 | * Some of the ops may be dynamically allocated, |
| 1161 | * they are freed after a synchronize_sched(). |
| 1162 | */ |
| 1163 | preempt_disable_notrace(); |
| 1164 | |
| 1165 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1166 | /* |
| 1167 | * This is to check for dynamically allocated trampolines. |
| 1168 | * Trampolines that are in kernel text will have |
| 1169 | * core_kernel_text() return true. |
| 1170 | */ |
| 1171 | if (op->trampoline && op->trampoline_size) |
| 1172 | if (addr >= op->trampoline && |
| 1173 | addr < op->trampoline + op->trampoline_size) { |
| 1174 | ret = true; |
| 1175 | goto out; |
| 1176 | } |
| 1177 | } while_for_each_ftrace_op(op); |
| 1178 | |
| 1179 | out: |
| 1180 | preempt_enable_notrace(); |
| 1181 | |
| 1182 | return ret; |
| 1183 | } |
| 1184 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1185 | struct ftrace_page { |
| 1186 | struct ftrace_page *next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1187 | struct dyn_ftrace *records; |
Steven Rostedt | 431aa3f | 2009-01-06 12:43:01 -0500 | [diff] [blame] | 1188 | int index; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1189 | int size; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 1190 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1191 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1192 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1193 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1194 | |
| 1195 | /* estimate from running different kernels */ |
| 1196 | #define NR_TO_INIT 10000 |
| 1197 | |
| 1198 | static struct ftrace_page *ftrace_pages_start; |
| 1199 | static struct ftrace_page *ftrace_pages; |
| 1200 | |
Steven Rostedt (Red Hat) | 68f4096 | 2014-05-01 12:44:50 -0400 | [diff] [blame] | 1201 | static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash) |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1202 | { |
| 1203 | return !hash || !hash->count; |
| 1204 | } |
| 1205 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1206 | static struct ftrace_func_entry * |
| 1207 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1208 | { |
| 1209 | unsigned long key; |
| 1210 | struct ftrace_func_entry *entry; |
| 1211 | struct hlist_head *hhd; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1212 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1213 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1214 | return NULL; |
| 1215 | |
| 1216 | if (hash->size_bits > 0) |
| 1217 | key = hash_long(ip, hash->size_bits); |
| 1218 | else |
| 1219 | key = 0; |
| 1220 | |
| 1221 | hhd = &hash->buckets[key]; |
| 1222 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 1223 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1224 | if (entry->ip == ip) |
| 1225 | return entry; |
| 1226 | } |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1230 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1231 | struct ftrace_func_entry *entry) |
| 1232 | { |
| 1233 | struct hlist_head *hhd; |
| 1234 | unsigned long key; |
| 1235 | |
| 1236 | if (hash->size_bits) |
| 1237 | key = hash_long(entry->ip, hash->size_bits); |
| 1238 | else |
| 1239 | key = 0; |
| 1240 | |
| 1241 | hhd = &hash->buckets[key]; |
| 1242 | hlist_add_head(&entry->hlist, hhd); |
| 1243 | hash->count++; |
| 1244 | } |
| 1245 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1246 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1247 | { |
| 1248 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1249 | |
| 1250 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1251 | if (!entry) |
| 1252 | return -ENOMEM; |
| 1253 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1254 | entry->ip = ip; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1255 | __add_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1256 | |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
| 1260 | static void |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1261 | free_hash_entry(struct ftrace_hash *hash, |
| 1262 | struct ftrace_func_entry *entry) |
| 1263 | { |
| 1264 | hlist_del(&entry->hlist); |
| 1265 | kfree(entry); |
| 1266 | hash->count--; |
| 1267 | } |
| 1268 | |
| 1269 | static void |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1270 | remove_hash_entry(struct ftrace_hash *hash, |
| 1271 | struct ftrace_func_entry *entry) |
| 1272 | { |
| 1273 | hlist_del(&entry->hlist); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1274 | hash->count--; |
| 1275 | } |
| 1276 | |
| 1277 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1278 | { |
| 1279 | struct hlist_head *hhd; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1280 | struct hlist_node *tn; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1281 | struct ftrace_func_entry *entry; |
| 1282 | int size = 1 << hash->size_bits; |
| 1283 | int i; |
| 1284 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1285 | if (!hash->count) |
| 1286 | return; |
| 1287 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1288 | for (i = 0; i < size; i++) { |
| 1289 | hhd = &hash->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1290 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1291 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1292 | } |
| 1293 | FTRACE_WARN_ON(hash->count); |
| 1294 | } |
| 1295 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1296 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1297 | { |
| 1298 | if (!hash || hash == EMPTY_HASH) |
| 1299 | return; |
| 1300 | ftrace_hash_clear(hash); |
| 1301 | kfree(hash->buckets); |
| 1302 | kfree(hash); |
| 1303 | } |
| 1304 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1305 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1306 | { |
| 1307 | struct ftrace_hash *hash; |
| 1308 | |
| 1309 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1310 | free_ftrace_hash(hash); |
| 1311 | } |
| 1312 | |
| 1313 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1314 | { |
| 1315 | if (!hash || hash == EMPTY_HASH) |
| 1316 | return; |
| 1317 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); |
| 1318 | } |
| 1319 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1320 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1321 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 1322 | ftrace_ops_init(ops); |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1323 | free_ftrace_hash(ops->func_hash->filter_hash); |
| 1324 | free_ftrace_hash(ops->func_hash->notrace_hash); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1327 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1328 | { |
| 1329 | struct ftrace_hash *hash; |
| 1330 | int size; |
| 1331 | |
| 1332 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1333 | if (!hash) |
| 1334 | return NULL; |
| 1335 | |
| 1336 | size = 1 << size_bits; |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1337 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1338 | |
| 1339 | if (!hash->buckets) { |
| 1340 | kfree(hash); |
| 1341 | return NULL; |
| 1342 | } |
| 1343 | |
| 1344 | hash->size_bits = size_bits; |
| 1345 | |
| 1346 | return hash; |
| 1347 | } |
| 1348 | |
| 1349 | static struct ftrace_hash * |
| 1350 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1351 | { |
| 1352 | struct ftrace_func_entry *entry; |
| 1353 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1354 | int size; |
| 1355 | int ret; |
| 1356 | int i; |
| 1357 | |
| 1358 | new_hash = alloc_ftrace_hash(size_bits); |
| 1359 | if (!new_hash) |
| 1360 | return NULL; |
| 1361 | |
| 1362 | /* Empty hash? */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1363 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1364 | return new_hash; |
| 1365 | |
| 1366 | size = 1 << hash->size_bits; |
| 1367 | for (i = 0; i < size; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1368 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1369 | ret = add_hash_entry(new_hash, entry->ip); |
| 1370 | if (ret < 0) |
| 1371 | goto free_hash; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1376 | |
| 1377 | return new_hash; |
| 1378 | |
| 1379 | free_hash: |
| 1380 | free_ftrace_hash(new_hash); |
| 1381 | return NULL; |
| 1382 | } |
| 1383 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1384 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1385 | ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1386 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1387 | ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1388 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1389 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1390 | struct ftrace_hash *new_hash); |
| 1391 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1392 | static int |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1393 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1394 | struct ftrace_hash **dst, 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; |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1402 | int ret; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1403 | int i; |
| 1404 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1405 | /* Reject setting notrace hash on IPMODIFY ftrace_ops */ |
| 1406 | if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) |
| 1407 | return -EINVAL; |
| 1408 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1409 | /* |
| 1410 | * If the new source is empty, just free dst and assign it |
| 1411 | * the empty_hash. |
| 1412 | */ |
| 1413 | if (!src->count) { |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1414 | new_hash = EMPTY_HASH; |
| 1415 | goto update; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1416 | } |
| 1417 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1418 | /* |
| 1419 | * Make the hash size about 1/2 the # found |
| 1420 | */ |
| 1421 | for (size /= 2; size; size >>= 1) |
| 1422 | bits++; |
| 1423 | |
| 1424 | /* Don't allocate too much */ |
| 1425 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1426 | bits = FTRACE_HASH_MAX_BITS; |
| 1427 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1428 | new_hash = alloc_ftrace_hash(bits); |
| 1429 | if (!new_hash) |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1430 | return -ENOMEM; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1431 | |
| 1432 | size = 1 << src->size_bits; |
| 1433 | for (i = 0; i < size; i++) { |
| 1434 | hhd = &src->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1435 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1436 | remove_hash_entry(src, entry); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1437 | __add_hash_entry(new_hash, entry); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1441 | update: |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1442 | /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ |
| 1443 | if (enable) { |
| 1444 | /* IPMODIFY should be updated only when filter_hash updating */ |
| 1445 | ret = ftrace_hash_ipmodify_update(ops, new_hash); |
| 1446 | if (ret < 0) { |
| 1447 | free_ftrace_hash(new_hash); |
| 1448 | return ret; |
| 1449 | } |
| 1450 | } |
| 1451 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1452 | /* |
| 1453 | * Remove the current set, update the hash and add |
| 1454 | * them back. |
| 1455 | */ |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1456 | ftrace_hash_rec_disable_modify(ops, enable); |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1457 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1458 | rcu_assign_pointer(*dst, new_hash); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1459 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1460 | ftrace_hash_rec_enable_modify(ops, enable); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1461 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1462 | return 0; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1463 | } |
| 1464 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1465 | static bool hash_contains_ip(unsigned long ip, |
| 1466 | struct ftrace_ops_hash *hash) |
| 1467 | { |
| 1468 | /* |
| 1469 | * The function record is a match if it exists in the filter |
| 1470 | * hash and not in the notrace hash. Note, an emty hash is |
| 1471 | * considered a match for the filter hash, but an empty |
| 1472 | * notrace hash is considered not in the notrace hash. |
| 1473 | */ |
| 1474 | return (ftrace_hash_empty(hash->filter_hash) || |
| 1475 | ftrace_lookup_ip(hash->filter_hash, ip)) && |
| 1476 | (ftrace_hash_empty(hash->notrace_hash) || |
| 1477 | !ftrace_lookup_ip(hash->notrace_hash, ip)); |
| 1478 | } |
| 1479 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1480 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1481 | * Test the hashes for this ops to see if we want to call |
| 1482 | * the ops->func or not. |
| 1483 | * |
| 1484 | * It's a match if the ip is in the ops->filter_hash or |
| 1485 | * the filter_hash does not exist or is empty, |
| 1486 | * AND |
| 1487 | * the ip is not in the ops->notrace_hash. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 1488 | * |
| 1489 | * This needs to be called with preemption disabled as |
| 1490 | * the hashes are freed with call_rcu_sched(). |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1491 | */ |
| 1492 | static int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1493 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1494 | { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1495 | struct ftrace_ops_hash hash; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1496 | int ret; |
| 1497 | |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1498 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1499 | /* |
| 1500 | * There's a small race when adding ops that the ftrace handler |
| 1501 | * that wants regs, may be called without them. We can not |
| 1502 | * allow that handler to be called if regs is NULL. |
| 1503 | */ |
| 1504 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) |
| 1505 | return 0; |
| 1506 | #endif |
| 1507 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1508 | hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash); |
| 1509 | hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1510 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1511 | if (hash_contains_ip(ip, &hash)) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1512 | ret = 1; |
| 1513 | else |
| 1514 | ret = 0; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1515 | |
| 1516 | return ret; |
| 1517 | } |
| 1518 | |
| 1519 | /* |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1520 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1521 | * you must use a goto. |
| 1522 | */ |
| 1523 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1524 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1525 | int _____i; \ |
| 1526 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1527 | rec = &pg->records[_____i]; |
| 1528 | |
| 1529 | #define while_for_each_ftrace_rec() \ |
| 1530 | } \ |
| 1531 | } |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 1532 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1533 | |
| 1534 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1535 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1536 | const struct dyn_ftrace *key = a; |
| 1537 | const struct dyn_ftrace *rec = b; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1538 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1539 | if (key->flags < rec->ip) |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1540 | return -1; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1541 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
| 1542 | return 1; |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
Michael Ellerman | 04cf31a | 2016-03-24 22:04:01 +1100 | [diff] [blame] | 1546 | /** |
| 1547 | * ftrace_location_range - return the first address of a traced location |
| 1548 | * if it touches the given ip range |
| 1549 | * @start: start of range to search. |
| 1550 | * @end: end of range to search (inclusive). @end points to the last byte |
| 1551 | * to check. |
| 1552 | * |
| 1553 | * Returns rec->ip if the related ftrace location is a least partly within |
| 1554 | * the given address range. That is, the first address of the instruction |
| 1555 | * that is either a NOP or call to the function tracer. It checks the ftrace |
| 1556 | * internal tables to determine if the address belongs or not. |
| 1557 | */ |
| 1558 | unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1559 | { |
| 1560 | struct ftrace_page *pg; |
| 1561 | struct dyn_ftrace *rec; |
| 1562 | struct dyn_ftrace key; |
| 1563 | |
| 1564 | key.ip = start; |
| 1565 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 1566 | |
| 1567 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1568 | if (end < pg->records[0].ip || |
| 1569 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 1570 | continue; |
| 1571 | rec = bsearch(&key, pg->records, pg->index, |
| 1572 | sizeof(struct dyn_ftrace), |
| 1573 | ftrace_cmp_recs); |
| 1574 | if (rec) |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1575 | return rec->ip; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1576 | } |
| 1577 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1578 | return 0; |
| 1579 | } |
| 1580 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1581 | /** |
| 1582 | * ftrace_location - return true if the ip giving is a traced location |
| 1583 | * @ip: the instruction pointer to check |
| 1584 | * |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1585 | * 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] | 1586 | * That is, the instruction that is either a NOP or call to |
| 1587 | * the function tracer. It checks the ftrace internal tables to |
| 1588 | * determine if the address belongs or not. |
| 1589 | */ |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1590 | unsigned long ftrace_location(unsigned long ip) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1591 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1592 | return ftrace_location_range(ip, ip); |
| 1593 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1594 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1595 | /** |
| 1596 | * ftrace_text_reserved - return true if range contains an ftrace location |
| 1597 | * @start: start of range to search |
| 1598 | * @end: end of range to search (inclusive). @end points to the last byte to check. |
| 1599 | * |
| 1600 | * Returns 1 if @start and @end contains a ftrace location. |
| 1601 | * That is, the instruction that is either a NOP or call to |
| 1602 | * the function tracer. It checks the ftrace internal tables to |
| 1603 | * determine if the address belongs or not. |
| 1604 | */ |
Sasha Levin | d88471c | 2013-01-09 18:09:20 -0500 | [diff] [blame] | 1605 | int ftrace_text_reserved(const void *start, const void *end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1606 | { |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1607 | unsigned long ret; |
| 1608 | |
| 1609 | ret = ftrace_location_range((unsigned long)start, |
| 1610 | (unsigned long)end); |
| 1611 | |
| 1612 | return (int)!!ret; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1613 | } |
| 1614 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1615 | /* Test if ops registered to this rec needs regs */ |
| 1616 | static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) |
| 1617 | { |
| 1618 | struct ftrace_ops *ops; |
| 1619 | bool keep_regs = false; |
| 1620 | |
| 1621 | for (ops = ftrace_ops_list; |
| 1622 | ops != &ftrace_list_end; ops = ops->next) { |
| 1623 | /* pass rec in as regs to have non-NULL val */ |
| 1624 | if (ftrace_ops_test(ops, rec->ip, rec)) { |
| 1625 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1626 | keep_regs = true; |
| 1627 | break; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | return keep_regs; |
| 1633 | } |
| 1634 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1635 | static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1636 | int filter_hash, |
| 1637 | bool inc) |
| 1638 | { |
| 1639 | struct ftrace_hash *hash; |
| 1640 | struct ftrace_hash *other_hash; |
| 1641 | struct ftrace_page *pg; |
| 1642 | struct dyn_ftrace *rec; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1643 | bool update = false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1644 | int count = 0; |
| 1645 | int all = 0; |
| 1646 | |
| 1647 | /* Only update if the ops has been registered */ |
| 1648 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1649 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1650 | |
| 1651 | /* |
| 1652 | * In the filter_hash case: |
| 1653 | * If the count is zero, we update all records. |
| 1654 | * Otherwise we just update the items in the hash. |
| 1655 | * |
| 1656 | * In the notrace_hash case: |
| 1657 | * We enable the update in the hash. |
| 1658 | * As disabling notrace means enabling the tracing, |
| 1659 | * and enabling notrace means disabling, the inc variable |
| 1660 | * gets inversed. |
| 1661 | */ |
| 1662 | if (filter_hash) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1663 | hash = ops->func_hash->filter_hash; |
| 1664 | other_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1665 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1666 | all = 1; |
| 1667 | } else { |
| 1668 | inc = !inc; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1669 | hash = ops->func_hash->notrace_hash; |
| 1670 | other_hash = ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1671 | /* |
| 1672 | * If the notrace hash has no items, |
| 1673 | * then there's nothing to do. |
| 1674 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1675 | if (ftrace_hash_empty(hash)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1676 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | do_for_each_ftrace_rec(pg, rec) { |
| 1680 | int in_other_hash = 0; |
| 1681 | int in_hash = 0; |
| 1682 | int match = 0; |
| 1683 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 1684 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1685 | continue; |
| 1686 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1687 | if (all) { |
| 1688 | /* |
| 1689 | * Only the filter_hash affects all records. |
| 1690 | * Update if the record is not in the notrace hash. |
| 1691 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1692 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1693 | match = 1; |
| 1694 | } else { |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1695 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
| 1696 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1697 | |
| 1698 | /* |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1699 | * If filter_hash is set, we want to match all functions |
| 1700 | * that are in the hash but not in the other hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1701 | * |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1702 | * If filter_hash is not set, then we are decrementing. |
| 1703 | * That means we match anything that is in the hash |
| 1704 | * and also in the other_hash. That is, we need to turn |
| 1705 | * off functions in the other hash because they are disabled |
| 1706 | * by this hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1707 | */ |
| 1708 | if (filter_hash && in_hash && !in_other_hash) |
| 1709 | match = 1; |
| 1710 | else if (!filter_hash && in_hash && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1711 | (in_other_hash || ftrace_hash_empty(other_hash))) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1712 | match = 1; |
| 1713 | } |
| 1714 | if (!match) |
| 1715 | continue; |
| 1716 | |
| 1717 | if (inc) { |
| 1718 | rec->flags++; |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1719 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1720 | return false; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1721 | |
| 1722 | /* |
| 1723 | * If there's only a single callback registered to a |
| 1724 | * function, and the ops has a trampoline registered |
| 1725 | * for it, then we can call it directly. |
| 1726 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1727 | if (ftrace_rec_count(rec) == 1 && ops->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1728 | rec->flags |= FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1729 | else |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1730 | /* |
| 1731 | * If we are adding another function callback |
| 1732 | * to this function, and the previous had a |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 1733 | * custom trampoline in use, then we need to go |
| 1734 | * back to the default trampoline. |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1735 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1736 | rec->flags &= ~FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1737 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1738 | /* |
| 1739 | * If any ops wants regs saved for this function |
| 1740 | * then all ops will get saved regs. |
| 1741 | */ |
| 1742 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 1743 | rec->flags |= FTRACE_FL_REGS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1744 | } else { |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1745 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1746 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1747 | rec->flags--; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1748 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1749 | /* |
| 1750 | * If the rec had REGS enabled and the ops that is |
| 1751 | * being removed had REGS set, then see if there is |
| 1752 | * still any ops for this record that wants regs. |
| 1753 | * If not, we can stop recording them. |
| 1754 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1755 | if (ftrace_rec_count(rec) > 0 && |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1756 | rec->flags & FTRACE_FL_REGS && |
| 1757 | ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1758 | if (!test_rec_ops_needs_regs(rec)) |
| 1759 | rec->flags &= ~FTRACE_FL_REGS; |
| 1760 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1761 | |
| 1762 | /* |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1763 | * If the rec had TRAMP enabled, then it needs to |
| 1764 | * be cleared. As TRAMP can only be enabled iff |
| 1765 | * there is only a single ops attached to it. |
| 1766 | * In otherwords, always disable it on decrementing. |
| 1767 | * In the future, we may set it if rec count is |
| 1768 | * decremented to one, and the ops that is left |
| 1769 | * has a trampoline. |
| 1770 | */ |
| 1771 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 1772 | |
| 1773 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1774 | * flags will be cleared in ftrace_check_record() |
| 1775 | * if rec count is zero. |
| 1776 | */ |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1777 | } |
| 1778 | count++; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1779 | |
| 1780 | /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */ |
| 1781 | update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE; |
| 1782 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1783 | /* Shortcut, if we handled all records, we are done. */ |
| 1784 | if (!all && count == hash->count) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1785 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1786 | } while_for_each_ftrace_rec(); |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1787 | |
| 1788 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1789 | } |
| 1790 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1791 | static bool ftrace_hash_rec_disable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1792 | int filter_hash) |
| 1793 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1794 | return __ftrace_hash_rec_update(ops, filter_hash, 0); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1795 | } |
| 1796 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1797 | static bool ftrace_hash_rec_enable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1798 | int filter_hash) |
| 1799 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1800 | return __ftrace_hash_rec_update(ops, filter_hash, 1); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1801 | } |
| 1802 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1803 | static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, |
| 1804 | int filter_hash, int inc) |
| 1805 | { |
| 1806 | struct ftrace_ops *op; |
| 1807 | |
| 1808 | __ftrace_hash_rec_update(ops, filter_hash, inc); |
| 1809 | |
| 1810 | if (ops->func_hash != &global_ops.local_hash) |
| 1811 | return; |
| 1812 | |
| 1813 | /* |
| 1814 | * If the ops shares the global_ops hash, then we need to update |
| 1815 | * all ops that are enabled and use this hash. |
| 1816 | */ |
| 1817 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1818 | /* Already done */ |
| 1819 | if (op == ops) |
| 1820 | continue; |
| 1821 | if (op->func_hash == &global_ops.local_hash) |
| 1822 | __ftrace_hash_rec_update(op, filter_hash, inc); |
| 1823 | } while_for_each_ftrace_op(op); |
| 1824 | } |
| 1825 | |
| 1826 | static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, |
| 1827 | int filter_hash) |
| 1828 | { |
| 1829 | ftrace_hash_rec_update_modify(ops, filter_hash, 0); |
| 1830 | } |
| 1831 | |
| 1832 | static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, |
| 1833 | int filter_hash) |
| 1834 | { |
| 1835 | ftrace_hash_rec_update_modify(ops, filter_hash, 1); |
| 1836 | } |
| 1837 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1838 | /* |
| 1839 | * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK |
| 1840 | * or no-needed to update, -EBUSY if it detects a conflict of the flag |
| 1841 | * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. |
| 1842 | * Note that old_hash and new_hash has below meanings |
| 1843 | * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) |
| 1844 | * - If the hash is EMPTY_HASH, it hits nothing |
| 1845 | * - Anything else hits the recs which match the hash entries. |
| 1846 | */ |
| 1847 | static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, |
| 1848 | struct ftrace_hash *old_hash, |
| 1849 | struct ftrace_hash *new_hash) |
| 1850 | { |
| 1851 | struct ftrace_page *pg; |
| 1852 | struct dyn_ftrace *rec, *end = NULL; |
| 1853 | int in_old, in_new; |
| 1854 | |
| 1855 | /* Only update if the ops has been registered */ |
| 1856 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1857 | return 0; |
| 1858 | |
| 1859 | if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 1860 | return 0; |
| 1861 | |
| 1862 | /* |
| 1863 | * Since the IPMODIFY is a very address sensitive action, we do not |
| 1864 | * allow ftrace_ops to set all functions to new hash. |
| 1865 | */ |
| 1866 | if (!new_hash || !old_hash) |
| 1867 | return -EINVAL; |
| 1868 | |
| 1869 | /* Update rec->flags */ |
| 1870 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1871 | |
| 1872 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1873 | continue; |
| 1874 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1875 | /* We need to update only differences of filter_hash */ |
| 1876 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1877 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1878 | if (in_old == in_new) |
| 1879 | continue; |
| 1880 | |
| 1881 | if (in_new) { |
| 1882 | /* New entries must ensure no others are using it */ |
| 1883 | if (rec->flags & FTRACE_FL_IPMODIFY) |
| 1884 | goto rollback; |
| 1885 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1886 | } else /* Removed entry */ |
| 1887 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1888 | } while_for_each_ftrace_rec(); |
| 1889 | |
| 1890 | return 0; |
| 1891 | |
| 1892 | rollback: |
| 1893 | end = rec; |
| 1894 | |
| 1895 | /* Roll back what we did above */ |
| 1896 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1897 | |
| 1898 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1899 | continue; |
| 1900 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1901 | if (rec == end) |
| 1902 | goto err_out; |
| 1903 | |
| 1904 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1905 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1906 | if (in_old == in_new) |
| 1907 | continue; |
| 1908 | |
| 1909 | if (in_new) |
| 1910 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1911 | else |
| 1912 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1913 | } while_for_each_ftrace_rec(); |
| 1914 | |
| 1915 | err_out: |
| 1916 | return -EBUSY; |
| 1917 | } |
| 1918 | |
| 1919 | static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) |
| 1920 | { |
| 1921 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1922 | |
| 1923 | if (ftrace_hash_empty(hash)) |
| 1924 | hash = NULL; |
| 1925 | |
| 1926 | return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash); |
| 1927 | } |
| 1928 | |
| 1929 | /* Disabling always succeeds */ |
| 1930 | static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) |
| 1931 | { |
| 1932 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1933 | |
| 1934 | if (ftrace_hash_empty(hash)) |
| 1935 | hash = NULL; |
| 1936 | |
| 1937 | __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH); |
| 1938 | } |
| 1939 | |
| 1940 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1941 | struct ftrace_hash *new_hash) |
| 1942 | { |
| 1943 | struct ftrace_hash *old_hash = ops->func_hash->filter_hash; |
| 1944 | |
| 1945 | if (ftrace_hash_empty(old_hash)) |
| 1946 | old_hash = NULL; |
| 1947 | |
| 1948 | if (ftrace_hash_empty(new_hash)) |
| 1949 | new_hash = NULL; |
| 1950 | |
| 1951 | return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash); |
| 1952 | } |
| 1953 | |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1954 | static void print_ip_ins(const char *fmt, const unsigned char *p) |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 1955 | { |
| 1956 | int i; |
| 1957 | |
| 1958 | printk(KERN_CONT "%s", fmt); |
| 1959 | |
| 1960 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 1961 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 1962 | } |
| 1963 | |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1964 | static struct ftrace_ops * |
| 1965 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 1966 | static struct ftrace_ops * |
| 1967 | 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] | 1968 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1969 | enum ftrace_bug_type ftrace_bug_type; |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1970 | const void *ftrace_expected; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1971 | |
| 1972 | static void print_bug_type(void) |
| 1973 | { |
| 1974 | switch (ftrace_bug_type) { |
| 1975 | case FTRACE_BUG_UNKNOWN: |
| 1976 | break; |
| 1977 | case FTRACE_BUG_INIT: |
| 1978 | pr_info("Initializing ftrace call sites\n"); |
| 1979 | break; |
| 1980 | case FTRACE_BUG_NOP: |
| 1981 | pr_info("Setting ftrace call site to NOP\n"); |
| 1982 | break; |
| 1983 | case FTRACE_BUG_CALL: |
| 1984 | pr_info("Setting ftrace call site to call ftrace function\n"); |
| 1985 | break; |
| 1986 | case FTRACE_BUG_UPDATE: |
| 1987 | pr_info("Updating ftrace call site to call a different ftrace function\n"); |
| 1988 | break; |
| 1989 | } |
| 1990 | } |
| 1991 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1992 | /** |
| 1993 | * ftrace_bug - report and shutdown function tracer |
| 1994 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1995 | * @rec: The record that failed |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1996 | * |
| 1997 | * The arch code that enables or disables the function tracing |
| 1998 | * can call ftrace_bug() when it has detected a problem in |
| 1999 | * modifying the code. @failed should be one of either: |
| 2000 | * EFAULT - if the problem happens on reading the @ip address |
| 2001 | * EINVAL - if what is read at @ip is not what was expected |
| 2002 | * EPERM - if the problem happens on writting to the @ip address |
| 2003 | */ |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2004 | void ftrace_bug(int failed, struct dyn_ftrace *rec) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2005 | { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2006 | unsigned long ip = rec ? rec->ip : 0; |
| 2007 | |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2008 | switch (failed) { |
| 2009 | case -EFAULT: |
| 2010 | FTRACE_WARN_ON_ONCE(1); |
| 2011 | pr_info("ftrace faulted on modifying "); |
| 2012 | print_ip_sym(ip); |
| 2013 | break; |
| 2014 | case -EINVAL: |
| 2015 | FTRACE_WARN_ON_ONCE(1); |
| 2016 | pr_info("ftrace failed to modify "); |
| 2017 | print_ip_sym(ip); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2018 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2019 | pr_cont("\n"); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2020 | if (ftrace_expected) { |
| 2021 | print_ip_ins(" expected: ", ftrace_expected); |
| 2022 | pr_cont("\n"); |
| 2023 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2024 | break; |
| 2025 | case -EPERM: |
| 2026 | FTRACE_WARN_ON_ONCE(1); |
| 2027 | pr_info("ftrace faulted on writing "); |
| 2028 | print_ip_sym(ip); |
| 2029 | break; |
| 2030 | default: |
| 2031 | FTRACE_WARN_ON_ONCE(1); |
| 2032 | pr_info("ftrace faulted on unknown error "); |
| 2033 | print_ip_sym(ip); |
| 2034 | } |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2035 | print_bug_type(); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2036 | if (rec) { |
| 2037 | struct ftrace_ops *ops = NULL; |
| 2038 | |
| 2039 | pr_info("ftrace record flags: %lx\n", rec->flags); |
| 2040 | pr_cont(" (%ld)%s", ftrace_rec_count(rec), |
| 2041 | rec->flags & FTRACE_FL_REGS ? " R" : " "); |
| 2042 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2043 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2044 | if (ops) { |
| 2045 | do { |
| 2046 | pr_cont("\ttramp: %pS (%pS)", |
| 2047 | (void *)ops->trampoline, |
| 2048 | (void *)ops->func); |
| 2049 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 2050 | } while (ops); |
| 2051 | } else |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2052 | pr_cont("\ttramp: ERROR!"); |
| 2053 | |
| 2054 | } |
| 2055 | ip = ftrace_get_addr_curr(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2056 | pr_cont("\n expected tramp: %lx\n", ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2057 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2058 | } |
| 2059 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2060 | 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] | 2061 | { |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2062 | unsigned long flag = 0UL; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 2063 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2064 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2065 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2066 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2067 | return FTRACE_UPDATE_IGNORE; |
| 2068 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2069 | /* |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2070 | * If we are updating calls: |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2071 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2072 | * If the record has a ref count, then we need to enable it |
| 2073 | * because someone is using it. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2074 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2075 | * Otherwise we make sure its disabled. |
| 2076 | * |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2077 | * If we are disabling calls, then disable all records that |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2078 | * are enabled. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2079 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2080 | if (enable && ftrace_rec_count(rec)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2081 | flag = FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2082 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2083 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2084 | * If enabling and the REGS flag does not match the REGS_EN, or |
| 2085 | * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore |
| 2086 | * this record. Set flags to fail the compare against ENABLED. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2087 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2088 | if (flag) { |
| 2089 | if (!(rec->flags & FTRACE_FL_REGS) != |
| 2090 | !(rec->flags & FTRACE_FL_REGS_EN)) |
| 2091 | flag |= FTRACE_FL_REGS; |
| 2092 | |
| 2093 | if (!(rec->flags & FTRACE_FL_TRAMP) != |
| 2094 | !(rec->flags & FTRACE_FL_TRAMP_EN)) |
| 2095 | flag |= FTRACE_FL_TRAMP; |
| 2096 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2097 | |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2098 | /* If the state of this record hasn't changed, then do nothing */ |
| 2099 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2100 | return FTRACE_UPDATE_IGNORE; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2101 | |
| 2102 | if (flag) { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2103 | /* Save off if rec is being enabled (for return value) */ |
| 2104 | flag ^= rec->flags & FTRACE_FL_ENABLED; |
| 2105 | |
| 2106 | if (update) { |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2107 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2108 | if (flag & FTRACE_FL_REGS) { |
| 2109 | if (rec->flags & FTRACE_FL_REGS) |
| 2110 | rec->flags |= FTRACE_FL_REGS_EN; |
| 2111 | else |
| 2112 | rec->flags &= ~FTRACE_FL_REGS_EN; |
| 2113 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2114 | if (flag & FTRACE_FL_TRAMP) { |
| 2115 | if (rec->flags & FTRACE_FL_TRAMP) |
| 2116 | rec->flags |= FTRACE_FL_TRAMP_EN; |
| 2117 | else |
| 2118 | rec->flags &= ~FTRACE_FL_TRAMP_EN; |
| 2119 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * If this record is being updated from a nop, then |
| 2124 | * return UPDATE_MAKE_CALL. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2125 | * Otherwise, |
| 2126 | * return UPDATE_MODIFY_CALL to tell the caller to convert |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2127 | * 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] | 2128 | * vice versa, or from a trampoline call. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2129 | */ |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2130 | if (flag & FTRACE_FL_ENABLED) { |
| 2131 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2132 | return FTRACE_UPDATE_MAKE_CALL; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2133 | } |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2134 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2135 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2136 | return FTRACE_UPDATE_MODIFY_CALL; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2137 | } |
| 2138 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2139 | if (update) { |
| 2140 | /* If there's no more users, clear all flags */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2141 | if (!ftrace_rec_count(rec)) |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2142 | rec->flags = 0; |
| 2143 | else |
Steven Rostedt (Red Hat) | b24d443 | 2015-03-04 23:10:28 -0500 | [diff] [blame] | 2144 | /* |
| 2145 | * Just disable the record, but keep the ops TRAMP |
| 2146 | * and REGS states. The _EN flags must be disabled though. |
| 2147 | */ |
| 2148 | rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN | |
| 2149 | FTRACE_FL_REGS_EN); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2150 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2151 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2152 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2153 | return FTRACE_UPDATE_MAKE_NOP; |
| 2154 | } |
| 2155 | |
| 2156 | /** |
| 2157 | * ftrace_update_record, set a record that now is tracing or not |
| 2158 | * @rec: the record to update |
| 2159 | * @enable: set to 1 if the record is tracing, zero to force disable |
| 2160 | * |
| 2161 | * The records that represent all functions that can be traced need |
| 2162 | * to be updated when tracing has been enabled. |
| 2163 | */ |
| 2164 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) |
| 2165 | { |
| 2166 | return ftrace_check_record(rec, enable, 1); |
| 2167 | } |
| 2168 | |
| 2169 | /** |
| 2170 | * ftrace_test_record, check if the record has been enabled or not |
| 2171 | * @rec: the record to test |
| 2172 | * @enable: set to 1 to check if enabled, 0 if it is disabled |
| 2173 | * |
| 2174 | * The arch code may need to test if a record is already set to |
| 2175 | * tracing to determine how to modify the function code that it |
| 2176 | * represents. |
| 2177 | */ |
| 2178 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) |
| 2179 | { |
| 2180 | return ftrace_check_record(rec, enable, 0); |
| 2181 | } |
| 2182 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2183 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2184 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) |
| 2185 | { |
| 2186 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2187 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2188 | |
| 2189 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2190 | |
| 2191 | if (!op->trampoline) |
| 2192 | continue; |
| 2193 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2194 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2195 | return op; |
| 2196 | } while_for_each_ftrace_op(op); |
| 2197 | |
| 2198 | return NULL; |
| 2199 | } |
| 2200 | |
| 2201 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2202 | ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, |
| 2203 | struct ftrace_ops *op) |
| 2204 | { |
| 2205 | unsigned long ip = rec->ip; |
| 2206 | |
| 2207 | while_for_each_ftrace_op(op) { |
| 2208 | |
| 2209 | if (!op->trampoline) |
| 2210 | continue; |
| 2211 | |
| 2212 | if (hash_contains_ip(ip, op->func_hash)) |
| 2213 | return op; |
| 2214 | } |
| 2215 | |
| 2216 | return NULL; |
| 2217 | } |
| 2218 | |
| 2219 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2220 | ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) |
| 2221 | { |
| 2222 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2223 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2224 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2225 | /* |
| 2226 | * Need to check removed ops first. |
| 2227 | * If they are being removed, and this rec has a tramp, |
| 2228 | * and this rec is in the ops list, then it would be the |
| 2229 | * one with the tramp. |
| 2230 | */ |
| 2231 | if (removed_ops) { |
| 2232 | if (hash_contains_ip(ip, &removed_ops->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2233 | return removed_ops; |
| 2234 | } |
| 2235 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2236 | /* |
| 2237 | * Need to find the current trampoline for a rec. |
| 2238 | * Now, a trampoline is only attached to a rec if there |
| 2239 | * was a single 'ops' attached to it. But this can be called |
| 2240 | * when we are adding another op to the rec or removing the |
| 2241 | * current one. Thus, if the op is being added, we can |
| 2242 | * 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] | 2243 | * yet. |
| 2244 | * |
| 2245 | * If an ops is being modified (hooking to different functions) |
| 2246 | * then we don't care about the new functions that are being |
| 2247 | * added, just the old ones (that are probably being removed). |
| 2248 | * |
| 2249 | * If we are adding an ops to a function that already is using |
| 2250 | * a trampoline, it needs to be removed (trampolines are only |
| 2251 | * for single ops connected), then an ops that is not being |
| 2252 | * modified also needs to be checked. |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2253 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2254 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2255 | |
| 2256 | if (!op->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2257 | continue; |
| 2258 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2259 | /* |
| 2260 | * If the ops is being added, it hasn't gotten to |
| 2261 | * the point to be removed from this tree yet. |
| 2262 | */ |
| 2263 | if (op->flags & FTRACE_OPS_FL_ADDING) |
| 2264 | continue; |
| 2265 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2266 | |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2267 | /* |
| 2268 | * If the ops is being modified and is in the old |
| 2269 | * hash, then it is probably being removed from this |
| 2270 | * function. |
| 2271 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2272 | if ((op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2273 | hash_contains_ip(ip, &op->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2274 | return op; |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2275 | /* |
| 2276 | * If the ops is not being added or modified, and it's |
| 2277 | * in its normal filter hash, then this must be the one |
| 2278 | * we want! |
| 2279 | */ |
| 2280 | if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2281 | hash_contains_ip(ip, op->func_hash)) |
| 2282 | return op; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2283 | |
| 2284 | } while_for_each_ftrace_op(op); |
| 2285 | |
| 2286 | return NULL; |
| 2287 | } |
| 2288 | |
| 2289 | static struct ftrace_ops * |
| 2290 | ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) |
| 2291 | { |
| 2292 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2293 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2294 | |
| 2295 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2296 | /* pass rec in as regs to have non-NULL val */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2297 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2298 | return op; |
| 2299 | } while_for_each_ftrace_op(op); |
| 2300 | |
| 2301 | return NULL; |
| 2302 | } |
| 2303 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2304 | /** |
| 2305 | * ftrace_get_addr_new - Get the call address to set to |
| 2306 | * @rec: The ftrace record descriptor |
| 2307 | * |
| 2308 | * If the record has the FTRACE_FL_REGS set, that means that it |
| 2309 | * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS |
| 2310 | * is not not set, then it wants to convert to the normal callback. |
| 2311 | * |
| 2312 | * Returns the address of the trampoline to set to |
| 2313 | */ |
| 2314 | unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) |
| 2315 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2316 | struct ftrace_ops *ops; |
| 2317 | |
| 2318 | /* Trampolines take precedence over regs */ |
| 2319 | if (rec->flags & FTRACE_FL_TRAMP) { |
| 2320 | ops = ftrace_find_tramp_ops_new(rec); |
| 2321 | if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 2322 | pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n", |
| 2323 | (void *)rec->ip, (void *)rec->ip, rec->flags); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2324 | /* Ftrace is shutting down, return anything */ |
| 2325 | return (unsigned long)FTRACE_ADDR; |
| 2326 | } |
| 2327 | return ops->trampoline; |
| 2328 | } |
| 2329 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2330 | if (rec->flags & FTRACE_FL_REGS) |
| 2331 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2332 | else |
| 2333 | return (unsigned long)FTRACE_ADDR; |
| 2334 | } |
| 2335 | |
| 2336 | /** |
| 2337 | * ftrace_get_addr_curr - Get the call address that is already there |
| 2338 | * @rec: The ftrace record descriptor |
| 2339 | * |
| 2340 | * The FTRACE_FL_REGS_EN is set when the record already points to |
| 2341 | * a function that saves all the regs. Basically the '_EN' version |
| 2342 | * represents the current state of the function. |
| 2343 | * |
| 2344 | * Returns the address of the trampoline that is currently being called |
| 2345 | */ |
| 2346 | unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) |
| 2347 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2348 | struct ftrace_ops *ops; |
| 2349 | |
| 2350 | /* Trampolines take precedence over regs */ |
| 2351 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2352 | ops = ftrace_find_tramp_ops_curr(rec); |
| 2353 | if (FTRACE_WARN_ON(!ops)) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 2354 | pr_warn("Bad trampoline accounting at: %p (%pS)\n", |
| 2355 | (void *)rec->ip, (void *)rec->ip); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2356 | /* Ftrace is shutting down, return anything */ |
| 2357 | return (unsigned long)FTRACE_ADDR; |
| 2358 | } |
| 2359 | return ops->trampoline; |
| 2360 | } |
| 2361 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2362 | if (rec->flags & FTRACE_FL_REGS_EN) |
| 2363 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2364 | else |
| 2365 | return (unsigned long)FTRACE_ADDR; |
| 2366 | } |
| 2367 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2368 | static int |
| 2369 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
| 2370 | { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2371 | unsigned long ftrace_old_addr; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2372 | unsigned long ftrace_addr; |
| 2373 | int ret; |
| 2374 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2375 | ftrace_addr = ftrace_get_addr_new(rec); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2376 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2377 | /* This needs to be done before we call ftrace_update_record */ |
| 2378 | ftrace_old_addr = ftrace_get_addr_curr(rec); |
| 2379 | |
| 2380 | ret = ftrace_update_record(rec, enable); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2381 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2382 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2383 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2384 | switch (ret) { |
| 2385 | case FTRACE_UPDATE_IGNORE: |
| 2386 | return 0; |
| 2387 | |
| 2388 | case FTRACE_UPDATE_MAKE_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2389 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2390 | return ftrace_make_call(rec, ftrace_addr); |
| 2391 | |
| 2392 | case FTRACE_UPDATE_MAKE_NOP: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2393 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt (Red Hat) | 39b5552 | 2014-08-17 20:59:10 -0400 | [diff] [blame] | 2394 | return ftrace_make_nop(NULL, rec, ftrace_old_addr); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2395 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2396 | case FTRACE_UPDATE_MODIFY_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2397 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2398 | return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | return -1; /* unknow ftrace bug */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2404 | void __weak ftrace_replace_code(int enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2405 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2406 | struct dyn_ftrace *rec; |
| 2407 | struct ftrace_page *pg; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2408 | int failed; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2409 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2410 | if (unlikely(ftrace_disabled)) |
| 2411 | return; |
| 2412 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2413 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 2414 | |
| 2415 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2416 | continue; |
| 2417 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2418 | failed = __ftrace_replace_code(rec, enable); |
Zhaolei | fa9d13c | 2009-03-13 17:16:34 +0800 | [diff] [blame] | 2419 | if (failed) { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2420 | ftrace_bug(failed, rec); |
Steven Rostedt | 3279ba3 | 2009-10-07 16:57:56 -0400 | [diff] [blame] | 2421 | /* Stop processing */ |
| 2422 | return; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2423 | } |
| 2424 | } while_for_each_ftrace_rec(); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2427 | struct ftrace_rec_iter { |
| 2428 | struct ftrace_page *pg; |
| 2429 | int index; |
| 2430 | }; |
| 2431 | |
| 2432 | /** |
| 2433 | * ftrace_rec_iter_start, start up iterating over traced functions |
| 2434 | * |
| 2435 | * Returns an iterator handle that is used to iterate over all |
| 2436 | * the records that represent address locations where functions |
| 2437 | * are traced. |
| 2438 | * |
| 2439 | * May return NULL if no records are available. |
| 2440 | */ |
| 2441 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 2442 | { |
| 2443 | /* |
| 2444 | * We only use a single iterator. |
| 2445 | * Protected by the ftrace_lock mutex. |
| 2446 | */ |
| 2447 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 2448 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 2449 | |
| 2450 | iter->pg = ftrace_pages_start; |
| 2451 | iter->index = 0; |
| 2452 | |
| 2453 | /* Could have empty pages */ |
| 2454 | while (iter->pg && !iter->pg->index) |
| 2455 | iter->pg = iter->pg->next; |
| 2456 | |
| 2457 | if (!iter->pg) |
| 2458 | return NULL; |
| 2459 | |
| 2460 | return iter; |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * ftrace_rec_iter_next, get the next record to process. |
| 2465 | * @iter: The handle to the iterator. |
| 2466 | * |
| 2467 | * Returns the next iterator after the given iterator @iter. |
| 2468 | */ |
| 2469 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 2470 | { |
| 2471 | iter->index++; |
| 2472 | |
| 2473 | if (iter->index >= iter->pg->index) { |
| 2474 | iter->pg = iter->pg->next; |
| 2475 | iter->index = 0; |
| 2476 | |
| 2477 | /* Could have empty pages */ |
| 2478 | while (iter->pg && !iter->pg->index) |
| 2479 | iter->pg = iter->pg->next; |
| 2480 | } |
| 2481 | |
| 2482 | if (!iter->pg) |
| 2483 | return NULL; |
| 2484 | |
| 2485 | return iter; |
| 2486 | } |
| 2487 | |
| 2488 | /** |
| 2489 | * ftrace_rec_iter_record, get the record at the iterator location |
| 2490 | * @iter: The current iterator location |
| 2491 | * |
| 2492 | * Returns the record that the current @iter is at. |
| 2493 | */ |
| 2494 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 2495 | { |
| 2496 | return &iter->pg->records[iter->index]; |
| 2497 | } |
| 2498 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2499 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2500 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2501 | { |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2502 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2503 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2504 | if (unlikely(ftrace_disabled)) |
| 2505 | return 0; |
| 2506 | |
Shaohua Li | 25aac9d | 2009-01-09 11:29:40 +0800 | [diff] [blame] | 2507 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2508 | if (ret) { |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2509 | ftrace_bug_type = FTRACE_BUG_INIT; |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2510 | ftrace_bug(ret, rec); |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2511 | return 0; |
Steven Rostedt | 37ad508 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2512 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2513 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2514 | } |
| 2515 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2516 | /* |
| 2517 | * archs can override this function if they must do something |
| 2518 | * before the modifying code is performed. |
| 2519 | */ |
| 2520 | int __weak ftrace_arch_code_modify_prepare(void) |
| 2521 | { |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * archs can override this function if they must do something |
| 2527 | * after the modifying code is performed. |
| 2528 | */ |
| 2529 | int __weak ftrace_arch_code_modify_post_process(void) |
| 2530 | { |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2534 | void ftrace_modify_all_code(int command) |
| 2535 | { |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2536 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2537 | int err = 0; |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2538 | |
| 2539 | /* |
| 2540 | * If the ftrace_caller calls a ftrace_ops func directly, |
| 2541 | * we need to make sure that it only traces functions it |
| 2542 | * expects to trace. When doing the switch of functions, |
| 2543 | * we need to update to the ftrace_ops_list_func first |
| 2544 | * before the transition between old and new calls are set, |
| 2545 | * as the ftrace_ops_list_func will check the ops hashes |
| 2546 | * to make sure the ops are having the right functions |
| 2547 | * traced. |
| 2548 | */ |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2549 | if (update) { |
| 2550 | err = ftrace_update_ftrace_func(ftrace_ops_list_func); |
| 2551 | if (FTRACE_WARN_ON(err)) |
| 2552 | return; |
| 2553 | } |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2554 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2555 | if (command & FTRACE_UPDATE_CALLS) |
| 2556 | ftrace_replace_code(1); |
| 2557 | else if (command & FTRACE_DISABLE_CALLS) |
| 2558 | ftrace_replace_code(0); |
| 2559 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2560 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
| 2561 | function_trace_op = set_function_trace_op; |
| 2562 | smp_wmb(); |
| 2563 | /* If irqs are disabled, we are in stop machine */ |
| 2564 | if (!irqs_disabled()) |
| 2565 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2566 | err = ftrace_update_ftrace_func(ftrace_trace_function); |
| 2567 | if (FTRACE_WARN_ON(err)) |
| 2568 | return; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2569 | } |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2570 | |
| 2571 | if (command & FTRACE_START_FUNC_RET) |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2572 | err = ftrace_enable_ftrace_graph_caller(); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2573 | else if (command & FTRACE_STOP_FUNC_RET) |
Petr Mladek | cd21067 | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2574 | err = ftrace_disable_ftrace_graph_caller(); |
| 2575 | FTRACE_WARN_ON(err); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2576 | } |
| 2577 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2578 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2579 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2580 | int *command = data; |
| 2581 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2582 | ftrace_modify_all_code(*command); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 2583 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2584 | return 0; |
| 2585 | } |
| 2586 | |
| 2587 | /** |
| 2588 | * ftrace_run_stop_machine, go back to the stop machine method |
| 2589 | * @command: The command to tell ftrace what to do |
| 2590 | * |
| 2591 | * If an arch needs to fall back to the stop machine method, the |
| 2592 | * it can call this function. |
| 2593 | */ |
| 2594 | void ftrace_run_stop_machine(int command) |
| 2595 | { |
| 2596 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 2597 | } |
| 2598 | |
| 2599 | /** |
| 2600 | * arch_ftrace_update_code, modify the code to trace or not trace |
| 2601 | * @command: The command that needs to be done |
| 2602 | * |
| 2603 | * Archs can override this function if it does not need to |
| 2604 | * run stop_machine() to modify code. |
| 2605 | */ |
| 2606 | void __weak arch_ftrace_update_code(int command) |
| 2607 | { |
| 2608 | ftrace_run_stop_machine(command); |
| 2609 | } |
| 2610 | |
| 2611 | static void ftrace_run_update_code(int command) |
| 2612 | { |
| 2613 | int ret; |
| 2614 | |
| 2615 | ret = ftrace_arch_code_modify_prepare(); |
| 2616 | FTRACE_WARN_ON(ret); |
| 2617 | if (ret) |
| 2618 | return; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2619 | |
| 2620 | /* |
| 2621 | * By default we use stop_machine() to modify the code. |
| 2622 | * But archs can do what ever they want as long as it |
| 2623 | * is safe. The stop_machine() is the safest, but also |
| 2624 | * produces the most overhead. |
| 2625 | */ |
| 2626 | arch_ftrace_update_code(command); |
| 2627 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2628 | ret = ftrace_arch_code_modify_post_process(); |
| 2629 | FTRACE_WARN_ON(ret); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2630 | } |
| 2631 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2632 | 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] | 2633 | struct ftrace_ops_hash *old_hash) |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2634 | { |
| 2635 | ops->flags |= FTRACE_OPS_FL_MODIFYING; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2636 | ops->old_hash.filter_hash = old_hash->filter_hash; |
| 2637 | ops->old_hash.notrace_hash = old_hash->notrace_hash; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2638 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2639 | ops->old_hash.filter_hash = NULL; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2640 | ops->old_hash.notrace_hash = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2641 | ops->flags &= ~FTRACE_OPS_FL_MODIFYING; |
| 2642 | } |
| 2643 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2644 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2645 | static int ftrace_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2646 | |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2647 | void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) |
| 2648 | { |
| 2649 | } |
| 2650 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2651 | static void per_cpu_ops_free(struct ftrace_ops *ops) |
Jiri Slaby | db0fbad | 2014-03-10 21:42:11 +0100 | [diff] [blame] | 2652 | { |
| 2653 | free_percpu(ops->disabled); |
| 2654 | } |
| 2655 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2656 | static void ftrace_startup_enable(int command) |
| 2657 | { |
| 2658 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2659 | saved_ftrace_func = ftrace_trace_function; |
| 2660 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2661 | } |
| 2662 | |
| 2663 | if (!command || !ftrace_enabled) |
| 2664 | return; |
| 2665 | |
| 2666 | ftrace_run_update_code(command); |
| 2667 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2668 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2669 | static void ftrace_startup_all(int command) |
| 2670 | { |
| 2671 | update_all_ops = true; |
| 2672 | ftrace_startup_enable(command); |
| 2673 | update_all_ops = false; |
| 2674 | } |
| 2675 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2676 | static int ftrace_startup(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2677 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2678 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2679 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2680 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2681 | return -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2682 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2683 | ret = __register_ftrace_function(ops); |
| 2684 | if (ret) |
| 2685 | return ret; |
| 2686 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2687 | ftrace_start_up++; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2688 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2689 | /* |
| 2690 | * Note that ftrace probes uses this to start up |
| 2691 | * and modify functions it will probe. But we still |
| 2692 | * set the ADDING flag for modification, as probes |
| 2693 | * do not have trampolines. If they add them in the |
| 2694 | * future, then the probes will need to distinguish |
| 2695 | * between adding and updating probes. |
| 2696 | */ |
| 2697 | ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; |
Steven Rostedt (Red Hat) | 66209a5 | 2014-05-06 21:57:49 -0400 | [diff] [blame] | 2698 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2699 | ret = ftrace_hash_ipmodify_enable(ops); |
| 2700 | if (ret < 0) { |
| 2701 | /* Rollback registration process */ |
| 2702 | __unregister_ftrace_function(ops); |
| 2703 | ftrace_start_up--; |
| 2704 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 2705 | return ret; |
| 2706 | } |
| 2707 | |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2708 | if (ftrace_hash_rec_enable(ops, 1)) |
| 2709 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2710 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2711 | ftrace_startup_enable(command); |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2712 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2713 | ops->flags &= ~FTRACE_OPS_FL_ADDING; |
| 2714 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2715 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2718 | static int ftrace_shutdown(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2719 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2720 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2721 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2722 | if (unlikely(ftrace_disabled)) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2723 | return -ENODEV; |
| 2724 | |
| 2725 | ret = __unregister_ftrace_function(ops); |
| 2726 | if (ret) |
| 2727 | return ret; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2728 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2729 | ftrace_start_up--; |
Frederic Weisbecker | 9ea1a15 | 2009-06-20 06:52:21 +0200 | [diff] [blame] | 2730 | /* |
| 2731 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 2732 | * critical but the ftrace_call callers may be never nopped again after |
| 2733 | * further ftrace uses. |
| 2734 | */ |
| 2735 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 2736 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2737 | /* Disabling ipmodify never fails */ |
| 2738 | ftrace_hash_ipmodify_disable(ops); |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2739 | |
| 2740 | if (ftrace_hash_rec_disable(ops, 1)) |
| 2741 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2742 | |
Namhyung Kim | a737e6d | 2014-06-12 23:56:12 +0900 | [diff] [blame] | 2743 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2744 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2745 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2746 | saved_ftrace_func = ftrace_trace_function; |
| 2747 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2748 | } |
| 2749 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2750 | if (!command || !ftrace_enabled) { |
| 2751 | /* |
Steven Rostedt (VMware) | 100553e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2752 | * If these are dynamic or per_cpu ops, they still |
| 2753 | * need their data freed. Since, function tracing is |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2754 | * not currently active, we can just free them |
| 2755 | * without synchronizing all CPUs. |
| 2756 | */ |
Steven Rostedt (VMware) | 100553e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2757 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) |
| 2758 | goto free_ops; |
| 2759 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2760 | return 0; |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2761 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2762 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2763 | /* |
| 2764 | * If the ops uses a trampoline, then it needs to be |
| 2765 | * tested first on update. |
| 2766 | */ |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2767 | ops->flags |= FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2768 | removed_ops = ops; |
| 2769 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2770 | /* The trampoline logic checks the old hashes */ |
| 2771 | ops->old_hash.filter_hash = ops->func_hash->filter_hash; |
| 2772 | ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; |
| 2773 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2774 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2775 | |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2776 | /* |
| 2777 | * If there's no more ops registered with ftrace, run a |
| 2778 | * sanity check to make sure all rec flags are cleared. |
| 2779 | */ |
| 2780 | if (ftrace_ops_list == &ftrace_list_end) { |
| 2781 | struct ftrace_page *pg; |
| 2782 | struct dyn_ftrace *rec; |
| 2783 | |
| 2784 | do_for_each_ftrace_rec(pg, rec) { |
Alexei Starovoitov | 977c1f9 | 2016-11-07 15:14:20 -0800 | [diff] [blame] | 2785 | if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED)) |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2786 | pr_warn(" %pS flags:%lx\n", |
| 2787 | (void *)rec->ip, rec->flags); |
| 2788 | } while_for_each_ftrace_rec(); |
| 2789 | } |
| 2790 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2791 | ops->old_hash.filter_hash = NULL; |
| 2792 | ops->old_hash.notrace_hash = NULL; |
| 2793 | |
| 2794 | removed_ops = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2795 | ops->flags &= ~FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2796 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2797 | /* |
| 2798 | * Dynamic ops may be freed, we must make sure that all |
| 2799 | * callers are done before leaving this function. |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2800 | * 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] | 2801 | * ops. |
| 2802 | * |
| 2803 | * Again, normal synchronize_sched() is not good enough. |
| 2804 | * We need to do a hard force of sched synchronization. |
| 2805 | * This is because we use preempt_disable() to do RCU, but |
| 2806 | * the function tracers can be called where RCU is not watching |
| 2807 | * (like before user_exit()). We can not rely on the RCU |
| 2808 | * infrastructure to do the synchronization, thus we must do it |
| 2809 | * ourselves. |
| 2810 | */ |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2811 | 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] | 2812 | schedule_on_each_cpu(ftrace_sync); |
| 2813 | |
Steven Rostedt (VMware) | 100553e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2814 | free_ops: |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2815 | arch_ftrace_trampoline_free(ops); |
| 2816 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2817 | if (ops->flags & FTRACE_OPS_FL_PER_CPU) |
| 2818 | per_cpu_ops_free(ops); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2819 | } |
| 2820 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2821 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2822 | } |
| 2823 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2824 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2825 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2826 | int command; |
| 2827 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2828 | if (unlikely(ftrace_disabled)) |
| 2829 | return; |
| 2830 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2831 | /* Force update next time */ |
| 2832 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2833 | /* ftrace_start_up is true if we want ftrace running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2834 | if (ftrace_start_up) { |
| 2835 | command = FTRACE_UPDATE_CALLS; |
| 2836 | if (ftrace_graph_active) |
| 2837 | command |= FTRACE_START_FUNC_RET; |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 2838 | ftrace_startup_enable(command); |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2839 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2840 | } |
| 2841 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2842 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2843 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2844 | int command; |
| 2845 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2846 | if (unlikely(ftrace_disabled)) |
| 2847 | return; |
| 2848 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2849 | /* ftrace_start_up is true if ftrace is running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2850 | if (ftrace_start_up) { |
| 2851 | command = FTRACE_DISABLE_CALLS; |
| 2852 | if (ftrace_graph_active) |
| 2853 | command |= FTRACE_STOP_FUNC_RET; |
| 2854 | ftrace_run_update_code(command); |
| 2855 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2856 | } |
| 2857 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2858 | static cycle_t ftrace_update_time; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2859 | unsigned long ftrace_update_tot_cnt; |
| 2860 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2861 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2862 | { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2863 | /* |
| 2864 | * Filter_hash being empty will default to trace module. |
| 2865 | * But notrace hash requires a test of individual module functions. |
| 2866 | */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2867 | return ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2868 | ftrace_hash_empty(ops->func_hash->notrace_hash); |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2869 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2870 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2871 | /* |
| 2872 | * Check if the current ops references the record. |
| 2873 | * |
| 2874 | * If the ops traces all functions, then it was already accounted for. |
| 2875 | * If the ops does not trace the current record function, skip it. |
| 2876 | * If the ops ignores the function via notrace filter, skip it. |
| 2877 | */ |
| 2878 | static inline bool |
| 2879 | ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 2880 | { |
| 2881 | /* If ops isn't enabled, ignore it */ |
| 2882 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 2883 | return 0; |
| 2884 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2885 | /* If ops traces all then it includes this function */ |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2886 | if (ops_traces_mod(ops)) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2887 | return 1; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2888 | |
| 2889 | /* The function must be in the filter */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2890 | if (!ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2891 | !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2892 | return 0; |
| 2893 | |
| 2894 | /* If in notrace hash, we ignore it too */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2895 | 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] | 2896 | return 0; |
| 2897 | |
| 2898 | return 1; |
| 2899 | } |
| 2900 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2901 | 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] | 2902 | { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2903 | struct ftrace_page *pg; |
Lai Jiangshan | e94142a | 2009-03-13 17:51:27 +0800 | [diff] [blame] | 2904 | struct dyn_ftrace *p; |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2905 | cycle_t start, stop; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2906 | unsigned long update_cnt = 0; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2907 | unsigned long rec_flags = 0; |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2908 | int i; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2909 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2910 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2911 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2912 | /* |
| 2913 | * When a module is loaded, this function is called to convert |
| 2914 | * the calls to mcount in its text to nops, and also to create |
| 2915 | * an entry in the ftrace data. Now, if ftrace is activated |
| 2916 | * after this call, but before the module sets its text to |
| 2917 | * read-only, the modification of enabling ftrace can fail if |
| 2918 | * the read-only is done while ftrace is converting the calls. |
| 2919 | * To prevent this, the module's records are set as disabled |
| 2920 | * and will be enabled after the call to set the module's text |
| 2921 | * to read-only. |
| 2922 | */ |
| 2923 | if (mod) |
| 2924 | rec_flags |= FTRACE_FL_DISABLED; |
| 2925 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2926 | for (pg = new_pgs; pg; pg = pg->next) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2927 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2928 | for (i = 0; i < pg->index; i++) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2929 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2930 | /* If something went wrong, bail without enabling anything */ |
| 2931 | if (unlikely(ftrace_disabled)) |
| 2932 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2933 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2934 | p = &pg->records[i]; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2935 | p->flags = rec_flags; |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 2936 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2937 | /* |
| 2938 | * Do the initial record conversion from mcount jump |
| 2939 | * to the NOP instructions. |
| 2940 | */ |
| 2941 | if (!ftrace_code_disable(mod, p)) |
| 2942 | break; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2943 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2944 | update_cnt++; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2945 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2948 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2949 | ftrace_update_time = stop - start; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2950 | ftrace_update_tot_cnt += update_cnt; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2951 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2952 | return 0; |
| 2953 | } |
| 2954 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2955 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2956 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2957 | int order; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2958 | int cnt; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2959 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2960 | if (WARN_ON(!count)) |
| 2961 | return -EINVAL; |
| 2962 | |
| 2963 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2964 | |
| 2965 | /* |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2966 | * We want to fill as much as possible. No more than a page |
| 2967 | * may be empty. |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2968 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2969 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
| 2970 | order--; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2971 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2972 | again: |
| 2973 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 2974 | |
| 2975 | if (!pg->records) { |
| 2976 | /* if we can't allocate this size, try something smaller */ |
| 2977 | if (!order) |
| 2978 | return -ENOMEM; |
| 2979 | order >>= 1; |
| 2980 | goto again; |
| 2981 | } |
| 2982 | |
| 2983 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 2984 | pg->size = cnt; |
| 2985 | |
| 2986 | if (cnt > count) |
| 2987 | cnt = count; |
| 2988 | |
| 2989 | return cnt; |
| 2990 | } |
| 2991 | |
| 2992 | static struct ftrace_page * |
| 2993 | ftrace_allocate_pages(unsigned long num_to_init) |
| 2994 | { |
| 2995 | struct ftrace_page *start_pg; |
| 2996 | struct ftrace_page *pg; |
| 2997 | int order; |
| 2998 | int cnt; |
| 2999 | |
| 3000 | if (!num_to_init) |
| 3001 | return 0; |
| 3002 | |
| 3003 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3004 | if (!pg) |
| 3005 | return NULL; |
| 3006 | |
| 3007 | /* |
| 3008 | * Try to allocate as much as possible in one continues |
| 3009 | * location that fills in all of the space. We want to |
| 3010 | * waste as little space as possible. |
| 3011 | */ |
| 3012 | for (;;) { |
| 3013 | cnt = ftrace_allocate_records(pg, num_to_init); |
| 3014 | if (cnt < 0) |
| 3015 | goto free_pages; |
| 3016 | |
| 3017 | num_to_init -= cnt; |
| 3018 | if (!num_to_init) |
| 3019 | break; |
| 3020 | |
| 3021 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3022 | if (!pg->next) |
| 3023 | goto free_pages; |
| 3024 | |
| 3025 | pg = pg->next; |
| 3026 | } |
| 3027 | |
| 3028 | return start_pg; |
| 3029 | |
| 3030 | free_pages: |
Namhyung Kim | 1f61be00 | 2014-06-11 17:06:53 +0900 | [diff] [blame] | 3031 | pg = start_pg; |
| 3032 | while (pg) { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3033 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 3034 | free_pages((unsigned long)pg->records, order); |
| 3035 | start_pg = pg->next; |
| 3036 | kfree(pg); |
| 3037 | pg = start_pg; |
| 3038 | } |
| 3039 | pr_info("ftrace: FAILED to allocate memory for functions\n"); |
| 3040 | return NULL; |
| 3041 | } |
| 3042 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3043 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 3044 | |
| 3045 | struct ftrace_iterator { |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3046 | loff_t pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3047 | loff_t func_pos; |
| 3048 | struct ftrace_page *pg; |
| 3049 | struct dyn_ftrace *func; |
| 3050 | struct ftrace_func_probe *probe; |
| 3051 | struct trace_parser parser; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3052 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3053 | struct ftrace_ops *ops; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3054 | int hidx; |
| 3055 | int idx; |
| 3056 | unsigned flags; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3057 | }; |
| 3058 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3059 | static void * |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3060 | t_hash_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3061 | { |
| 3062 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3063 | struct hlist_node *hnd = NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3064 | struct hlist_head *hhd; |
| 3065 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3066 | (*pos)++; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3067 | iter->pos = *pos; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3068 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3069 | if (iter->probe) |
| 3070 | hnd = &iter->probe->node; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3071 | retry: |
| 3072 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) |
| 3073 | return NULL; |
| 3074 | |
| 3075 | hhd = &ftrace_func_hash[iter->hidx]; |
| 3076 | |
| 3077 | if (hlist_empty(hhd)) { |
| 3078 | iter->hidx++; |
| 3079 | hnd = NULL; |
| 3080 | goto retry; |
| 3081 | } |
| 3082 | |
| 3083 | if (!hnd) |
| 3084 | hnd = hhd->first; |
| 3085 | else { |
| 3086 | hnd = hnd->next; |
| 3087 | if (!hnd) { |
| 3088 | iter->hidx++; |
| 3089 | goto retry; |
| 3090 | } |
| 3091 | } |
| 3092 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3093 | if (WARN_ON_ONCE(!hnd)) |
| 3094 | return NULL; |
| 3095 | |
| 3096 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); |
| 3097 | |
| 3098 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | static void *t_hash_start(struct seq_file *m, loff_t *pos) |
| 3102 | { |
| 3103 | struct ftrace_iterator *iter = m->private; |
| 3104 | void *p = NULL; |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3105 | loff_t l; |
| 3106 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3107 | if (!(iter->flags & FTRACE_ITER_DO_HASH)) |
| 3108 | return NULL; |
| 3109 | |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3110 | if (iter->func_pos > *pos) |
| 3111 | return NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3112 | |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3113 | iter->hidx = 0; |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3114 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3115 | p = t_hash_next(m, &l); |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3116 | if (!p) |
| 3117 | break; |
| 3118 | } |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3119 | if (!p) |
| 3120 | return NULL; |
| 3121 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3122 | /* Only set this if we have an item */ |
| 3123 | iter->flags |= FTRACE_ITER_HASH; |
| 3124 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3125 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3126 | } |
| 3127 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3128 | static int |
| 3129 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3130 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3131 | struct ftrace_func_probe *rec; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3132 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3133 | rec = iter->probe; |
| 3134 | if (WARN_ON_ONCE(!rec)) |
| 3135 | return -EIO; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3136 | |
Steven Rostedt | 809dcf2 | 2009-02-16 23:06:01 -0500 | [diff] [blame] | 3137 | if (rec->ops->print) |
| 3138 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); |
| 3139 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 3140 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3141 | |
| 3142 | if (rec->data) |
| 3143 | seq_printf(m, ":%p", rec->data); |
| 3144 | seq_putc(m, '\n'); |
| 3145 | |
| 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | static void * |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3150 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 3151 | { |
| 3152 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3153 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3154 | struct dyn_ftrace *rec = NULL; |
| 3155 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3156 | if (unlikely(ftrace_disabled)) |
| 3157 | return NULL; |
| 3158 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3159 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3160 | return t_hash_next(m, pos); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3161 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3162 | (*pos)++; |
Jiri Olsa | 1106b69 | 2011-02-16 17:35:34 +0100 | [diff] [blame] | 3163 | iter->pos = iter->func_pos = *pos; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3164 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3165 | if (iter->flags & FTRACE_ITER_PRINTALL) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3166 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3167 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3168 | retry: |
| 3169 | if (iter->idx >= iter->pg->index) { |
| 3170 | if (iter->pg->next) { |
| 3171 | iter->pg = iter->pg->next; |
| 3172 | iter->idx = 0; |
| 3173 | goto retry; |
| 3174 | } |
| 3175 | } else { |
| 3176 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3177 | if (((iter->flags & FTRACE_ITER_FILTER) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3178 | !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) || |
Steven Rostedt | 0183fb1 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3179 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3180 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3181 | !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) || |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3182 | |
| 3183 | ((iter->flags & FTRACE_ITER_ENABLED) && |
Steven Rostedt (Red Hat) | 23ea9c4 | 2013-05-09 19:31:48 -0400 | [diff] [blame] | 3184 | !(rec->flags & FTRACE_FL_ENABLED))) { |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3185 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3186 | rec = NULL; |
| 3187 | goto retry; |
| 3188 | } |
| 3189 | } |
| 3190 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3191 | if (!rec) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 3192 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3193 | |
| 3194 | iter->func = rec; |
| 3195 | |
| 3196 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3197 | } |
| 3198 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3199 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 3200 | { |
| 3201 | iter->pos = 0; |
| 3202 | iter->func_pos = 0; |
Dan Carpenter | 70f77b3 | 2012-06-09 19:10:27 +0300 | [diff] [blame] | 3203 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 3207 | { |
| 3208 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3209 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3210 | void *p = NULL; |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3211 | loff_t l; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3212 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3213 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3214 | |
| 3215 | if (unlikely(ftrace_disabled)) |
| 3216 | return NULL; |
| 3217 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3218 | /* |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3219 | * If an lseek was done, then reset and start from beginning. |
| 3220 | */ |
| 3221 | if (*pos < iter->pos) |
| 3222 | reset_iter_read(iter); |
| 3223 | |
| 3224 | /* |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3225 | * For set_ftrace_filter reading, if we have the filter |
| 3226 | * off, we can short cut and just print out that all |
| 3227 | * functions are enabled. |
| 3228 | */ |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3229 | if ((iter->flags & FTRACE_ITER_FILTER && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3230 | ftrace_hash_empty(ops->func_hash->filter_hash)) || |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3231 | (iter->flags & FTRACE_ITER_NOTRACE && |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3232 | ftrace_hash_empty(ops->func_hash->notrace_hash))) { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3233 | if (*pos > 0) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3234 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3235 | iter->flags |= FTRACE_ITER_PRINTALL; |
Chris Wright | df09162 | 2010-09-09 16:34:59 -0700 | [diff] [blame] | 3236 | /* reset in case of seek/pread */ |
| 3237 | iter->flags &= ~FTRACE_ITER_HASH; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3238 | return iter; |
| 3239 | } |
| 3240 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3241 | if (iter->flags & FTRACE_ITER_HASH) |
| 3242 | return t_hash_start(m, pos); |
| 3243 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3244 | /* |
| 3245 | * Unfortunately, we need to restart at ftrace_pages_start |
| 3246 | * every time we let go of the ftrace_mutex. This is because |
| 3247 | * those pointers can change without the lock. |
| 3248 | */ |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3249 | iter->pg = ftrace_pages_start; |
| 3250 | iter->idx = 0; |
| 3251 | for (l = 0; l <= *pos; ) { |
| 3252 | p = t_next(m, p, &l); |
| 3253 | if (!p) |
| 3254 | break; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 3255 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 3256 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3257 | if (!p) |
| 3258 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3259 | |
| 3260 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | static void t_stop(struct seq_file *m, void *p) |
| 3264 | { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3265 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3266 | } |
| 3267 | |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3268 | void * __weak |
| 3269 | arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 3270 | { |
| 3271 | return NULL; |
| 3272 | } |
| 3273 | |
| 3274 | static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, |
| 3275 | struct dyn_ftrace *rec) |
| 3276 | { |
| 3277 | void *ptr; |
| 3278 | |
| 3279 | ptr = arch_ftrace_trampoline_func(ops, rec); |
| 3280 | if (ptr) |
| 3281 | seq_printf(m, " ->%pS", ptr); |
| 3282 | } |
| 3283 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3284 | static int t_show(struct seq_file *m, void *v) |
| 3285 | { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3286 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3287 | struct dyn_ftrace *rec; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3288 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3289 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3290 | return t_hash_show(m, iter); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3291 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3292 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3293 | if (iter->flags & FTRACE_ITER_NOTRACE) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3294 | seq_puts(m, "#### no functions disabled ####\n"); |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3295 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3296 | seq_puts(m, "#### all functions enabled ####\n"); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3297 | return 0; |
| 3298 | } |
| 3299 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3300 | rec = iter->func; |
| 3301 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3302 | if (!rec) |
| 3303 | return 0; |
| 3304 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3305 | seq_printf(m, "%ps", (void *)rec->ip); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3306 | if (iter->flags & FTRACE_ITER_ENABLED) { |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3307 | struct ftrace_ops *ops; |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3308 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3309 | seq_printf(m, " (%ld)%s%s", |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 3310 | ftrace_rec_count(rec), |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3311 | rec->flags & FTRACE_FL_REGS ? " R" : " ", |
| 3312 | rec->flags & FTRACE_FL_IPMODIFY ? " I" : " "); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3313 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 3314 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3315 | if (ops) { |
| 3316 | do { |
| 3317 | seq_printf(m, "\ttramp: %pS (%pS)", |
| 3318 | (void *)ops->trampoline, |
| 3319 | (void *)ops->func); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3320 | add_trampoline_func(m, ops, rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3321 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 3322 | } while (ops); |
| 3323 | } else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3324 | seq_puts(m, "\ttramp: ERROR!"); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3325 | } else { |
| 3326 | add_trampoline_func(m, NULL, rec); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3330 | seq_putc(m, '\n'); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3331 | |
| 3332 | return 0; |
| 3333 | } |
| 3334 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3335 | static const struct seq_operations show_ftrace_seq_ops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3336 | .start = t_start, |
| 3337 | .next = t_next, |
| 3338 | .stop = t_stop, |
| 3339 | .show = t_show, |
| 3340 | }; |
| 3341 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3342 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3343 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 3344 | { |
| 3345 | struct ftrace_iterator *iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3346 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3347 | if (unlikely(ftrace_disabled)) |
| 3348 | return -ENODEV; |
| 3349 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3350 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3351 | if (iter) { |
| 3352 | iter->pg = ftrace_pages_start; |
| 3353 | iter->ops = &global_ops; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3354 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3355 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3356 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3357 | } |
| 3358 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3359 | static int |
| 3360 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 3361 | { |
| 3362 | struct ftrace_iterator *iter; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3363 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3364 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 3365 | if (iter) { |
| 3366 | iter->pg = ftrace_pages_start; |
| 3367 | iter->flags = FTRACE_ITER_ENABLED; |
| 3368 | iter->ops = &global_ops; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3369 | } |
| 3370 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3371 | return iter ? 0 : -ENOMEM; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3372 | } |
| 3373 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3374 | /** |
| 3375 | * ftrace_regex_open - initialize function tracer filter files |
| 3376 | * @ops: The ftrace_ops that hold the hash filters |
| 3377 | * @flag: The type of filter to process |
| 3378 | * @inode: The inode, usually passed in to your open routine |
| 3379 | * @file: The file, usually passed in to your open routine |
| 3380 | * |
| 3381 | * ftrace_regex_open() initializes the filter files for the |
| 3382 | * @ops. Depending on @flag it may process the filter hash or |
| 3383 | * the notrace hash of @ops. With this called from the open |
| 3384 | * routine, you can use ftrace_filter_write() for the write |
| 3385 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 3386 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3387 | * tracing_lseek() should be used as the lseek routine, and |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3388 | * release must call ftrace_regex_release(). |
| 3389 | */ |
| 3390 | int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3391 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3392 | struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3393 | { |
| 3394 | struct ftrace_iterator *iter; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3395 | struct ftrace_hash *hash; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3396 | int ret = 0; |
| 3397 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3398 | ftrace_ops_init(ops); |
| 3399 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3400 | if (unlikely(ftrace_disabled)) |
| 3401 | return -ENODEV; |
| 3402 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3403 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 3404 | if (!iter) |
| 3405 | return -ENOMEM; |
| 3406 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3407 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
| 3408 | kfree(iter); |
| 3409 | return -ENOMEM; |
| 3410 | } |
| 3411 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3412 | iter->ops = ops; |
| 3413 | iter->flags = flag; |
| 3414 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3415 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3416 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3417 | if (flag & FTRACE_ITER_NOTRACE) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3418 | hash = ops->func_hash->notrace_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3419 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3420 | hash = ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3421 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3422 | if (file->f_mode & FMODE_WRITE) { |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3423 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 3424 | |
| 3425 | if (file->f_flags & O_TRUNC) |
| 3426 | iter->hash = alloc_ftrace_hash(size_bits); |
| 3427 | else |
| 3428 | iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); |
| 3429 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3430 | if (!iter->hash) { |
| 3431 | trace_parser_put(&iter->parser); |
| 3432 | kfree(iter); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3433 | ret = -ENOMEM; |
| 3434 | goto out_unlock; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3435 | } |
| 3436 | } |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3437 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3438 | if (file->f_mode & FMODE_READ) { |
| 3439 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3440 | |
| 3441 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 3442 | if (!ret) { |
| 3443 | struct seq_file *m = file->private_data; |
| 3444 | m->private = iter; |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3445 | } else { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3446 | /* Failed */ |
| 3447 | free_ftrace_hash(iter->hash); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3448 | trace_parser_put(&iter->parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3449 | kfree(iter); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3450 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3451 | } else |
| 3452 | file->private_data = iter; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3453 | |
| 3454 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3455 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3456 | |
| 3457 | return ret; |
| 3458 | } |
| 3459 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3460 | static int |
| 3461 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 3462 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3463 | struct ftrace_ops *ops = inode->i_private; |
| 3464 | |
| 3465 | return ftrace_regex_open(ops, |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3466 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, |
| 3467 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | static int |
| 3471 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 3472 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3473 | struct ftrace_ops *ops = inode->i_private; |
| 3474 | |
| 3475 | return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3476 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3477 | } |
| 3478 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3479 | /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */ |
| 3480 | struct ftrace_glob { |
| 3481 | char *search; |
| 3482 | unsigned len; |
| 3483 | int type; |
| 3484 | }; |
| 3485 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3486 | /* |
| 3487 | * If symbols in an architecture don't correspond exactly to the user-visible |
| 3488 | * name of what they represent, it is possible to define this function to |
| 3489 | * perform the necessary adjustments. |
| 3490 | */ |
| 3491 | char * __weak arch_ftrace_match_adjust(char *str, const char *search) |
| 3492 | { |
| 3493 | return str; |
| 3494 | } |
| 3495 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3496 | static int ftrace_match(char *str, struct ftrace_glob *g) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3497 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3498 | int matched = 0; |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3499 | int slen; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3500 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3501 | str = arch_ftrace_match_adjust(str, g->search); |
| 3502 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3503 | switch (g->type) { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3504 | case MATCH_FULL: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3505 | if (strcmp(str, g->search) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3506 | matched = 1; |
| 3507 | break; |
| 3508 | case MATCH_FRONT_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3509 | if (strncmp(str, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3510 | matched = 1; |
| 3511 | break; |
| 3512 | case MATCH_MIDDLE_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3513 | if (strstr(str, g->search)) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3514 | matched = 1; |
| 3515 | break; |
| 3516 | case MATCH_END_ONLY: |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3517 | slen = strlen(str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3518 | if (slen >= g->len && |
| 3519 | memcmp(str + slen - g->len, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3520 | matched = 1; |
| 3521 | break; |
| 3522 | } |
| 3523 | |
| 3524 | return matched; |
| 3525 | } |
| 3526 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3527 | static int |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3528 | 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] | 3529 | { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3530 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3531 | int ret = 0; |
| 3532 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3533 | entry = ftrace_lookup_ip(hash, rec->ip); |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3534 | if (clear_filter) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3535 | /* Do nothing if it doesn't exist */ |
| 3536 | if (!entry) |
| 3537 | return 0; |
| 3538 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3539 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3540 | } else { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3541 | /* Do nothing if it exists */ |
| 3542 | if (entry) |
| 3543 | return 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3544 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3545 | ret = add_hash_entry(hash, rec->ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3546 | } |
| 3547 | return ret; |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 3548 | } |
| 3549 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3550 | static int |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3551 | ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g, |
| 3552 | struct ftrace_glob *mod_g, int exclude_mod) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3553 | { |
| 3554 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3555 | char *modname; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3556 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3557 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); |
| 3558 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3559 | if (mod_g) { |
| 3560 | int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0; |
| 3561 | |
| 3562 | /* blank module name to match all modules */ |
| 3563 | if (!mod_g->len) { |
| 3564 | /* blank module globbing: modname xor exclude_mod */ |
| 3565 | if ((!exclude_mod) != (!modname)) |
| 3566 | goto func_match; |
| 3567 | return 0; |
| 3568 | } |
| 3569 | |
| 3570 | /* not matching the module */ |
| 3571 | if (!modname || !mod_matches) { |
| 3572 | if (exclude_mod) |
| 3573 | goto func_match; |
| 3574 | else |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | if (mod_matches && exclude_mod) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3579 | return 0; |
| 3580 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3581 | func_match: |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3582 | /* blank search means to match all funcs in the mod */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3583 | if (!func_g->len) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3584 | return 1; |
| 3585 | } |
| 3586 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3587 | return ftrace_match(str, func_g); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3588 | } |
| 3589 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3590 | static int |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3591 | match_records(struct ftrace_hash *hash, char *func, int len, char *mod) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3592 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3593 | struct ftrace_page *pg; |
| 3594 | struct dyn_ftrace *rec; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3595 | struct ftrace_glob func_g = { .type = MATCH_FULL }; |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3596 | struct ftrace_glob mod_g = { .type = MATCH_FULL }; |
| 3597 | struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL; |
| 3598 | int exclude_mod = 0; |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3599 | int found = 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3600 | int ret; |
Dan Carpenter | 198bd49 | 2017-07-12 10:35:57 +0300 | [diff] [blame] | 3601 | int clear_filter = 0; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3602 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3603 | if (func) { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3604 | func_g.type = filter_parse_regex(func, len, &func_g.search, |
| 3605 | &clear_filter); |
| 3606 | func_g.len = strlen(func_g.search); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3607 | } |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3608 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3609 | if (mod) { |
| 3610 | mod_g.type = filter_parse_regex(mod, strlen(mod), |
| 3611 | &mod_g.search, &exclude_mod); |
| 3612 | mod_g.len = strlen(mod_g.search); |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3613 | } |
| 3614 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3615 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3616 | |
| 3617 | if (unlikely(ftrace_disabled)) |
| 3618 | goto out_unlock; |
| 3619 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3620 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 3621 | |
| 3622 | if (rec->flags & FTRACE_FL_DISABLED) |
| 3623 | continue; |
| 3624 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3625 | if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3626 | ret = enter_record(hash, rec, clear_filter); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3627 | if (ret < 0) { |
| 3628 | found = ret; |
| 3629 | goto out_unlock; |
| 3630 | } |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3631 | found = 1; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3632 | } |
| 3633 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3634 | out_unlock: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3635 | mutex_unlock(&ftrace_lock); |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3636 | |
| 3637 | return found; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3638 | } |
| 3639 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3640 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3641 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3642 | { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3643 | return match_records(hash, buff, len, NULL); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3644 | } |
| 3645 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3646 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3647 | /* |
| 3648 | * We register the module command as a template to show others how |
| 3649 | * to register the a command as well. |
| 3650 | */ |
| 3651 | |
| 3652 | static int |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 3653 | ftrace_mod_callback(struct ftrace_hash *hash, |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3654 | char *func, char *cmd, char *module, int enable) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3655 | { |
Dmitry Safonov | 5e3949f | 2015-09-29 19:46:12 +0300 | [diff] [blame] | 3656 | int ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3657 | |
| 3658 | /* |
| 3659 | * cmd == 'mod' because we only registered this func |
| 3660 | * for the 'mod' ftrace_func_command. |
| 3661 | * But if you register one func with multiple commands, |
| 3662 | * you can tell which command was used by the cmd |
| 3663 | * parameter. |
| 3664 | */ |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3665 | ret = match_records(hash, func, strlen(func), module); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3666 | if (!ret) |
Dmitry Safonov | 5e3949f | 2015-09-29 19:46:12 +0300 | [diff] [blame] | 3667 | return -EINVAL; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3668 | if (ret < 0) |
| 3669 | return ret; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3670 | return 0; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 3674 | .name = "mod", |
| 3675 | .func = ftrace_mod_callback, |
| 3676 | }; |
| 3677 | |
| 3678 | static int __init ftrace_mod_cmd_init(void) |
| 3679 | { |
| 3680 | return register_ftrace_command(&ftrace_mod_cmd); |
| 3681 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 3682 | core_initcall(ftrace_mod_cmd_init); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3683 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 3684 | 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] | 3685 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3686 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3687 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3688 | struct hlist_head *hhd; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3689 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3690 | |
| 3691 | key = hash_long(ip, FTRACE_HASH_BITS); |
| 3692 | |
| 3693 | hhd = &ftrace_func_hash[key]; |
| 3694 | |
| 3695 | if (hlist_empty(hhd)) |
| 3696 | return; |
| 3697 | |
| 3698 | /* |
| 3699 | * Disable preemption for these calls to prevent a RCU grace |
| 3700 | * period. This syncs the hash iteration and freeing of items |
| 3701 | * on the hash. rcu_read_lock is too dangerous here. |
| 3702 | */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3703 | preempt_disable_notrace(); |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 3704 | hlist_for_each_entry_rcu_notrace(entry, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3705 | if (entry->ip == ip) |
| 3706 | entry->ops->func(ip, parent_ip, &entry->data); |
| 3707 | } |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3708 | preempt_enable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3709 | } |
| 3710 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3711 | static struct ftrace_ops trace_probe_ops __read_mostly = |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3712 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 3713 | .func = function_trace_probe_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3714 | .flags = FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3715 | INIT_OPS_HASH(trace_probe_ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3716 | }; |
| 3717 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3718 | static int ftrace_probe_registered; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3719 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3720 | static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3721 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3722 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3723 | int i; |
| 3724 | |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3725 | if (ftrace_probe_registered) { |
| 3726 | /* still need to update the function call sites */ |
| 3727 | if (ftrace_enabled) |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3728 | ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, |
| 3729 | old_hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3730 | return; |
Steven Rostedt (Red Hat) | 19dd603 | 2013-05-09 19:37:36 -0400 | [diff] [blame] | 3731 | } |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3732 | |
| 3733 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3734 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3735 | if (hhd->first) |
| 3736 | break; |
| 3737 | } |
| 3738 | /* Nothing registered? */ |
| 3739 | if (i == FTRACE_FUNC_HASHSIZE) |
| 3740 | return; |
| 3741 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3742 | ret = ftrace_startup(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3743 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3744 | ftrace_probe_registered = 1; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3745 | } |
| 3746 | |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3747 | static bool __disable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3748 | { |
| 3749 | int i; |
| 3750 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3751 | if (!ftrace_probe_registered) |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3752 | return false; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3753 | |
| 3754 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3755 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3756 | if (hhd->first) |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3757 | return false; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | /* no more funcs left */ |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 3761 | ftrace_shutdown(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3762 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3763 | ftrace_probe_registered = 0; |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3764 | return true; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3768 | static void ftrace_free_entry(struct ftrace_func_probe *entry) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3769 | { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3770 | if (entry->ops->free) |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3771 | entry->ops->free(entry->ops, entry->ip, &entry->data); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3772 | kfree(entry); |
| 3773 | } |
| 3774 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3775 | int |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3776 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3777 | void *data) |
| 3778 | { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3779 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3780 | struct ftrace_func_probe *entry; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3781 | struct ftrace_glob func_g; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3782 | 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] | 3783 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3784 | struct ftrace_hash *hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3785 | struct ftrace_page *pg; |
| 3786 | struct dyn_ftrace *rec; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3787 | int not; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3788 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3789 | int count = 0; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3790 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3791 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3792 | func_g.type = filter_parse_regex(glob, strlen(glob), |
| 3793 | &func_g.search, ¬); |
| 3794 | func_g.len = strlen(func_g.search); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3795 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3796 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3797 | if (WARN_ON(not)) |
| 3798 | return -EINVAL; |
| 3799 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3800 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3801 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3802 | old_hash_ops.filter_hash = old_hash; |
| 3803 | /* Probes only have filters */ |
| 3804 | old_hash_ops.notrace_hash = NULL; |
| 3805 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3806 | 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] | 3807 | if (!hash) { |
| 3808 | count = -ENOMEM; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3809 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | if (unlikely(ftrace_disabled)) { |
| 3813 | count = -ENODEV; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3814 | goto out; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3815 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3816 | |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3817 | mutex_lock(&ftrace_lock); |
| 3818 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3819 | do_for_each_ftrace_rec(pg, rec) { |
| 3820 | |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 3821 | if (rec->flags & FTRACE_FL_DISABLED) |
| 3822 | continue; |
| 3823 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3824 | if (!ftrace_match_record(rec, &func_g, NULL, 0)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3825 | continue; |
| 3826 | |
| 3827 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 3828 | if (!entry) { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3829 | /* If we did not process any, then return error */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3830 | if (!count) |
| 3831 | count = -ENOMEM; |
| 3832 | goto out_unlock; |
| 3833 | } |
| 3834 | |
| 3835 | count++; |
| 3836 | |
| 3837 | entry->data = data; |
| 3838 | |
| 3839 | /* |
| 3840 | * The caller might want to do something special |
| 3841 | * for each function we find. We call the callback |
| 3842 | * to give the caller an opportunity to do so. |
| 3843 | */ |
Steven Rostedt (Red Hat) | e67efb9 | 2013-03-12 15:07:59 -0400 | [diff] [blame] | 3844 | if (ops->init) { |
| 3845 | if (ops->init(ops, rec->ip, &entry->data) < 0) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3846 | /* caller does not like this func */ |
| 3847 | kfree(entry); |
| 3848 | continue; |
| 3849 | } |
| 3850 | } |
| 3851 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3852 | ret = enter_record(hash, rec, 0); |
| 3853 | if (ret < 0) { |
| 3854 | kfree(entry); |
| 3855 | count = ret; |
| 3856 | goto out_unlock; |
| 3857 | } |
| 3858 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3859 | entry->ops = ops; |
| 3860 | entry->ip = rec->ip; |
| 3861 | |
| 3862 | key = hash_long(entry->ip, FTRACE_HASH_BITS); |
| 3863 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); |
| 3864 | |
| 3865 | } while_for_each_ftrace_rec(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3866 | |
| 3867 | 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] | 3868 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 3869 | __enable_ftrace_function_probe(&old_hash_ops); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 3870 | |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3871 | if (!ret) |
| 3872 | free_ftrace_hash_rcu(old_hash); |
| 3873 | else |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3874 | count = ret; |
| 3875 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3876 | out_unlock: |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 3877 | mutex_unlock(&ftrace_lock); |
| 3878 | out: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3879 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3880 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3881 | |
| 3882 | return count; |
| 3883 | } |
| 3884 | |
| 3885 | enum { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3886 | PROBE_TEST_FUNC = 1, |
| 3887 | PROBE_TEST_DATA = 2 |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3888 | }; |
| 3889 | |
| 3890 | static void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3891 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3892 | void *data, int flags) |
| 3893 | { |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3894 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3895 | struct ftrace_func_entry *rec_entry; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3896 | struct ftrace_func_probe *entry; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3897 | struct ftrace_func_probe *p; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3898 | struct ftrace_glob func_g; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3899 | 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] | 3900 | struct ftrace_hash *old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3901 | struct list_head free_list; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3902 | struct ftrace_hash *hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3903 | struct hlist_node *tmp; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3904 | char str[KSYM_SYMBOL_LEN]; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3905 | int i, ret; |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3906 | bool disabled; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3907 | |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3908 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3909 | func_g.search = NULL; |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3910 | else if (glob) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3911 | int not; |
| 3912 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3913 | func_g.type = filter_parse_regex(glob, strlen(glob), |
| 3914 | &func_g.search, ¬); |
| 3915 | func_g.len = strlen(func_g.search); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3916 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3917 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3918 | if (WARN_ON(not)) |
| 3919 | return; |
| 3920 | } |
| 3921 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3922 | mutex_lock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3923 | |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3924 | old_hash_ops.filter_hash = old_hash; |
| 3925 | /* Probes only have filters */ |
| 3926 | old_hash_ops.notrace_hash = NULL; |
| 3927 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3928 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 3929 | if (!hash) |
| 3930 | /* Hmm, should report this somehow */ |
| 3931 | goto out_unlock; |
| 3932 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3933 | INIT_LIST_HEAD(&free_list); |
| 3934 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3935 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3936 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3937 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3938 | hlist_for_each_entry_safe(entry, tmp, hhd, node) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3939 | |
| 3940 | /* break up if statements for readability */ |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3941 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3942 | continue; |
| 3943 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3944 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3945 | continue; |
| 3946 | |
| 3947 | /* do this last, since it is the most expensive */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3948 | if (func_g.search) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3949 | kallsyms_lookup(entry->ip, NULL, NULL, |
| 3950 | NULL, str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3951 | if (!ftrace_match(str, &func_g)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3952 | continue; |
| 3953 | } |
| 3954 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3955 | rec_entry = ftrace_lookup_ip(hash, entry->ip); |
| 3956 | /* It is possible more than one entry had this ip */ |
| 3957 | if (rec_entry) |
| 3958 | free_hash_entry(hash, rec_entry); |
| 3959 | |
Steven Rostedt (Red Hat) | 740466b | 2013-03-13 11:15:19 -0400 | [diff] [blame] | 3960 | hlist_del_rcu(&entry->node); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3961 | list_add(&entry->free_list, &free_list); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3962 | } |
| 3963 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3964 | mutex_lock(&ftrace_lock); |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3965 | disabled = __disable_ftrace_function_probe(); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3966 | /* |
| 3967 | * Remove after the disable is called. Otherwise, if the last |
| 3968 | * probe is removed, a null hash means *all enabled*. |
| 3969 | */ |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3970 | ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); |
Steven Rostedt (VMware) | 666452f | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 3971 | |
| 3972 | /* still need to update the function call sites */ |
| 3973 | if (ftrace_enabled && !disabled) |
| 3974 | ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, |
| 3975 | &old_hash_ops); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3976 | synchronize_sched(); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 3977 | if (!ret) |
| 3978 | free_ftrace_hash_rcu(old_hash); |
| 3979 | |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 3980 | list_for_each_entry_safe(entry, p, &free_list, free_list) { |
| 3981 | list_del(&entry->free_list); |
| 3982 | ftrace_free_entry(entry); |
| 3983 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3984 | mutex_unlock(&ftrace_lock); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3985 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3986 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3987 | mutex_unlock(&trace_probe_ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 3988 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3992 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3993 | void *data) |
| 3994 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3995 | __unregister_ftrace_function_probe(glob, ops, data, |
| 3996 | PROBE_TEST_FUNC | PROBE_TEST_DATA); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4000 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4001 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4002 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4003 | } |
| 4004 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4005 | void unregister_ftrace_function_probe_all(char *glob) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4006 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4007 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4008 | } |
| 4009 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4010 | static LIST_HEAD(ftrace_commands); |
| 4011 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 4012 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4013 | /* |
| 4014 | * Currently we only register ftrace commands from __init, so mark this |
| 4015 | * __init too. |
| 4016 | */ |
| 4017 | __init int register_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4018 | { |
| 4019 | struct ftrace_func_command *p; |
| 4020 | int ret = 0; |
| 4021 | |
| 4022 | mutex_lock(&ftrace_cmd_mutex); |
| 4023 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4024 | if (strcmp(cmd->name, p->name) == 0) { |
| 4025 | ret = -EBUSY; |
| 4026 | goto out_unlock; |
| 4027 | } |
| 4028 | } |
| 4029 | list_add(&cmd->list, &ftrace_commands); |
| 4030 | out_unlock: |
| 4031 | mutex_unlock(&ftrace_cmd_mutex); |
| 4032 | |
| 4033 | return ret; |
| 4034 | } |
| 4035 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4036 | /* |
| 4037 | * Currently we only unregister ftrace commands from __init, so mark |
| 4038 | * this __init too. |
| 4039 | */ |
| 4040 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4041 | { |
| 4042 | struct ftrace_func_command *p, *n; |
| 4043 | int ret = -ENODEV; |
| 4044 | |
| 4045 | mutex_lock(&ftrace_cmd_mutex); |
| 4046 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 4047 | if (strcmp(cmd->name, p->name) == 0) { |
| 4048 | ret = 0; |
| 4049 | list_del_init(&p->list); |
| 4050 | goto out_unlock; |
| 4051 | } |
| 4052 | } |
| 4053 | out_unlock: |
| 4054 | mutex_unlock(&ftrace_cmd_mutex); |
| 4055 | |
| 4056 | return ret; |
| 4057 | } |
| 4058 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4059 | static int ftrace_process_regex(struct ftrace_hash *hash, |
| 4060 | char *buff, int len, int enable) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4061 | { |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4062 | char *func, *command, *next = buff; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 4063 | struct ftrace_func_command *p; |
GuoWen Li | 0aff1c0 | 2011-06-01 19:18:47 +0800 | [diff] [blame] | 4064 | int ret = -EINVAL; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4065 | |
| 4066 | func = strsep(&next, ":"); |
| 4067 | |
| 4068 | if (!next) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4069 | ret = ftrace_match_records(hash, func, len); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 4070 | if (!ret) |
| 4071 | ret = -EINVAL; |
| 4072 | if (ret < 0) |
| 4073 | return ret; |
| 4074 | return 0; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4075 | } |
| 4076 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4077 | /* command found */ |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4078 | |
| 4079 | command = strsep(&next, ":"); |
| 4080 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4081 | mutex_lock(&ftrace_cmd_mutex); |
| 4082 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4083 | if (strcmp(p->name, command) == 0) { |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 4084 | ret = p->func(hash, func, command, next, enable); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4085 | goto out_unlock; |
| 4086 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4087 | } |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4088 | out_unlock: |
| 4089 | mutex_unlock(&ftrace_cmd_mutex); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4090 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4091 | return ret; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4092 | } |
| 4093 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4094 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4095 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 4096 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4097 | { |
| 4098 | struct ftrace_iterator *iter; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4099 | struct trace_parser *parser; |
| 4100 | ssize_t ret, read; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4101 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4102 | if (!cnt) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4103 | return 0; |
| 4104 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4105 | if (file->f_mode & FMODE_READ) { |
| 4106 | struct seq_file *m = file->private_data; |
| 4107 | iter = m->private; |
| 4108 | } else |
| 4109 | iter = file->private_data; |
| 4110 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4111 | if (unlikely(ftrace_disabled)) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4112 | return -ENODEV; |
| 4113 | |
| 4114 | /* 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] | 4115 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4116 | parser = &iter->parser; |
| 4117 | read = trace_get_user(parser, ubuf, cnt, ppos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4118 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4119 | if (read >= 0 && trace_parser_loaded(parser) && |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4120 | !trace_parser_cont(parser)) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4121 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4122 | parser->idx, enable); |
Li Zefan | 313254a | 2009-12-08 11:15:30 +0800 | [diff] [blame] | 4123 | trace_parser_clear(parser); |
Steven Rostedt (Red Hat) | 7c088b5 | 2013-05-09 11:35:12 -0400 | [diff] [blame] | 4124 | if (ret < 0) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4125 | goto out; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4128 | ret = read; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4129 | out: |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4130 | return ret; |
| 4131 | } |
| 4132 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4133 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4134 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 4135 | size_t cnt, loff_t *ppos) |
| 4136 | { |
| 4137 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 4138 | } |
| 4139 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4140 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4141 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 4142 | size_t cnt, loff_t *ppos) |
| 4143 | { |
| 4144 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 4145 | } |
| 4146 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4147 | static int |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4148 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
| 4149 | { |
| 4150 | struct ftrace_func_entry *entry; |
| 4151 | |
| 4152 | if (!ftrace_location(ip)) |
| 4153 | return -EINVAL; |
| 4154 | |
| 4155 | if (remove) { |
| 4156 | entry = ftrace_lookup_ip(hash, ip); |
| 4157 | if (!entry) |
| 4158 | return -ENOENT; |
| 4159 | free_hash_entry(hash, entry); |
| 4160 | return 0; |
| 4161 | } |
| 4162 | |
| 4163 | return add_hash_entry(hash, ip); |
| 4164 | } |
| 4165 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4166 | static void ftrace_ops_update_code(struct ftrace_ops *ops, |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4167 | struct ftrace_ops_hash *old_hash) |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4168 | { |
Steven Rostedt (Red Hat) | 8f86f83 | 2015-01-13 11:20:43 -0500 | [diff] [blame] | 4169 | struct ftrace_ops *op; |
| 4170 | |
| 4171 | if (!ftrace_enabled) |
| 4172 | return; |
| 4173 | |
| 4174 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 4175 | ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); |
Steven Rostedt (Red Hat) | 8f86f83 | 2015-01-13 11:20:43 -0500 | [diff] [blame] | 4176 | return; |
| 4177 | } |
| 4178 | |
| 4179 | /* |
| 4180 | * If this is the shared global_ops filter, then we need to |
| 4181 | * check if there is another ops that shares it, is enabled. |
| 4182 | * If so, we still need to run the modify code. |
| 4183 | */ |
| 4184 | if (ops->func_hash != &global_ops.local_hash) |
| 4185 | return; |
| 4186 | |
| 4187 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 4188 | if (op->func_hash == &global_ops.local_hash && |
| 4189 | op->flags & FTRACE_OPS_FL_ENABLED) { |
| 4190 | ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash); |
| 4191 | /* Only need to do this once */ |
| 4192 | return; |
| 4193 | } |
| 4194 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 1c80c43 | 2013-07-25 20:22:00 -0400 | [diff] [blame] | 4195 | } |
| 4196 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4197 | static int |
| 4198 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4199 | unsigned long ip, int remove, int reset, int enable) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4200 | { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4201 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4202 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4203 | struct ftrace_hash *old_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4204 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4205 | int ret; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4206 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4207 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4208 | return -ENODEV; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4209 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4210 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4211 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4212 | if (enable) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4213 | orig_hash = &ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4214 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4215 | orig_hash = &ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4216 | |
Wang Nan | b972cc5 | 2014-07-15 08:40:20 +0800 | [diff] [blame] | 4217 | if (reset) |
| 4218 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4219 | else |
| 4220 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 4221 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4222 | if (!hash) { |
| 4223 | ret = -ENOMEM; |
| 4224 | goto out_regex_unlock; |
| 4225 | } |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4226 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4227 | if (buf && !ftrace_match_records(hash, buf, len)) { |
| 4228 | ret = -EINVAL; |
| 4229 | goto out_regex_unlock; |
| 4230 | } |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4231 | if (ip) { |
| 4232 | ret = ftrace_match_addr(hash, ip, remove); |
| 4233 | if (ret < 0) |
| 4234 | goto out_regex_unlock; |
| 4235 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4236 | |
| 4237 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4238 | old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4239 | old_hash_ops.filter_hash = ops->func_hash->filter_hash; |
| 4240 | old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4241 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4242 | if (!ret) { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4243 | ftrace_ops_update_code(ops, &old_hash_ops); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4244 | free_ftrace_hash_rcu(old_hash); |
| 4245 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4246 | mutex_unlock(&ftrace_lock); |
| 4247 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4248 | out_regex_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4249 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4250 | |
| 4251 | free_ftrace_hash(hash); |
| 4252 | return ret; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4253 | } |
| 4254 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4255 | static int |
| 4256 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, |
| 4257 | int reset, int enable) |
| 4258 | { |
| 4259 | return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); |
| 4260 | } |
| 4261 | |
| 4262 | /** |
| 4263 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address |
| 4264 | * @ops - the ops to set the filter with |
| 4265 | * @ip - the address to add to or remove from the filter. |
| 4266 | * @remove - non zero to remove the ip from the filter |
| 4267 | * @reset - non zero to reset all filters before applying this filter. |
| 4268 | * |
| 4269 | * Filters denote which functions should be enabled when tracing is enabled |
| 4270 | * If @ip is NULL, it failes to update filter. |
| 4271 | */ |
| 4272 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, |
| 4273 | int remove, int reset) |
| 4274 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4275 | ftrace_ops_init(ops); |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4276 | return ftrace_set_addr(ops, ip, remove, reset, 1); |
| 4277 | } |
| 4278 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); |
| 4279 | |
| 4280 | static int |
| 4281 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4282 | int reset, int enable) |
| 4283 | { |
| 4284 | return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); |
| 4285 | } |
| 4286 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4287 | /** |
| 4288 | * ftrace_set_filter - set a function to filter on in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4289 | * @ops - the ops to set the filter with |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4290 | * @buf - the string that holds the function filter text. |
| 4291 | * @len - the length of the string. |
| 4292 | * @reset - non zero to reset all filters before applying this filter. |
| 4293 | * |
| 4294 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4295 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4296 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4297 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4298 | int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4299 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4300 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4301 | return ftrace_set_regex(ops, buf, len, reset, 1); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4302 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4303 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4304 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4305 | /** |
| 4306 | * ftrace_set_notrace - set a function to not trace in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4307 | * @ops - the ops to set the notrace filter with |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4308 | * @buf - the string that holds the function notrace text. |
| 4309 | * @len - the length of the string. |
| 4310 | * @reset - non zero to reset all filters before applying this filter. |
| 4311 | * |
| 4312 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4313 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4314 | * for tracing. |
| 4315 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4316 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4317 | int len, int reset) |
| 4318 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4319 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4320 | return ftrace_set_regex(ops, buf, len, reset, 0); |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4321 | } |
| 4322 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 4323 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4324 | * 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] | 4325 | * @buf - the string that holds the function filter text. |
| 4326 | * @len - the length of the string. |
| 4327 | * @reset - non zero to reset all filters before applying this filter. |
| 4328 | * |
| 4329 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4330 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4331 | */ |
| 4332 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 4333 | { |
| 4334 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
| 4335 | } |
| 4336 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 4337 | |
| 4338 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4339 | * 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] | 4340 | * @buf - the string that holds the function notrace text. |
| 4341 | * @len - the length of the string. |
| 4342 | * @reset - non zero to reset all filters before applying this filter. |
| 4343 | * |
| 4344 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4345 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4346 | * for tracing. |
| 4347 | */ |
| 4348 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4349 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4350 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4351 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4352 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4353 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4354 | /* |
| 4355 | * command line interface to allow users to set filters on boot up. |
| 4356 | */ |
| 4357 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 4358 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4359 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4360 | |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4361 | /* Used by function selftest to not test if filter is set */ |
| 4362 | bool ftrace_filter_param __initdata; |
| 4363 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4364 | static int __init set_ftrace_notrace(char *str) |
| 4365 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4366 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4367 | strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4368 | return 1; |
| 4369 | } |
| 4370 | __setup("ftrace_notrace=", set_ftrace_notrace); |
| 4371 | |
| 4372 | static int __init set_ftrace_filter(char *str) |
| 4373 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4374 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4375 | strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4376 | return 1; |
| 4377 | } |
| 4378 | __setup("ftrace_filter=", set_ftrace_filter); |
| 4379 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4380 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Lai Jiangshan | f6060f4 | 2009-11-05 11:16:17 +0800 | [diff] [blame] | 4381 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4382 | static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4383 | static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer); |
Steven Rostedt | 801c29f | 2010-03-05 20:02:19 -0500 | [diff] [blame] | 4384 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4385 | static int __init set_graph_function(char *str) |
| 4386 | { |
Frederic Weisbecker | 06f43d6 | 2009-10-14 20:43:39 +0200 | [diff] [blame] | 4387 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4388 | return 1; |
| 4389 | } |
| 4390 | __setup("ftrace_graph_filter=", set_graph_function); |
| 4391 | |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4392 | static int __init set_graph_notrace_function(char *str) |
| 4393 | { |
| 4394 | strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 4395 | return 1; |
| 4396 | } |
| 4397 | __setup("ftrace_graph_notrace=", set_graph_notrace_function); |
| 4398 | |
| 4399 | static void __init set_ftrace_early_graph(char *buf, int enable) |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4400 | { |
| 4401 | int ret; |
| 4402 | char *func; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4403 | unsigned long *table = ftrace_graph_funcs; |
| 4404 | int *count = &ftrace_graph_count; |
| 4405 | |
| 4406 | if (!enable) { |
| 4407 | table = ftrace_graph_notrace_funcs; |
| 4408 | count = &ftrace_graph_notrace_count; |
| 4409 | } |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4410 | |
| 4411 | while (buf) { |
| 4412 | func = strsep(&buf, ","); |
| 4413 | /* we allow only one expression at a time */ |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4414 | ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4415 | if (ret) |
| 4416 | printk(KERN_DEBUG "ftrace: function %s not " |
| 4417 | "traceable\n", func); |
| 4418 | } |
| 4419 | } |
| 4420 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4421 | |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4422 | void __init |
| 4423 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4424 | { |
| 4425 | char *func; |
| 4426 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4427 | ftrace_ops_init(ops); |
| 4428 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4429 | while (buf) { |
| 4430 | func = strsep(&buf, ","); |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4431 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | static void __init set_ftrace_early_filters(void) |
| 4436 | { |
| 4437 | if (ftrace_filter_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4438 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4439 | if (ftrace_notrace_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4440 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4441 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4442 | if (ftrace_graph_buf[0]) |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4443 | set_ftrace_early_graph(ftrace_graph_buf, 1); |
| 4444 | if (ftrace_graph_notrace_buf[0]) |
| 4445 | set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4446 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4447 | } |
| 4448 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4449 | int ftrace_regex_release(struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4450 | { |
| 4451 | struct seq_file *m = (struct seq_file *)file->private_data; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4452 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4453 | struct ftrace_iterator *iter; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4454 | struct ftrace_hash **orig_hash; |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4455 | struct ftrace_hash *old_hash; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4456 | struct trace_parser *parser; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4457 | int filter_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4458 | int ret; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4459 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4460 | if (file->f_mode & FMODE_READ) { |
| 4461 | iter = m->private; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4462 | seq_release(inode, file); |
| 4463 | } else |
| 4464 | iter = file->private_data; |
| 4465 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4466 | parser = &iter->parser; |
| 4467 | if (trace_parser_loaded(parser)) { |
| 4468 | parser->buffer[parser->idx] = 0; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4469 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4470 | } |
| 4471 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4472 | trace_parser_put(parser); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4473 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4474 | mutex_lock(&iter->ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4475 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4476 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4477 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 4478 | |
| 4479 | if (filter_hash) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4480 | orig_hash = &iter->ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4481 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4482 | orig_hash = &iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4483 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4484 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4485 | old_hash = *orig_hash; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4486 | old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash; |
| 4487 | old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 4488 | ret = ftrace_hash_move(iter->ops, filter_hash, |
| 4489 | orig_hash, iter->hash); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4490 | if (!ret) { |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4491 | ftrace_ops_update_code(iter->ops, &old_hash_ops); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4492 | free_ftrace_hash_rcu(old_hash); |
| 4493 | } |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4494 | mutex_unlock(&ftrace_lock); |
| 4495 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4496 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4497 | mutex_unlock(&iter->ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4498 | free_ftrace_hash(iter->hash); |
| 4499 | kfree(iter); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4500 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4504 | static const struct file_operations ftrace_avail_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4505 | .open = ftrace_avail_open, |
| 4506 | .read = seq_read, |
| 4507 | .llseek = seq_lseek, |
Li Zefan | 3be04b4 | 2009-08-17 16:54:03 +0800 | [diff] [blame] | 4508 | .release = seq_release_private, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4509 | }; |
| 4510 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4511 | static const struct file_operations ftrace_enabled_fops = { |
| 4512 | .open = ftrace_enabled_open, |
| 4513 | .read = seq_read, |
| 4514 | .llseek = seq_lseek, |
| 4515 | .release = seq_release_private, |
| 4516 | }; |
| 4517 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4518 | static const struct file_operations ftrace_filter_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4519 | .open = ftrace_filter_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4520 | .read = seq_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4521 | .write = ftrace_filter_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4522 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4523 | .release = ftrace_regex_release, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4524 | }; |
| 4525 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4526 | static const struct file_operations ftrace_notrace_fops = { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4527 | .open = ftrace_notrace_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 4528 | .read = seq_read, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4529 | .write = ftrace_notrace_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4530 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4531 | .release = ftrace_regex_release, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4532 | }; |
| 4533 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4534 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4535 | |
| 4536 | static DEFINE_MUTEX(graph_lock); |
| 4537 | |
| 4538 | int ftrace_graph_count; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4539 | int ftrace_graph_notrace_count; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4540 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4541 | unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4542 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4543 | struct ftrace_graph_data { |
| 4544 | unsigned long *table; |
| 4545 | size_t size; |
| 4546 | int *count; |
| 4547 | const struct seq_operations *seq_ops; |
| 4548 | }; |
| 4549 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4550 | static void * |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4551 | __g_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4552 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4553 | struct ftrace_graph_data *fgd = m->private; |
| 4554 | |
| 4555 | if (*pos >= *fgd->count) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4556 | return NULL; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4557 | return &fgd->table[*pos]; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4558 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4559 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4560 | static void * |
| 4561 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 4562 | { |
| 4563 | (*pos)++; |
| 4564 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 4568 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4569 | struct ftrace_graph_data *fgd = m->private; |
| 4570 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4571 | mutex_lock(&graph_lock); |
| 4572 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4573 | /* Nothing, tell g_show to print all functions are enabled */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4574 | if (!*fgd->count && !*pos) |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4575 | return (void *)1; |
| 4576 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 4577 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | static void g_stop(struct seq_file *m, void *p) |
| 4581 | { |
| 4582 | mutex_unlock(&graph_lock); |
| 4583 | } |
| 4584 | |
| 4585 | static int g_show(struct seq_file *m, void *v) |
| 4586 | { |
| 4587 | unsigned long *ptr = v; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4588 | |
| 4589 | if (!ptr) |
| 4590 | return 0; |
| 4591 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4592 | if (ptr == (unsigned long *)1) { |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4593 | struct ftrace_graph_data *fgd = m->private; |
| 4594 | |
| 4595 | if (fgd->table == ftrace_graph_funcs) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4596 | seq_puts(m, "#### all functions enabled ####\n"); |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 4597 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4598 | seq_puts(m, "#### no functions disabled ####\n"); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4599 | return 0; |
| 4600 | } |
| 4601 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 4602 | seq_printf(m, "%ps\n", (void *)*ptr); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4603 | |
| 4604 | return 0; |
| 4605 | } |
| 4606 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 4607 | static const struct seq_operations ftrace_graph_seq_ops = { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4608 | .start = g_start, |
| 4609 | .next = g_next, |
| 4610 | .stop = g_stop, |
| 4611 | .show = g_show, |
| 4612 | }; |
| 4613 | |
| 4614 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4615 | __ftrace_graph_open(struct inode *inode, struct file *file, |
| 4616 | struct ftrace_graph_data *fgd) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4617 | { |
| 4618 | int ret = 0; |
| 4619 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4620 | mutex_lock(&graph_lock); |
| 4621 | if ((file->f_mode & FMODE_WRITE) && |
Steven Rostedt | 8650ae3 | 2009-07-22 23:29:30 -0400 | [diff] [blame] | 4622 | (file->f_flags & O_TRUNC)) { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4623 | *fgd->count = 0; |
| 4624 | memset(fgd->table, 0, fgd->size * sizeof(*fgd->table)); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4625 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4626 | mutex_unlock(&graph_lock); |
| 4627 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4628 | if (file->f_mode & FMODE_READ) { |
| 4629 | ret = seq_open(file, fgd->seq_ops); |
| 4630 | if (!ret) { |
| 4631 | struct seq_file *m = file->private_data; |
| 4632 | m->private = fgd; |
| 4633 | } |
| 4634 | } else |
| 4635 | file->private_data = fgd; |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 4636 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4637 | return ret; |
| 4638 | } |
| 4639 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4640 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4641 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 4642 | { |
| 4643 | struct ftrace_graph_data *fgd; |
| 4644 | |
| 4645 | if (unlikely(ftrace_disabled)) |
| 4646 | return -ENODEV; |
| 4647 | |
| 4648 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4649 | if (fgd == NULL) |
| 4650 | return -ENOMEM; |
| 4651 | |
| 4652 | fgd->table = ftrace_graph_funcs; |
| 4653 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 4654 | fgd->count = &ftrace_graph_count; |
| 4655 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4656 | |
| 4657 | return __ftrace_graph_open(inode, file, fgd); |
| 4658 | } |
| 4659 | |
| 4660 | static int |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4661 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) |
| 4662 | { |
| 4663 | struct ftrace_graph_data *fgd; |
| 4664 | |
| 4665 | if (unlikely(ftrace_disabled)) |
| 4666 | return -ENODEV; |
| 4667 | |
| 4668 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 4669 | if (fgd == NULL) |
| 4670 | return -ENOMEM; |
| 4671 | |
| 4672 | fgd->table = ftrace_graph_notrace_funcs; |
| 4673 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; |
| 4674 | fgd->count = &ftrace_graph_notrace_count; |
| 4675 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 4676 | |
| 4677 | return __ftrace_graph_open(inode, file, fgd); |
| 4678 | } |
| 4679 | |
| 4680 | static int |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4681 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 4682 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4683 | if (file->f_mode & FMODE_READ) { |
| 4684 | struct seq_file *m = file->private_data; |
| 4685 | |
| 4686 | kfree(m->private); |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4687 | seq_release(inode, file); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4688 | } else { |
| 4689 | kfree(file->private_data); |
| 4690 | } |
| 4691 | |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4692 | return 0; |
| 4693 | } |
| 4694 | |
| 4695 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4696 | ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4697 | { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4698 | struct ftrace_glob func_g; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4699 | struct dyn_ftrace *rec; |
| 4700 | struct ftrace_page *pg; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4701 | int fail = 1; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4702 | int not; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4703 | bool exists; |
| 4704 | int i; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4705 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4706 | /* decode regex */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4707 | func_g.type = filter_parse_regex(buffer, strlen(buffer), |
| 4708 | &func_g.search, ¬); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4709 | if (!not && *idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4710 | return -EBUSY; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4711 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4712 | func_g.len = strlen(func_g.search); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4713 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4714 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4715 | |
| 4716 | if (unlikely(ftrace_disabled)) { |
| 4717 | mutex_unlock(&ftrace_lock); |
| 4718 | return -ENODEV; |
| 4719 | } |
| 4720 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4721 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4722 | |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 4723 | if (rec->flags & FTRACE_FL_DISABLED) |
| 4724 | continue; |
| 4725 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 4726 | if (ftrace_match_record(rec, &func_g, NULL, 0)) { |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4727 | /* if it is in the array */ |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4728 | exists = false; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4729 | for (i = 0; i < *idx; i++) { |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 4730 | if (array[i] == rec->ip) { |
| 4731 | exists = true; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4732 | break; |
| 4733 | } |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | if (!not) { |
| 4737 | fail = 0; |
| 4738 | if (!exists) { |
| 4739 | array[(*idx)++] = rec->ip; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4740 | if (*idx >= size) |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4741 | goto out; |
| 4742 | } |
| 4743 | } else { |
| 4744 | if (exists) { |
| 4745 | array[i] = array[--(*idx)]; |
| 4746 | array[*idx] = 0; |
| 4747 | fail = 0; |
| 4748 | } |
| 4749 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4750 | } |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 4751 | } while_for_each_ftrace_rec(); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4752 | out: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 4753 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4754 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4755 | if (fail) |
| 4756 | return -EINVAL; |
| 4757 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4758 | return 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | static ssize_t |
| 4762 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 4763 | size_t cnt, loff_t *ppos) |
| 4764 | { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4765 | struct trace_parser parser; |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4766 | ssize_t read, ret = 0; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4767 | struct ftrace_graph_data *fgd = file->private_data; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4768 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 4769 | if (!cnt) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4770 | return 0; |
| 4771 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4772 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) |
| 4773 | return -ENOMEM; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4774 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4775 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
| 4776 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4777 | if (read >= 0 && trace_parser_loaded((&parser))) { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4778 | parser.buffer[parser.idx] = 0; |
| 4779 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4780 | mutex_lock(&graph_lock); |
| 4781 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4782 | /* we allow only one expression at a time */ |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 4783 | ret = ftrace_set_func(fgd->table, fgd->count, fgd->size, |
| 4784 | parser.buffer); |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4785 | |
| 4786 | mutex_unlock(&graph_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4787 | } |
| 4788 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 4789 | if (!ret) |
| 4790 | ret = read; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 4791 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4792 | trace_parser_put(&parser); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4793 | |
| 4794 | return ret; |
| 4795 | } |
| 4796 | |
| 4797 | static const struct file_operations ftrace_graph_fops = { |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4798 | .open = ftrace_graph_open, |
| 4799 | .read = seq_read, |
| 4800 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4801 | .llseek = tracing_lseek, |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 4802 | .release = ftrace_graph_release, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4803 | }; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4804 | |
| 4805 | static const struct file_operations ftrace_graph_notrace_fops = { |
| 4806 | .open = ftrace_graph_notrace_open, |
| 4807 | .read = seq_read, |
| 4808 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4809 | .llseek = tracing_lseek, |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4810 | .release = ftrace_graph_release, |
| 4811 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4812 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4813 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4814 | void ftrace_create_filter_files(struct ftrace_ops *ops, |
| 4815 | struct dentry *parent) |
| 4816 | { |
| 4817 | |
| 4818 | trace_create_file("set_ftrace_filter", 0644, parent, |
| 4819 | ops, &ftrace_filter_fops); |
| 4820 | |
| 4821 | trace_create_file("set_ftrace_notrace", 0644, parent, |
| 4822 | ops, &ftrace_notrace_fops); |
| 4823 | } |
| 4824 | |
| 4825 | /* |
| 4826 | * The name "destroy_filter_files" is really a misnomer. Although |
| 4827 | * in the future, it may actualy delete the files, but this is |
| 4828 | * really intended to make sure the ops passed in are disabled |
| 4829 | * and that when this function returns, the caller is free to |
| 4830 | * free the ops. |
| 4831 | * |
| 4832 | * The "destroy" name is only to match the "create" name that this |
| 4833 | * should be paired with. |
| 4834 | */ |
| 4835 | void ftrace_destroy_filter_files(struct ftrace_ops *ops) |
| 4836 | { |
| 4837 | mutex_lock(&ftrace_lock); |
| 4838 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 4839 | ftrace_shutdown(ops, 0); |
| 4840 | ops->flags |= FTRACE_OPS_FL_DELETED; |
Steven Rostedt (VMware) | 326c9e1 | 2018-12-10 23:58:01 -0500 | [diff] [blame] | 4841 | ftrace_free_filter(ops); |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4842 | mutex_unlock(&ftrace_lock); |
| 4843 | } |
| 4844 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 4845 | static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4846 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4847 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4848 | trace_create_file("available_filter_functions", 0444, |
| 4849 | d_tracer, NULL, &ftrace_avail_fops); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4850 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 4851 | trace_create_file("enabled_functions", 0444, |
| 4852 | d_tracer, NULL, &ftrace_enabled_fops); |
| 4853 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 4854 | ftrace_create_filter_files(&global_ops, d_tracer); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 4855 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4856 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4857 | trace_create_file("set_graph_function", 0444, d_tracer, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4858 | NULL, |
| 4859 | &ftrace_graph_fops); |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 4860 | trace_create_file("set_graph_notrace", 0444, d_tracer, |
| 4861 | NULL, |
| 4862 | &ftrace_graph_notrace_fops); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 4863 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4864 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4865 | return 0; |
| 4866 | } |
| 4867 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4868 | static int ftrace_cmp_ips(const void *a, const void *b) |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4869 | { |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4870 | const unsigned long *ipa = a; |
| 4871 | const unsigned long *ipb = b; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 4872 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4873 | if (*ipa > *ipb) |
| 4874 | return 1; |
| 4875 | if (*ipa < *ipb) |
| 4876 | return -1; |
| 4877 | return 0; |
| 4878 | } |
| 4879 | |
Sami Tolvanen | 7bd125e | 2017-06-16 12:52:57 -0700 | [diff] [blame] | 4880 | static int __norecordmcount ftrace_process_locs(struct module *mod, |
| 4881 | unsigned long *start, |
| 4882 | unsigned long *end) |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4883 | { |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4884 | struct ftrace_page *start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4885 | struct ftrace_page *pg; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4886 | struct dyn_ftrace *rec; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4887 | unsigned long count; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4888 | unsigned long *p; |
| 4889 | unsigned long addr; |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4890 | unsigned long flags = 0; /* Shut up gcc */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4891 | int ret = -ENOMEM; |
| 4892 | |
| 4893 | count = end - start; |
| 4894 | |
| 4895 | if (!count) |
| 4896 | return 0; |
| 4897 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4898 | sort(start, count, sizeof(*start), |
Rasmus Villemoes | 6db0290 | 2015-09-09 23:27:02 +0200 | [diff] [blame] | 4899 | ftrace_cmp_ips, NULL); |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 4900 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4901 | start_pg = ftrace_allocate_pages(count); |
| 4902 | if (!start_pg) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4903 | return -ENOMEM; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4904 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4905 | mutex_lock(&ftrace_lock); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4906 | |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4907 | /* |
| 4908 | * Core and each module needs their own pages, as |
| 4909 | * modules will free them when they are removed. |
| 4910 | * Force a new page to be allocated for modules. |
| 4911 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4912 | if (!mod) { |
| 4913 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 4914 | /* First initialization */ |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4915 | ftrace_pages = ftrace_pages_start = start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4916 | } else { |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4917 | if (!ftrace_pages) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4918 | goto out; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4919 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4920 | if (WARN_ON(ftrace_pages->next)) { |
| 4921 | /* Hmm, we have free pages? */ |
| 4922 | while (ftrace_pages->next) |
| 4923 | ftrace_pages = ftrace_pages->next; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4924 | } |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4925 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4926 | ftrace_pages->next = start_pg; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4927 | } |
| 4928 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4929 | p = start; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4930 | pg = start_pg; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4931 | while (p < end) { |
| 4932 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 4933 | /* |
| 4934 | * Some architecture linkers will pad between |
| 4935 | * the different mcount_loc sections of different |
| 4936 | * object files to satisfy alignments. |
| 4937 | * Skip any NULL pointers. |
| 4938 | */ |
| 4939 | if (!addr) |
| 4940 | continue; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4941 | |
| 4942 | if (pg->index == pg->size) { |
| 4943 | /* We should have allocated enough */ |
| 4944 | if (WARN_ON(!pg->next)) |
| 4945 | break; |
| 4946 | pg = pg->next; |
| 4947 | } |
| 4948 | |
| 4949 | rec = &pg->records[pg->index++]; |
| 4950 | rec->ip = addr; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4951 | } |
| 4952 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 4953 | /* We should have used all pages */ |
| 4954 | WARN_ON(pg->next); |
| 4955 | |
| 4956 | /* Assign the last page to ftrace_pages */ |
| 4957 | ftrace_pages = pg; |
| 4958 | |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4959 | /* |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4960 | * We only need to disable interrupts on start up |
| 4961 | * because we are modifying code that an interrupt |
| 4962 | * may execute, and the modification is not atomic. |
| 4963 | * But for modules, nothing runs the code we modify |
| 4964 | * until we are finished with it, and there's no |
| 4965 | * reason to cause large interrupt latencies while we do it. |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 4966 | */ |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4967 | if (!mod) |
| 4968 | local_irq_save(flags); |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 4969 | ftrace_update_code(mod, start_pg); |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 4970 | if (!mod) |
| 4971 | local_irq_restore(flags); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4972 | ret = 0; |
| 4973 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4974 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4975 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 4976 | return ret; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 4977 | } |
| 4978 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4979 | #ifdef CONFIG_MODULES |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4980 | |
| 4981 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 4982 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 4983 | static int referenced_filters(struct dyn_ftrace *rec) |
| 4984 | { |
| 4985 | struct ftrace_ops *ops; |
| 4986 | int cnt = 0; |
| 4987 | |
| 4988 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { |
| 4989 | if (ops_references_rec(ops, rec)) |
| 4990 | cnt++; |
| 4991 | } |
| 4992 | |
| 4993 | return cnt; |
| 4994 | } |
| 4995 | |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 4996 | void ftrace_release_mod(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 4997 | { |
| 4998 | struct dyn_ftrace *rec; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 4999 | struct ftrace_page **last_pg; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5000 | struct ftrace_page *pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5001 | int order; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5002 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5003 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5004 | |
| 5005 | if (ftrace_disabled) |
| 5006 | goto out_unlock; |
| 5007 | |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5008 | /* |
| 5009 | * Each module has its own ftrace_pages, remove |
| 5010 | * them from the list. |
| 5011 | */ |
| 5012 | last_pg = &ftrace_pages_start; |
| 5013 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 5014 | rec = &pg->records[0]; |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 5015 | if (within_module_core(rec->ip, mod)) { |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5016 | /* |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5017 | * As core pages are first, the first |
| 5018 | * page should never be a module page. |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5019 | */ |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5020 | if (WARN_ON(pg == ftrace_pages_start)) |
| 5021 | goto out_unlock; |
| 5022 | |
| 5023 | /* Check if we are deleting the last page */ |
| 5024 | if (pg == ftrace_pages) |
| 5025 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 5026 | |
| 5027 | *last_pg = pg->next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5028 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 5029 | free_pages((unsigned long)pg->records, order); |
| 5030 | kfree(pg); |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5031 | } else |
| 5032 | last_pg = &pg->next; |
| 5033 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5034 | out_unlock: |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5035 | mutex_unlock(&ftrace_lock); |
| 5036 | } |
| 5037 | |
Jessica Yu | 7dcd182 | 2016-02-16 17:32:33 -0500 | [diff] [blame] | 5038 | void ftrace_module_enable(struct module *mod) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5039 | { |
| 5040 | struct dyn_ftrace *rec; |
| 5041 | struct ftrace_page *pg; |
| 5042 | |
| 5043 | mutex_lock(&ftrace_lock); |
| 5044 | |
| 5045 | if (ftrace_disabled) |
| 5046 | goto out_unlock; |
| 5047 | |
| 5048 | /* |
| 5049 | * If the tracing is enabled, go ahead and enable the record. |
| 5050 | * |
| 5051 | * The reason not to enable the record immediatelly is the |
| 5052 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 5053 | * correct previous instructions. Making first the NOP |
| 5054 | * conversion puts the module to the correct state, thus |
| 5055 | * passing the ftrace_make_call check. |
| 5056 | * |
| 5057 | * We also delay this to after the module code already set the |
| 5058 | * text to read-only, as we now need to set it back to read-write |
| 5059 | * so that we can modify the text. |
| 5060 | */ |
| 5061 | if (ftrace_start_up) |
| 5062 | ftrace_arch_code_modify_prepare(); |
| 5063 | |
| 5064 | do_for_each_ftrace_rec(pg, rec) { |
| 5065 | int cnt; |
| 5066 | /* |
| 5067 | * do_for_each_ftrace_rec() is a double loop. |
| 5068 | * module text shares the pg. If a record is |
| 5069 | * not part of this module, then skip this pg, |
| 5070 | * which the "break" will do. |
| 5071 | */ |
| 5072 | if (!within_module_core(rec->ip, mod)) |
| 5073 | break; |
| 5074 | |
| 5075 | cnt = 0; |
| 5076 | |
| 5077 | /* |
| 5078 | * When adding a module, we need to check if tracers are |
| 5079 | * currently enabled and if they are, and can trace this record, |
| 5080 | * we need to enable the module functions as well as update the |
| 5081 | * reference counts for those function records. |
| 5082 | */ |
| 5083 | if (ftrace_start_up) |
| 5084 | cnt += referenced_filters(rec); |
| 5085 | |
| 5086 | /* This clears FTRACE_FL_DISABLED */ |
| 5087 | rec->flags = cnt; |
| 5088 | |
| 5089 | if (ftrace_start_up && cnt) { |
| 5090 | int failed = __ftrace_replace_code(rec, 1); |
| 5091 | if (failed) { |
| 5092 | ftrace_bug(failed, rec); |
| 5093 | goto out_loop; |
| 5094 | } |
| 5095 | } |
| 5096 | |
| 5097 | } while_for_each_ftrace_rec(); |
| 5098 | |
| 5099 | out_loop: |
| 5100 | if (ftrace_start_up) |
| 5101 | ftrace_arch_code_modify_post_process(); |
| 5102 | |
| 5103 | out_unlock: |
| 5104 | mutex_unlock(&ftrace_lock); |
| 5105 | } |
| 5106 | |
Steven Rostedt (Red Hat) | a949ae5 | 2014-04-24 10:40:12 -0400 | [diff] [blame] | 5107 | void ftrace_module_init(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5108 | { |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5109 | if (ftrace_disabled || !mod->num_ftrace_callsites) |
Abel Vesa | b6b71f6 | 2015-12-02 15:39:57 +0100 | [diff] [blame] | 5110 | return; |
| 5111 | |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5112 | ftrace_process_locs(mod, mod->ftrace_callsites, |
| 5113 | mod->ftrace_callsites + mod->num_ftrace_callsites); |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 5114 | } |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5115 | #endif /* CONFIG_MODULES */ |
| 5116 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5117 | void __init ftrace_init(void) |
| 5118 | { |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 5119 | extern unsigned long __start_mcount_loc[]; |
| 5120 | extern unsigned long __stop_mcount_loc[]; |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 5121 | unsigned long count, flags; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5122 | int ret; |
| 5123 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5124 | local_irq_save(flags); |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 5125 | ret = ftrace_dyn_arch_init(); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5126 | local_irq_restore(flags); |
Jiri Slaby | af64a7c | 2014-02-24 19:59:58 +0100 | [diff] [blame] | 5127 | if (ret) |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5128 | goto failed; |
| 5129 | |
| 5130 | count = __stop_mcount_loc - __start_mcount_loc; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 5131 | if (!count) { |
| 5132 | pr_info("ftrace: No functions to be traced?\n"); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5133 | goto failed; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | pr_info("ftrace: allocating %ld entries in %ld pages\n", |
| 5137 | count, count / ENTRIES_PER_PAGE + 1); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5138 | |
| 5139 | last_ftrace_enabled = ftrace_enabled = 1; |
| 5140 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 5141 | ret = ftrace_process_locs(NULL, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 5142 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5143 | __stop_mcount_loc); |
| 5144 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 5145 | set_ftrace_early_filters(); |
| 5146 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5147 | return; |
| 5148 | failed: |
| 5149 | ftrace_disabled = 1; |
| 5150 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5151 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5152 | /* Do nothing if arch does not support this */ |
| 5153 | void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5154 | { |
| 5155 | } |
| 5156 | |
| 5157 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5158 | { |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 5159 | |
| 5160 | /* |
| 5161 | * Currently there's no safe way to free a trampoline when the kernel |
| 5162 | * is configured with PREEMPT. That is because a task could be preempted |
| 5163 | * when it jumped to the trampoline, it may be preempted for a long time |
| 5164 | * depending on the system load, and currently there's no way to know |
| 5165 | * when it will be off the trampoline. If the trampoline is freed |
| 5166 | * too early, when the task runs again, it will be executing on freed |
| 5167 | * memory and crash. |
| 5168 | */ |
| 5169 | #ifdef CONFIG_PREEMPT |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5170 | /* Currently, only non dynamic ops can have a trampoline */ |
| 5171 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) |
| 5172 | return; |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 5173 | #endif |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5174 | |
| 5175 | arch_ftrace_update_trampoline(ops); |
| 5176 | } |
| 5177 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5178 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5179 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 5180 | static struct ftrace_ops global_ops = { |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 5181 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 5182 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 5183 | FTRACE_OPS_FL_INITIALIZED | |
| 5184 | FTRACE_OPS_FL_PID, |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 5185 | }; |
| 5186 | |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5187 | static int __init ftrace_nodyn_init(void) |
| 5188 | { |
| 5189 | ftrace_enabled = 1; |
| 5190 | return 0; |
| 5191 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 5192 | core_initcall(ftrace_nodyn_init); |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 5193 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 5194 | 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] | 5195 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5196 | static inline void ftrace_startup_all(int command) { } |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 5197 | /* 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] | 5198 | # define ftrace_startup(ops, command) \ |
| 5199 | ({ \ |
| 5200 | int ___ret = __register_ftrace_function(ops); \ |
| 5201 | if (!___ret) \ |
| 5202 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ |
| 5203 | ___ret; \ |
Steven Rostedt | 3b6cfdb | 2011-05-23 15:33:49 -0400 | [diff] [blame] | 5204 | }) |
Steven Rostedt (Red Hat) | 1fcc155 | 2014-02-19 15:12:18 -0500 | [diff] [blame] | 5205 | # define ftrace_shutdown(ops, command) \ |
| 5206 | ({ \ |
| 5207 | int ___ret = __unregister_ftrace_function(ops); \ |
| 5208 | if (!___ret) \ |
| 5209 | (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \ |
| 5210 | ___ret; \ |
| 5211 | }) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5212 | |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 5213 | # define ftrace_startup_sysctl() do { } while (0) |
| 5214 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5215 | |
| 5216 | static inline int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 5217 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5218 | { |
| 5219 | return 1; |
| 5220 | } |
| 5221 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 5222 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 5223 | { |
| 5224 | } |
| 5225 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5226 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 5227 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5228 | __init void ftrace_init_global_array_ops(struct trace_array *tr) |
| 5229 | { |
| 5230 | tr->ops = &global_ops; |
| 5231 | tr->ops->private = tr; |
| 5232 | } |
| 5233 | |
| 5234 | void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) |
| 5235 | { |
| 5236 | /* If we filter on pids, update to use the pid function */ |
| 5237 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { |
| 5238 | if (WARN_ON(tr->ops->func != ftrace_stub)) |
| 5239 | printk("ftrace ops had %pS for function\n", |
| 5240 | tr->ops->func); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5241 | } |
| 5242 | tr->ops->func = func; |
| 5243 | tr->ops->private = tr; |
| 5244 | } |
| 5245 | |
| 5246 | void ftrace_reset_array_ops(struct trace_array *tr) |
| 5247 | { |
| 5248 | tr->ops->func = ftrace_stub; |
| 5249 | } |
| 5250 | |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5251 | static nokprobe_inline void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5252 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5253 | struct ftrace_ops *ignored, struct pt_regs *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5254 | { |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5255 | struct ftrace_ops *op; |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5256 | int bit; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5257 | |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5258 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5259 | if (bit < 0) |
| 5260 | return; |
Steven Rostedt | c29f122 | 2012-11-02 17:17:59 -0400 | [diff] [blame] | 5261 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5262 | /* |
| 5263 | * Some of the ops may be dynamically allocated, |
| 5264 | * they must be freed after a synchronize_sched(). |
| 5265 | */ |
| 5266 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 5267 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5268 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 5269 | /* |
| 5270 | * Check the following for each ops before calling their func: |
| 5271 | * if RCU flag is set, then rcu_is_watching() must be true |
| 5272 | * if PER_CPU is set, then ftrace_function_local_disable() |
| 5273 | * must be false |
| 5274 | * Otherwise test if the ip matches the ops filter |
| 5275 | * |
| 5276 | * If any of the above fails then the op->func() is not executed. |
| 5277 | */ |
| 5278 | if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) && |
| 5279 | (!(op->flags & FTRACE_OPS_FL_PER_CPU) || |
| 5280 | !ftrace_function_local_disabled(op)) && |
| 5281 | ftrace_ops_test(op, ip, regs)) { |
| 5282 | |
Steven Rostedt (Red Hat) | 1d48d59 | 2014-06-25 11:54:03 -0400 | [diff] [blame] | 5283 | if (FTRACE_WARN_ON(!op->func)) { |
| 5284 | pr_warn("op=%p %pS\n", op, op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5285 | goto out; |
| 5286 | } |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5287 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5288 | } |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 5289 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 5290 | out: |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5291 | preempt_enable_notrace(); |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 5292 | trace_clear_recursion(bit); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5293 | } |
| 5294 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5295 | /* |
| 5296 | * Some archs only support passing ip and parent_ip. Even though |
| 5297 | * the list function ignores the op parameter, we do not want any |
| 5298 | * C side effects, where a function is called without the caller |
| 5299 | * sending a third parameter. |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5300 | * Archs are to support both the regs and ftrace_ops at the same time. |
| 5301 | * If they support ftrace_ops, it is assumed they support regs. |
| 5302 | * 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] | 5303 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
| 5304 | * 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] | 5305 | * An architecture can pass partial regs with ftrace_ops and still |
Li Bin | b8ec330 | 2015-11-30 18:23:36 +0800 | [diff] [blame] | 5306 | * set the ARCH_SUPPORTS_FTRACE_OPS. |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5307 | */ |
| 5308 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 5309 | 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] | 5310 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5311 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5312 | __ftrace_ops_list_func(ip, parent_ip, NULL, regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5313 | } |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5314 | NOKPROBE_SYMBOL(ftrace_ops_list_func); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5315 | #else |
Sami Tolvanen | c2f9bce | 2018-05-10 14:56:41 -0700 | [diff] [blame] | 5316 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip, |
| 5317 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5318 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 5319 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5320 | } |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5321 | NOKPROBE_SYMBOL(ftrace_ops_no_ops); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 5322 | #endif |
| 5323 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5324 | /* |
| 5325 | * 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] | 5326 | * recursion, needs RCU protection and/or requires per cpu handling, then |
| 5327 | * this function will be called by the mcount trampoline. |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5328 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5329 | 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] | 5330 | struct ftrace_ops *op, struct pt_regs *regs) |
| 5331 | { |
| 5332 | int bit; |
| 5333 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5334 | if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching()) |
| 5335 | return; |
| 5336 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5337 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 5338 | if (bit < 0) |
| 5339 | return; |
| 5340 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5341 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5342 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5343 | if (!(op->flags & FTRACE_OPS_FL_PER_CPU) || |
| 5344 | !ftrace_function_local_disabled(op)) { |
| 5345 | op->func(ip, parent_ip, op, regs); |
| 5346 | } |
| 5347 | |
| 5348 | preempt_enable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5349 | trace_clear_recursion(bit); |
| 5350 | } |
Masami Hiramatsu | 25f467d | 2019-02-24 01:50:20 +0900 | [diff] [blame] | 5351 | NOKPROBE_SYMBOL(ftrace_ops_assist_func); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 5352 | |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5353 | /** |
| 5354 | * ftrace_ops_get_func - get the function a trampoline should call |
| 5355 | * @ops: the ops to get the function for |
| 5356 | * |
| 5357 | * Normally the mcount trampoline will call the ops->func, but there |
| 5358 | * are times that it should not. For example, if the ops does not |
| 5359 | * have its own recursion protection, then it should call the |
| 5360 | * ftrace_ops_recurs_func() instead. |
| 5361 | * |
| 5362 | * Returns the function that the trampoline should call for @ops. |
| 5363 | */ |
| 5364 | ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) |
| 5365 | { |
| 5366 | /* |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5367 | * If the function does not handle recursion, needs to be RCU safe, |
| 5368 | * 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] | 5369 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 5370 | if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) || |
| 5371 | ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU)) |
| 5372 | return ftrace_ops_assist_func; |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 5373 | |
| 5374 | return ops->func; |
| 5375 | } |
| 5376 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5377 | static void |
| 5378 | ftrace_filter_pid_sched_switch_probe(void *data, bool preempt, |
| 5379 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5380 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5381 | struct trace_array *tr = data; |
| 5382 | struct trace_pid_list *pid_list; |
| 5383 | |
| 5384 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 5385 | |
| 5386 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 5387 | trace_ignore_this_task(pid_list, next)); |
| 5388 | } |
| 5389 | |
| 5390 | static void clear_ftrace_pids(struct trace_array *tr) |
| 5391 | { |
| 5392 | struct trace_pid_list *pid_list; |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5393 | int cpu; |
| 5394 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5395 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 5396 | lockdep_is_held(&ftrace_lock)); |
| 5397 | if (!pid_list) |
| 5398 | return; |
| 5399 | |
| 5400 | unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 5401 | |
| 5402 | for_each_possible_cpu(cpu) |
| 5403 | per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false; |
| 5404 | |
| 5405 | rcu_assign_pointer(tr->function_pids, NULL); |
| 5406 | |
| 5407 | /* Wait till all users are no longer using pid filtering */ |
| 5408 | synchronize_sched(); |
| 5409 | |
| 5410 | trace_free_pid_list(pid_list); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5411 | } |
| 5412 | |
Namhyung Kim | 7da0f8e | 2017-04-17 11:44:27 +0900 | [diff] [blame] | 5413 | void ftrace_clear_pids(struct trace_array *tr) |
| 5414 | { |
| 5415 | mutex_lock(&ftrace_lock); |
| 5416 | |
| 5417 | clear_ftrace_pids(tr); |
| 5418 | |
| 5419 | mutex_unlock(&ftrace_lock); |
| 5420 | } |
| 5421 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5422 | static void ftrace_pid_reset(struct trace_array *tr) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 5423 | { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5424 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5425 | clear_ftrace_pids(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5426 | |
| 5427 | ftrace_update_pid_func(); |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 5428 | ftrace_startup_all(0); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5429 | |
| 5430 | mutex_unlock(&ftrace_lock); |
| 5431 | } |
| 5432 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5433 | /* Greater than any max PID */ |
| 5434 | #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1) |
| 5435 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5436 | 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] | 5437 | __acquires(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5438 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5439 | struct trace_pid_list *pid_list; |
| 5440 | struct trace_array *tr = m->private; |
| 5441 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5442 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5443 | rcu_read_lock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5444 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5445 | pid_list = rcu_dereference_sched(tr->function_pids); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5446 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5447 | if (!pid_list) |
| 5448 | return !(*pos) ? FTRACE_NO_PIDS : NULL; |
| 5449 | |
| 5450 | return trace_pid_start(pid_list, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5451 | } |
| 5452 | |
| 5453 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 5454 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5455 | struct trace_array *tr = m->private; |
| 5456 | struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids); |
| 5457 | |
| 5458 | if (v == FTRACE_NO_PIDS) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5459 | return NULL; |
| 5460 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5461 | return trace_pid_next(pid_list, v, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | static void fpid_stop(struct seq_file *m, void *p) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5465 | __releases(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5466 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5467 | rcu_read_unlock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5468 | mutex_unlock(&ftrace_lock); |
| 5469 | } |
| 5470 | |
| 5471 | static int fpid_show(struct seq_file *m, void *v) |
| 5472 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5473 | if (v == FTRACE_NO_PIDS) { |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 5474 | seq_puts(m, "no pid\n"); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5475 | return 0; |
| 5476 | } |
| 5477 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5478 | return trace_pid_show(m, v); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | static const struct seq_operations ftrace_pid_sops = { |
| 5482 | .start = fpid_start, |
| 5483 | .next = fpid_next, |
| 5484 | .stop = fpid_stop, |
| 5485 | .show = fpid_show, |
| 5486 | }; |
| 5487 | |
| 5488 | static int |
| 5489 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 5490 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5491 | struct trace_array *tr = inode->i_private; |
| 5492 | struct seq_file *m; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5493 | int ret = 0; |
| 5494 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5495 | if (trace_array_get(tr) < 0) |
| 5496 | return -ENODEV; |
| 5497 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5498 | if ((file->f_mode & FMODE_WRITE) && |
| 5499 | (file->f_flags & O_TRUNC)) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5500 | ftrace_pid_reset(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5501 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5502 | ret = seq_open(file, &ftrace_pid_sops); |
| 5503 | if (ret < 0) { |
| 5504 | trace_array_put(tr); |
| 5505 | } else { |
| 5506 | m = file->private_data; |
| 5507 | /* copy tr over to seq ops */ |
| 5508 | m->private = tr; |
| 5509 | } |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5510 | |
| 5511 | return ret; |
| 5512 | } |
| 5513 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5514 | static void ignore_task_cpu(void *data) |
| 5515 | { |
| 5516 | struct trace_array *tr = data; |
| 5517 | struct trace_pid_list *pid_list; |
| 5518 | |
| 5519 | /* |
| 5520 | * This function is called by on_each_cpu() while the |
| 5521 | * event_mutex is held. |
| 5522 | */ |
| 5523 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 5524 | mutex_is_locked(&ftrace_lock)); |
| 5525 | |
| 5526 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 5527 | trace_ignore_this_task(pid_list, current)); |
| 5528 | } |
| 5529 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5530 | static ssize_t |
| 5531 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 5532 | size_t cnt, loff_t *ppos) |
| 5533 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5534 | struct seq_file *m = filp->private_data; |
| 5535 | struct trace_array *tr = m->private; |
| 5536 | struct trace_pid_list *filtered_pids = NULL; |
| 5537 | struct trace_pid_list *pid_list; |
| 5538 | ssize_t ret; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5539 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5540 | if (!cnt) |
| 5541 | return 0; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5542 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5543 | mutex_lock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5544 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5545 | filtered_pids = rcu_dereference_protected(tr->function_pids, |
| 5546 | lockdep_is_held(&ftrace_lock)); |
| 5547 | |
| 5548 | ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); |
| 5549 | if (ret < 0) |
| 5550 | goto out; |
| 5551 | |
| 5552 | rcu_assign_pointer(tr->function_pids, pid_list); |
| 5553 | |
| 5554 | if (filtered_pids) { |
| 5555 | synchronize_sched(); |
| 5556 | trace_free_pid_list(filtered_pids); |
| 5557 | } else if (pid_list) { |
| 5558 | /* Register a probe to set whether to ignore the tracing of a task */ |
| 5559 | register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 5560 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5561 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5562 | /* |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5563 | * Ignoring of pids is done at task switch. But we have to |
| 5564 | * check for those tasks that are currently running. |
| 5565 | * Always do this in case a pid was appended or removed. |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5566 | */ |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5567 | on_each_cpu(ignore_task_cpu, tr, 1); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5568 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5569 | ftrace_update_pid_func(); |
| 5570 | ftrace_startup_all(0); |
| 5571 | out: |
| 5572 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5573 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5574 | if (ret > 0) |
| 5575 | *ppos += ret; |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 5576 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5577 | return ret; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5578 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5579 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5580 | static int |
| 5581 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 5582 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5583 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5584 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5585 | trace_array_put(tr); |
| 5586 | |
| 5587 | return seq_release(inode, file); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5588 | } |
| 5589 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 5590 | static const struct file_operations ftrace_pid_fops = { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5591 | .open = ftrace_pid_open, |
| 5592 | .write = ftrace_pid_write, |
| 5593 | .read = seq_read, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5594 | .llseek = tracing_lseek, |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 5595 | .release = ftrace_pid_release, |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5596 | }; |
| 5597 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5598 | void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5599 | { |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 5600 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 5601 | tr, &ftrace_pid_fops); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5602 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 5603 | |
Steven Rostedt (Red Hat) | 501c237 | 2016-07-05 10:04:34 -0400 | [diff] [blame] | 5604 | void __init ftrace_init_tracefs_toplevel(struct trace_array *tr, |
| 5605 | struct dentry *d_tracer) |
| 5606 | { |
| 5607 | /* Only the top level directory has the dyn_tracefs and profile */ |
| 5608 | WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); |
| 5609 | |
| 5610 | ftrace_init_dyn_tracefs(d_tracer); |
| 5611 | ftrace_profile_tracefs(d_tracer); |
| 5612 | } |
| 5613 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5614 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5615 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5616 | * |
| 5617 | * This function should be used by panic code. It stops ftrace |
| 5618 | * but in a not so nice way. If you need to simply kill ftrace |
| 5619 | * from a non-atomic section, use ftrace_kill. |
| 5620 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 5621 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5622 | { |
| 5623 | ftrace_disabled = 1; |
| 5624 | ftrace_enabled = 0; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 5625 | clear_ftrace_function(); |
| 5626 | } |
| 5627 | |
| 5628 | /** |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 5629 | * Test if ftrace is dead or not. |
| 5630 | */ |
| 5631 | int ftrace_is_dead(void) |
| 5632 | { |
| 5633 | return ftrace_disabled; |
| 5634 | } |
| 5635 | |
| 5636 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5637 | * register_ftrace_function - register a function for profiling |
| 5638 | * @ops - ops structure that holds the function for profiling. |
| 5639 | * |
| 5640 | * Register a function to be called by all functions in the |
| 5641 | * kernel. |
| 5642 | * |
| 5643 | * Note: @ops->func and all the functions it calls must be labeled |
| 5644 | * with "notrace", otherwise it will go into a |
| 5645 | * recursive loop. |
| 5646 | */ |
| 5647 | int register_ftrace_function(struct ftrace_ops *ops) |
| 5648 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5649 | int ret = -1; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5650 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 5651 | ftrace_ops_init(ops); |
| 5652 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5653 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5654 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5655 | ret = ftrace_startup(ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 5656 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5657 | mutex_unlock(&ftrace_lock); |
Borislav Petkov | 8d240dd | 2012-03-29 19:11:40 +0200 | [diff] [blame] | 5658 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5659 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5660 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5661 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5662 | |
| 5663 | /** |
Uwe Kleine-Koenig | 3263292 | 2009-01-12 23:35:50 +0100 | [diff] [blame] | 5664 | * unregister_ftrace_function - unregister a function for profiling. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5665 | * @ops - ops structure that holds the function to unregister |
| 5666 | * |
| 5667 | * Unregister a function that was added to be called by ftrace profiling. |
| 5668 | */ |
| 5669 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 5670 | { |
| 5671 | int ret; |
| 5672 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5673 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 5674 | ret = ftrace_shutdown(ops, 0); |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5675 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5676 | |
| 5677 | return ret; |
| 5678 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 5679 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5680 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 5681 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5682 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 5683 | void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5684 | loff_t *ppos) |
| 5685 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5686 | int ret = -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5687 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5688 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5689 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5690 | if (unlikely(ftrace_disabled)) |
| 5691 | goto out; |
| 5692 | |
| 5693 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5694 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5695 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5696 | goto out; |
| 5697 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 5698 | last_ftrace_enabled = !!ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5699 | |
| 5700 | if (ftrace_enabled) { |
| 5701 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5702 | /* we are starting ftrace again */ |
Jan Kiszka | 5000c41 | 2013-03-26 17:53:03 +0100 | [diff] [blame] | 5703 | if (ftrace_ops_list != &ftrace_list_end) |
| 5704 | update_ftrace_function(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5705 | |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 5706 | ftrace_startup_sysctl(); |
| 5707 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5708 | } else { |
| 5709 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 5710 | ftrace_trace_function = ftrace_stub; |
| 5711 | |
| 5712 | ftrace_shutdown_sysctl(); |
| 5713 | } |
| 5714 | |
| 5715 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5716 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5717 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5718 | } |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 5719 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5720 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5721 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5722 | static struct ftrace_ops graph_ops = { |
| 5723 | .func = ftrace_stub, |
| 5724 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 5725 | FTRACE_OPS_FL_INITIALIZED | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 5726 | FTRACE_OPS_FL_PID | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5727 | FTRACE_OPS_FL_STUB, |
| 5728 | #ifdef FTRACE_GRAPH_TRAMP_ADDR |
| 5729 | .trampoline = FTRACE_GRAPH_TRAMP_ADDR, |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 5730 | /* trampoline_size is only needed for dynamically allocated tramps */ |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5731 | #endif |
| 5732 | ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash) |
| 5733 | }; |
| 5734 | |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 5735 | void ftrace_graph_sleep_time_control(bool enable) |
| 5736 | { |
| 5737 | fgraph_sleep_time = enable; |
| 5738 | } |
| 5739 | |
| 5740 | void ftrace_graph_graph_time_control(bool enable) |
| 5741 | { |
| 5742 | fgraph_graph_time = enable; |
| 5743 | } |
| 5744 | |
Sami Tolvanen | c2f9bce | 2018-05-10 14:56:41 -0700 | [diff] [blame] | 5745 | void ftrace_graph_return_stub(struct ftrace_graph_ret *trace) |
| 5746 | { |
| 5747 | } |
| 5748 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5749 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 5750 | { |
| 5751 | return 0; |
| 5752 | } |
| 5753 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5754 | /* The callbacks that hook a function */ |
Sami Tolvanen | c2f9bce | 2018-05-10 14:56:41 -0700 | [diff] [blame] | 5755 | trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_return_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5756 | 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] | 5757 | 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] | 5758 | |
| 5759 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 5760 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 5761 | { |
| 5762 | int i; |
| 5763 | int ret = 0; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5764 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 5765 | struct task_struct *g, *t; |
| 5766 | |
| 5767 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 5768 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH |
| 5769 | * sizeof(struct ftrace_ret_stack), |
| 5770 | GFP_KERNEL); |
| 5771 | if (!ret_stack_list[i]) { |
| 5772 | start = 0; |
| 5773 | end = i; |
| 5774 | ret = -ENOMEM; |
| 5775 | goto free; |
| 5776 | } |
| 5777 | } |
| 5778 | |
Soumya PN | 6112a30 | 2016-05-17 21:31:14 +0530 | [diff] [blame] | 5779 | read_lock(&tasklist_lock); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5780 | do_each_thread(g, t) { |
| 5781 | if (start == end) { |
| 5782 | ret = -EAGAIN; |
| 5783 | goto unlock; |
| 5784 | } |
| 5785 | |
| 5786 | if (t->ret_stack == NULL) { |
Frederic Weisbecker | 380c4b1 | 2008-12-06 03:43:41 +0100 | [diff] [blame] | 5787 | atomic_set(&t->tracing_graph_pause, 0); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5788 | atomic_set(&t->trace_overrun, 0); |
Steven Rostedt | 26c0162 | 2009-06-02 14:01:19 -0400 | [diff] [blame] | 5789 | t->curr_ret_stack = -1; |
| 5790 | /* Make sure the tasks see the -1 first: */ |
| 5791 | smp_wmb(); |
| 5792 | t->ret_stack = ret_stack_list[start++]; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5793 | } |
| 5794 | } while_each_thread(g, t); |
| 5795 | |
| 5796 | unlock: |
Soumya PN | 6112a30 | 2016-05-17 21:31:14 +0530 | [diff] [blame] | 5797 | read_unlock(&tasklist_lock); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5798 | free: |
| 5799 | for (i = start; i < end; i++) |
| 5800 | kfree(ret_stack_list[i]); |
| 5801 | return ret; |
| 5802 | } |
| 5803 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5804 | static void |
Peter Zijlstra | c73464b | 2015-09-28 18:06:56 +0200 | [diff] [blame] | 5805 | ftrace_graph_probe_sched_switch(void *ignore, bool preempt, |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5806 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5807 | { |
| 5808 | unsigned long long timestamp; |
| 5809 | int index; |
| 5810 | |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 5811 | /* |
| 5812 | * Does the user want to count the time a function was asleep. |
| 5813 | * If so, do not update the time stamps. |
| 5814 | */ |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 5815 | if (fgraph_sleep_time) |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 5816 | return; |
| 5817 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5818 | timestamp = trace_clock_local(); |
| 5819 | |
| 5820 | prev->ftrace_timestamp = timestamp; |
| 5821 | |
| 5822 | /* only process tasks that we timestamped */ |
| 5823 | if (!next->ftrace_timestamp) |
| 5824 | return; |
| 5825 | |
| 5826 | /* |
| 5827 | * Update all the counters in next to make up for the |
| 5828 | * time next was sleeping. |
| 5829 | */ |
| 5830 | timestamp -= next->ftrace_timestamp; |
| 5831 | |
| 5832 | for (index = next->curr_ret_stack; index >= 0; index--) |
| 5833 | next->ret_stack[index].calltime += timestamp; |
| 5834 | } |
| 5835 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5836 | /* Allocate a return stack for each task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5837 | static int start_graph_tracing(void) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5838 | { |
| 5839 | struct ftrace_ret_stack **ret_stack_list; |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5840 | int ret, cpu; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5841 | |
| 5842 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * |
| 5843 | sizeof(struct ftrace_ret_stack *), |
| 5844 | GFP_KERNEL); |
| 5845 | |
| 5846 | if (!ret_stack_list) |
| 5847 | return -ENOMEM; |
| 5848 | |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5849 | /* The cpu_boot init_task->ret_stack will never be freed */ |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 5850 | for_each_online_cpu(cpu) { |
| 5851 | if (!idle_task(cpu)->ret_stack) |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5852 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 5853 | } |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 5854 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5855 | do { |
| 5856 | ret = alloc_retstack_tasklist(ret_stack_list); |
| 5857 | } while (ret == -EAGAIN); |
| 5858 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5859 | if (!ret) { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5860 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 5861 | if (ret) |
| 5862 | pr_info("ftrace_graph: Couldn't activate tracepoint" |
| 5863 | " probe to kernel_sched_switch\n"); |
| 5864 | } |
| 5865 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5866 | kfree(ret_stack_list); |
| 5867 | return ret; |
| 5868 | } |
| 5869 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5870 | /* |
| 5871 | * Hibernation protection. |
| 5872 | * The state of the current task is too much unstable during |
| 5873 | * suspend/restore to disk. We want to protect against that. |
| 5874 | */ |
| 5875 | static int |
| 5876 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, |
| 5877 | void *unused) |
| 5878 | { |
| 5879 | switch (state) { |
| 5880 | case PM_HIBERNATION_PREPARE: |
| 5881 | pause_graph_tracing(); |
| 5882 | break; |
| 5883 | |
| 5884 | case PM_POST_HIBERNATION: |
| 5885 | unpause_graph_tracing(); |
| 5886 | break; |
| 5887 | } |
| 5888 | return NOTIFY_DONE; |
| 5889 | } |
| 5890 | |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5891 | static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) |
| 5892 | { |
| 5893 | if (!ftrace_ops_test(&global_ops, trace->func, NULL)) |
| 5894 | return 0; |
| 5895 | return __ftrace_graph_entry(trace); |
| 5896 | } |
| 5897 | |
| 5898 | /* |
| 5899 | * The function graph tracer should only trace the functions defined |
| 5900 | * by set_ftrace_filter and set_ftrace_notrace. If another function |
| 5901 | * tracer ops is registered, the graph tracer requires testing the |
| 5902 | * function against the global ops, and not just trace any function |
| 5903 | * that any ftrace_ops registered. |
| 5904 | */ |
| 5905 | static void update_function_graph_func(void) |
| 5906 | { |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5907 | struct ftrace_ops *op; |
| 5908 | bool do_test = false; |
| 5909 | |
| 5910 | /* |
| 5911 | * The graph and global ops share the same set of functions |
| 5912 | * to test. If any other ops is on the list, then |
| 5913 | * the graph tracing needs to test if its the function |
| 5914 | * it should call. |
| 5915 | */ |
| 5916 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 5917 | if (op != &global_ops && op != &graph_ops && |
| 5918 | op != &ftrace_list_end) { |
| 5919 | do_test = true; |
| 5920 | /* in double loop, break out with goto */ |
| 5921 | goto out; |
| 5922 | } |
| 5923 | } while_for_each_ftrace_op(op); |
| 5924 | out: |
| 5925 | if (do_test) |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5926 | ftrace_graph_entry = ftrace_graph_entry_test; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5927 | else |
| 5928 | ftrace_graph_entry = __ftrace_graph_entry; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5929 | } |
| 5930 | |
Mathias Krause | 8275f69 | 2014-03-30 15:31:50 +0200 | [diff] [blame] | 5931 | static struct notifier_block ftrace_suspend_notifier = { |
| 5932 | .notifier_call = ftrace_suspend_notifier_call, |
| 5933 | }; |
| 5934 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5935 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 5936 | trace_func_graph_ent_t entryfunc) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5937 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5938 | int ret = 0; |
| 5939 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5940 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5941 | |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5942 | /* we currently allow only one tracer registered at a time */ |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5943 | if (ftrace_graph_active) { |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 5944 | ret = -EBUSY; |
| 5945 | goto out; |
| 5946 | } |
| 5947 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5948 | register_pm_notifier(&ftrace_suspend_notifier); |
| 5949 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5950 | ftrace_graph_active++; |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5951 | ret = start_graph_tracing(); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5952 | if (ret) { |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5953 | ftrace_graph_active--; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5954 | goto out; |
| 5955 | } |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5956 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 5957 | ftrace_graph_return = retfunc; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5958 | |
| 5959 | /* |
| 5960 | * Update the indirect function to the entryfunc, and the |
| 5961 | * function that gets called to the entry_test first. Then |
| 5962 | * call the update fgraph entry function to determine if |
| 5963 | * the entryfunc should be called directly or not. |
| 5964 | */ |
| 5965 | __ftrace_graph_entry = entryfunc; |
| 5966 | ftrace_graph_entry = ftrace_graph_entry_test; |
| 5967 | update_function_graph_func(); |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 5968 | |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5969 | ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5970 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5971 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5972 | return ret; |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5973 | } |
| 5974 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 5975 | void unregister_ftrace_graph(void) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5976 | { |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5977 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5978 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5979 | if (unlikely(!ftrace_graph_active)) |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5980 | goto out; |
| 5981 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 5982 | ftrace_graph_active--; |
Sami Tolvanen | c2f9bce | 2018-05-10 14:56:41 -0700 | [diff] [blame] | 5983 | ftrace_graph_return = ftrace_graph_return_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 5984 | ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 23a8e84 | 2014-01-13 10:30:23 -0500 | [diff] [blame] | 5985 | __ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 5986 | ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET); |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 5987 | unregister_pm_notifier(&ftrace_suspend_notifier); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 5988 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 5989 | |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 5990 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5991 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 5992 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 5993 | |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 5994 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
| 5995 | |
| 5996 | static void |
| 5997 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) |
| 5998 | { |
| 5999 | atomic_set(&t->tracing_graph_pause, 0); |
| 6000 | atomic_set(&t->trace_overrun, 0); |
| 6001 | t->ftrace_timestamp = 0; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6002 | /* make curr_ret_stack visible before we add the ret_stack */ |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 6003 | smp_wmb(); |
| 6004 | t->ret_stack = ret_stack; |
| 6005 | } |
| 6006 | |
| 6007 | /* |
| 6008 | * Allocate a return stack for the idle task. May be the first |
| 6009 | * time through, or it may be done by CPU hotplug online. |
| 6010 | */ |
| 6011 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) |
| 6012 | { |
| 6013 | t->curr_ret_stack = -1; |
| 6014 | /* |
| 6015 | * The idle task has no parent, it either has its own |
| 6016 | * stack or no stack at all. |
| 6017 | */ |
| 6018 | if (t->ret_stack) |
| 6019 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); |
| 6020 | |
| 6021 | if (ftrace_graph_active) { |
| 6022 | struct ftrace_ret_stack *ret_stack; |
| 6023 | |
| 6024 | ret_stack = per_cpu(idle_ret_stack, cpu); |
| 6025 | if (!ret_stack) { |
| 6026 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
| 6027 | * sizeof(struct ftrace_ret_stack), |
| 6028 | GFP_KERNEL); |
| 6029 | if (!ret_stack) |
| 6030 | return; |
| 6031 | per_cpu(idle_ret_stack, cpu) = ret_stack; |
| 6032 | } |
| 6033 | graph_init_task(t, ret_stack); |
| 6034 | } |
| 6035 | } |
| 6036 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6037 | /* Allocate a return stack for newly created task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6038 | void ftrace_graph_init_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6039 | { |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6040 | /* Make sure we do not use the parent ret_stack */ |
| 6041 | t->ret_stack = NULL; |
Steven Rostedt | ea14eb7 | 2010-03-12 19:41:23 -0500 | [diff] [blame] | 6042 | t->curr_ret_stack = -1; |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6043 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 6044 | if (ftrace_graph_active) { |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 6045 | struct ftrace_ret_stack *ret_stack; |
| 6046 | |
| 6047 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6048 | * sizeof(struct ftrace_ret_stack), |
| 6049 | GFP_KERNEL); |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 6050 | if (!ret_stack) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6051 | return; |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 6052 | graph_init_task(t, ret_stack); |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 6053 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6054 | } |
| 6055 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 6056 | void ftrace_graph_exit_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6057 | { |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 6058 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 6059 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6060 | t->ret_stack = NULL; |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 6061 | /* NULL must become visible to IRQs before we free it: */ |
| 6062 | barrier(); |
| 6063 | |
| 6064 | kfree(ret_stack); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 6065 | } |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 6066 | #endif |