Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Infrastructure for profiling code inserted by 'gcc -pg'. |
| 4 | * |
| 5 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 6 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> |
| 7 | * |
| 8 | * Originally ported from the -rt patch by: |
| 9 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 10 | * |
| 11 | * Based on code in the latency_tracer, that is: |
| 12 | * |
| 13 | * Copyright (C) 2004-2006 Ingo Molnar |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 14 | * Copyright (C) 2004 Nadia Yvette Chambers |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 17 | #include <linux/stop_machine.h> |
| 18 | #include <linux/clocksource.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 19 | #include <linux/sched/task.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 20 | #include <linux/kallsyms.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 22 | #include <linux/tracefs.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 23 | #include <linux/hardirq.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 26 | #include <linux/bsearch.h> |
Paul Gortmaker | 56d82e0 | 2011-05-26 17:53:52 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 28 | #include <linux/ftrace.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 29 | #include <linux/sysctl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 32 | #include <linux/sort.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 33 | #include <linux/list.h> |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 34 | #include <linux/hash.h> |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 35 | #include <linux/rcupdate.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 36 | |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 37 | #include <trace/events/sched.h> |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 38 | |
Steven Rostedt (VMware) | b80f0f6 | 2017-04-03 12:57:35 -0400 | [diff] [blame] | 39 | #include <asm/sections.h> |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 40 | #include <asm/setup.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 41 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 42 | #include "ftrace_internal.h" |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 43 | #include "trace_output.h" |
Steven Rostedt | bac429f | 2009-03-20 12:50:56 -0400 | [diff] [blame] | 44 | #include "trace_stat.h" |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 45 | |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 46 | #define FTRACE_WARN_ON(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 47 | ({ \ |
| 48 | int ___r = cond; \ |
| 49 | if (WARN_ON(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 50 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 51 | ___r; \ |
| 52 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 53 | |
| 54 | #define FTRACE_WARN_ON_ONCE(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 55 | ({ \ |
| 56 | int ___r = cond; \ |
| 57 | if (WARN_ON_ONCE(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 58 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 59 | ___r; \ |
| 60 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 61 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 62 | /* hash bits for specific function selection */ |
| 63 | #define FTRACE_HASH_BITS 7 |
| 64 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 65 | #define FTRACE_HASH_DEFAULT_BITS 10 |
| 66 | #define FTRACE_HASH_MAX_BITS 12 |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 67 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 68 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 69 | #define INIT_OPS_HASH(opsname) \ |
| 70 | .func_hash = &opsname.local_hash, \ |
| 71 | .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] | 72 | #define ASSIGN_OPS_HASH(opsname, val) \ |
| 73 | .func_hash = val, \ |
| 74 | .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 75 | #else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 76 | #define INIT_OPS_HASH(opsname) |
Steven Rostedt (Red Hat) | 5f151b2 | 2014-08-15 17:18:46 -0400 | [diff] [blame] | 77 | #define ASSIGN_OPS_HASH(opsname, val) |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 78 | #endif |
| 79 | |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 80 | enum { |
| 81 | FTRACE_MODIFY_ENABLE_FL = (1 << 0), |
| 82 | FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1), |
| 83 | }; |
| 84 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 85 | struct ftrace_ops ftrace_list_end __read_mostly = { |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 86 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | 395b97a | 2013-03-27 09:31:28 -0400 | [diff] [blame] | 87 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 88 | INIT_OPS_HASH(ftrace_list_end) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 91 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 92 | int ftrace_enabled __read_mostly; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 93 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 94 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 95 | /* Current function tracing op */ |
| 96 | 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] | 97 | /* What to set function_trace_op to */ |
| 98 | static struct ftrace_ops *set_function_trace_op; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 99 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 100 | static bool ftrace_pids_enabled(struct ftrace_ops *ops) |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 101 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 102 | struct trace_array *tr; |
| 103 | |
| 104 | if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private) |
| 105 | return false; |
| 106 | |
| 107 | tr = ops->private; |
| 108 | |
| 109 | return tr->function_pids != NULL; |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 113 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 114 | /* |
| 115 | * ftrace_disabled is set when an anomaly is discovered. |
| 116 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 117 | */ |
| 118 | static int ftrace_disabled __read_mostly; |
| 119 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 120 | DEFINE_MUTEX(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 121 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 122 | struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 123 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 124 | struct ftrace_ops global_ops; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 125 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 126 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 127 | 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] | 128 | struct ftrace_ops *op, struct pt_regs *regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 129 | #else |
| 130 | /* See comment below, where ftrace_ops_list_func is defined */ |
| 131 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip); |
| 132 | #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops) |
| 133 | #endif |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 134 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 135 | static inline void ftrace_ops_init(struct ftrace_ops *ops) |
| 136 | { |
| 137 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 138 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 139 | mutex_init(&ops->local_hash.regex_lock); |
| 140 | ops->func_hash = &ops->local_hash; |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 141 | ops->flags |= FTRACE_OPS_FL_INITIALIZED; |
| 142 | } |
| 143 | #endif |
| 144 | } |
| 145 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 146 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 147 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 148 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 149 | struct trace_array *tr = op->private; |
| 150 | |
| 151 | if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid)) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 152 | return; |
| 153 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 154 | op->saved_func(ip, parent_ip, op, regs); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 157 | static void ftrace_sync(struct work_struct *work) |
| 158 | { |
| 159 | /* |
| 160 | * This function is just a stub to implement a hard force |
| 161 | * of synchronize_sched(). This requires synchronizing |
| 162 | * tasks even in userspace and idle. |
| 163 | * |
| 164 | * Yes, function tracing is rude. |
| 165 | */ |
| 166 | } |
| 167 | |
| 168 | static void ftrace_sync_ipi(void *data) |
| 169 | { |
| 170 | /* Probably not needed, but do it anyway */ |
| 171 | smp_rmb(); |
| 172 | } |
| 173 | |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 174 | static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops) |
| 175 | { |
| 176 | /* |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 177 | * 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] | 178 | * then it needs to call the list anyway. |
| 179 | */ |
Peter Zijlstra | b3a8880 | 2017-10-11 09:45:32 +0200 | [diff] [blame] | 180 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) || |
| 181 | FTRACE_FORCE_LIST_FUNC) |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 182 | return ftrace_ops_list_func; |
| 183 | |
| 184 | return ftrace_ops_get_func(ops); |
| 185 | } |
| 186 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 187 | static void update_ftrace_function(void) |
| 188 | { |
| 189 | ftrace_func_t func; |
| 190 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 191 | /* |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 192 | * Prepare the ftrace_ops that the arch callback will use. |
| 193 | * If there's only one ftrace_ops registered, the ftrace_ops_list |
| 194 | * will point to the ops we want. |
| 195 | */ |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 196 | set_function_trace_op = rcu_dereference_protected(ftrace_ops_list, |
| 197 | lockdep_is_held(&ftrace_lock)); |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 198 | |
| 199 | /* If there's no ftrace_ops registered, just call the stub function */ |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 200 | if (set_function_trace_op == &ftrace_list_end) { |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 201 | func = ftrace_stub; |
| 202 | |
| 203 | /* |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 204 | * 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] | 205 | * recursion safe and not dynamic and the arch supports passing ops, |
| 206 | * then have the mcount trampoline call the function directly. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 207 | */ |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 208 | } else if (rcu_dereference_protected(ftrace_ops_list->next, |
| 209 | lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { |
Steven Rostedt (Red Hat) | 00ccbf2 | 2015-02-19 15:56:14 +0100 | [diff] [blame] | 210 | func = ftrace_ops_get_list_func(ftrace_ops_list); |
Steven Rostedt (Red Hat) | f7aad4e | 2014-09-10 10:42:46 -0400 | [diff] [blame] | 211 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 212 | } else { |
| 213 | /* Just use the default ftrace_ops */ |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 214 | set_function_trace_op = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 215 | func = ftrace_ops_list_func; |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 216 | } |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 217 | |
Steven Rostedt (Red Hat) | 5f8bf2d2 | 2014-07-15 11:05:12 -0400 | [diff] [blame] | 218 | update_function_graph_func(); |
| 219 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 220 | /* If there's no change, then do nothing more here */ |
| 221 | if (ftrace_trace_function == func) |
| 222 | return; |
| 223 | |
| 224 | /* |
| 225 | * If we are using the list function, it doesn't care |
| 226 | * about the function_trace_ops. |
| 227 | */ |
| 228 | if (func == ftrace_ops_list_func) { |
| 229 | ftrace_trace_function = func; |
| 230 | /* |
| 231 | * Don't even bother setting function_trace_ops, |
| 232 | * it would be racy to do so anyway. |
| 233 | */ |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | #ifndef CONFIG_DYNAMIC_FTRACE |
| 238 | /* |
| 239 | * For static tracing, we need to be a bit more careful. |
| 240 | * The function change takes affect immediately. Thus, |
| 241 | * we need to coorditate the setting of the function_trace_ops |
| 242 | * with the setting of the ftrace_trace_function. |
| 243 | * |
| 244 | * Set the function to the list ops, which will call the |
| 245 | * function we want, albeit indirectly, but it handles the |
| 246 | * ftrace_ops and doesn't depend on function_trace_op. |
| 247 | */ |
| 248 | ftrace_trace_function = ftrace_ops_list_func; |
| 249 | /* |
| 250 | * Make sure all CPUs see this. Yes this is slow, but static |
| 251 | * tracing is slow and nasty to have enabled. |
| 252 | */ |
| 253 | schedule_on_each_cpu(ftrace_sync); |
| 254 | /* Now all cpus are using the list ops. */ |
| 255 | function_trace_op = set_function_trace_op; |
| 256 | /* Make sure the function_trace_op is visible on all CPUs */ |
| 257 | smp_wmb(); |
| 258 | /* Nasty way to force a rmb on all cpus */ |
| 259 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
| 260 | /* OK, we are all set to update the ftrace_trace_function now! */ |
| 261 | #endif /* !CONFIG_DYNAMIC_FTRACE */ |
| 262 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 263 | ftrace_trace_function = func; |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 266 | static void add_ftrace_ops(struct ftrace_ops __rcu **list, |
| 267 | struct ftrace_ops *ops) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 268 | { |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 269 | rcu_assign_pointer(ops->next, *list); |
| 270 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 271 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 272 | * We are entering ops into the list but another |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 273 | * CPU might be walking that list. We need to make sure |
| 274 | * the ops->next pointer is valid before another CPU sees |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 275 | * the ops pointer included into the list. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 276 | */ |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 277 | rcu_assign_pointer(*list, ops); |
| 278 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 279 | |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 280 | static int remove_ftrace_ops(struct ftrace_ops __rcu **list, |
| 281 | struct ftrace_ops *ops) |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 282 | { |
| 283 | struct ftrace_ops **p; |
| 284 | |
| 285 | /* |
| 286 | * If we are removing the last function, then simply point |
| 287 | * to the ftrace_stub. |
| 288 | */ |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 289 | if (rcu_dereference_protected(*list, |
| 290 | lockdep_is_held(&ftrace_lock)) == ops && |
| 291 | rcu_dereference_protected(ops->next, |
| 292 | lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 293 | *list = &ftrace_list_end; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
| 298 | if (*p == ops) |
| 299 | break; |
| 300 | |
| 301 | if (*p != ops) |
| 302 | return -1; |
| 303 | |
| 304 | *p = (*p)->next; |
| 305 | return 0; |
| 306 | } |
| 307 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 308 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 309 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 310 | int __register_ftrace_function(struct ftrace_ops *ops) |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 311 | { |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 312 | if (ops->flags & FTRACE_OPS_FL_DELETED) |
| 313 | return -EINVAL; |
| 314 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 315 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 316 | return -EBUSY; |
| 317 | |
Masami Hiramatsu | 06aeaae | 2012-09-28 17:15:17 +0900 | [diff] [blame] | 318 | #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 319 | /* |
| 320 | * If the ftrace_ops specifies SAVE_REGS, then it only can be used |
| 321 | * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. |
| 322 | * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. |
| 323 | */ |
| 324 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && |
| 325 | !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) |
| 329 | ops->flags |= FTRACE_OPS_FL_SAVE_REGS; |
| 330 | #endif |
| 331 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 332 | if (!core_kernel_data((unsigned long)ops)) |
| 333 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; |
| 334 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 335 | add_ftrace_ops(&ftrace_ops_list, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 336 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 337 | /* Always save the function, and reset at unregistering */ |
| 338 | ops->saved_func = ops->func; |
| 339 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 340 | if (ftrace_pids_enabled(ops)) |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 341 | ops->func = ftrace_pid_func; |
| 342 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 343 | ftrace_update_trampoline(ops); |
| 344 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 345 | if (ftrace_enabled) |
| 346 | update_ftrace_function(); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 347 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 351 | int __unregister_ftrace_function(struct ftrace_ops *ops) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 352 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 353 | int ret; |
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 | if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) |
| 356 | return -EBUSY; |
| 357 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 358 | ret = remove_ftrace_ops(&ftrace_ops_list, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 359 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 360 | if (ret < 0) |
| 361 | return ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 362 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 363 | if (ftrace_enabled) |
| 364 | update_ftrace_function(); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 365 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 366 | ops->func = ops->saved_func; |
| 367 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 368 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 371 | static void ftrace_update_pid_func(void) |
| 372 | { |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 373 | struct ftrace_ops *op; |
| 374 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 375 | /* Only do something if we are tracing something */ |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 376 | if (ftrace_trace_function == ftrace_stub) |
KOSAKI Motohiro | 10dd3eb | 2009-03-06 15:29:04 +0900 | [diff] [blame] | 377 | return; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 378 | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 379 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 380 | if (op->flags & FTRACE_OPS_FL_PID) { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 381 | op->func = ftrace_pids_enabled(op) ? |
| 382 | ftrace_pid_func : op->saved_func; |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 383 | ftrace_update_trampoline(op); |
| 384 | } |
| 385 | } while_for_each_ftrace_op(op); |
| 386 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 387 | update_ftrace_function(); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 390 | #ifdef CONFIG_FUNCTION_PROFILER |
| 391 | struct ftrace_profile { |
| 392 | struct hlist_node node; |
| 393 | unsigned long ip; |
| 394 | unsigned long counter; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 395 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 396 | unsigned long long time; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 397 | unsigned long long time_squared; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 398 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | struct ftrace_profile_page { |
| 402 | struct ftrace_profile_page *next; |
| 403 | unsigned long index; |
| 404 | struct ftrace_profile records[]; |
| 405 | }; |
| 406 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 407 | struct ftrace_profile_stat { |
| 408 | atomic_t disabled; |
| 409 | struct hlist_head *hash; |
| 410 | struct ftrace_profile_page *pages; |
| 411 | struct ftrace_profile_page *start; |
| 412 | struct tracer_stat stat; |
| 413 | }; |
| 414 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 415 | #define PROFILE_RECORDS_SIZE \ |
| 416 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) |
| 417 | |
| 418 | #define PROFILES_PER_PAGE \ |
| 419 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) |
| 420 | |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 421 | static int ftrace_profile_enabled __read_mostly; |
| 422 | |
| 423 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 424 | static DEFINE_MUTEX(ftrace_profile_lock); |
| 425 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 426 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 427 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 428 | #define FTRACE_PROFILE_HASH_BITS 10 |
| 429 | #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 430 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 431 | static void * |
| 432 | function_stat_next(void *v, int idx) |
| 433 | { |
| 434 | struct ftrace_profile *rec = v; |
| 435 | struct ftrace_profile_page *pg; |
| 436 | |
| 437 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
| 438 | |
| 439 | again: |
Li Zefan | 0296e42 | 2009-06-26 11:15:37 +0800 | [diff] [blame] | 440 | if (idx != 0) |
| 441 | rec++; |
| 442 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 443 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
| 444 | pg = pg->next; |
| 445 | if (!pg) |
| 446 | return NULL; |
| 447 | rec = &pg->records[0]; |
| 448 | if (!rec->counter) |
| 449 | goto again; |
| 450 | } |
| 451 | |
| 452 | return rec; |
| 453 | } |
| 454 | |
| 455 | static void *function_stat_start(struct tracer_stat *trace) |
| 456 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 457 | struct ftrace_profile_stat *stat = |
| 458 | container_of(trace, struct ftrace_profile_stat, stat); |
| 459 | |
| 460 | if (!stat || !stat->start) |
| 461 | return NULL; |
| 462 | |
| 463 | return function_stat_next(&stat->start->records[0], 0); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 466 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 467 | /* function graph compares on total time */ |
| 468 | static int function_stat_cmp(void *p1, void *p2) |
| 469 | { |
| 470 | struct ftrace_profile *a = p1; |
| 471 | struct ftrace_profile *b = p2; |
| 472 | |
| 473 | if (a->time < b->time) |
| 474 | return -1; |
| 475 | if (a->time > b->time) |
| 476 | return 1; |
| 477 | else |
| 478 | return 0; |
| 479 | } |
| 480 | #else |
| 481 | /* not function graph compares against hits */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 482 | static int function_stat_cmp(void *p1, void *p2) |
| 483 | { |
| 484 | struct ftrace_profile *a = p1; |
| 485 | struct ftrace_profile *b = p2; |
| 486 | |
| 487 | if (a->counter < b->counter) |
| 488 | return -1; |
| 489 | if (a->counter > b->counter) |
| 490 | return 1; |
| 491 | else |
| 492 | return 0; |
| 493 | } |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 494 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 495 | |
| 496 | static int function_stat_headers(struct seq_file *m) |
| 497 | { |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 498 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 499 | seq_puts(m, " Function " |
| 500 | "Hit Time Avg s^2\n" |
| 501 | " -------- " |
| 502 | "--- ---- --- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 503 | #else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 504 | seq_puts(m, " Function Hit\n" |
| 505 | " -------- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 506 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int function_stat_show(struct seq_file *m, void *v) |
| 511 | { |
| 512 | struct ftrace_profile *rec = v; |
| 513 | char str[KSYM_SYMBOL_LEN]; |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 514 | int ret = 0; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 515 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 516 | static struct trace_seq s; |
| 517 | unsigned long long avg; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 518 | unsigned long long stddev; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 519 | #endif |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 520 | mutex_lock(&ftrace_profile_lock); |
| 521 | |
| 522 | /* we raced with function_profile_reset() */ |
| 523 | if (unlikely(rec->counter == 0)) { |
| 524 | ret = -EBUSY; |
| 525 | goto out; |
| 526 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 527 | |
Umesh Tiwari | 8e436ca | 2015-06-22 16:58:08 +0530 | [diff] [blame] | 528 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 529 | avg = rec->time; |
| 530 | do_div(avg, rec->counter); |
| 531 | if (tracing_thresh && (avg < tracing_thresh)) |
| 532 | goto out; |
| 533 | #endif |
| 534 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 535 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 536 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 537 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 538 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 539 | seq_puts(m, " "); |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 540 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 541 | /* Sample standard deviation (s^2) */ |
| 542 | if (rec->counter <= 1) |
| 543 | stddev = 0; |
| 544 | else { |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 545 | /* |
| 546 | * Apply Welford's method: |
| 547 | * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) |
| 548 | */ |
| 549 | stddev = rec->counter * rec->time_squared - |
| 550 | rec->time * rec->time; |
| 551 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 552 | /* |
| 553 | * Divide only 1000 for ns^2 -> us^2 conversion. |
| 554 | * trace_print_graph_duration will divide 1000 again. |
| 555 | */ |
Juri Lelli | 52d85d7 | 2013-06-12 12:03:18 +0200 | [diff] [blame] | 556 | do_div(stddev, rec->counter * (rec->counter - 1) * 1000); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 557 | } |
| 558 | |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 559 | trace_seq_init(&s); |
| 560 | trace_print_graph_duration(rec->time, &s); |
| 561 | trace_seq_puts(&s, " "); |
| 562 | trace_print_graph_duration(avg, &s); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 563 | trace_seq_puts(&s, " "); |
| 564 | trace_print_graph_duration(stddev, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 565 | trace_print_seq(m, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 566 | #endif |
| 567 | seq_putc(m, '\n'); |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 568 | out: |
| 569 | mutex_unlock(&ftrace_profile_lock); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 570 | |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 571 | return ret; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 572 | } |
| 573 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 574 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 575 | { |
| 576 | struct ftrace_profile_page *pg; |
| 577 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 578 | pg = stat->pages = stat->start; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 579 | |
| 580 | while (pg) { |
| 581 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); |
| 582 | pg->index = 0; |
| 583 | pg = pg->next; |
| 584 | } |
| 585 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 586 | memset(stat->hash, 0, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 587 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
| 588 | } |
| 589 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 590 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 591 | { |
| 592 | struct ftrace_profile_page *pg; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 593 | int functions; |
| 594 | int pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 595 | int i; |
| 596 | |
| 597 | /* If we already allocated, do nothing */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 598 | if (stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 599 | return 0; |
| 600 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 601 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
| 602 | if (!stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 603 | return -ENOMEM; |
| 604 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 605 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 606 | functions = ftrace_update_tot_cnt; |
| 607 | #else |
| 608 | /* |
| 609 | * We do not know the number of functions that exist because |
| 610 | * dynamic tracing is what counts them. With past experience |
| 611 | * we have around 20K functions. That should be more than enough. |
| 612 | * It is highly unlikely we will execute every function in |
| 613 | * the kernel. |
| 614 | */ |
| 615 | functions = 20000; |
| 616 | #endif |
| 617 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 618 | pg = stat->start = stat->pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 619 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 620 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
| 621 | |
Namhyung Kim | 39e30cd | 2013-04-01 21:46:24 +0900 | [diff] [blame] | 622 | for (i = 1; i < pages; i++) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 623 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 624 | if (!pg->next) |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 625 | goto out_free; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 626 | pg = pg->next; |
| 627 | } |
| 628 | |
| 629 | return 0; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 630 | |
| 631 | out_free: |
| 632 | pg = stat->start; |
| 633 | while (pg) { |
| 634 | unsigned long tmp = (unsigned long)pg; |
| 635 | |
| 636 | pg = pg->next; |
| 637 | free_page(tmp); |
| 638 | } |
| 639 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 640 | stat->pages = NULL; |
| 641 | stat->start = NULL; |
| 642 | |
| 643 | return -ENOMEM; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 644 | } |
| 645 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 646 | static int ftrace_profile_init_cpu(int cpu) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 647 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 648 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 649 | int size; |
| 650 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 651 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 652 | |
| 653 | if (stat->hash) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 654 | /* If the profile is already created, simply reset it */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 655 | ftrace_profile_reset(stat); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * We are profiling all functions, but usually only a few thousand |
| 661 | * functions are hit. We'll make a hash of 1024 items. |
| 662 | */ |
| 663 | size = FTRACE_PROFILE_HASH_SIZE; |
| 664 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 665 | stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 666 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 667 | if (!stat->hash) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 668 | return -ENOMEM; |
| 669 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 670 | /* Preallocate the function profiling pages */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 671 | if (ftrace_profile_pages_init(stat) < 0) { |
| 672 | kfree(stat->hash); |
| 673 | stat->hash = NULL; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 674 | return -ENOMEM; |
| 675 | } |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 680 | static int ftrace_profile_init(void) |
| 681 | { |
| 682 | int cpu; |
| 683 | int ret = 0; |
| 684 | |
Miao Xie | c4602c1 | 2013-12-16 15:20:01 +0800 | [diff] [blame] | 685 | for_each_possible_cpu(cpu) { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 686 | ret = ftrace_profile_init_cpu(cpu); |
| 687 | if (ret) |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | return ret; |
| 692 | } |
| 693 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 694 | /* interrupts must be disabled */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 695 | static struct ftrace_profile * |
| 696 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 697 | { |
| 698 | struct ftrace_profile *rec; |
| 699 | struct hlist_head *hhd; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 700 | unsigned long key; |
| 701 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 702 | key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 703 | hhd = &stat->hash[key]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 704 | |
| 705 | if (hlist_empty(hhd)) |
| 706 | return NULL; |
| 707 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 708 | hlist_for_each_entry_rcu_notrace(rec, hhd, node) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 709 | if (rec->ip == ip) |
| 710 | return rec; |
| 711 | } |
| 712 | |
| 713 | return NULL; |
| 714 | } |
| 715 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 716 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
| 717 | struct ftrace_profile *rec) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 718 | { |
| 719 | unsigned long key; |
| 720 | |
Namhyung Kim | 20079eb | 2013-04-10 08:55:50 +0900 | [diff] [blame] | 721 | key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 722 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 723 | } |
| 724 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 725 | /* |
| 726 | * The memory is already allocated, this simply finds a new record to use. |
| 727 | */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 728 | static struct ftrace_profile * |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 729 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 730 | { |
| 731 | struct ftrace_profile *rec = NULL; |
| 732 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 733 | /* prevent recursion (from NMIs) */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 734 | if (atomic_inc_return(&stat->disabled) != 1) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 735 | goto out; |
| 736 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 737 | /* |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 738 | * Try to find the function again since an NMI |
| 739 | * could have added it |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 740 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 741 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 742 | if (rec) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 743 | goto out; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 744 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 745 | if (stat->pages->index == PROFILES_PER_PAGE) { |
| 746 | if (!stat->pages->next) |
| 747 | goto out; |
| 748 | stat->pages = stat->pages->next; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 749 | } |
| 750 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 751 | rec = &stat->pages->records[stat->pages->index++]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 752 | rec->ip = ip; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 753 | ftrace_add_profile(stat, rec); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 754 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 755 | out: |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 756 | atomic_dec(&stat->disabled); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 757 | |
| 758 | return rec; |
| 759 | } |
| 760 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 761 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 762 | function_profile_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 763 | struct ftrace_ops *ops, struct pt_regs *regs) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 764 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 765 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 766 | struct ftrace_profile *rec; |
| 767 | unsigned long flags; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 768 | |
| 769 | if (!ftrace_profile_enabled) |
| 770 | return; |
| 771 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 772 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 773 | |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 774 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 775 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 776 | goto out; |
| 777 | |
| 778 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 779 | if (!rec) { |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 780 | rec = ftrace_profile_alloc(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 781 | if (!rec) |
| 782 | goto out; |
| 783 | } |
| 784 | |
| 785 | rec->counter++; |
| 786 | out: |
| 787 | local_irq_restore(flags); |
| 788 | } |
| 789 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 790 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt (VMware) | e73e679 | 2018-11-15 12:35:13 -0500 | [diff] [blame] | 791 | static bool fgraph_graph_time = true; |
| 792 | |
| 793 | void ftrace_graph_graph_time_control(bool enable) |
| 794 | { |
| 795 | fgraph_graph_time = enable; |
| 796 | } |
| 797 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 798 | static int profile_graph_entry(struct ftrace_graph_ent *trace) |
| 799 | { |
Steven Rostedt (VMware) | b0e21a6 | 2018-11-19 20:54:08 -0500 | [diff] [blame] | 800 | struct ftrace_ret_stack *ret_stack; |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 801 | |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 802 | function_profile_call(trace->func, 0, NULL, NULL); |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 803 | |
Steven Rostedt (VMware) | a8f0f9e | 2017-08-17 16:37:25 -0400 | [diff] [blame] | 804 | /* If function graph is shutting down, ret_stack can be NULL */ |
| 805 | if (!current->ret_stack) |
| 806 | return 0; |
| 807 | |
Steven Rostedt (VMware) | b0e21a6 | 2018-11-19 20:54:08 -0500 | [diff] [blame] | 808 | ret_stack = ftrace_graph_get_ret_stack(current, 0); |
| 809 | if (ret_stack) |
| 810 | ret_stack->subtime = 0; |
Namhyung Kim | 8861dd3 | 2016-08-31 11:55:29 +0900 | [diff] [blame] | 811 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 812 | return 1; |
| 813 | } |
| 814 | |
| 815 | static void profile_graph_return(struct ftrace_graph_ret *trace) |
| 816 | { |
Steven Rostedt (VMware) | b0e21a6 | 2018-11-19 20:54:08 -0500 | [diff] [blame] | 817 | struct ftrace_ret_stack *ret_stack; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 818 | struct ftrace_profile_stat *stat; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 819 | unsigned long long calltime; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 820 | struct ftrace_profile *rec; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 821 | unsigned long flags; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 822 | |
| 823 | local_irq_save(flags); |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 824 | stat = this_cpu_ptr(&ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 825 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 826 | goto out; |
| 827 | |
Steven Rostedt | 37e44bc | 2010-04-27 21:04:24 -0400 | [diff] [blame] | 828 | /* If the calltime was zero'd ignore it */ |
| 829 | if (!trace->calltime) |
| 830 | goto out; |
| 831 | |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 832 | calltime = trace->rettime - trace->calltime; |
| 833 | |
Steven Rostedt (Red Hat) | 5557720 | 2015-09-29 19:06:50 -0400 | [diff] [blame] | 834 | if (!fgraph_graph_time) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 835 | |
| 836 | /* Append this call time to the parent time to subtract */ |
Steven Rostedt (VMware) | b0e21a6 | 2018-11-19 20:54:08 -0500 | [diff] [blame] | 837 | ret_stack = ftrace_graph_get_ret_stack(current, 1); |
| 838 | if (ret_stack) |
| 839 | ret_stack->subtime += calltime; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 840 | |
Steven Rostedt (VMware) | b0e21a6 | 2018-11-19 20:54:08 -0500 | [diff] [blame] | 841 | ret_stack = ftrace_graph_get_ret_stack(current, 0); |
| 842 | if (ret_stack && ret_stack->subtime < calltime) |
| 843 | calltime -= ret_stack->subtime; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 844 | else |
| 845 | calltime = 0; |
| 846 | } |
| 847 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 848 | rec = ftrace_find_profiled_func(stat, trace->func); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 849 | if (rec) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 850 | rec->time += calltime; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 851 | rec->time_squared += calltime * calltime; |
| 852 | } |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 853 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 854 | out: |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 855 | local_irq_restore(flags); |
| 856 | } |
| 857 | |
Steven Rostedt (VMware) | 688f708 | 2018-11-15 14:06:47 -0500 | [diff] [blame] | 858 | static struct fgraph_ops fprofiler_ops = { |
| 859 | .entryfunc = &profile_graph_entry, |
| 860 | .retfunc = &profile_graph_return, |
| 861 | }; |
| 862 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 863 | static int register_ftrace_profiler(void) |
| 864 | { |
Steven Rostedt (VMware) | 688f708 | 2018-11-15 14:06:47 -0500 | [diff] [blame] | 865 | return register_ftrace_graph(&fprofiler_ops); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static void unregister_ftrace_profiler(void) |
| 869 | { |
Steven Rostedt (VMware) | 688f708 | 2018-11-15 14:06:47 -0500 | [diff] [blame] | 870 | unregister_ftrace_graph(&fprofiler_ops); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 871 | } |
| 872 | #else |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 873 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 874 | .func = function_profile_call, |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 875 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 876 | INIT_OPS_HASH(ftrace_profile_ops) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 877 | }; |
| 878 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 879 | static int register_ftrace_profiler(void) |
| 880 | { |
| 881 | return register_ftrace_function(&ftrace_profile_ops); |
| 882 | } |
| 883 | |
| 884 | static void unregister_ftrace_profiler(void) |
| 885 | { |
| 886 | unregister_ftrace_function(&ftrace_profile_ops); |
| 887 | } |
| 888 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 889 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 890 | static ssize_t |
| 891 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 892 | size_t cnt, loff_t *ppos) |
| 893 | { |
| 894 | unsigned long val; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 895 | int ret; |
| 896 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 897 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 898 | if (ret) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 899 | return ret; |
| 900 | |
| 901 | val = !!val; |
| 902 | |
| 903 | mutex_lock(&ftrace_profile_lock); |
| 904 | if (ftrace_profile_enabled ^ val) { |
| 905 | if (val) { |
| 906 | ret = ftrace_profile_init(); |
| 907 | if (ret < 0) { |
| 908 | cnt = ret; |
| 909 | goto out; |
| 910 | } |
| 911 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 912 | ret = register_ftrace_profiler(); |
| 913 | if (ret < 0) { |
| 914 | cnt = ret; |
| 915 | goto out; |
| 916 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 917 | ftrace_profile_enabled = 1; |
| 918 | } else { |
| 919 | ftrace_profile_enabled = 0; |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 920 | /* |
| 921 | * unregister_ftrace_profiler calls stop_machine |
| 922 | * so this acts like an synchronize_sched. |
| 923 | */ |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 924 | unregister_ftrace_profiler(); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | out: |
| 928 | mutex_unlock(&ftrace_profile_lock); |
| 929 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 930 | *ppos += cnt; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 931 | |
| 932 | return cnt; |
| 933 | } |
| 934 | |
| 935 | static ssize_t |
| 936 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 937 | size_t cnt, loff_t *ppos) |
| 938 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 939 | char buf[64]; /* big enough to hold a number */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 940 | int r; |
| 941 | |
| 942 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); |
| 943 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 944 | } |
| 945 | |
| 946 | static const struct file_operations ftrace_profile_fops = { |
| 947 | .open = tracing_open_generic, |
| 948 | .read = ftrace_profile_read, |
| 949 | .write = ftrace_profile_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 950 | .llseek = default_llseek, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 951 | }; |
| 952 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 953 | /* used to initialize the real stat files */ |
| 954 | static struct tracer_stat function_stats __initdata = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 955 | .name = "functions", |
| 956 | .stat_start = function_stat_start, |
| 957 | .stat_next = function_stat_next, |
| 958 | .stat_cmp = function_stat_cmp, |
| 959 | .stat_headers = function_stat_headers, |
| 960 | .stat_show = function_stat_show |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 961 | }; |
| 962 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 963 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 964 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 965 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 966 | struct dentry *entry; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 967 | char *name; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 968 | int ret; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 969 | int cpu; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 970 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 971 | for_each_possible_cpu(cpu) { |
| 972 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 973 | |
Geliang Tang | 6363c6b | 2016-03-15 22:12:34 +0800 | [diff] [blame] | 974 | name = kasprintf(GFP_KERNEL, "function%d", cpu); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 975 | if (!name) { |
| 976 | /* |
| 977 | * The files created are permanent, if something happens |
| 978 | * we still do not free memory. |
| 979 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 980 | WARN(1, |
| 981 | "Could not allocate stat file for cpu %d\n", |
| 982 | cpu); |
| 983 | return; |
| 984 | } |
| 985 | stat->stat = function_stats; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 986 | stat->stat.name = name; |
| 987 | ret = register_stat_tracer(&stat->stat); |
| 988 | if (ret) { |
| 989 | WARN(1, |
| 990 | "Could not register function stat for cpu %d\n", |
| 991 | cpu); |
| 992 | kfree(name); |
| 993 | return; |
| 994 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 995 | } |
| 996 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 997 | entry = tracefs_create_file("function_profile_enabled", 0644, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 998 | d_tracer, NULL, &ftrace_profile_fops); |
| 999 | if (!entry) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1000 | pr_warn("Could not create tracefs 'function_profile_enabled' entry\n"); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | #else /* CONFIG_FUNCTION_PROFILER */ |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 1004 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 1005 | { |
| 1006 | } |
| 1007 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 1008 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1009 | #ifdef CONFIG_DYNAMIC_FTRACE |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1010 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1011 | static struct ftrace_ops *removed_ops; |
| 1012 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 1013 | /* |
| 1014 | * Set when doing a global update, like enabling all recs or disabling them. |
| 1015 | * It is not set when just updating a single ftrace_ops. |
| 1016 | */ |
| 1017 | static bool update_all_ops; |
| 1018 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1019 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1020 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1021 | #endif |
| 1022 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1023 | struct ftrace_func_entry { |
| 1024 | struct hlist_node hlist; |
| 1025 | unsigned long ip; |
| 1026 | }; |
| 1027 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 1028 | struct ftrace_func_probe { |
| 1029 | struct ftrace_probe_ops *probe_ops; |
| 1030 | struct ftrace_ops ops; |
| 1031 | struct trace_array *tr; |
| 1032 | struct list_head list; |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 1033 | void *data; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 1034 | int ref; |
| 1035 | }; |
| 1036 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1037 | /* |
| 1038 | * We make these constant because no one should touch them, |
| 1039 | * but they are used as the default "empty hash", to avoid allocating |
| 1040 | * it all the time. These are in a read only section such that if |
| 1041 | * anyone does try to modify it, it will cause an exception. |
| 1042 | */ |
| 1043 | static const struct hlist_head empty_buckets[1]; |
| 1044 | static const struct ftrace_hash empty_hash = { |
| 1045 | .buckets = (struct hlist_head *)empty_buckets, |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1046 | }; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1047 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1048 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 1049 | struct ftrace_ops global_ops = { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1050 | .func = ftrace_stub, |
| 1051 | .local_hash.notrace_hash = EMPTY_HASH, |
| 1052 | .local_hash.filter_hash = EMPTY_HASH, |
| 1053 | INIT_OPS_HASH(global_ops) |
| 1054 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 1055 | FTRACE_OPS_FL_INITIALIZED | |
| 1056 | FTRACE_OPS_FL_PID, |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1057 | }; |
| 1058 | |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1059 | /* |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 1060 | * Used by the stack undwinder to know about dynamic ftrace trampolines. |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1061 | */ |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 1062 | struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr) |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1063 | { |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 1064 | struct ftrace_ops *op = NULL; |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1065 | |
| 1066 | /* |
| 1067 | * Some of the ops may be dynamically allocated, |
| 1068 | * they are freed after a synchronize_sched(). |
| 1069 | */ |
| 1070 | preempt_disable_notrace(); |
| 1071 | |
| 1072 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1073 | /* |
| 1074 | * This is to check for dynamically allocated trampolines. |
| 1075 | * Trampolines that are in kernel text will have |
| 1076 | * core_kernel_text() return true. |
| 1077 | */ |
| 1078 | if (op->trampoline && op->trampoline_size) |
| 1079 | if (addr >= op->trampoline && |
| 1080 | addr < op->trampoline + op->trampoline_size) { |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 1081 | preempt_enable_notrace(); |
| 1082 | return op; |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1083 | } |
| 1084 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1085 | preempt_enable_notrace(); |
| 1086 | |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * This is used by __kernel_text_address() to return true if the |
| 1092 | * address is on a dynamically allocated trampoline that would |
| 1093 | * not return true for either core_kernel_text() or |
| 1094 | * is_module_text_address(). |
| 1095 | */ |
| 1096 | bool is_ftrace_trampoline(unsigned long addr) |
| 1097 | { |
| 1098 | return ftrace_ops_trampoline(addr) != NULL; |
Steven Rostedt (Red Hat) | aec0be2 | 2014-11-18 21:14:11 -0500 | [diff] [blame] | 1099 | } |
| 1100 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1101 | struct ftrace_page { |
| 1102 | struct ftrace_page *next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1103 | struct dyn_ftrace *records; |
Steven Rostedt | 431aa3f | 2009-01-06 12:43:01 -0500 | [diff] [blame] | 1104 | int index; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1105 | int size; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 1106 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1107 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1108 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1109 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* estimate from running different kernels */ |
| 1112 | #define NR_TO_INIT 10000 |
| 1113 | |
| 1114 | static struct ftrace_page *ftrace_pages_start; |
| 1115 | static struct ftrace_page *ftrace_pages; |
| 1116 | |
Steven Rostedt (VMware) | 2b0cce0 | 2017-02-01 12:19:33 -0500 | [diff] [blame] | 1117 | static __always_inline unsigned long |
| 1118 | ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip) |
| 1119 | { |
| 1120 | if (hash->size_bits > 0) |
| 1121 | return hash_long(ip, hash->size_bits); |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1126 | /* Only use this function if ftrace_hash_empty() has already been tested */ |
| 1127 | static __always_inline struct ftrace_func_entry * |
| 1128 | __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1129 | { |
| 1130 | unsigned long key; |
| 1131 | struct ftrace_func_entry *entry; |
| 1132 | struct hlist_head *hhd; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1133 | |
Steven Rostedt (VMware) | 2b0cce0 | 2017-02-01 12:19:33 -0500 | [diff] [blame] | 1134 | key = ftrace_hash_key(hash, ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1135 | hhd = &hash->buckets[key]; |
| 1136 | |
Steven Rostedt | 1bb539c | 2013-05-28 14:38:43 -0400 | [diff] [blame] | 1137 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1138 | if (entry->ip == ip) |
| 1139 | return entry; |
| 1140 | } |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1144 | /** |
| 1145 | * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash |
| 1146 | * @hash: The hash to look at |
| 1147 | * @ip: The instruction pointer to test |
| 1148 | * |
| 1149 | * Search a given @hash to see if a given instruction pointer (@ip) |
| 1150 | * exists in it. |
| 1151 | * |
| 1152 | * Returns the entry that holds the @ip if found. NULL otherwise. |
| 1153 | */ |
| 1154 | struct ftrace_func_entry * |
| 1155 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1156 | { |
| 1157 | if (ftrace_hash_empty(hash)) |
| 1158 | return NULL; |
| 1159 | |
| 1160 | return __ftrace_lookup_ip(hash, ip); |
| 1161 | } |
| 1162 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1163 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1164 | struct ftrace_func_entry *entry) |
| 1165 | { |
| 1166 | struct hlist_head *hhd; |
| 1167 | unsigned long key; |
| 1168 | |
Steven Rostedt (VMware) | 2b0cce0 | 2017-02-01 12:19:33 -0500 | [diff] [blame] | 1169 | key = ftrace_hash_key(hash, entry->ip); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1170 | hhd = &hash->buckets[key]; |
| 1171 | hlist_add_head(&entry->hlist, hhd); |
| 1172 | hash->count++; |
| 1173 | } |
| 1174 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1175 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1176 | { |
| 1177 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1178 | |
| 1179 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1180 | if (!entry) |
| 1181 | return -ENOMEM; |
| 1182 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1183 | entry->ip = ip; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1184 | __add_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | static void |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1190 | free_hash_entry(struct ftrace_hash *hash, |
| 1191 | struct ftrace_func_entry *entry) |
| 1192 | { |
| 1193 | hlist_del(&entry->hlist); |
| 1194 | kfree(entry); |
| 1195 | hash->count--; |
| 1196 | } |
| 1197 | |
| 1198 | static void |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1199 | remove_hash_entry(struct ftrace_hash *hash, |
| 1200 | struct ftrace_func_entry *entry) |
| 1201 | { |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 1202 | hlist_del_rcu(&entry->hlist); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1203 | hash->count--; |
| 1204 | } |
| 1205 | |
| 1206 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1207 | { |
| 1208 | struct hlist_head *hhd; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1209 | struct hlist_node *tn; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1210 | struct ftrace_func_entry *entry; |
| 1211 | int size = 1 << hash->size_bits; |
| 1212 | int i; |
| 1213 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1214 | if (!hash->count) |
| 1215 | return; |
| 1216 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1217 | for (i = 0; i < size; i++) { |
| 1218 | hhd = &hash->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1219 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1220 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1221 | } |
| 1222 | FTRACE_WARN_ON(hash->count); |
| 1223 | } |
| 1224 | |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 1225 | static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod) |
| 1226 | { |
| 1227 | list_del(&ftrace_mod->list); |
| 1228 | kfree(ftrace_mod->module); |
| 1229 | kfree(ftrace_mod->func); |
| 1230 | kfree(ftrace_mod); |
| 1231 | } |
| 1232 | |
| 1233 | static void clear_ftrace_mod_list(struct list_head *head) |
| 1234 | { |
| 1235 | struct ftrace_mod_load *p, *n; |
| 1236 | |
| 1237 | /* stack tracer isn't supported yet */ |
| 1238 | if (!head) |
| 1239 | return; |
| 1240 | |
| 1241 | mutex_lock(&ftrace_lock); |
| 1242 | list_for_each_entry_safe(p, n, head, list) |
| 1243 | free_ftrace_mod(p); |
| 1244 | mutex_unlock(&ftrace_lock); |
| 1245 | } |
| 1246 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1247 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1248 | { |
| 1249 | if (!hash || hash == EMPTY_HASH) |
| 1250 | return; |
| 1251 | ftrace_hash_clear(hash); |
| 1252 | kfree(hash->buckets); |
| 1253 | kfree(hash); |
| 1254 | } |
| 1255 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1256 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1257 | { |
| 1258 | struct ftrace_hash *hash; |
| 1259 | |
| 1260 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1261 | free_ftrace_hash(hash); |
| 1262 | } |
| 1263 | |
| 1264 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1265 | { |
| 1266 | if (!hash || hash == EMPTY_HASH) |
| 1267 | return; |
| 1268 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); |
| 1269 | } |
| 1270 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1271 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1272 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 1273 | ftrace_ops_init(ops); |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1274 | free_ftrace_hash(ops->func_hash->filter_hash); |
| 1275 | free_ftrace_hash(ops->func_hash->notrace_hash); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1278 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1279 | { |
| 1280 | struct ftrace_hash *hash; |
| 1281 | int size; |
| 1282 | |
| 1283 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1284 | if (!hash) |
| 1285 | return NULL; |
| 1286 | |
| 1287 | size = 1 << size_bits; |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1288 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1289 | |
| 1290 | if (!hash->buckets) { |
| 1291 | kfree(hash); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
| 1295 | hash->size_bits = size_bits; |
| 1296 | |
| 1297 | return hash; |
| 1298 | } |
| 1299 | |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 1300 | |
| 1301 | static int ftrace_add_mod(struct trace_array *tr, |
| 1302 | const char *func, const char *module, |
| 1303 | int enable) |
| 1304 | { |
| 1305 | struct ftrace_mod_load *ftrace_mod; |
| 1306 | struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace; |
| 1307 | |
| 1308 | ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL); |
| 1309 | if (!ftrace_mod) |
| 1310 | return -ENOMEM; |
| 1311 | |
| 1312 | ftrace_mod->func = kstrdup(func, GFP_KERNEL); |
| 1313 | ftrace_mod->module = kstrdup(module, GFP_KERNEL); |
| 1314 | ftrace_mod->enable = enable; |
| 1315 | |
| 1316 | if (!ftrace_mod->func || !ftrace_mod->module) |
| 1317 | goto out_free; |
| 1318 | |
| 1319 | list_add(&ftrace_mod->list, mod_head); |
| 1320 | |
| 1321 | return 0; |
| 1322 | |
| 1323 | out_free: |
| 1324 | free_ftrace_mod(ftrace_mod); |
| 1325 | |
| 1326 | return -ENOMEM; |
| 1327 | } |
| 1328 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1329 | static struct ftrace_hash * |
| 1330 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1331 | { |
| 1332 | struct ftrace_func_entry *entry; |
| 1333 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1334 | int size; |
| 1335 | int ret; |
| 1336 | int i; |
| 1337 | |
| 1338 | new_hash = alloc_ftrace_hash(size_bits); |
| 1339 | if (!new_hash) |
| 1340 | return NULL; |
| 1341 | |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 1342 | if (hash) |
| 1343 | new_hash->flags = hash->flags; |
| 1344 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1345 | /* Empty hash? */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1346 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1347 | return new_hash; |
| 1348 | |
| 1349 | size = 1 << hash->size_bits; |
| 1350 | for (i = 0; i < size; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1351 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1352 | ret = add_hash_entry(new_hash, entry->ip); |
| 1353 | if (ret < 0) |
| 1354 | goto free_hash; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1359 | |
| 1360 | return new_hash; |
| 1361 | |
| 1362 | free_hash: |
| 1363 | free_ftrace_hash(new_hash); |
| 1364 | return NULL; |
| 1365 | } |
| 1366 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1367 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1368 | ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1369 | static void |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1370 | ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1371 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1372 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1373 | struct ftrace_hash *new_hash); |
| 1374 | |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1375 | static struct ftrace_hash * |
| 1376 | __ftrace_hash_move(struct ftrace_hash *src) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1377 | { |
| 1378 | struct ftrace_func_entry *entry; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1379 | struct hlist_node *tn; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1380 | struct hlist_head *hhd; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1381 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1382 | int size = src->count; |
| 1383 | int bits = 0; |
| 1384 | int i; |
| 1385 | |
| 1386 | /* |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1387 | * If the new source is empty, just return the empty_hash. |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1388 | */ |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 1389 | if (ftrace_hash_empty(src)) |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1390 | return EMPTY_HASH; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1391 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1392 | /* |
| 1393 | * Make the hash size about 1/2 the # found |
| 1394 | */ |
| 1395 | for (size /= 2; size; size >>= 1) |
| 1396 | bits++; |
| 1397 | |
| 1398 | /* Don't allocate too much */ |
| 1399 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1400 | bits = FTRACE_HASH_MAX_BITS; |
| 1401 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1402 | new_hash = alloc_ftrace_hash(bits); |
| 1403 | if (!new_hash) |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1404 | return NULL; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1405 | |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 1406 | new_hash->flags = src->flags; |
| 1407 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1408 | size = 1 << src->size_bits; |
| 1409 | for (i = 0; i < size; i++) { |
| 1410 | hhd = &src->buckets[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1411 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1412 | remove_hash_entry(src, entry); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1413 | __add_hash_entry(new_hash, entry); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
Namhyung Kim | 3e278c0 | 2017-01-20 11:44:45 +0900 | [diff] [blame] | 1417 | return new_hash; |
| 1418 | } |
| 1419 | |
| 1420 | static int |
| 1421 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1422 | struct ftrace_hash **dst, struct ftrace_hash *src) |
| 1423 | { |
| 1424 | struct ftrace_hash *new_hash; |
| 1425 | int ret; |
| 1426 | |
| 1427 | /* Reject setting notrace hash on IPMODIFY ftrace_ops */ |
| 1428 | if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) |
| 1429 | return -EINVAL; |
| 1430 | |
| 1431 | new_hash = __ftrace_hash_move(src); |
| 1432 | if (!new_hash) |
| 1433 | return -ENOMEM; |
| 1434 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1435 | /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ |
| 1436 | if (enable) { |
| 1437 | /* IPMODIFY should be updated only when filter_hash updating */ |
| 1438 | ret = ftrace_hash_ipmodify_update(ops, new_hash); |
| 1439 | if (ret < 0) { |
| 1440 | free_ftrace_hash(new_hash); |
| 1441 | return ret; |
| 1442 | } |
| 1443 | } |
| 1444 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1445 | /* |
| 1446 | * Remove the current set, update the hash and add |
| 1447 | * them back. |
| 1448 | */ |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1449 | ftrace_hash_rec_disable_modify(ops, enable); |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1450 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1451 | rcu_assign_pointer(*dst, new_hash); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1452 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1453 | ftrace_hash_rec_enable_modify(ops, enable); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1454 | |
Masami Hiramatsu | 5c27c77 | 2014-06-17 11:04:42 +0000 | [diff] [blame] | 1455 | return 0; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1456 | } |
| 1457 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1458 | static bool hash_contains_ip(unsigned long ip, |
| 1459 | struct ftrace_ops_hash *hash) |
| 1460 | { |
| 1461 | /* |
| 1462 | * The function record is a match if it exists in the filter |
| 1463 | * hash and not in the notrace hash. Note, an emty hash is |
| 1464 | * considered a match for the filter hash, but an empty |
| 1465 | * notrace hash is considered not in the notrace hash. |
| 1466 | */ |
| 1467 | return (ftrace_hash_empty(hash->filter_hash) || |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1468 | __ftrace_lookup_ip(hash->filter_hash, ip)) && |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1469 | (ftrace_hash_empty(hash->notrace_hash) || |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 1470 | !__ftrace_lookup_ip(hash->notrace_hash, ip)); |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1471 | } |
| 1472 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1473 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1474 | * Test the hashes for this ops to see if we want to call |
| 1475 | * the ops->func or not. |
| 1476 | * |
| 1477 | * It's a match if the ip is in the ops->filter_hash or |
| 1478 | * the filter_hash does not exist or is empty, |
| 1479 | * AND |
| 1480 | * the ip is not in the ops->notrace_hash. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 1481 | * |
| 1482 | * This needs to be called with preemption disabled as |
| 1483 | * the hashes are freed with call_rcu_sched(). |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1484 | */ |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 1485 | int |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1486 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1487 | { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1488 | struct ftrace_ops_hash hash; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1489 | int ret; |
| 1490 | |
Steven Rostedt (Red Hat) | 195a8af | 2013-07-23 22:06:15 -0400 | [diff] [blame] | 1491 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1492 | /* |
| 1493 | * There's a small race when adding ops that the ftrace handler |
| 1494 | * that wants regs, may be called without them. We can not |
| 1495 | * allow that handler to be called if regs is NULL. |
| 1496 | */ |
| 1497 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) |
| 1498 | return 0; |
| 1499 | #endif |
| 1500 | |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 1501 | rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash); |
| 1502 | rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1503 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1504 | if (hash_contains_ip(ip, &hash)) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1505 | ret = 1; |
| 1506 | else |
| 1507 | ret = 0; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1508 | |
| 1509 | return ret; |
| 1510 | } |
| 1511 | |
| 1512 | /* |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1513 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1514 | * you must use a goto. |
| 1515 | */ |
| 1516 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1517 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1518 | int _____i; \ |
| 1519 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1520 | rec = &pg->records[_____i]; |
| 1521 | |
| 1522 | #define while_for_each_ftrace_rec() \ |
| 1523 | } \ |
| 1524 | } |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 1525 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1526 | |
| 1527 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1528 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1529 | const struct dyn_ftrace *key = a; |
| 1530 | const struct dyn_ftrace *rec = b; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1531 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1532 | if (key->flags < rec->ip) |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1533 | return -1; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1534 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
| 1535 | return 1; |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
Michael Ellerman | 04cf31a | 2016-03-24 22:04:01 +1100 | [diff] [blame] | 1539 | /** |
| 1540 | * ftrace_location_range - return the first address of a traced location |
| 1541 | * if it touches the given ip range |
| 1542 | * @start: start of range to search. |
| 1543 | * @end: end of range to search (inclusive). @end points to the last byte |
| 1544 | * to check. |
| 1545 | * |
| 1546 | * Returns rec->ip if the related ftrace location is a least partly within |
| 1547 | * the given address range. That is, the first address of the instruction |
| 1548 | * that is either a NOP or call to the function tracer. It checks the ftrace |
| 1549 | * internal tables to determine if the address belongs or not. |
| 1550 | */ |
| 1551 | unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1552 | { |
| 1553 | struct ftrace_page *pg; |
| 1554 | struct dyn_ftrace *rec; |
| 1555 | struct dyn_ftrace key; |
| 1556 | |
| 1557 | key.ip = start; |
| 1558 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 1559 | |
| 1560 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1561 | if (end < pg->records[0].ip || |
| 1562 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 1563 | continue; |
| 1564 | rec = bsearch(&key, pg->records, pg->index, |
| 1565 | sizeof(struct dyn_ftrace), |
| 1566 | ftrace_cmp_recs); |
| 1567 | if (rec) |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1568 | return rec->ip; |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1569 | } |
| 1570 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
| 1573 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1574 | /** |
| 1575 | * ftrace_location - return true if the ip giving is a traced location |
| 1576 | * @ip: the instruction pointer to check |
| 1577 | * |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1578 | * 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] | 1579 | * That is, the instruction that is either a NOP or call to |
| 1580 | * the function tracer. It checks the ftrace internal tables to |
| 1581 | * determine if the address belongs or not. |
| 1582 | */ |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1583 | unsigned long ftrace_location(unsigned long ip) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1584 | { |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1585 | return ftrace_location_range(ip, ip); |
| 1586 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1587 | |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1588 | /** |
| 1589 | * ftrace_text_reserved - return true if range contains an ftrace location |
| 1590 | * @start: start of range to search |
| 1591 | * @end: end of range to search (inclusive). @end points to the last byte to check. |
| 1592 | * |
| 1593 | * Returns 1 if @start and @end contains a ftrace location. |
| 1594 | * That is, the instruction that is either a NOP or call to |
| 1595 | * the function tracer. It checks the ftrace internal tables to |
| 1596 | * determine if the address belongs or not. |
| 1597 | */ |
Sasha Levin | d88471c | 2013-01-09 18:09:20 -0500 | [diff] [blame] | 1598 | int ftrace_text_reserved(const void *start, const void *end) |
Steven Rostedt | a650e02 | 2012-04-25 13:48:13 -0400 | [diff] [blame] | 1599 | { |
Steven Rostedt | f0cf973 | 2012-04-25 14:39:54 -0400 | [diff] [blame] | 1600 | unsigned long ret; |
| 1601 | |
| 1602 | ret = ftrace_location_range((unsigned long)start, |
| 1603 | (unsigned long)end); |
| 1604 | |
| 1605 | return (int)!!ret; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1606 | } |
| 1607 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1608 | /* Test if ops registered to this rec needs regs */ |
| 1609 | static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) |
| 1610 | { |
| 1611 | struct ftrace_ops *ops; |
| 1612 | bool keep_regs = false; |
| 1613 | |
| 1614 | for (ops = ftrace_ops_list; |
| 1615 | ops != &ftrace_list_end; ops = ops->next) { |
| 1616 | /* pass rec in as regs to have non-NULL val */ |
| 1617 | if (ftrace_ops_test(ops, rec->ip, rec)) { |
| 1618 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1619 | keep_regs = true; |
| 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | return keep_regs; |
| 1626 | } |
| 1627 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1628 | static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1629 | int filter_hash, |
| 1630 | bool inc) |
| 1631 | { |
| 1632 | struct ftrace_hash *hash; |
| 1633 | struct ftrace_hash *other_hash; |
| 1634 | struct ftrace_page *pg; |
| 1635 | struct dyn_ftrace *rec; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1636 | bool update = false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1637 | int count = 0; |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 1638 | int all = false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1639 | |
| 1640 | /* Only update if the ops has been registered */ |
| 1641 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1642 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1643 | |
| 1644 | /* |
| 1645 | * In the filter_hash case: |
| 1646 | * If the count is zero, we update all records. |
| 1647 | * Otherwise we just update the items in the hash. |
| 1648 | * |
| 1649 | * In the notrace_hash case: |
| 1650 | * We enable the update in the hash. |
| 1651 | * As disabling notrace means enabling the tracing, |
| 1652 | * and enabling notrace means disabling, the inc variable |
| 1653 | * gets inversed. |
| 1654 | */ |
| 1655 | if (filter_hash) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1656 | hash = ops->func_hash->filter_hash; |
| 1657 | other_hash = ops->func_hash->notrace_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1658 | if (ftrace_hash_empty(hash)) |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 1659 | all = true; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1660 | } else { |
| 1661 | inc = !inc; |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 1662 | hash = ops->func_hash->notrace_hash; |
| 1663 | other_hash = ops->func_hash->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1664 | /* |
| 1665 | * If the notrace hash has no items, |
| 1666 | * then there's nothing to do. |
| 1667 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1668 | if (ftrace_hash_empty(hash)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1669 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | do_for_each_ftrace_rec(pg, rec) { |
| 1673 | int in_other_hash = 0; |
| 1674 | int in_hash = 0; |
| 1675 | int match = 0; |
| 1676 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 1677 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1678 | continue; |
| 1679 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1680 | if (all) { |
| 1681 | /* |
| 1682 | * Only the filter_hash affects all records. |
| 1683 | * Update if the record is not in the notrace hash. |
| 1684 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1685 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1686 | match = 1; |
| 1687 | } else { |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1688 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
| 1689 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1690 | |
| 1691 | /* |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1692 | * If filter_hash is set, we want to match all functions |
| 1693 | * that are in the hash but not in the other hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1694 | * |
Steven Rostedt (Red Hat) | 19eab4a | 2014-05-07 15:06:14 -0400 | [diff] [blame] | 1695 | * If filter_hash is not set, then we are decrementing. |
| 1696 | * That means we match anything that is in the hash |
| 1697 | * and also in the other_hash. That is, we need to turn |
| 1698 | * off functions in the other hash because they are disabled |
| 1699 | * by this hash. |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1700 | */ |
| 1701 | if (filter_hash && in_hash && !in_other_hash) |
| 1702 | match = 1; |
| 1703 | else if (!filter_hash && in_hash && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1704 | (in_other_hash || ftrace_hash_empty(other_hash))) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1705 | match = 1; |
| 1706 | } |
| 1707 | if (!match) |
| 1708 | continue; |
| 1709 | |
| 1710 | if (inc) { |
| 1711 | rec->flags++; |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1712 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1713 | return false; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1714 | |
| 1715 | /* |
| 1716 | * If there's only a single callback registered to a |
| 1717 | * function, and the ops has a trampoline registered |
| 1718 | * for it, then we can call it directly. |
| 1719 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1720 | if (ftrace_rec_count(rec) == 1 && ops->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1721 | rec->flags |= FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1722 | else |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1723 | /* |
| 1724 | * If we are adding another function callback |
| 1725 | * to this function, and the previous had a |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 1726 | * custom trampoline in use, then we need to go |
| 1727 | * back to the default trampoline. |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1728 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1729 | rec->flags &= ~FTRACE_FL_TRAMP; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1730 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 1731 | /* |
| 1732 | * If any ops wants regs saved for this function |
| 1733 | * then all ops will get saved regs. |
| 1734 | */ |
| 1735 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 1736 | rec->flags |= FTRACE_FL_REGS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1737 | } else { |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1738 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1739 | return false; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1740 | rec->flags--; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1741 | |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1742 | /* |
| 1743 | * If the rec had REGS enabled and the ops that is |
| 1744 | * being removed had REGS set, then see if there is |
| 1745 | * still any ops for this record that wants regs. |
| 1746 | * If not, we can stop recording them. |
| 1747 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 1748 | if (ftrace_rec_count(rec) > 0 && |
Steven Rostedt (Red Hat) | 4fbb48c | 2014-04-30 22:35:48 -0400 | [diff] [blame] | 1749 | rec->flags & FTRACE_FL_REGS && |
| 1750 | ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1751 | if (!test_rec_ops_needs_regs(rec)) |
| 1752 | rec->flags &= ~FTRACE_FL_REGS; |
| 1753 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1754 | |
| 1755 | /* |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 1756 | * If the rec had TRAMP enabled, then it needs to |
| 1757 | * be cleared. As TRAMP can only be enabled iff |
| 1758 | * there is only a single ops attached to it. |
| 1759 | * In otherwords, always disable it on decrementing. |
| 1760 | * In the future, we may set it if rec count is |
| 1761 | * decremented to one, and the ops that is left |
| 1762 | * has a trampoline. |
| 1763 | */ |
| 1764 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 1765 | |
| 1766 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 1767 | * flags will be cleared in ftrace_check_record() |
| 1768 | * if rec count is zero. |
| 1769 | */ |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1770 | } |
| 1771 | count++; |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1772 | |
| 1773 | /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */ |
| 1774 | update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE; |
| 1775 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1776 | /* Shortcut, if we handled all records, we are done. */ |
| 1777 | if (!all && count == hash->count) |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1778 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1779 | } while_for_each_ftrace_rec(); |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1780 | |
| 1781 | return update; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1782 | } |
| 1783 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1784 | static bool ftrace_hash_rec_disable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1785 | int filter_hash) |
| 1786 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1787 | return __ftrace_hash_rec_update(ops, filter_hash, 0); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1788 | } |
| 1789 | |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1790 | static bool ftrace_hash_rec_enable(struct ftrace_ops *ops, |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1791 | int filter_hash) |
| 1792 | { |
Jiri Olsa | 84b6d3e | 2016-03-16 15:34:32 +0100 | [diff] [blame] | 1793 | return __ftrace_hash_rec_update(ops, filter_hash, 1); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1794 | } |
| 1795 | |
Steven Rostedt (Red Hat) | 8426191 | 2014-08-18 13:21:08 -0400 | [diff] [blame] | 1796 | static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, |
| 1797 | int filter_hash, int inc) |
| 1798 | { |
| 1799 | struct ftrace_ops *op; |
| 1800 | |
| 1801 | __ftrace_hash_rec_update(ops, filter_hash, inc); |
| 1802 | |
| 1803 | if (ops->func_hash != &global_ops.local_hash) |
| 1804 | return; |
| 1805 | |
| 1806 | /* |
| 1807 | * If the ops shares the global_ops hash, then we need to update |
| 1808 | * all ops that are enabled and use this hash. |
| 1809 | */ |
| 1810 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1811 | /* Already done */ |
| 1812 | if (op == ops) |
| 1813 | continue; |
| 1814 | if (op->func_hash == &global_ops.local_hash) |
| 1815 | __ftrace_hash_rec_update(op, filter_hash, inc); |
| 1816 | } while_for_each_ftrace_op(op); |
| 1817 | } |
| 1818 | |
| 1819 | static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, |
| 1820 | int filter_hash) |
| 1821 | { |
| 1822 | ftrace_hash_rec_update_modify(ops, filter_hash, 0); |
| 1823 | } |
| 1824 | |
| 1825 | static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, |
| 1826 | int filter_hash) |
| 1827 | { |
| 1828 | ftrace_hash_rec_update_modify(ops, filter_hash, 1); |
| 1829 | } |
| 1830 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1831 | /* |
| 1832 | * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK |
| 1833 | * or no-needed to update, -EBUSY if it detects a conflict of the flag |
| 1834 | * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. |
| 1835 | * Note that old_hash and new_hash has below meanings |
| 1836 | * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) |
| 1837 | * - If the hash is EMPTY_HASH, it hits nothing |
| 1838 | * - Anything else hits the recs which match the hash entries. |
| 1839 | */ |
| 1840 | static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, |
| 1841 | struct ftrace_hash *old_hash, |
| 1842 | struct ftrace_hash *new_hash) |
| 1843 | { |
| 1844 | struct ftrace_page *pg; |
| 1845 | struct dyn_ftrace *rec, *end = NULL; |
| 1846 | int in_old, in_new; |
| 1847 | |
| 1848 | /* Only update if the ops has been registered */ |
| 1849 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1850 | return 0; |
| 1851 | |
| 1852 | if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 1853 | return 0; |
| 1854 | |
| 1855 | /* |
| 1856 | * Since the IPMODIFY is a very address sensitive action, we do not |
| 1857 | * allow ftrace_ops to set all functions to new hash. |
| 1858 | */ |
| 1859 | if (!new_hash || !old_hash) |
| 1860 | return -EINVAL; |
| 1861 | |
| 1862 | /* Update rec->flags */ |
| 1863 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1864 | |
| 1865 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1866 | continue; |
| 1867 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1868 | /* We need to update only differences of filter_hash */ |
| 1869 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1870 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1871 | if (in_old == in_new) |
| 1872 | continue; |
| 1873 | |
| 1874 | if (in_new) { |
| 1875 | /* New entries must ensure no others are using it */ |
| 1876 | if (rec->flags & FTRACE_FL_IPMODIFY) |
| 1877 | goto rollback; |
| 1878 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1879 | } else /* Removed entry */ |
| 1880 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1881 | } while_for_each_ftrace_rec(); |
| 1882 | |
| 1883 | return 0; |
| 1884 | |
| 1885 | rollback: |
| 1886 | end = rec; |
| 1887 | |
| 1888 | /* Roll back what we did above */ |
| 1889 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 1890 | |
| 1891 | if (rec->flags & FTRACE_FL_DISABLED) |
| 1892 | continue; |
| 1893 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 1894 | if (rec == end) |
| 1895 | goto err_out; |
| 1896 | |
| 1897 | in_old = !!ftrace_lookup_ip(old_hash, rec->ip); |
| 1898 | in_new = !!ftrace_lookup_ip(new_hash, rec->ip); |
| 1899 | if (in_old == in_new) |
| 1900 | continue; |
| 1901 | |
| 1902 | if (in_new) |
| 1903 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 1904 | else |
| 1905 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 1906 | } while_for_each_ftrace_rec(); |
| 1907 | |
| 1908 | err_out: |
| 1909 | return -EBUSY; |
| 1910 | } |
| 1911 | |
| 1912 | static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) |
| 1913 | { |
| 1914 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1915 | |
| 1916 | if (ftrace_hash_empty(hash)) |
| 1917 | hash = NULL; |
| 1918 | |
| 1919 | return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash); |
| 1920 | } |
| 1921 | |
| 1922 | /* Disabling always succeeds */ |
| 1923 | static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) |
| 1924 | { |
| 1925 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 1926 | |
| 1927 | if (ftrace_hash_empty(hash)) |
| 1928 | hash = NULL; |
| 1929 | |
| 1930 | __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH); |
| 1931 | } |
| 1932 | |
| 1933 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1934 | struct ftrace_hash *new_hash) |
| 1935 | { |
| 1936 | struct ftrace_hash *old_hash = ops->func_hash->filter_hash; |
| 1937 | |
| 1938 | if (ftrace_hash_empty(old_hash)) |
| 1939 | old_hash = NULL; |
| 1940 | |
| 1941 | if (ftrace_hash_empty(new_hash)) |
| 1942 | new_hash = NULL; |
| 1943 | |
| 1944 | return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash); |
| 1945 | } |
| 1946 | |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1947 | static void print_ip_ins(const char *fmt, const unsigned char *p) |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 1948 | { |
| 1949 | int i; |
| 1950 | |
| 1951 | printk(KERN_CONT "%s", fmt); |
| 1952 | |
| 1953 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 1954 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 1955 | } |
| 1956 | |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1957 | static struct ftrace_ops * |
| 1958 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 1959 | static struct ftrace_ops * |
| 1960 | 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] | 1961 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1962 | enum ftrace_bug_type ftrace_bug_type; |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 1963 | const void *ftrace_expected; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 1964 | |
| 1965 | static void print_bug_type(void) |
| 1966 | { |
| 1967 | switch (ftrace_bug_type) { |
| 1968 | case FTRACE_BUG_UNKNOWN: |
| 1969 | break; |
| 1970 | case FTRACE_BUG_INIT: |
| 1971 | pr_info("Initializing ftrace call sites\n"); |
| 1972 | break; |
| 1973 | case FTRACE_BUG_NOP: |
| 1974 | pr_info("Setting ftrace call site to NOP\n"); |
| 1975 | break; |
| 1976 | case FTRACE_BUG_CALL: |
| 1977 | pr_info("Setting ftrace call site to call ftrace function\n"); |
| 1978 | break; |
| 1979 | case FTRACE_BUG_UPDATE: |
| 1980 | pr_info("Updating ftrace call site to call a different ftrace function\n"); |
| 1981 | break; |
| 1982 | } |
| 1983 | } |
| 1984 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1985 | /** |
| 1986 | * ftrace_bug - report and shutdown function tracer |
| 1987 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1988 | * @rec: The record that failed |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1989 | * |
| 1990 | * The arch code that enables or disables the function tracing |
| 1991 | * can call ftrace_bug() when it has detected a problem in |
| 1992 | * modifying the code. @failed should be one of either: |
| 1993 | * EFAULT - if the problem happens on reading the @ip address |
| 1994 | * EINVAL - if what is read at @ip is not what was expected |
| 1995 | * EPERM - if the problem happens on writting to the @ip address |
| 1996 | */ |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1997 | void ftrace_bug(int failed, struct dyn_ftrace *rec) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1998 | { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 1999 | unsigned long ip = rec ? rec->ip : 0; |
| 2000 | |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2001 | switch (failed) { |
| 2002 | case -EFAULT: |
| 2003 | FTRACE_WARN_ON_ONCE(1); |
| 2004 | pr_info("ftrace faulted on modifying "); |
| 2005 | print_ip_sym(ip); |
| 2006 | break; |
| 2007 | case -EINVAL: |
| 2008 | FTRACE_WARN_ON_ONCE(1); |
| 2009 | pr_info("ftrace failed to modify "); |
| 2010 | print_ip_sym(ip); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2011 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2012 | pr_cont("\n"); |
Steven Rostedt (Red Hat) | b05086c | 2015-11-25 14:13:11 -0500 | [diff] [blame] | 2013 | if (ftrace_expected) { |
| 2014 | print_ip_ins(" expected: ", ftrace_expected); |
| 2015 | pr_cont("\n"); |
| 2016 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2017 | break; |
| 2018 | case -EPERM: |
| 2019 | FTRACE_WARN_ON_ONCE(1); |
| 2020 | pr_info("ftrace faulted on writing "); |
| 2021 | print_ip_sym(ip); |
| 2022 | break; |
| 2023 | default: |
| 2024 | FTRACE_WARN_ON_ONCE(1); |
| 2025 | pr_info("ftrace faulted on unknown error "); |
| 2026 | print_ip_sym(ip); |
| 2027 | } |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2028 | print_bug_type(); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2029 | if (rec) { |
| 2030 | struct ftrace_ops *ops = NULL; |
| 2031 | |
| 2032 | pr_info("ftrace record flags: %lx\n", rec->flags); |
| 2033 | pr_cont(" (%ld)%s", ftrace_rec_count(rec), |
| 2034 | rec->flags & FTRACE_FL_REGS ? " R" : " "); |
| 2035 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2036 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2037 | if (ops) { |
| 2038 | do { |
| 2039 | pr_cont("\ttramp: %pS (%pS)", |
| 2040 | (void *)ops->trampoline, |
| 2041 | (void *)ops->func); |
| 2042 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 2043 | } while (ops); |
| 2044 | } else |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2045 | pr_cont("\ttramp: ERROR!"); |
| 2046 | |
| 2047 | } |
| 2048 | ip = ftrace_get_addr_curr(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2049 | pr_cont("\n expected tramp: %lx\n", ip); |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2050 | } |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2051 | } |
| 2052 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2053 | 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] | 2054 | { |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2055 | unsigned long flag = 0UL; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 2056 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2057 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2058 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2059 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2060 | return FTRACE_UPDATE_IGNORE; |
| 2061 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2062 | /* |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2063 | * If we are updating calls: |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2064 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2065 | * If the record has a ref count, then we need to enable it |
| 2066 | * because someone is using it. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2067 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2068 | * Otherwise we make sure its disabled. |
| 2069 | * |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2070 | * If we are disabling calls, then disable all records that |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2071 | * are enabled. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 2072 | */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2073 | if (enable && ftrace_rec_count(rec)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2074 | flag = FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2075 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2076 | /* |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2077 | * If enabling and the REGS flag does not match the REGS_EN, or |
| 2078 | * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore |
| 2079 | * this record. Set flags to fail the compare against ENABLED. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2080 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2081 | if (flag) { |
| 2082 | if (!(rec->flags & FTRACE_FL_REGS) != |
| 2083 | !(rec->flags & FTRACE_FL_REGS_EN)) |
| 2084 | flag |= FTRACE_FL_REGS; |
| 2085 | |
| 2086 | if (!(rec->flags & FTRACE_FL_TRAMP) != |
| 2087 | !(rec->flags & FTRACE_FL_TRAMP_EN)) |
| 2088 | flag |= FTRACE_FL_TRAMP; |
| 2089 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2090 | |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2091 | /* If the state of this record hasn't changed, then do nothing */ |
| 2092 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2093 | return FTRACE_UPDATE_IGNORE; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2094 | |
| 2095 | if (flag) { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2096 | /* Save off if rec is being enabled (for return value) */ |
| 2097 | flag ^= rec->flags & FTRACE_FL_ENABLED; |
| 2098 | |
| 2099 | if (update) { |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2100 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2101 | if (flag & FTRACE_FL_REGS) { |
| 2102 | if (rec->flags & FTRACE_FL_REGS) |
| 2103 | rec->flags |= FTRACE_FL_REGS_EN; |
| 2104 | else |
| 2105 | rec->flags &= ~FTRACE_FL_REGS_EN; |
| 2106 | } |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2107 | if (flag & FTRACE_FL_TRAMP) { |
| 2108 | if (rec->flags & FTRACE_FL_TRAMP) |
| 2109 | rec->flags |= FTRACE_FL_TRAMP_EN; |
| 2110 | else |
| 2111 | rec->flags &= ~FTRACE_FL_TRAMP_EN; |
| 2112 | } |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * If this record is being updated from a nop, then |
| 2117 | * return UPDATE_MAKE_CALL. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2118 | * Otherwise, |
| 2119 | * return UPDATE_MODIFY_CALL to tell the caller to convert |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2120 | * 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] | 2121 | * vice versa, or from a trampoline call. |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2122 | */ |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2123 | if (flag & FTRACE_FL_ENABLED) { |
| 2124 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2125 | return FTRACE_UPDATE_MAKE_CALL; |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2126 | } |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2127 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2128 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt (Red Hat) | f1b2f2b | 2014-05-07 16:09:49 -0400 | [diff] [blame] | 2129 | return FTRACE_UPDATE_MODIFY_CALL; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 2130 | } |
| 2131 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2132 | if (update) { |
| 2133 | /* If there's no more users, clear all flags */ |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 2134 | if (!ftrace_rec_count(rec)) |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2135 | rec->flags = 0; |
| 2136 | else |
Steven Rostedt (Red Hat) | b24d443 | 2015-03-04 23:10:28 -0500 | [diff] [blame] | 2137 | /* |
| 2138 | * Just disable the record, but keep the ops TRAMP |
| 2139 | * and REGS states. The _EN flags must be disabled though. |
| 2140 | */ |
| 2141 | rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN | |
| 2142 | FTRACE_FL_REGS_EN); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2143 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2144 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2145 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2146 | return FTRACE_UPDATE_MAKE_NOP; |
| 2147 | } |
| 2148 | |
| 2149 | /** |
| 2150 | * ftrace_update_record, set a record that now is tracing or not |
| 2151 | * @rec: the record to update |
| 2152 | * @enable: set to 1 if the record is tracing, zero to force disable |
| 2153 | * |
| 2154 | * The records that represent all functions that can be traced need |
| 2155 | * to be updated when tracing has been enabled. |
| 2156 | */ |
| 2157 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) |
| 2158 | { |
| 2159 | return ftrace_check_record(rec, enable, 1); |
| 2160 | } |
| 2161 | |
| 2162 | /** |
| 2163 | * ftrace_test_record, check if the record has been enabled or not |
| 2164 | * @rec: the record to test |
| 2165 | * @enable: set to 1 to check if enabled, 0 if it is disabled |
| 2166 | * |
| 2167 | * The arch code may need to test if a record is already set to |
| 2168 | * tracing to determine how to modify the function code that it |
| 2169 | * represents. |
| 2170 | */ |
| 2171 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) |
| 2172 | { |
| 2173 | return ftrace_check_record(rec, enable, 0); |
| 2174 | } |
| 2175 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2176 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2177 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) |
| 2178 | { |
| 2179 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2180 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2181 | |
| 2182 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2183 | |
| 2184 | if (!op->trampoline) |
| 2185 | continue; |
| 2186 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2187 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 2188 | return op; |
| 2189 | } while_for_each_ftrace_op(op); |
| 2190 | |
| 2191 | return NULL; |
| 2192 | } |
| 2193 | |
| 2194 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 2195 | ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, |
| 2196 | struct ftrace_ops *op) |
| 2197 | { |
| 2198 | unsigned long ip = rec->ip; |
| 2199 | |
| 2200 | while_for_each_ftrace_op(op) { |
| 2201 | |
| 2202 | if (!op->trampoline) |
| 2203 | continue; |
| 2204 | |
| 2205 | if (hash_contains_ip(ip, op->func_hash)) |
| 2206 | return op; |
| 2207 | } |
| 2208 | |
| 2209 | return NULL; |
| 2210 | } |
| 2211 | |
| 2212 | static struct ftrace_ops * |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2213 | ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) |
| 2214 | { |
| 2215 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2216 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2217 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2218 | /* |
| 2219 | * Need to check removed ops first. |
| 2220 | * If they are being removed, and this rec has a tramp, |
| 2221 | * and this rec is in the ops list, then it would be the |
| 2222 | * one with the tramp. |
| 2223 | */ |
| 2224 | if (removed_ops) { |
| 2225 | if (hash_contains_ip(ip, &removed_ops->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2226 | return removed_ops; |
| 2227 | } |
| 2228 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2229 | /* |
| 2230 | * Need to find the current trampoline for a rec. |
| 2231 | * Now, a trampoline is only attached to a rec if there |
| 2232 | * was a single 'ops' attached to it. But this can be called |
| 2233 | * when we are adding another op to the rec or removing the |
| 2234 | * current one. Thus, if the op is being added, we can |
| 2235 | * 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] | 2236 | * yet. |
| 2237 | * |
| 2238 | * If an ops is being modified (hooking to different functions) |
| 2239 | * then we don't care about the new functions that are being |
| 2240 | * added, just the old ones (that are probably being removed). |
| 2241 | * |
| 2242 | * If we are adding an ops to a function that already is using |
| 2243 | * a trampoline, it needs to be removed (trampolines are only |
| 2244 | * for single ops connected), then an ops that is not being |
| 2245 | * modified also needs to be checked. |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2246 | */ |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2247 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2248 | |
| 2249 | if (!op->trampoline) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2250 | continue; |
| 2251 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2252 | /* |
| 2253 | * If the ops is being added, it hasn't gotten to |
| 2254 | * the point to be removed from this tree yet. |
| 2255 | */ |
| 2256 | if (op->flags & FTRACE_OPS_FL_ADDING) |
| 2257 | continue; |
| 2258 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2259 | |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2260 | /* |
| 2261 | * If the ops is being modified and is in the old |
| 2262 | * hash, then it is probably being removed from this |
| 2263 | * function. |
| 2264 | */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2265 | if ((op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2266 | hash_contains_ip(ip, &op->old_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2267 | return op; |
Steven Rostedt (Red Hat) | 4fc4090 | 2014-10-24 14:48:35 -0400 | [diff] [blame] | 2268 | /* |
| 2269 | * If the ops is not being added or modified, and it's |
| 2270 | * in its normal filter hash, then this must be the one |
| 2271 | * we want! |
| 2272 | */ |
| 2273 | if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2274 | hash_contains_ip(ip, op->func_hash)) |
| 2275 | return op; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2276 | |
| 2277 | } while_for_each_ftrace_op(op); |
| 2278 | |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | |
| 2282 | static struct ftrace_ops * |
| 2283 | ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) |
| 2284 | { |
| 2285 | struct ftrace_ops *op; |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2286 | unsigned long ip = rec->ip; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2287 | |
| 2288 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2289 | /* pass rec in as regs to have non-NULL val */ |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2290 | if (hash_contains_ip(ip, op->func_hash)) |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2291 | return op; |
| 2292 | } while_for_each_ftrace_op(op); |
| 2293 | |
| 2294 | return NULL; |
| 2295 | } |
| 2296 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2297 | /** |
| 2298 | * ftrace_get_addr_new - Get the call address to set to |
| 2299 | * @rec: The ftrace record descriptor |
| 2300 | * |
| 2301 | * If the record has the FTRACE_FL_REGS set, that means that it |
| 2302 | * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS |
| 2303 | * is not not set, then it wants to convert to the normal callback. |
| 2304 | * |
| 2305 | * Returns the address of the trampoline to set to |
| 2306 | */ |
| 2307 | unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) |
| 2308 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2309 | struct ftrace_ops *ops; |
| 2310 | |
| 2311 | /* Trampolines take precedence over regs */ |
| 2312 | if (rec->flags & FTRACE_FL_TRAMP) { |
| 2313 | ops = ftrace_find_tramp_ops_new(rec); |
| 2314 | if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { |
Steven Rostedt (Red Hat) | bce0b6c | 2014-08-20 23:57:04 -0400 | [diff] [blame] | 2315 | pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n", |
| 2316 | (void *)rec->ip, (void *)rec->ip, rec->flags); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2317 | /* Ftrace is shutting down, return anything */ |
| 2318 | return (unsigned long)FTRACE_ADDR; |
| 2319 | } |
| 2320 | return ops->trampoline; |
| 2321 | } |
| 2322 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2323 | if (rec->flags & FTRACE_FL_REGS) |
| 2324 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2325 | else |
| 2326 | return (unsigned long)FTRACE_ADDR; |
| 2327 | } |
| 2328 | |
| 2329 | /** |
| 2330 | * ftrace_get_addr_curr - Get the call address that is already there |
| 2331 | * @rec: The ftrace record descriptor |
| 2332 | * |
| 2333 | * The FTRACE_FL_REGS_EN is set when the record already points to |
| 2334 | * a function that saves all the regs. Basically the '_EN' version |
| 2335 | * represents the current state of the function. |
| 2336 | * |
| 2337 | * Returns the address of the trampoline that is currently being called |
| 2338 | */ |
| 2339 | unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) |
| 2340 | { |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2341 | struct ftrace_ops *ops; |
| 2342 | |
| 2343 | /* Trampolines take precedence over regs */ |
| 2344 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2345 | ops = ftrace_find_tramp_ops_curr(rec); |
| 2346 | if (FTRACE_WARN_ON(!ops)) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 2347 | pr_warn("Bad trampoline accounting at: %p (%pS)\n", |
| 2348 | (void *)rec->ip, (void *)rec->ip); |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2349 | /* Ftrace is shutting down, return anything */ |
| 2350 | return (unsigned long)FTRACE_ADDR; |
| 2351 | } |
| 2352 | return ops->trampoline; |
| 2353 | } |
| 2354 | |
Steven Rostedt (Red Hat) | 7413af1 | 2014-05-06 21:34:14 -0400 | [diff] [blame] | 2355 | if (rec->flags & FTRACE_FL_REGS_EN) |
| 2356 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2357 | else |
| 2358 | return (unsigned long)FTRACE_ADDR; |
| 2359 | } |
| 2360 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2361 | static int |
| 2362 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
| 2363 | { |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2364 | unsigned long ftrace_old_addr; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2365 | unsigned long ftrace_addr; |
| 2366 | int ret; |
| 2367 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2368 | ftrace_addr = ftrace_get_addr_new(rec); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2369 | |
Steven Rostedt (Red Hat) | 7c0868e | 2014-05-08 07:01:21 -0400 | [diff] [blame] | 2370 | /* This needs to be done before we call ftrace_update_record */ |
| 2371 | ftrace_old_addr = ftrace_get_addr_curr(rec); |
| 2372 | |
| 2373 | ret = ftrace_update_record(rec, enable); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2374 | |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2375 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2376 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2377 | switch (ret) { |
| 2378 | case FTRACE_UPDATE_IGNORE: |
| 2379 | return 0; |
| 2380 | |
| 2381 | case FTRACE_UPDATE_MAKE_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2382 | ftrace_bug_type = FTRACE_BUG_CALL; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2383 | return ftrace_make_call(rec, ftrace_addr); |
| 2384 | |
| 2385 | case FTRACE_UPDATE_MAKE_NOP: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2386 | ftrace_bug_type = FTRACE_BUG_NOP; |
Steven Rostedt (Red Hat) | 39b5552 | 2014-08-17 20:59:10 -0400 | [diff] [blame] | 2387 | return ftrace_make_nop(NULL, rec, ftrace_old_addr); |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2388 | |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2389 | case FTRACE_UPDATE_MODIFY_CALL: |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2390 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
Steven Rostedt | 08f6fba | 2012-04-30 16:20:23 -0400 | [diff] [blame] | 2391 | return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | return -1; /* unknow ftrace bug */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2395 | } |
| 2396 | |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 2397 | void __weak ftrace_replace_code(int mod_flags) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2398 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2399 | struct dyn_ftrace *rec; |
| 2400 | struct ftrace_page *pg; |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 2401 | int enable = mod_flags & FTRACE_MODIFY_ENABLE_FL; |
| 2402 | int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2403 | int failed; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2404 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2405 | if (unlikely(ftrace_disabled)) |
| 2406 | return; |
| 2407 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2408 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 2409 | |
| 2410 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2411 | continue; |
| 2412 | |
Steven Rostedt | e4f5d54 | 2012-04-27 09:13:18 -0400 | [diff] [blame] | 2413 | failed = __ftrace_replace_code(rec, enable); |
Zhaolei | fa9d13c | 2009-03-13 17:16:34 +0800 | [diff] [blame] | 2414 | if (failed) { |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2415 | ftrace_bug(failed, rec); |
Steven Rostedt | 3279ba3 | 2009-10-07 16:57:56 -0400 | [diff] [blame] | 2416 | /* Stop processing */ |
| 2417 | return; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2418 | } |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 2419 | if (schedulable) |
| 2420 | cond_resched(); |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2421 | } while_for_each_ftrace_rec(); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2422 | } |
| 2423 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2424 | struct ftrace_rec_iter { |
| 2425 | struct ftrace_page *pg; |
| 2426 | int index; |
| 2427 | }; |
| 2428 | |
| 2429 | /** |
| 2430 | * ftrace_rec_iter_start, start up iterating over traced functions |
| 2431 | * |
| 2432 | * Returns an iterator handle that is used to iterate over all |
| 2433 | * the records that represent address locations where functions |
| 2434 | * are traced. |
| 2435 | * |
| 2436 | * May return NULL if no records are available. |
| 2437 | */ |
| 2438 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 2439 | { |
| 2440 | /* |
| 2441 | * We only use a single iterator. |
| 2442 | * Protected by the ftrace_lock mutex. |
| 2443 | */ |
| 2444 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 2445 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 2446 | |
| 2447 | iter->pg = ftrace_pages_start; |
| 2448 | iter->index = 0; |
| 2449 | |
| 2450 | /* Could have empty pages */ |
| 2451 | while (iter->pg && !iter->pg->index) |
| 2452 | iter->pg = iter->pg->next; |
| 2453 | |
| 2454 | if (!iter->pg) |
| 2455 | return NULL; |
| 2456 | |
| 2457 | return iter; |
| 2458 | } |
| 2459 | |
| 2460 | /** |
| 2461 | * ftrace_rec_iter_next, get the next record to process. |
| 2462 | * @iter: The handle to the iterator. |
| 2463 | * |
| 2464 | * Returns the next iterator after the given iterator @iter. |
| 2465 | */ |
| 2466 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 2467 | { |
| 2468 | iter->index++; |
| 2469 | |
| 2470 | if (iter->index >= iter->pg->index) { |
| 2471 | iter->pg = iter->pg->next; |
| 2472 | iter->index = 0; |
| 2473 | |
| 2474 | /* Could have empty pages */ |
| 2475 | while (iter->pg && !iter->pg->index) |
| 2476 | iter->pg = iter->pg->next; |
| 2477 | } |
| 2478 | |
| 2479 | if (!iter->pg) |
| 2480 | return NULL; |
| 2481 | |
| 2482 | return iter; |
| 2483 | } |
| 2484 | |
| 2485 | /** |
| 2486 | * ftrace_rec_iter_record, get the record at the iterator location |
| 2487 | * @iter: The current iterator location |
| 2488 | * |
| 2489 | * Returns the record that the current @iter is at. |
| 2490 | */ |
| 2491 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 2492 | { |
| 2493 | return &iter->pg->records[iter->index]; |
| 2494 | } |
| 2495 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2496 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2497 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2498 | { |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2499 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2500 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2501 | if (unlikely(ftrace_disabled)) |
| 2502 | return 0; |
| 2503 | |
Shaohua Li | 25aac9d | 2009-01-09 11:29:40 +0800 | [diff] [blame] | 2504 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 2505 | if (ret) { |
Steven Rostedt (Red Hat) | 02a392a | 2015-11-25 12:50:47 -0500 | [diff] [blame] | 2506 | ftrace_bug_type = FTRACE_BUG_INIT; |
Steven Rostedt (Red Hat) | 4fd3279 | 2014-10-24 17:56:04 -0400 | [diff] [blame] | 2507 | ftrace_bug(ret, rec); |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2508 | return 0; |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2509 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 2510 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2511 | } |
| 2512 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2513 | /* |
| 2514 | * archs can override this function if they must do something |
| 2515 | * before the modifying code is performed. |
| 2516 | */ |
| 2517 | int __weak ftrace_arch_code_modify_prepare(void) |
| 2518 | { |
| 2519 | return 0; |
| 2520 | } |
| 2521 | |
| 2522 | /* |
| 2523 | * archs can override this function if they must do something |
| 2524 | * after the modifying code is performed. |
| 2525 | */ |
| 2526 | int __weak ftrace_arch_code_modify_post_process(void) |
| 2527 | { |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2531 | void ftrace_modify_all_code(int command) |
| 2532 | { |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2533 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 2534 | int mod_flags = 0; |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2535 | int err = 0; |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2536 | |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 2537 | if (command & FTRACE_MAY_SLEEP) |
| 2538 | mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL; |
| 2539 | |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2540 | /* |
| 2541 | * If the ftrace_caller calls a ftrace_ops func directly, |
| 2542 | * we need to make sure that it only traces functions it |
| 2543 | * expects to trace. When doing the switch of functions, |
| 2544 | * we need to update to the ftrace_ops_list_func first |
| 2545 | * before the transition between old and new calls are set, |
| 2546 | * as the ftrace_ops_list_func will check the ops hashes |
| 2547 | * to make sure the ops are having the right functions |
| 2548 | * traced. |
| 2549 | */ |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2550 | if (update) { |
| 2551 | err = ftrace_update_ftrace_func(ftrace_ops_list_func); |
| 2552 | if (FTRACE_WARN_ON(err)) |
| 2553 | return; |
| 2554 | } |
Steven Rostedt (Red Hat) | 59338f7 | 2013-08-31 01:04:07 -0400 | [diff] [blame] | 2555 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2556 | if (command & FTRACE_UPDATE_CALLS) |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 2557 | ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2558 | else if (command & FTRACE_DISABLE_CALLS) |
Steven Rostedt (VMware) | a0572f6 | 2018-12-05 12:48:53 -0500 | [diff] [blame^] | 2559 | ftrace_replace_code(mod_flags); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2560 | |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2561 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
| 2562 | function_trace_op = set_function_trace_op; |
| 2563 | smp_wmb(); |
| 2564 | /* If irqs are disabled, we are in stop machine */ |
| 2565 | if (!irqs_disabled()) |
| 2566 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2567 | err = ftrace_update_ftrace_func(ftrace_trace_function); |
| 2568 | if (FTRACE_WARN_ON(err)) |
| 2569 | return; |
Steven Rostedt (Red Hat) | 405e1d8 | 2013-11-08 14:17:30 -0500 | [diff] [blame] | 2570 | } |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2571 | |
| 2572 | if (command & FTRACE_START_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2573 | err = ftrace_enable_ftrace_graph_caller(); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2574 | else if (command & FTRACE_STOP_FUNC_RET) |
Petr Mladek | cd21067f | 2014-02-24 17:12:21 +0100 | [diff] [blame] | 2575 | err = ftrace_disable_ftrace_graph_caller(); |
| 2576 | FTRACE_WARN_ON(err); |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2577 | } |
| 2578 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2579 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2580 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2581 | int *command = data; |
| 2582 | |
Steven Rostedt | 8ed3e2c | 2012-04-26 14:59:43 -0400 | [diff] [blame] | 2583 | ftrace_modify_all_code(*command); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 2584 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | /** |
| 2589 | * ftrace_run_stop_machine, go back to the stop machine method |
| 2590 | * @command: The command to tell ftrace what to do |
| 2591 | * |
| 2592 | * If an arch needs to fall back to the stop machine method, the |
| 2593 | * it can call this function. |
| 2594 | */ |
| 2595 | void ftrace_run_stop_machine(int command) |
| 2596 | { |
| 2597 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 2598 | } |
| 2599 | |
| 2600 | /** |
| 2601 | * arch_ftrace_update_code, modify the code to trace or not trace |
| 2602 | * @command: The command that needs to be done |
| 2603 | * |
| 2604 | * Archs can override this function if it does not need to |
| 2605 | * run stop_machine() to modify code. |
| 2606 | */ |
| 2607 | void __weak arch_ftrace_update_code(int command) |
| 2608 | { |
| 2609 | ftrace_run_stop_machine(command); |
| 2610 | } |
| 2611 | |
| 2612 | static void ftrace_run_update_code(int command) |
| 2613 | { |
| 2614 | int ret; |
| 2615 | |
| 2616 | ret = ftrace_arch_code_modify_prepare(); |
| 2617 | FTRACE_WARN_ON(ret); |
| 2618 | if (ret) |
| 2619 | return; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 2620 | |
| 2621 | /* |
| 2622 | * By default we use stop_machine() to modify the code. |
| 2623 | * But archs can do what ever they want as long as it |
| 2624 | * is safe. The stop_machine() is the safest, but also |
| 2625 | * produces the most overhead. |
| 2626 | */ |
| 2627 | arch_ftrace_update_code(command); |
| 2628 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 2629 | ret = ftrace_arch_code_modify_post_process(); |
| 2630 | FTRACE_WARN_ON(ret); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2631 | } |
| 2632 | |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2633 | 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] | 2634 | struct ftrace_ops_hash *old_hash) |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2635 | { |
| 2636 | ops->flags |= FTRACE_OPS_FL_MODIFYING; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2637 | ops->old_hash.filter_hash = old_hash->filter_hash; |
| 2638 | ops->old_hash.notrace_hash = old_hash->notrace_hash; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2639 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | 8252ecf | 2014-10-24 14:56:01 -0400 | [diff] [blame] | 2640 | ops->old_hash.filter_hash = NULL; |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 2641 | ops->old_hash.notrace_hash = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2642 | ops->flags &= ~FTRACE_OPS_FL_MODIFYING; |
| 2643 | } |
| 2644 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2645 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2646 | static int ftrace_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2647 | |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2648 | void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) |
| 2649 | { |
| 2650 | } |
| 2651 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2652 | static void ftrace_startup_enable(int command) |
| 2653 | { |
| 2654 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2655 | saved_ftrace_func = ftrace_trace_function; |
| 2656 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2657 | } |
| 2658 | |
| 2659 | if (!command || !ftrace_enabled) |
| 2660 | return; |
| 2661 | |
| 2662 | ftrace_run_update_code(command); |
| 2663 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2664 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2665 | static void ftrace_startup_all(int command) |
| 2666 | { |
| 2667 | update_all_ops = true; |
| 2668 | ftrace_startup_enable(command); |
| 2669 | update_all_ops = false; |
| 2670 | } |
| 2671 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 2672 | int ftrace_startup(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2673 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2674 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2675 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2676 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2677 | return -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2678 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2679 | ret = __register_ftrace_function(ops); |
| 2680 | if (ret) |
| 2681 | return ret; |
| 2682 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2683 | ftrace_start_up++; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2684 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2685 | /* |
| 2686 | * Note that ftrace probes uses this to start up |
| 2687 | * and modify functions it will probe. But we still |
| 2688 | * set the ADDING flag for modification, as probes |
| 2689 | * do not have trampolines. If they add them in the |
| 2690 | * future, then the probes will need to distinguish |
| 2691 | * between adding and updating probes. |
| 2692 | */ |
| 2693 | ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; |
Steven Rostedt (Red Hat) | 66209a5 | 2014-05-06 21:57:49 -0400 | [diff] [blame] | 2694 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2695 | ret = ftrace_hash_ipmodify_enable(ops); |
| 2696 | if (ret < 0) { |
| 2697 | /* Rollback registration process */ |
| 2698 | __unregister_ftrace_function(ops); |
| 2699 | ftrace_start_up--; |
| 2700 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 2701 | return ret; |
| 2702 | } |
| 2703 | |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2704 | if (ftrace_hash_rec_enable(ops, 1)) |
| 2705 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 2706 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 2707 | ftrace_startup_enable(command); |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2708 | |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2709 | ops->flags &= ~FTRACE_OPS_FL_ADDING; |
| 2710 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2711 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2712 | } |
| 2713 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 2714 | int ftrace_shutdown(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2715 | { |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2716 | int ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2717 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2718 | if (unlikely(ftrace_disabled)) |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2719 | return -ENODEV; |
| 2720 | |
| 2721 | ret = __unregister_ftrace_function(ops); |
| 2722 | if (ret) |
| 2723 | return ret; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2724 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2725 | ftrace_start_up--; |
Frederic Weisbecker | 9ea1a15 | 2009-06-20 06:52:21 +0200 | [diff] [blame] | 2726 | /* |
| 2727 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 2728 | * critical but the ftrace_call callers may be never nopped again after |
| 2729 | * further ftrace uses. |
| 2730 | */ |
| 2731 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 2732 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 2733 | /* Disabling ipmodify never fails */ |
| 2734 | ftrace_hash_ipmodify_disable(ops); |
Jiri Olsa | 7f50d06 | 2016-03-16 15:34:33 +0100 | [diff] [blame] | 2735 | |
| 2736 | if (ftrace_hash_rec_disable(ops, 1)) |
| 2737 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2738 | |
Namhyung Kim | a737e6d | 2014-06-12 23:56:12 +0900 | [diff] [blame] | 2739 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2740 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2741 | if (saved_ftrace_func != ftrace_trace_function) { |
| 2742 | saved_ftrace_func = ftrace_trace_function; |
| 2743 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 2744 | } |
| 2745 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2746 | if (!command || !ftrace_enabled) { |
| 2747 | /* |
Steven Rostedt (VMware) | edb096e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2748 | * If these are dynamic or per_cpu ops, they still |
| 2749 | * need their data freed. Since, function tracing is |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2750 | * not currently active, we can just free them |
| 2751 | * without synchronizing all CPUs. |
| 2752 | */ |
Peter Zijlstra | b3a8880 | 2017-10-11 09:45:32 +0200 | [diff] [blame] | 2753 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) |
Steven Rostedt (VMware) | edb096e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2754 | goto free_ops; |
| 2755 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2756 | return 0; |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2757 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2758 | |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2759 | /* |
| 2760 | * If the ops uses a trampoline, then it needs to be |
| 2761 | * tested first on update. |
| 2762 | */ |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2763 | ops->flags |= FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2764 | removed_ops = ops; |
| 2765 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2766 | /* The trampoline logic checks the old hashes */ |
| 2767 | ops->old_hash.filter_hash = ops->func_hash->filter_hash; |
| 2768 | ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; |
| 2769 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2770 | ftrace_run_update_code(command); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2771 | |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2772 | /* |
| 2773 | * If there's no more ops registered with ftrace, run a |
| 2774 | * sanity check to make sure all rec flags are cleared. |
| 2775 | */ |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 2776 | if (rcu_dereference_protected(ftrace_ops_list, |
| 2777 | lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2778 | struct ftrace_page *pg; |
| 2779 | struct dyn_ftrace *rec; |
| 2780 | |
| 2781 | do_for_each_ftrace_rec(pg, rec) { |
Alexei Starovoitov | 977c1f9 | 2016-11-07 15:14:20 -0800 | [diff] [blame] | 2782 | if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED)) |
Steven Rostedt (Red Hat) | 84bde62 | 2014-09-12 14:21:13 -0400 | [diff] [blame] | 2783 | pr_warn(" %pS flags:%lx\n", |
| 2784 | (void *)rec->ip, rec->flags); |
| 2785 | } while_for_each_ftrace_rec(); |
| 2786 | } |
| 2787 | |
Steven Rostedt (Red Hat) | fef5aee | 2014-07-24 12:25:47 -0400 | [diff] [blame] | 2788 | ops->old_hash.filter_hash = NULL; |
| 2789 | ops->old_hash.notrace_hash = NULL; |
| 2790 | |
| 2791 | removed_ops = NULL; |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 2792 | ops->flags &= ~FTRACE_OPS_FL_REMOVING; |
Steven Rostedt (Red Hat) | 79922b8 | 2014-05-06 21:56:17 -0400 | [diff] [blame] | 2793 | |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2794 | /* |
| 2795 | * Dynamic ops may be freed, we must make sure that all |
| 2796 | * callers are done before leaving this function. |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 2797 | * 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] | 2798 | * ops. |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2799 | */ |
Peter Zijlstra | b3a8880 | 2017-10-11 09:45:32 +0200 | [diff] [blame] | 2800 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) { |
Steven Rostedt (VMware) | 0598e4f | 2017-04-06 10:28:12 -0400 | [diff] [blame] | 2801 | /* |
| 2802 | * We need to do a hard force of sched synchronization. |
| 2803 | * This is because we use preempt_disable() to do RCU, but |
| 2804 | * the function tracers can be called where RCU is not watching |
| 2805 | * (like before user_exit()). We can not rely on the RCU |
| 2806 | * infrastructure to do the synchronization, thus we must do it |
| 2807 | * ourselves. |
| 2808 | */ |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2809 | schedule_on_each_cpu(ftrace_sync); |
| 2810 | |
Steven Rostedt (VMware) | 0598e4f | 2017-04-06 10:28:12 -0400 | [diff] [blame] | 2811 | /* |
| 2812 | * When the kernel is preeptive, tasks can be preempted |
| 2813 | * while on a ftrace trampoline. Just scheduling a task on |
| 2814 | * a CPU is not good enough to flush them. Calling |
| 2815 | * synchornize_rcu_tasks() will wait for those tasks to |
| 2816 | * execute and either schedule voluntarily or enter user space. |
| 2817 | */ |
| 2818 | if (IS_ENABLED(CONFIG_PREEMPT)) |
| 2819 | synchronize_rcu_tasks(); |
| 2820 | |
Steven Rostedt (VMware) | edb096e | 2017-09-01 12:18:28 -0400 | [diff] [blame] | 2821 | free_ops: |
Steven Rostedt (Red Hat) | 12cce59 | 2014-07-03 15:48:16 -0400 | [diff] [blame] | 2822 | arch_ftrace_trampoline_free(ops); |
Steven Rostedt (Red Hat) | a4c35ed2 | 2014-01-13 12:56:21 -0500 | [diff] [blame] | 2823 | } |
| 2824 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 2825 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2828 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2829 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2830 | int command; |
| 2831 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2832 | if (unlikely(ftrace_disabled)) |
| 2833 | return; |
| 2834 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2835 | /* Force update next time */ |
| 2836 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2837 | /* ftrace_start_up is true if we want ftrace running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2838 | if (ftrace_start_up) { |
| 2839 | command = FTRACE_UPDATE_CALLS; |
| 2840 | if (ftrace_graph_active) |
| 2841 | command |= FTRACE_START_FUNC_RET; |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 2842 | ftrace_startup_enable(command); |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2843 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2844 | } |
| 2845 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2846 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2847 | { |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2848 | int command; |
| 2849 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2850 | if (unlikely(ftrace_disabled)) |
| 2851 | return; |
| 2852 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2853 | /* ftrace_start_up is true if ftrace is running */ |
Pratyush Anand | 1619dc3 | 2015-03-06 23:58:06 +0530 | [diff] [blame] | 2854 | if (ftrace_start_up) { |
| 2855 | command = FTRACE_DISABLE_CALLS; |
| 2856 | if (ftrace_graph_active) |
| 2857 | command |= FTRACE_STOP_FUNC_RET; |
| 2858 | ftrace_run_update_code(command); |
| 2859 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2860 | } |
| 2861 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2862 | static u64 ftrace_update_time; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2863 | unsigned long ftrace_update_tot_cnt; |
| 2864 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2865 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2866 | { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2867 | /* |
| 2868 | * Filter_hash being empty will default to trace module. |
| 2869 | * But notrace hash requires a test of individual module functions. |
| 2870 | */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2871 | return ftrace_hash_empty(ops->func_hash->filter_hash) && |
| 2872 | ftrace_hash_empty(ops->func_hash->notrace_hash); |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2873 | } |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2874 | |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2875 | /* |
| 2876 | * Check if the current ops references the record. |
| 2877 | * |
| 2878 | * If the ops traces all functions, then it was already accounted for. |
| 2879 | * If the ops does not trace the current record function, skip it. |
| 2880 | * If the ops ignores the function via notrace filter, skip it. |
| 2881 | */ |
| 2882 | static inline bool |
| 2883 | ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 2884 | { |
| 2885 | /* If ops isn't enabled, ignore it */ |
| 2886 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
Gustavo A. R. Silva | 44ec3ec | 2018-08-01 20:00:56 -0500 | [diff] [blame] | 2887 | return false; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2888 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2889 | /* If ops traces all then it includes this function */ |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2890 | if (ops_traces_mod(ops)) |
Gustavo A. R. Silva | 44ec3ec | 2018-08-01 20:00:56 -0500 | [diff] [blame] | 2891 | return true; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2892 | |
| 2893 | /* The function must be in the filter */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2894 | if (!ftrace_hash_empty(ops->func_hash->filter_hash) && |
Steven Rostedt (VMware) | 2b2c279 | 2017-02-01 15:37:07 -0500 | [diff] [blame] | 2895 | !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) |
Gustavo A. R. Silva | 44ec3ec | 2018-08-01 20:00:56 -0500 | [diff] [blame] | 2896 | return false; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2897 | |
| 2898 | /* If in notrace hash, we ignore it too */ |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 2899 | if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) |
Gustavo A. R. Silva | 44ec3ec | 2018-08-01 20:00:56 -0500 | [diff] [blame] | 2900 | return false; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2901 | |
Gustavo A. R. Silva | 44ec3ec | 2018-08-01 20:00:56 -0500 | [diff] [blame] | 2902 | return true; |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2903 | } |
| 2904 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2905 | 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] | 2906 | { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2907 | struct ftrace_page *pg; |
Lai Jiangshan | e94142a | 2009-03-13 17:51:27 +0800 | [diff] [blame] | 2908 | struct dyn_ftrace *p; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2909 | u64 start, stop; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2910 | unsigned long update_cnt = 0; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2911 | unsigned long rec_flags = 0; |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2912 | int i; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2913 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2914 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2915 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2916 | /* |
| 2917 | * When a module is loaded, this function is called to convert |
| 2918 | * the calls to mcount in its text to nops, and also to create |
| 2919 | * an entry in the ftrace data. Now, if ftrace is activated |
| 2920 | * after this call, but before the module sets its text to |
| 2921 | * read-only, the modification of enabling ftrace can fail if |
| 2922 | * the read-only is done while ftrace is converting the calls. |
| 2923 | * To prevent this, the module's records are set as disabled |
| 2924 | * and will be enabled after the call to set the module's text |
| 2925 | * to read-only. |
| 2926 | */ |
| 2927 | if (mod) |
| 2928 | rec_flags |= FTRACE_FL_DISABLED; |
| 2929 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2930 | for (pg = new_pgs; pg; pg = pg->next) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2931 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2932 | for (i = 0; i < pg->index; i++) { |
Steven Rostedt (Red Hat) | 8c4f3c3 | 2013-07-30 00:04:32 -0400 | [diff] [blame] | 2933 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2934 | /* If something went wrong, bail without enabling anything */ |
| 2935 | if (unlikely(ftrace_disabled)) |
| 2936 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2937 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2938 | p = &pg->records[i]; |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 2939 | p->flags = rec_flags; |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 2940 | |
Vasily Gorbik | 2f4df00 | 2018-08-06 15:17:46 +0200 | [diff] [blame] | 2941 | #ifndef CC_USING_NOP_MCOUNT |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2942 | /* |
| 2943 | * Do the initial record conversion from mcount jump |
| 2944 | * to the NOP instructions. |
| 2945 | */ |
| 2946 | if (!ftrace_code_disable(mod, p)) |
| 2947 | break; |
Vasily Gorbik | 2f4df00 | 2018-08-06 15:17:46 +0200 | [diff] [blame] | 2948 | #endif |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2949 | |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2950 | update_cnt++; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2951 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2952 | } |
| 2953 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2954 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2955 | ftrace_update_time = stop - start; |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 2956 | ftrace_update_tot_cnt += update_cnt; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2957 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2958 | return 0; |
| 2959 | } |
| 2960 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2961 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2962 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2963 | int order; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2964 | int cnt; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2965 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2966 | if (WARN_ON(!count)) |
| 2967 | return -EINVAL; |
| 2968 | |
| 2969 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2970 | |
| 2971 | /* |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2972 | * We want to fill as much as possible. No more than a page |
| 2973 | * may be empty. |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2974 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2975 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
| 2976 | order--; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2977 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2978 | again: |
| 2979 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 2980 | |
| 2981 | if (!pg->records) { |
| 2982 | /* if we can't allocate this size, try something smaller */ |
| 2983 | if (!order) |
| 2984 | return -ENOMEM; |
| 2985 | order >>= 1; |
| 2986 | goto again; |
| 2987 | } |
| 2988 | |
| 2989 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 2990 | pg->size = cnt; |
| 2991 | |
| 2992 | if (cnt > count) |
| 2993 | cnt = count; |
| 2994 | |
| 2995 | return cnt; |
| 2996 | } |
| 2997 | |
| 2998 | static struct ftrace_page * |
| 2999 | ftrace_allocate_pages(unsigned long num_to_init) |
| 3000 | { |
| 3001 | struct ftrace_page *start_pg; |
| 3002 | struct ftrace_page *pg; |
| 3003 | int order; |
| 3004 | int cnt; |
| 3005 | |
| 3006 | if (!num_to_init) |
| 3007 | return 0; |
| 3008 | |
| 3009 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3010 | if (!pg) |
| 3011 | return NULL; |
| 3012 | |
| 3013 | /* |
| 3014 | * Try to allocate as much as possible in one continues |
| 3015 | * location that fills in all of the space. We want to |
| 3016 | * waste as little space as possible. |
| 3017 | */ |
| 3018 | for (;;) { |
| 3019 | cnt = ftrace_allocate_records(pg, num_to_init); |
| 3020 | if (cnt < 0) |
| 3021 | goto free_pages; |
| 3022 | |
| 3023 | num_to_init -= cnt; |
| 3024 | if (!num_to_init) |
| 3025 | break; |
| 3026 | |
| 3027 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3028 | if (!pg->next) |
| 3029 | goto free_pages; |
| 3030 | |
| 3031 | pg = pg->next; |
| 3032 | } |
| 3033 | |
| 3034 | return start_pg; |
| 3035 | |
| 3036 | free_pages: |
Namhyung Kim | 1f61be00 | 2014-06-11 17:06:53 +0900 | [diff] [blame] | 3037 | pg = start_pg; |
| 3038 | while (pg) { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3039 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 3040 | free_pages((unsigned long)pg->records, order); |
| 3041 | start_pg = pg->next; |
| 3042 | kfree(pg); |
| 3043 | pg = start_pg; |
| 3044 | } |
| 3045 | pr_info("ftrace: FAILED to allocate memory for functions\n"); |
| 3046 | return NULL; |
| 3047 | } |
| 3048 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3049 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 3050 | |
| 3051 | struct ftrace_iterator { |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3052 | loff_t pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3053 | loff_t func_pos; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3054 | loff_t mod_pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3055 | struct ftrace_page *pg; |
| 3056 | struct dyn_ftrace *func; |
| 3057 | struct ftrace_func_probe *probe; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3058 | struct ftrace_func_entry *probe_entry; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3059 | struct trace_parser parser; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3060 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3061 | struct ftrace_ops *ops; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3062 | struct trace_array *tr; |
| 3063 | struct list_head *mod_list; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3064 | int pidx; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3065 | int idx; |
| 3066 | unsigned flags; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3067 | }; |
| 3068 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3069 | static void * |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3070 | t_probe_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3071 | { |
| 3072 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt (VMware) | d2afd57a | 2017-04-20 11:31:35 -0400 | [diff] [blame] | 3073 | struct trace_array *tr = iter->ops->private; |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 3074 | struct list_head *func_probes; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3075 | struct ftrace_hash *hash; |
| 3076 | struct list_head *next; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3077 | struct hlist_node *hnd = NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3078 | struct hlist_head *hhd; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3079 | int size; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3080 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3081 | (*pos)++; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3082 | iter->pos = *pos; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3083 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 3084 | if (!tr) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3085 | return NULL; |
| 3086 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 3087 | func_probes = &tr->func_probes; |
| 3088 | if (list_empty(func_probes)) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3089 | return NULL; |
| 3090 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3091 | if (!iter->probe) { |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 3092 | next = func_probes->next; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 3093 | iter->probe = list_entry(next, struct ftrace_func_probe, list); |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | if (iter->probe_entry) |
| 3097 | hnd = &iter->probe_entry->hlist; |
| 3098 | |
| 3099 | hash = iter->probe->ops.func_hash->filter_hash; |
| 3100 | size = 1 << hash->size_bits; |
| 3101 | |
| 3102 | retry: |
| 3103 | if (iter->pidx >= size) { |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 3104 | if (iter->probe->list.next == func_probes) |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3105 | return NULL; |
| 3106 | next = iter->probe->list.next; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 3107 | iter->probe = list_entry(next, struct ftrace_func_probe, list); |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3108 | hash = iter->probe->ops.func_hash->filter_hash; |
| 3109 | size = 1 << hash->size_bits; |
| 3110 | iter->pidx = 0; |
| 3111 | } |
| 3112 | |
| 3113 | hhd = &hash->buckets[iter->pidx]; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3114 | |
| 3115 | if (hlist_empty(hhd)) { |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3116 | iter->pidx++; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3117 | hnd = NULL; |
| 3118 | goto retry; |
| 3119 | } |
| 3120 | |
| 3121 | if (!hnd) |
| 3122 | hnd = hhd->first; |
| 3123 | else { |
| 3124 | hnd = hnd->next; |
| 3125 | if (!hnd) { |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3126 | iter->pidx++; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3127 | goto retry; |
| 3128 | } |
| 3129 | } |
| 3130 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3131 | if (WARN_ON_ONCE(!hnd)) |
| 3132 | return NULL; |
| 3133 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3134 | iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3135 | |
| 3136 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3137 | } |
| 3138 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3139 | static void *t_probe_start(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3140 | { |
| 3141 | struct ftrace_iterator *iter = m->private; |
| 3142 | void *p = NULL; |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3143 | loff_t l; |
| 3144 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3145 | if (!(iter->flags & FTRACE_ITER_DO_PROBES)) |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3146 | return NULL; |
| 3147 | |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3148 | if (iter->mod_pos > *pos) |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 3149 | return NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3150 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3151 | iter->probe = NULL; |
| 3152 | iter->probe_entry = NULL; |
| 3153 | iter->pidx = 0; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3154 | for (l = 0; l <= (*pos - iter->mod_pos); ) { |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3155 | p = t_probe_next(m, &l); |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 3156 | if (!p) |
| 3157 | break; |
| 3158 | } |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3159 | if (!p) |
| 3160 | return NULL; |
| 3161 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3162 | /* Only set this if we have an item */ |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3163 | iter->flags |= FTRACE_ITER_PROBE; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3164 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3165 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3166 | } |
| 3167 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3168 | static int |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3169 | t_probe_show(struct seq_file *m, struct ftrace_iterator *iter) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3170 | { |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3171 | struct ftrace_func_entry *probe_entry; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 3172 | struct ftrace_probe_ops *probe_ops; |
| 3173 | struct ftrace_func_probe *probe; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3174 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3175 | probe = iter->probe; |
| 3176 | probe_entry = iter->probe_entry; |
| 3177 | |
| 3178 | if (WARN_ON_ONCE(!probe || !probe_entry)) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3179 | return -EIO; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3180 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 3181 | probe_ops = probe->probe_ops; |
Steven Rostedt | 809dcf2 | 2009-02-16 23:06:01 -0500 | [diff] [blame] | 3182 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 3183 | if (probe_ops->print) |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 3184 | return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3185 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 3186 | seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip, |
| 3187 | (void *)probe_ops->func); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3188 | |
| 3189 | return 0; |
| 3190 | } |
| 3191 | |
| 3192 | static void * |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3193 | t_mod_next(struct seq_file *m, loff_t *pos) |
| 3194 | { |
| 3195 | struct ftrace_iterator *iter = m->private; |
| 3196 | struct trace_array *tr = iter->tr; |
| 3197 | |
| 3198 | (*pos)++; |
| 3199 | iter->pos = *pos; |
| 3200 | |
| 3201 | iter->mod_list = iter->mod_list->next; |
| 3202 | |
| 3203 | if (iter->mod_list == &tr->mod_trace || |
| 3204 | iter->mod_list == &tr->mod_notrace) { |
| 3205 | iter->flags &= ~FTRACE_ITER_MOD; |
| 3206 | return NULL; |
| 3207 | } |
| 3208 | |
| 3209 | iter->mod_pos = *pos; |
| 3210 | |
| 3211 | return iter; |
| 3212 | } |
| 3213 | |
| 3214 | static void *t_mod_start(struct seq_file *m, loff_t *pos) |
| 3215 | { |
| 3216 | struct ftrace_iterator *iter = m->private; |
| 3217 | void *p = NULL; |
| 3218 | loff_t l; |
| 3219 | |
| 3220 | if (iter->func_pos > *pos) |
| 3221 | return NULL; |
| 3222 | |
| 3223 | iter->mod_pos = iter->func_pos; |
| 3224 | |
| 3225 | /* probes are only available if tr is set */ |
| 3226 | if (!iter->tr) |
| 3227 | return NULL; |
| 3228 | |
| 3229 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
| 3230 | p = t_mod_next(m, &l); |
| 3231 | if (!p) |
| 3232 | break; |
| 3233 | } |
| 3234 | if (!p) { |
| 3235 | iter->flags &= ~FTRACE_ITER_MOD; |
| 3236 | return t_probe_start(m, pos); |
| 3237 | } |
| 3238 | |
| 3239 | /* Only set this if we have an item */ |
| 3240 | iter->flags |= FTRACE_ITER_MOD; |
| 3241 | |
| 3242 | return iter; |
| 3243 | } |
| 3244 | |
| 3245 | static int |
| 3246 | t_mod_show(struct seq_file *m, struct ftrace_iterator *iter) |
| 3247 | { |
| 3248 | struct ftrace_mod_load *ftrace_mod; |
| 3249 | struct trace_array *tr = iter->tr; |
| 3250 | |
| 3251 | if (WARN_ON_ONCE(!iter->mod_list) || |
| 3252 | iter->mod_list == &tr->mod_trace || |
| 3253 | iter->mod_list == &tr->mod_notrace) |
| 3254 | return -EIO; |
| 3255 | |
| 3256 | ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list); |
| 3257 | |
| 3258 | if (ftrace_mod->func) |
| 3259 | seq_printf(m, "%s", ftrace_mod->func); |
| 3260 | else |
| 3261 | seq_putc(m, '*'); |
| 3262 | |
| 3263 | seq_printf(m, ":mod:%s\n", ftrace_mod->module); |
| 3264 | |
| 3265 | return 0; |
| 3266 | } |
| 3267 | |
| 3268 | static void * |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3269 | t_func_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3270 | { |
| 3271 | struct ftrace_iterator *iter = m->private; |
| 3272 | struct dyn_ftrace *rec = NULL; |
| 3273 | |
| 3274 | (*pos)++; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3275 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3276 | retry: |
| 3277 | if (iter->idx >= iter->pg->index) { |
| 3278 | if (iter->pg->next) { |
| 3279 | iter->pg = iter->pg->next; |
| 3280 | iter->idx = 0; |
| 3281 | goto retry; |
| 3282 | } |
| 3283 | } else { |
| 3284 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt (VMware) | c20489d | 2017-03-29 14:55:49 -0400 | [diff] [blame] | 3285 | if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && |
| 3286 | !ftrace_lookup_ip(iter->hash, rec->ip)) || |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3287 | |
| 3288 | ((iter->flags & FTRACE_ITER_ENABLED) && |
Steven Rostedt (Red Hat) | 23ea9c4 | 2013-05-09 19:31:48 -0400 | [diff] [blame] | 3289 | !(rec->flags & FTRACE_FL_ENABLED))) { |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3290 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3291 | rec = NULL; |
| 3292 | goto retry; |
| 3293 | } |
| 3294 | } |
| 3295 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3296 | if (!rec) |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3297 | return NULL; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3298 | |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3299 | iter->pos = iter->func_pos = *pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3300 | iter->func = rec; |
| 3301 | |
| 3302 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3303 | } |
| 3304 | |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3305 | static void * |
| 3306 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 3307 | { |
| 3308 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3309 | loff_t l = *pos; /* t_probe_start() must use original pos */ |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3310 | void *ret; |
| 3311 | |
| 3312 | if (unlikely(ftrace_disabled)) |
| 3313 | return NULL; |
| 3314 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3315 | if (iter->flags & FTRACE_ITER_PROBE) |
| 3316 | return t_probe_next(m, pos); |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3317 | |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3318 | if (iter->flags & FTRACE_ITER_MOD) |
| 3319 | return t_mod_next(m, pos); |
| 3320 | |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3321 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3322 | /* next must increment pos, and t_probe_start does not */ |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3323 | (*pos)++; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3324 | return t_mod_start(m, &l); |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | ret = t_func_next(m, pos); |
| 3328 | |
| 3329 | if (!ret) |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3330 | return t_mod_start(m, &l); |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3331 | |
| 3332 | return ret; |
| 3333 | } |
| 3334 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3335 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 3336 | { |
| 3337 | iter->pos = 0; |
| 3338 | iter->func_pos = 0; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3339 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 3343 | { |
| 3344 | struct ftrace_iterator *iter = m->private; |
| 3345 | void *p = NULL; |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3346 | loff_t l; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3347 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3348 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3349 | |
| 3350 | if (unlikely(ftrace_disabled)) |
| 3351 | return NULL; |
| 3352 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3353 | /* |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3354 | * If an lseek was done, then reset and start from beginning. |
| 3355 | */ |
| 3356 | if (*pos < iter->pos) |
| 3357 | reset_iter_read(iter); |
| 3358 | |
| 3359 | /* |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3360 | * For set_ftrace_filter reading, if we have the filter |
| 3361 | * off, we can short cut and just print out that all |
| 3362 | * functions are enabled. |
| 3363 | */ |
Steven Rostedt (VMware) | c20489d | 2017-03-29 14:55:49 -0400 | [diff] [blame] | 3364 | if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && |
| 3365 | ftrace_hash_empty(iter->hash)) { |
Steven Rostedt (VMware) | 43ff926 | 2017-03-30 16:51:43 -0400 | [diff] [blame] | 3366 | iter->func_pos = 1; /* Account for the message */ |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3367 | if (*pos > 0) |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3368 | return t_mod_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3369 | iter->flags |= FTRACE_ITER_PRINTALL; |
Chris Wright | df09162 | 2010-09-09 16:34:59 -0700 | [diff] [blame] | 3370 | /* reset in case of seek/pread */ |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3371 | iter->flags &= ~FTRACE_ITER_PROBE; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3372 | return iter; |
| 3373 | } |
| 3374 | |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3375 | if (iter->flags & FTRACE_ITER_MOD) |
| 3376 | return t_mod_start(m, pos); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3377 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3378 | /* |
| 3379 | * Unfortunately, we need to restart at ftrace_pages_start |
| 3380 | * every time we let go of the ftrace_mutex. This is because |
| 3381 | * those pointers can change without the lock. |
| 3382 | */ |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3383 | iter->pg = ftrace_pages_start; |
| 3384 | iter->idx = 0; |
| 3385 | for (l = 0; l <= *pos; ) { |
Steven Rostedt (VMware) | 5bd8462 | 2017-03-29 22:45:18 -0400 | [diff] [blame] | 3386 | p = t_func_next(m, &l); |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 3387 | if (!p) |
| 3388 | break; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 3389 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 3390 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3391 | if (!p) |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3392 | return t_mod_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3393 | |
| 3394 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | static void t_stop(struct seq_file *m, void *p) |
| 3398 | { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3399 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3400 | } |
| 3401 | |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3402 | void * __weak |
| 3403 | arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 3404 | { |
| 3405 | return NULL; |
| 3406 | } |
| 3407 | |
| 3408 | static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, |
| 3409 | struct dyn_ftrace *rec) |
| 3410 | { |
| 3411 | void *ptr; |
| 3412 | |
| 3413 | ptr = arch_ftrace_trampoline_func(ops, rec); |
| 3414 | if (ptr) |
| 3415 | seq_printf(m, " ->%pS", ptr); |
| 3416 | } |
| 3417 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3418 | static int t_show(struct seq_file *m, void *v) |
| 3419 | { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3420 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3421 | struct dyn_ftrace *rec; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3422 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3423 | if (iter->flags & FTRACE_ITER_PROBE) |
| 3424 | return t_probe_show(m, iter); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 3425 | |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3426 | if (iter->flags & FTRACE_ITER_MOD) |
| 3427 | return t_mod_show(m, iter); |
| 3428 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3429 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3430 | if (iter->flags & FTRACE_ITER_NOTRACE) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3431 | seq_puts(m, "#### no functions disabled ####\n"); |
Namhyung Kim | 8c006cf | 2014-06-13 16:24:06 +0900 | [diff] [blame] | 3432 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3433 | seq_puts(m, "#### all functions enabled ####\n"); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 3434 | return 0; |
| 3435 | } |
| 3436 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 3437 | rec = iter->func; |
| 3438 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3439 | if (!rec) |
| 3440 | return 0; |
| 3441 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3442 | seq_printf(m, "%ps", (void *)rec->ip); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3443 | if (iter->flags & FTRACE_ITER_ENABLED) { |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3444 | struct ftrace_ops *ops; |
Steven Rostedt (Red Hat) | 15d5b02 | 2014-07-03 14:51:36 -0400 | [diff] [blame] | 3445 | |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3446 | seq_printf(m, " (%ld)%s%s", |
Steven Rostedt (Red Hat) | 0376bde | 2014-05-07 13:46:45 -0400 | [diff] [blame] | 3447 | ftrace_rec_count(rec), |
Masami Hiramatsu | f8b8be8 | 2014-11-21 05:25:16 -0500 | [diff] [blame] | 3448 | rec->flags & FTRACE_FL_REGS ? " R" : " ", |
| 3449 | rec->flags & FTRACE_FL_IPMODIFY ? " I" : " "); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3450 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
Steven Rostedt (Red Hat) | 5fecaa0 | 2014-07-24 16:00:31 -0400 | [diff] [blame] | 3451 | ops = ftrace_find_tramp_ops_any(rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3452 | if (ops) { |
| 3453 | do { |
| 3454 | seq_printf(m, "\ttramp: %pS (%pS)", |
| 3455 | (void *)ops->trampoline, |
| 3456 | (void *)ops->func); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3457 | add_trampoline_func(m, ops, rec); |
Steven Rostedt (Red Hat) | 39daa7b | 2015-11-25 15:12:38 -0500 | [diff] [blame] | 3458 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 3459 | } while (ops); |
| 3460 | } else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3461 | seq_puts(m, "\ttramp: ERROR!"); |
Steven Rostedt (Red Hat) | 030f4e1 | 2015-12-01 12:24:45 -0500 | [diff] [blame] | 3462 | } else { |
| 3463 | add_trampoline_func(m, NULL, rec); |
Steven Rostedt (Red Hat) | 9674b2f | 2014-05-09 16:54:59 -0400 | [diff] [blame] | 3464 | } |
| 3465 | } |
| 3466 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3467 | seq_putc(m, '\n'); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3468 | |
| 3469 | return 0; |
| 3470 | } |
| 3471 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3472 | static const struct seq_operations show_ftrace_seq_ops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3473 | .start = t_start, |
| 3474 | .next = t_next, |
| 3475 | .stop = t_stop, |
| 3476 | .show = t_show, |
| 3477 | }; |
| 3478 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3479 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3480 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 3481 | { |
| 3482 | struct ftrace_iterator *iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3483 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3484 | if (unlikely(ftrace_disabled)) |
| 3485 | return -ENODEV; |
| 3486 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3487 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
Steven Rostedt (VMware) | c1bc591 | 2017-03-29 11:38:13 -0400 | [diff] [blame] | 3488 | if (!iter) |
| 3489 | return -ENOMEM; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3490 | |
Steven Rostedt (VMware) | c1bc591 | 2017-03-29 11:38:13 -0400 | [diff] [blame] | 3491 | iter->pg = ftrace_pages_start; |
| 3492 | iter->ops = &global_ops; |
| 3493 | |
| 3494 | return 0; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3495 | } |
| 3496 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3497 | static int |
| 3498 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 3499 | { |
| 3500 | struct ftrace_iterator *iter; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3501 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3502 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
Steven Rostedt (VMware) | c1bc591 | 2017-03-29 11:38:13 -0400 | [diff] [blame] | 3503 | if (!iter) |
| 3504 | return -ENOMEM; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3505 | |
Steven Rostedt (VMware) | c1bc591 | 2017-03-29 11:38:13 -0400 | [diff] [blame] | 3506 | iter->pg = ftrace_pages_start; |
| 3507 | iter->flags = FTRACE_ITER_ENABLED; |
| 3508 | iter->ops = &global_ops; |
| 3509 | |
| 3510 | return 0; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3511 | } |
| 3512 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3513 | /** |
| 3514 | * ftrace_regex_open - initialize function tracer filter files |
| 3515 | * @ops: The ftrace_ops that hold the hash filters |
| 3516 | * @flag: The type of filter to process |
| 3517 | * @inode: The inode, usually passed in to your open routine |
| 3518 | * @file: The file, usually passed in to your open routine |
| 3519 | * |
| 3520 | * ftrace_regex_open() initializes the filter files for the |
| 3521 | * @ops. Depending on @flag it may process the filter hash or |
| 3522 | * the notrace hash of @ops. With this called from the open |
| 3523 | * routine, you can use ftrace_filter_write() for the write |
| 3524 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 3525 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3526 | * tracing_lseek() should be used as the lseek routine, and |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3527 | * release must call ftrace_regex_release(). |
| 3528 | */ |
| 3529 | int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3530 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3531 | struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3532 | { |
| 3533 | struct ftrace_iterator *iter; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3534 | struct ftrace_hash *hash; |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3535 | struct list_head *mod_head; |
| 3536 | struct trace_array *tr = ops->private; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3537 | int ret = 0; |
| 3538 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 3539 | ftrace_ops_init(ops); |
| 3540 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3541 | if (unlikely(ftrace_disabled)) |
| 3542 | return -ENODEV; |
| 3543 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3544 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 3545 | if (!iter) |
| 3546 | return -ENOMEM; |
| 3547 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3548 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
| 3549 | kfree(iter); |
| 3550 | return -ENOMEM; |
| 3551 | } |
| 3552 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3553 | iter->ops = ops; |
| 3554 | iter->flags = flag; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3555 | iter->tr = tr; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3556 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3557 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3558 | |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3559 | if (flag & FTRACE_ITER_NOTRACE) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3560 | hash = ops->func_hash->notrace_hash; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3561 | mod_head = tr ? &tr->mod_notrace : NULL; |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3562 | } else { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3563 | hash = ops->func_hash->filter_hash; |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3564 | mod_head = tr ? &tr->mod_trace : NULL; |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3565 | } |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3566 | |
Steven Rostedt (VMware) | 5985ea8 | 2017-06-23 16:05:11 -0400 | [diff] [blame] | 3567 | iter->mod_list = mod_head; |
| 3568 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3569 | if (file->f_mode & FMODE_WRITE) { |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3570 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 3571 | |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3572 | if (file->f_flags & O_TRUNC) { |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3573 | iter->hash = alloc_ftrace_hash(size_bits); |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3574 | clear_ftrace_mod_list(mod_head); |
| 3575 | } else { |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3576 | iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3577 | } |
Namhyung Kim | ef2fbe1 | 2014-06-11 17:06:54 +0900 | [diff] [blame] | 3578 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3579 | if (!iter->hash) { |
| 3580 | trace_parser_put(&iter->parser); |
| 3581 | kfree(iter); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3582 | ret = -ENOMEM; |
| 3583 | goto out_unlock; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3584 | } |
Steven Rostedt (VMware) | c20489d | 2017-03-29 14:55:49 -0400 | [diff] [blame] | 3585 | } else |
| 3586 | iter->hash = hash; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3587 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3588 | if (file->f_mode & FMODE_READ) { |
| 3589 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3590 | |
| 3591 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 3592 | if (!ret) { |
| 3593 | struct seq_file *m = file->private_data; |
| 3594 | m->private = iter; |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3595 | } else { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3596 | /* Failed */ |
| 3597 | free_ftrace_hash(iter->hash); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3598 | trace_parser_put(&iter->parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3599 | kfree(iter); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 3600 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3601 | } else |
| 3602 | file->private_data = iter; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 3603 | |
| 3604 | out_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 3605 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3606 | |
| 3607 | return ret; |
| 3608 | } |
| 3609 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3610 | static int |
| 3611 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 3612 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3613 | struct ftrace_ops *ops = inode->i_private; |
| 3614 | |
| 3615 | return ftrace_regex_open(ops, |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 3616 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES, |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 3617 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | static int |
| 3621 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 3622 | { |
Steven Rostedt (Red Hat) | e3b3e2e | 2013-11-11 23:07:14 -0500 | [diff] [blame] | 3623 | struct ftrace_ops *ops = inode->i_private; |
| 3624 | |
| 3625 | return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3626 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3629 | /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */ |
| 3630 | struct ftrace_glob { |
| 3631 | char *search; |
| 3632 | unsigned len; |
| 3633 | int type; |
| 3634 | }; |
| 3635 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3636 | /* |
| 3637 | * If symbols in an architecture don't correspond exactly to the user-visible |
| 3638 | * name of what they represent, it is possible to define this function to |
| 3639 | * perform the necessary adjustments. |
| 3640 | */ |
| 3641 | char * __weak arch_ftrace_match_adjust(char *str, const char *search) |
| 3642 | { |
| 3643 | return str; |
| 3644 | } |
| 3645 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3646 | static int ftrace_match(char *str, struct ftrace_glob *g) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3647 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3648 | int matched = 0; |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3649 | int slen; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3650 | |
Thiago Jung Bauermann | 7132e2d | 2016-04-25 18:56:14 -0300 | [diff] [blame] | 3651 | str = arch_ftrace_match_adjust(str, g->search); |
| 3652 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3653 | switch (g->type) { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3654 | case MATCH_FULL: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3655 | if (strcmp(str, g->search) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3656 | matched = 1; |
| 3657 | break; |
| 3658 | case MATCH_FRONT_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3659 | if (strncmp(str, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3660 | matched = 1; |
| 3661 | break; |
| 3662 | case MATCH_MIDDLE_ONLY: |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3663 | if (strstr(str, g->search)) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3664 | matched = 1; |
| 3665 | break; |
| 3666 | case MATCH_END_ONLY: |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 3667 | slen = strlen(str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3668 | if (slen >= g->len && |
| 3669 | memcmp(str + slen - g->len, g->search, g->len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3670 | matched = 1; |
| 3671 | break; |
Masami Hiramatsu | 60f1d5e | 2016-10-05 20:58:15 +0900 | [diff] [blame] | 3672 | case MATCH_GLOB: |
| 3673 | if (glob_match(g->search, str)) |
| 3674 | matched = 1; |
| 3675 | break; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | return matched; |
| 3679 | } |
| 3680 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3681 | static int |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3682 | 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] | 3683 | { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3684 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3685 | int ret = 0; |
| 3686 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3687 | entry = ftrace_lookup_ip(hash, rec->ip); |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3688 | if (clear_filter) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3689 | /* Do nothing if it doesn't exist */ |
| 3690 | if (!entry) |
| 3691 | return 0; |
| 3692 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3693 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3694 | } else { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3695 | /* Do nothing if it exists */ |
| 3696 | if (entry) |
| 3697 | return 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3698 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3699 | ret = add_hash_entry(hash, rec->ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3700 | } |
| 3701 | return ret; |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 3702 | } |
| 3703 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3704 | static int |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3705 | ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g, |
| 3706 | struct ftrace_glob *mod_g, int exclude_mod) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3707 | { |
| 3708 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3709 | char *modname; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3710 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3711 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); |
| 3712 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3713 | if (mod_g) { |
| 3714 | int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0; |
| 3715 | |
| 3716 | /* blank module name to match all modules */ |
| 3717 | if (!mod_g->len) { |
| 3718 | /* blank module globbing: modname xor exclude_mod */ |
Steven Rostedt (VMware) | 77c0edd | 2017-05-03 11:41:44 -0400 | [diff] [blame] | 3719 | if (!exclude_mod != !modname) |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3720 | goto func_match; |
| 3721 | return 0; |
| 3722 | } |
| 3723 | |
Steven Rostedt (VMware) | 77c0edd | 2017-05-03 11:41:44 -0400 | [diff] [blame] | 3724 | /* |
| 3725 | * exclude_mod is set to trace everything but the given |
| 3726 | * module. If it is set and the module matches, then |
| 3727 | * return 0. If it is not set, and the module doesn't match |
| 3728 | * also return 0. Otherwise, check the function to see if |
| 3729 | * that matches. |
| 3730 | */ |
| 3731 | if (!mod_matches == !exclude_mod) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3732 | return 0; |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3733 | func_match: |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3734 | /* blank search means to match all funcs in the mod */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3735 | if (!func_g->len) |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3736 | return 1; |
| 3737 | } |
| 3738 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3739 | return ftrace_match(str, func_g); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3740 | } |
| 3741 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3742 | static int |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3743 | match_records(struct ftrace_hash *hash, char *func, int len, char *mod) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3744 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3745 | struct ftrace_page *pg; |
| 3746 | struct dyn_ftrace *rec; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3747 | struct ftrace_glob func_g = { .type = MATCH_FULL }; |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3748 | struct ftrace_glob mod_g = { .type = MATCH_FULL }; |
| 3749 | struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL; |
| 3750 | int exclude_mod = 0; |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3751 | int found = 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3752 | int ret; |
Dan Carpenter | 2e028c4 | 2017-07-12 10:35:57 +0300 | [diff] [blame] | 3753 | int clear_filter = 0; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3754 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3755 | if (func) { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 3756 | func_g.type = filter_parse_regex(func, len, &func_g.search, |
| 3757 | &clear_filter); |
| 3758 | func_g.len = strlen(func_g.search); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3759 | } |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3760 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3761 | if (mod) { |
| 3762 | mod_g.type = filter_parse_regex(mod, strlen(mod), |
| 3763 | &mod_g.search, &exclude_mod); |
| 3764 | mod_g.len = strlen(mod_g.search); |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 3765 | } |
| 3766 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3767 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3768 | |
| 3769 | if (unlikely(ftrace_disabled)) |
| 3770 | goto out_unlock; |
| 3771 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3772 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 3773 | |
| 3774 | if (rec->flags & FTRACE_FL_DISABLED) |
| 3775 | continue; |
| 3776 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 3777 | if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3778 | ret = enter_record(hash, rec, clear_filter); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3779 | if (ret < 0) { |
| 3780 | found = ret; |
| 3781 | goto out_unlock; |
| 3782 | } |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3783 | found = 1; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3784 | } |
| 3785 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3786 | out_unlock: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3787 | mutex_unlock(&ftrace_lock); |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 3788 | |
| 3789 | return found; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3790 | } |
| 3791 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3792 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3793 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3794 | { |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 3795 | return match_records(hash, buff, len, NULL); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3796 | } |
| 3797 | |
Steven Rostedt (VMware) | e16b35d | 2017-04-04 14:46:56 -0400 | [diff] [blame] | 3798 | static void ftrace_ops_update_code(struct ftrace_ops *ops, |
| 3799 | struct ftrace_ops_hash *old_hash) |
| 3800 | { |
| 3801 | struct ftrace_ops *op; |
| 3802 | |
| 3803 | if (!ftrace_enabled) |
| 3804 | return; |
| 3805 | |
| 3806 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
| 3807 | ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); |
| 3808 | return; |
| 3809 | } |
| 3810 | |
| 3811 | /* |
| 3812 | * If this is the shared global_ops filter, then we need to |
| 3813 | * check if there is another ops that shares it, is enabled. |
| 3814 | * If so, we still need to run the modify code. |
| 3815 | */ |
| 3816 | if (ops->func_hash != &global_ops.local_hash) |
| 3817 | return; |
| 3818 | |
| 3819 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 3820 | if (op->func_hash == &global_ops.local_hash && |
| 3821 | op->flags & FTRACE_OPS_FL_ENABLED) { |
| 3822 | ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash); |
| 3823 | /* Only need to do this once */ |
| 3824 | return; |
| 3825 | } |
| 3826 | } while_for_each_ftrace_op(op); |
| 3827 | } |
| 3828 | |
| 3829 | static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops, |
| 3830 | struct ftrace_hash **orig_hash, |
| 3831 | struct ftrace_hash *hash, |
| 3832 | int enable) |
| 3833 | { |
| 3834 | struct ftrace_ops_hash old_hash_ops; |
| 3835 | struct ftrace_hash *old_hash; |
| 3836 | int ret; |
| 3837 | |
| 3838 | old_hash = *orig_hash; |
| 3839 | old_hash_ops.filter_hash = ops->func_hash->filter_hash; |
| 3840 | old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; |
| 3841 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
| 3842 | if (!ret) { |
| 3843 | ftrace_ops_update_code(ops, &old_hash_ops); |
| 3844 | free_ftrace_hash_rcu(old_hash); |
| 3845 | } |
| 3846 | return ret; |
| 3847 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3848 | |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3849 | static bool module_exists(const char *module) |
| 3850 | { |
| 3851 | /* All modules have the symbol __this_module */ |
| 3852 | const char this_mod[] = "__this_module"; |
Salvatore Mesoraca | 419e9fe | 2018-03-30 10:53:08 +0200 | [diff] [blame] | 3853 | char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2]; |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3854 | unsigned long val; |
| 3855 | int n; |
| 3856 | |
Salvatore Mesoraca | 419e9fe | 2018-03-30 10:53:08 +0200 | [diff] [blame] | 3857 | n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod); |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3858 | |
Salvatore Mesoraca | 419e9fe | 2018-03-30 10:53:08 +0200 | [diff] [blame] | 3859 | if (n > sizeof(modname) - 1) |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3860 | return false; |
| 3861 | |
| 3862 | val = module_kallsyms_lookup_name(modname); |
| 3863 | return val != 0; |
| 3864 | } |
| 3865 | |
| 3866 | static int cache_mod(struct trace_array *tr, |
| 3867 | const char *func, char *module, int enable) |
| 3868 | { |
| 3869 | struct ftrace_mod_load *ftrace_mod, *n; |
| 3870 | struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace; |
| 3871 | int ret; |
| 3872 | |
| 3873 | mutex_lock(&ftrace_lock); |
| 3874 | |
| 3875 | /* We do not cache inverse filters */ |
| 3876 | if (func[0] == '!') { |
| 3877 | func++; |
| 3878 | ret = -EINVAL; |
| 3879 | |
| 3880 | /* Look to remove this hash */ |
| 3881 | list_for_each_entry_safe(ftrace_mod, n, head, list) { |
| 3882 | if (strcmp(ftrace_mod->module, module) != 0) |
| 3883 | continue; |
| 3884 | |
| 3885 | /* no func matches all */ |
Dan Carpenter | 44925df | 2017-07-12 10:33:40 +0300 | [diff] [blame] | 3886 | if (strcmp(func, "*") == 0 || |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 3887 | (ftrace_mod->func && |
| 3888 | strcmp(ftrace_mod->func, func) == 0)) { |
| 3889 | ret = 0; |
| 3890 | free_ftrace_mod(ftrace_mod); |
| 3891 | continue; |
| 3892 | } |
| 3893 | } |
| 3894 | goto out; |
| 3895 | } |
| 3896 | |
| 3897 | ret = -EINVAL; |
| 3898 | /* We only care about modules that have not been loaded yet */ |
| 3899 | if (module_exists(module)) |
| 3900 | goto out; |
| 3901 | |
| 3902 | /* Save this string off, and execute it when the module is loaded */ |
| 3903 | ret = ftrace_add_mod(tr, func, module, enable); |
| 3904 | out: |
| 3905 | mutex_unlock(&ftrace_lock); |
| 3906 | |
| 3907 | return ret; |
| 3908 | } |
| 3909 | |
Steven Rostedt (VMware) | d7fbf8d | 2017-06-26 10:57:21 -0400 | [diff] [blame] | 3910 | static int |
| 3911 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 3912 | int reset, int enable); |
| 3913 | |
Arnd Bergmann | 69449bbd | 2017-07-10 10:44:03 +0200 | [diff] [blame] | 3914 | #ifdef CONFIG_MODULES |
Steven Rostedt (VMware) | d7fbf8d | 2017-06-26 10:57:21 -0400 | [diff] [blame] | 3915 | static void process_mod_list(struct list_head *head, struct ftrace_ops *ops, |
| 3916 | char *mod, bool enable) |
| 3917 | { |
| 3918 | struct ftrace_mod_load *ftrace_mod, *n; |
| 3919 | struct ftrace_hash **orig_hash, *new_hash; |
| 3920 | LIST_HEAD(process_mods); |
| 3921 | char *func; |
| 3922 | int ret; |
| 3923 | |
| 3924 | mutex_lock(&ops->func_hash->regex_lock); |
| 3925 | |
| 3926 | if (enable) |
| 3927 | orig_hash = &ops->func_hash->filter_hash; |
| 3928 | else |
| 3929 | orig_hash = &ops->func_hash->notrace_hash; |
| 3930 | |
| 3931 | new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, |
| 3932 | *orig_hash); |
| 3933 | if (!new_hash) |
Steven Rostedt (VMware) | 3b58a3c | 2017-06-28 09:09:38 -0400 | [diff] [blame] | 3934 | goto out; /* warn? */ |
Steven Rostedt (VMware) | d7fbf8d | 2017-06-26 10:57:21 -0400 | [diff] [blame] | 3935 | |
| 3936 | mutex_lock(&ftrace_lock); |
| 3937 | |
| 3938 | list_for_each_entry_safe(ftrace_mod, n, head, list) { |
| 3939 | |
| 3940 | if (strcmp(ftrace_mod->module, mod) != 0) |
| 3941 | continue; |
| 3942 | |
| 3943 | if (ftrace_mod->func) |
| 3944 | func = kstrdup(ftrace_mod->func, GFP_KERNEL); |
| 3945 | else |
| 3946 | func = kstrdup("*", GFP_KERNEL); |
| 3947 | |
| 3948 | if (!func) /* warn? */ |
| 3949 | continue; |
| 3950 | |
| 3951 | list_del(&ftrace_mod->list); |
| 3952 | list_add(&ftrace_mod->list, &process_mods); |
| 3953 | |
| 3954 | /* Use the newly allocated func, as it may be "*" */ |
| 3955 | kfree(ftrace_mod->func); |
| 3956 | ftrace_mod->func = func; |
| 3957 | } |
| 3958 | |
| 3959 | mutex_unlock(&ftrace_lock); |
| 3960 | |
| 3961 | list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) { |
| 3962 | |
| 3963 | func = ftrace_mod->func; |
| 3964 | |
| 3965 | /* Grabs ftrace_lock, which is why we have this extra step */ |
| 3966 | match_records(new_hash, func, strlen(func), mod); |
| 3967 | free_ftrace_mod(ftrace_mod); |
| 3968 | } |
| 3969 | |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 3970 | if (enable && list_empty(head)) |
| 3971 | new_hash->flags &= ~FTRACE_HASH_FL_MOD; |
| 3972 | |
Steven Rostedt (VMware) | d7fbf8d | 2017-06-26 10:57:21 -0400 | [diff] [blame] | 3973 | mutex_lock(&ftrace_lock); |
| 3974 | |
| 3975 | ret = ftrace_hash_move_and_update_ops(ops, orig_hash, |
| 3976 | new_hash, enable); |
| 3977 | mutex_unlock(&ftrace_lock); |
| 3978 | |
Steven Rostedt (VMware) | 3b58a3c | 2017-06-28 09:09:38 -0400 | [diff] [blame] | 3979 | out: |
Steven Rostedt (VMware) | d7fbf8d | 2017-06-26 10:57:21 -0400 | [diff] [blame] | 3980 | mutex_unlock(&ops->func_hash->regex_lock); |
| 3981 | |
| 3982 | free_ftrace_hash(new_hash); |
| 3983 | } |
| 3984 | |
| 3985 | static void process_cached_mods(const char *mod_name) |
| 3986 | { |
| 3987 | struct trace_array *tr; |
| 3988 | char *mod; |
| 3989 | |
| 3990 | mod = kstrdup(mod_name, GFP_KERNEL); |
| 3991 | if (!mod) |
| 3992 | return; |
| 3993 | |
| 3994 | mutex_lock(&trace_types_lock); |
| 3995 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 3996 | if (!list_empty(&tr->mod_trace)) |
| 3997 | process_mod_list(&tr->mod_trace, tr->ops, mod, true); |
| 3998 | if (!list_empty(&tr->mod_notrace)) |
| 3999 | process_mod_list(&tr->mod_notrace, tr->ops, mod, false); |
| 4000 | } |
| 4001 | mutex_unlock(&trace_types_lock); |
| 4002 | |
| 4003 | kfree(mod); |
| 4004 | } |
Arnd Bergmann | 69449bbd | 2017-07-10 10:44:03 +0200 | [diff] [blame] | 4005 | #endif |
Steven Rostedt (VMware) | d7fbf8d | 2017-06-26 10:57:21 -0400 | [diff] [blame] | 4006 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4007 | /* |
| 4008 | * We register the module command as a template to show others how |
| 4009 | * to register the a command as well. |
| 4010 | */ |
| 4011 | |
| 4012 | static int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 4013 | ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash, |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 4014 | char *func_orig, char *cmd, char *module, int enable) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4015 | { |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 4016 | char *func; |
Dmitry Safonov | 5e3949f | 2015-09-29 19:46:12 +0300 | [diff] [blame] | 4017 | int ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4018 | |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 4019 | /* match_records() modifies func, and we need the original */ |
| 4020 | func = kstrdup(func_orig, GFP_KERNEL); |
| 4021 | if (!func) |
| 4022 | return -ENOMEM; |
| 4023 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4024 | /* |
| 4025 | * cmd == 'mod' because we only registered this func |
| 4026 | * for the 'mod' ftrace_func_command. |
| 4027 | * But if you register one func with multiple commands, |
| 4028 | * you can tell which command was used by the cmd |
| 4029 | * parameter. |
| 4030 | */ |
Dmitry Safonov | f0a3b15 | 2015-09-29 19:46:13 +0300 | [diff] [blame] | 4031 | ret = match_records(hash, func, strlen(func), module); |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 4032 | kfree(func); |
| 4033 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 4034 | if (!ret) |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 4035 | return cache_mod(tr, func_orig, module, enable); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 4036 | if (ret < 0) |
| 4037 | return ret; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 4038 | return 0; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 4042 | .name = "mod", |
| 4043 | .func = ftrace_mod_callback, |
| 4044 | }; |
| 4045 | |
| 4046 | static int __init ftrace_mod_cmd_init(void) |
| 4047 | { |
| 4048 | return register_ftrace_command(&ftrace_mod_cmd); |
| 4049 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 4050 | core_initcall(ftrace_mod_cmd_init); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4051 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 4052 | 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] | 4053 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4054 | { |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4055 | struct ftrace_probe_ops *probe_ops; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4056 | struct ftrace_func_probe *probe; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4057 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4058 | probe = container_of(op, struct ftrace_func_probe, ops); |
| 4059 | probe_ops = probe->probe_ops; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4060 | |
| 4061 | /* |
| 4062 | * Disable preemption for these calls to prevent a RCU grace |
| 4063 | * period. This syncs the hash iteration and freeing of items |
| 4064 | * on the hash. rcu_read_lock is too dangerous here. |
| 4065 | */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 4066 | preempt_disable_notrace(); |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 4067 | probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data); |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 4068 | preempt_enable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4069 | } |
| 4070 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4071 | struct ftrace_func_map { |
| 4072 | struct ftrace_func_entry entry; |
| 4073 | void *data; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4074 | }; |
| 4075 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4076 | struct ftrace_func_mapper { |
| 4077 | struct ftrace_hash hash; |
| 4078 | }; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4079 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4080 | /** |
| 4081 | * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper |
| 4082 | * |
| 4083 | * Returns a ftrace_func_mapper descriptor that can be used to map ips to data. |
| 4084 | */ |
| 4085 | struct ftrace_func_mapper *allocate_ftrace_func_mapper(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4086 | { |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4087 | struct ftrace_hash *hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4088 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4089 | /* |
| 4090 | * The mapper is simply a ftrace_hash, but since the entries |
| 4091 | * in the hash are not ftrace_func_entry type, we define it |
| 4092 | * as a separate structure. |
| 4093 | */ |
| 4094 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4095 | return (struct ftrace_func_mapper *)hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4096 | } |
| 4097 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4098 | /** |
| 4099 | * ftrace_func_mapper_find_ip - Find some data mapped to an ip |
| 4100 | * @mapper: The mapper that has the ip maps |
| 4101 | * @ip: the instruction pointer to find the data for |
| 4102 | * |
| 4103 | * Returns the data mapped to @ip if found otherwise NULL. The return |
| 4104 | * is actually the address of the mapper data pointer. The address is |
| 4105 | * returned for use cases where the data is no bigger than a long, and |
| 4106 | * the user can use the data pointer as its data instead of having to |
| 4107 | * allocate more memory for the reference. |
| 4108 | */ |
| 4109 | void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper, |
| 4110 | unsigned long ip) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4111 | { |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4112 | struct ftrace_func_entry *entry; |
| 4113 | struct ftrace_func_map *map; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4114 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4115 | entry = ftrace_lookup_ip(&mapper->hash, ip); |
| 4116 | if (!entry) |
| 4117 | return NULL; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4118 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4119 | map = (struct ftrace_func_map *)entry; |
| 4120 | return &map->data; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4121 | } |
| 4122 | |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4123 | /** |
| 4124 | * ftrace_func_mapper_add_ip - Map some data to an ip |
| 4125 | * @mapper: The mapper that has the ip maps |
| 4126 | * @ip: The instruction pointer address to map @data to |
| 4127 | * @data: The data to map to @ip |
| 4128 | * |
| 4129 | * Returns 0 on succes otherwise an error. |
| 4130 | */ |
| 4131 | int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper, |
| 4132 | unsigned long ip, void *data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4133 | { |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4134 | struct ftrace_func_entry *entry; |
| 4135 | struct ftrace_func_map *map; |
| 4136 | |
| 4137 | entry = ftrace_lookup_ip(&mapper->hash, ip); |
| 4138 | if (entry) |
| 4139 | return -EBUSY; |
| 4140 | |
| 4141 | map = kmalloc(sizeof(*map), GFP_KERNEL); |
| 4142 | if (!map) |
| 4143 | return -ENOMEM; |
| 4144 | |
| 4145 | map->entry.ip = ip; |
| 4146 | map->data = data; |
| 4147 | |
| 4148 | __add_hash_entry(&mapper->hash, &map->entry); |
| 4149 | |
| 4150 | return 0; |
| 4151 | } |
| 4152 | |
| 4153 | /** |
| 4154 | * ftrace_func_mapper_remove_ip - Remove an ip from the mapping |
| 4155 | * @mapper: The mapper that has the ip maps |
| 4156 | * @ip: The instruction pointer address to remove the data from |
| 4157 | * |
| 4158 | * Returns the data if it is found, otherwise NULL. |
| 4159 | * Note, if the data pointer is used as the data itself, (see |
| 4160 | * ftrace_func_mapper_find_ip(), then the return value may be meaningless, |
| 4161 | * if the data pointer was set to zero. |
| 4162 | */ |
| 4163 | void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper, |
| 4164 | unsigned long ip) |
| 4165 | { |
| 4166 | struct ftrace_func_entry *entry; |
| 4167 | struct ftrace_func_map *map; |
| 4168 | void *data; |
| 4169 | |
| 4170 | entry = ftrace_lookup_ip(&mapper->hash, ip); |
| 4171 | if (!entry) |
| 4172 | return NULL; |
| 4173 | |
| 4174 | map = (struct ftrace_func_map *)entry; |
| 4175 | data = map->data; |
| 4176 | |
| 4177 | remove_hash_entry(&mapper->hash, entry); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4178 | kfree(entry); |
Steven Rostedt (VMware) | 41794f1 | 2017-04-03 20:58:35 -0400 | [diff] [blame] | 4179 | |
| 4180 | return data; |
| 4181 | } |
| 4182 | |
| 4183 | /** |
| 4184 | * free_ftrace_func_mapper - free a mapping of ips and data |
| 4185 | * @mapper: The mapper that has the ip maps |
| 4186 | * @free_func: A function to be called on each data item. |
| 4187 | * |
| 4188 | * This is used to free the function mapper. The @free_func is optional |
| 4189 | * and can be used if the data needs to be freed as well. |
| 4190 | */ |
| 4191 | void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper, |
| 4192 | ftrace_mapper_func free_func) |
| 4193 | { |
| 4194 | struct ftrace_func_entry *entry; |
| 4195 | struct ftrace_func_map *map; |
| 4196 | struct hlist_head *hhd; |
| 4197 | int size = 1 << mapper->hash.size_bits; |
| 4198 | int i; |
| 4199 | |
| 4200 | if (free_func && mapper->hash.count) { |
| 4201 | for (i = 0; i < size; i++) { |
| 4202 | hhd = &mapper->hash.buckets[i]; |
| 4203 | hlist_for_each_entry(entry, hhd, hlist) { |
| 4204 | map = (struct ftrace_func_map *)entry; |
| 4205 | free_func(map); |
| 4206 | } |
| 4207 | } |
| 4208 | } |
| 4209 | free_ftrace_hash(&mapper->hash); |
| 4210 | } |
| 4211 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4212 | static void release_probe(struct ftrace_func_probe *probe) |
| 4213 | { |
| 4214 | struct ftrace_probe_ops *probe_ops; |
| 4215 | |
| 4216 | mutex_lock(&ftrace_lock); |
| 4217 | |
| 4218 | WARN_ON(probe->ref <= 0); |
| 4219 | |
| 4220 | /* Subtract the ref that was used to protect this instance */ |
| 4221 | probe->ref--; |
| 4222 | |
| 4223 | if (!probe->ref) { |
| 4224 | probe_ops = probe->probe_ops; |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 4225 | /* |
| 4226 | * Sending zero as ip tells probe_ops to free |
| 4227 | * the probe->data itself |
| 4228 | */ |
| 4229 | if (probe_ops->free) |
| 4230 | probe_ops->free(probe_ops, probe->tr, 0, probe->data); |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4231 | list_del(&probe->list); |
| 4232 | kfree(probe); |
| 4233 | } |
| 4234 | mutex_unlock(&ftrace_lock); |
| 4235 | } |
| 4236 | |
| 4237 | static void acquire_probe_locked(struct ftrace_func_probe *probe) |
| 4238 | { |
| 4239 | /* |
| 4240 | * Add one ref to keep it from being freed when releasing the |
| 4241 | * ftrace_lock mutex. |
| 4242 | */ |
| 4243 | probe->ref++; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4244 | } |
| 4245 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4246 | int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 4247 | register_ftrace_function_probe(char *glob, struct trace_array *tr, |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4248 | struct ftrace_probe_ops *probe_ops, |
| 4249 | void *data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4250 | { |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4251 | struct ftrace_func_entry *entry; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4252 | struct ftrace_func_probe *probe; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4253 | struct ftrace_hash **orig_hash; |
| 4254 | struct ftrace_hash *old_hash; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4255 | struct ftrace_hash *hash; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4256 | int count = 0; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4257 | int size; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4258 | int ret; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4259 | int i; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4260 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 4261 | if (WARN_ON(!tr)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4262 | return -EINVAL; |
| 4263 | |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4264 | /* We do not support '!' for function probes */ |
| 4265 | if (WARN_ON(glob[0] == '!')) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4266 | return -EINVAL; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4267 | |
Steven Rostedt (Red Hat) | 7485058e | 2015-01-13 14:03:38 -0500 | [diff] [blame] | 4268 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4269 | mutex_lock(&ftrace_lock); |
| 4270 | /* Check if the probe_ops is already registered */ |
| 4271 | list_for_each_entry(probe, &tr->func_probes, list) { |
| 4272 | if (probe->probe_ops == probe_ops) |
| 4273 | break; |
| 4274 | } |
| 4275 | if (&probe->list == &tr->func_probes) { |
| 4276 | probe = kzalloc(sizeof(*probe), GFP_KERNEL); |
| 4277 | if (!probe) { |
| 4278 | mutex_unlock(&ftrace_lock); |
| 4279 | return -ENOMEM; |
| 4280 | } |
| 4281 | probe->probe_ops = probe_ops; |
| 4282 | probe->ops.func = function_trace_probe_call; |
| 4283 | probe->tr = tr; |
| 4284 | ftrace_ops_init(&probe->ops); |
| 4285 | list_add(&probe->list, &tr->func_probes); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4286 | } |
| 4287 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4288 | acquire_probe_locked(probe); |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4289 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4290 | mutex_unlock(&ftrace_lock); |
| 4291 | |
| 4292 | mutex_lock(&probe->ops.func_hash->regex_lock); |
| 4293 | |
| 4294 | orig_hash = &probe->ops.func_hash->filter_hash; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4295 | old_hash = *orig_hash; |
| 4296 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash); |
| 4297 | |
| 4298 | ret = ftrace_match_records(hash, glob, strlen(glob)); |
| 4299 | |
| 4300 | /* Nothing found? */ |
| 4301 | if (!ret) |
| 4302 | ret = -EINVAL; |
| 4303 | |
| 4304 | if (ret < 0) |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 4305 | goto out; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4306 | |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4307 | size = 1 << hash->size_bits; |
| 4308 | for (i = 0; i < size; i++) { |
| 4309 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 4310 | if (ftrace_lookup_ip(old_hash, entry->ip)) |
| 4311 | continue; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4312 | /* |
| 4313 | * The caller might want to do something special |
| 4314 | * for each function we find. We call the callback |
| 4315 | * to give the caller an opportunity to do so. |
| 4316 | */ |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4317 | if (probe_ops->init) { |
| 4318 | ret = probe_ops->init(probe_ops, tr, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 4319 | entry->ip, data, |
| 4320 | &probe->data); |
| 4321 | if (ret < 0) { |
| 4322 | if (probe_ops->free && count) |
| 4323 | probe_ops->free(probe_ops, tr, |
| 4324 | 0, probe->data); |
| 4325 | probe->data = NULL; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4326 | goto out; |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 4327 | } |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4328 | } |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4329 | count++; |
| 4330 | } |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4331 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4332 | |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 4333 | mutex_lock(&ftrace_lock); |
| 4334 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4335 | if (!count) { |
| 4336 | /* Nothing was added? */ |
| 4337 | ret = -EINVAL; |
| 4338 | goto out_unlock; |
| 4339 | } |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4340 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4341 | ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash, |
| 4342 | hash, 1); |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4343 | if (ret < 0) |
Steven Rostedt (VMware) | 8d70725 | 2017-04-05 13:36:18 -0400 | [diff] [blame] | 4344 | goto err_unlock; |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 4345 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4346 | /* One ref for each new function traced */ |
| 4347 | probe->ref += count; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4348 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4349 | if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED)) |
| 4350 | ret = ftrace_startup(&probe->ops, 0); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4351 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4352 | out_unlock: |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 4353 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4354 | |
| 4355 | if (!ret) |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4356 | ret = count; |
Steven Rostedt (Red Hat) | 5ae0bf5 | 2013-05-09 18:20:37 -0400 | [diff] [blame] | 4357 | out: |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4358 | mutex_unlock(&probe->ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4359 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4360 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4361 | release_probe(probe); |
| 4362 | |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4363 | return ret; |
Steven Rostedt (VMware) | 8d70725 | 2017-04-05 13:36:18 -0400 | [diff] [blame] | 4364 | |
| 4365 | err_unlock: |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4366 | if (!probe_ops->free || !count) |
Steven Rostedt (VMware) | 8d70725 | 2017-04-05 13:36:18 -0400 | [diff] [blame] | 4367 | goto out_unlock; |
| 4368 | |
| 4369 | /* Failed to do the move, need to call the free functions */ |
| 4370 | for (i = 0; i < size; i++) { |
| 4371 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 4372 | if (ftrace_lookup_ip(old_hash, entry->ip)) |
| 4373 | continue; |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 4374 | probe_ops->free(probe_ops, tr, entry->ip, probe->data); |
Steven Rostedt (VMware) | 8d70725 | 2017-04-05 13:36:18 -0400 | [diff] [blame] | 4375 | } |
| 4376 | } |
| 4377 | goto out_unlock; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4378 | } |
| 4379 | |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 4380 | int |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4381 | unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr, |
| 4382 | struct ftrace_probe_ops *probe_ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4383 | { |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 4384 | struct ftrace_ops_hash old_hash_ops; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4385 | struct ftrace_func_entry *entry; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4386 | struct ftrace_func_probe *probe; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4387 | struct ftrace_glob func_g; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4388 | struct ftrace_hash **orig_hash; |
| 4389 | struct ftrace_hash *old_hash; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4390 | struct ftrace_hash *hash = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4391 | struct hlist_node *tmp; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4392 | struct hlist_head hhd; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4393 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4394 | int count = 0; |
| 4395 | int i, ret = -ENODEV; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4396 | int size; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4397 | |
Naveen N. Rao | cbab567 | 2017-05-16 23:21:25 +0530 | [diff] [blame] | 4398 | if (!glob || !strlen(glob) || !strcmp(glob, "*")) |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4399 | func_g.search = NULL; |
Naveen N. Rao | cbab567 | 2017-05-16 23:21:25 +0530 | [diff] [blame] | 4400 | else { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4401 | int not; |
| 4402 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4403 | func_g.type = filter_parse_regex(glob, strlen(glob), |
| 4404 | &func_g.search, ¬); |
| 4405 | func_g.len = strlen(func_g.search); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4406 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 4407 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4408 | if (WARN_ON(not)) |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 4409 | return -EINVAL; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4410 | } |
| 4411 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4412 | mutex_lock(&ftrace_lock); |
| 4413 | /* Check if the probe_ops is already registered */ |
| 4414 | list_for_each_entry(probe, &tr->func_probes, list) { |
| 4415 | if (probe->probe_ops == probe_ops) |
| 4416 | break; |
| 4417 | } |
| 4418 | if (&probe->list == &tr->func_probes) |
| 4419 | goto err_unlock_ftrace; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4420 | |
| 4421 | ret = -EINVAL; |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4422 | if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED)) |
| 4423 | goto err_unlock_ftrace; |
| 4424 | |
| 4425 | acquire_probe_locked(probe); |
| 4426 | |
| 4427 | mutex_unlock(&ftrace_lock); |
| 4428 | |
| 4429 | mutex_lock(&probe->ops.func_hash->regex_lock); |
| 4430 | |
| 4431 | orig_hash = &probe->ops.func_hash->filter_hash; |
| 4432 | old_hash = *orig_hash; |
| 4433 | |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4434 | if (ftrace_hash_empty(old_hash)) |
| 4435 | goto out_unlock; |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4436 | |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 4437 | old_hash_ops.filter_hash = old_hash; |
| 4438 | /* Probes only have filters */ |
| 4439 | old_hash_ops.notrace_hash = NULL; |
| 4440 | |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 4441 | ret = -ENOMEM; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4442 | 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] | 4443 | if (!hash) |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4444 | goto out_unlock; |
| 4445 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4446 | INIT_HLIST_HEAD(&hhd); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 4447 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4448 | size = 1 << hash->size_bits; |
| 4449 | for (i = 0; i < size; i++) { |
| 4450 | hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4451 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4452 | if (func_g.search) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4453 | kallsyms_lookup(entry->ip, NULL, NULL, |
| 4454 | NULL, str); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4455 | if (!ftrace_match(str, &func_g)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4456 | continue; |
| 4457 | } |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4458 | count++; |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4459 | remove_hash_entry(hash, entry); |
| 4460 | hlist_add_head(&entry->hlist, &hhd); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4461 | } |
| 4462 | } |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 4463 | |
| 4464 | /* Nothing found? */ |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4465 | if (!count) { |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 4466 | ret = -EINVAL; |
| 4467 | goto out_unlock; |
| 4468 | } |
| 4469 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4470 | mutex_lock(&ftrace_lock); |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4471 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4472 | WARN_ON(probe->ref < count); |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4473 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4474 | probe->ref -= count; |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4475 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4476 | if (ftrace_hash_empty(hash)) |
| 4477 | ftrace_shutdown(&probe->ops, 0); |
| 4478 | |
| 4479 | ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash, |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4480 | hash, 1); |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 4481 | |
| 4482 | /* still need to update the function call sites */ |
Steven Rostedt (VMware) | 1ec3a81 | 2017-04-04 18:16:29 -0400 | [diff] [blame] | 4483 | if (ftrace_enabled && !ftrace_hash_empty(hash)) |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4484 | ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS, |
Steven Rostedt (VMware) | 82cc4fc | 2017-04-14 17:45:45 -0400 | [diff] [blame] | 4485 | &old_hash_ops); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 4486 | synchronize_sched(); |
Steven Rostedt (Red Hat) | 3296fc4 | 2014-07-24 15:33:41 -0400 | [diff] [blame] | 4487 | |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4488 | hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) { |
| 4489 | hlist_del(&entry->hlist); |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4490 | if (probe_ops->free) |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 4491 | probe_ops->free(probe_ops, tr, entry->ip, probe->data); |
Steven Rostedt (VMware) | eee8ded | 2017-04-04 21:31:28 -0400 | [diff] [blame] | 4492 | kfree(entry); |
Steven Rostedt (Red Hat) | 7818b38 | 2013-03-13 12:42:58 -0400 | [diff] [blame] | 4493 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4494 | mutex_unlock(&ftrace_lock); |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 4495 | |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4496 | out_unlock: |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4497 | mutex_unlock(&probe->ops.func_hash->regex_lock); |
Steven Rostedt (Red Hat) | e1df4cb | 2013-03-12 10:09:42 -0400 | [diff] [blame] | 4498 | free_ftrace_hash(hash); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4499 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4500 | release_probe(probe); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4501 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4502 | return ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4503 | |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 4504 | err_unlock_ftrace: |
| 4505 | mutex_unlock(&ftrace_lock); |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 4506 | return ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 4507 | } |
| 4508 | |
Naveen N. Rao | a0e6369 | 2017-05-16 23:21:26 +0530 | [diff] [blame] | 4509 | void clear_ftrace_function_probes(struct trace_array *tr) |
| 4510 | { |
| 4511 | struct ftrace_func_probe *probe, *n; |
| 4512 | |
| 4513 | list_for_each_entry_safe(probe, n, &tr->func_probes, list) |
| 4514 | unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops); |
| 4515 | } |
| 4516 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4517 | static LIST_HEAD(ftrace_commands); |
| 4518 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 4519 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4520 | /* |
| 4521 | * Currently we only register ftrace commands from __init, so mark this |
| 4522 | * __init too. |
| 4523 | */ |
| 4524 | __init int register_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4525 | { |
| 4526 | struct ftrace_func_command *p; |
| 4527 | int ret = 0; |
| 4528 | |
| 4529 | mutex_lock(&ftrace_cmd_mutex); |
| 4530 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4531 | if (strcmp(cmd->name, p->name) == 0) { |
| 4532 | ret = -EBUSY; |
| 4533 | goto out_unlock; |
| 4534 | } |
| 4535 | } |
| 4536 | list_add(&cmd->list, &ftrace_commands); |
| 4537 | out_unlock: |
| 4538 | mutex_unlock(&ftrace_cmd_mutex); |
| 4539 | |
| 4540 | return ret; |
| 4541 | } |
| 4542 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 4543 | /* |
| 4544 | * Currently we only unregister ftrace commands from __init, so mark |
| 4545 | * this __init too. |
| 4546 | */ |
| 4547 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4548 | { |
| 4549 | struct ftrace_func_command *p, *n; |
| 4550 | int ret = -ENODEV; |
| 4551 | |
| 4552 | mutex_lock(&ftrace_cmd_mutex); |
| 4553 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 4554 | if (strcmp(cmd->name, p->name) == 0) { |
| 4555 | ret = 0; |
| 4556 | list_del_init(&p->list); |
| 4557 | goto out_unlock; |
| 4558 | } |
| 4559 | } |
| 4560 | out_unlock: |
| 4561 | mutex_unlock(&ftrace_cmd_mutex); |
| 4562 | |
| 4563 | return ret; |
| 4564 | } |
| 4565 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 4566 | static int ftrace_process_regex(struct ftrace_iterator *iter, |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4567 | char *buff, int len, int enable) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4568 | { |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 4569 | struct ftrace_hash *hash = iter->hash; |
Steven Rostedt (VMware) | d2afd57a | 2017-04-20 11:31:35 -0400 | [diff] [blame] | 4570 | struct trace_array *tr = iter->ops->private; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4571 | char *func, *command, *next = buff; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 4572 | struct ftrace_func_command *p; |
GuoWen Li | 0aff1c0 | 2011-06-01 19:18:47 +0800 | [diff] [blame] | 4573 | int ret = -EINVAL; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4574 | |
| 4575 | func = strsep(&next, ":"); |
| 4576 | |
| 4577 | if (!next) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4578 | ret = ftrace_match_records(hash, func, len); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 4579 | if (!ret) |
| 4580 | ret = -EINVAL; |
| 4581 | if (ret < 0) |
| 4582 | return ret; |
| 4583 | return 0; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4584 | } |
| 4585 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4586 | /* command found */ |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4587 | |
| 4588 | command = strsep(&next, ":"); |
| 4589 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4590 | mutex_lock(&ftrace_cmd_mutex); |
| 4591 | list_for_each_entry(p, &ftrace_commands, list) { |
| 4592 | if (strcmp(p->name, command) == 0) { |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 4593 | ret = p->func(tr, hash, func, command, next, enable); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4594 | goto out_unlock; |
| 4595 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4596 | } |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4597 | out_unlock: |
| 4598 | mutex_unlock(&ftrace_cmd_mutex); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4599 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 4600 | return ret; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 4601 | } |
| 4602 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4603 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4604 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 4605 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4606 | { |
| 4607 | struct ftrace_iterator *iter; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4608 | struct trace_parser *parser; |
| 4609 | ssize_t ret, read; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4610 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4611 | if (!cnt) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4612 | return 0; |
| 4613 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4614 | if (file->f_mode & FMODE_READ) { |
| 4615 | struct seq_file *m = file->private_data; |
| 4616 | iter = m->private; |
| 4617 | } else |
| 4618 | iter = file->private_data; |
| 4619 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4620 | if (unlikely(ftrace_disabled)) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4621 | return -ENODEV; |
| 4622 | |
| 4623 | /* 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] | 4624 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4625 | parser = &iter->parser; |
| 4626 | read = trace_get_user(parser, ubuf, cnt, ppos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4627 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 4628 | if (read >= 0 && trace_parser_loaded(parser) && |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4629 | !trace_parser_cont(parser)) { |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 4630 | ret = ftrace_process_regex(iter, parser->buffer, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4631 | parser->idx, enable); |
Li Zefan | 313254a | 2009-12-08 11:15:30 +0800 | [diff] [blame] | 4632 | trace_parser_clear(parser); |
Steven Rostedt (Red Hat) | 7c088b5 | 2013-05-09 11:35:12 -0400 | [diff] [blame] | 4633 | if (ret < 0) |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4634 | goto out; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4635 | } |
| 4636 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4637 | ret = read; |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4638 | out: |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4639 | return ret; |
| 4640 | } |
| 4641 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4642 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4643 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 4644 | size_t cnt, loff_t *ppos) |
| 4645 | { |
| 4646 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 4647 | } |
| 4648 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4649 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4650 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 4651 | size_t cnt, loff_t *ppos) |
| 4652 | { |
| 4653 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 4654 | } |
| 4655 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4656 | static int |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4657 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
| 4658 | { |
| 4659 | struct ftrace_func_entry *entry; |
| 4660 | |
| 4661 | if (!ftrace_location(ip)) |
| 4662 | return -EINVAL; |
| 4663 | |
| 4664 | if (remove) { |
| 4665 | entry = ftrace_lookup_ip(hash, ip); |
| 4666 | if (!entry) |
| 4667 | return -ENOENT; |
| 4668 | free_hash_entry(hash, entry); |
| 4669 | return 0; |
| 4670 | } |
| 4671 | |
| 4672 | return add_hash_entry(hash, ip); |
| 4673 | } |
| 4674 | |
| 4675 | static int |
| 4676 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4677 | unsigned long ip, int remove, int reset, int enable) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4678 | { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4679 | struct ftrace_hash **orig_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4680 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4681 | int ret; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4682 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4683 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4684 | return -ENODEV; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4685 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4686 | mutex_lock(&ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4687 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4688 | if (enable) |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4689 | orig_hash = &ops->func_hash->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4690 | else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4691 | orig_hash = &ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4692 | |
Wang Nan | b972cc5 | 2014-07-15 08:40:20 +0800 | [diff] [blame] | 4693 | if (reset) |
| 4694 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4695 | else |
| 4696 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 4697 | |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4698 | if (!hash) { |
| 4699 | ret = -ENOMEM; |
| 4700 | goto out_regex_unlock; |
| 4701 | } |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4702 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4703 | if (buf && !ftrace_match_records(hash, buf, len)) { |
| 4704 | ret = -EINVAL; |
| 4705 | goto out_regex_unlock; |
| 4706 | } |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4707 | if (ip) { |
| 4708 | ret = ftrace_match_addr(hash, ip, remove); |
| 4709 | if (ret < 0) |
| 4710 | goto out_regex_unlock; |
| 4711 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4712 | |
| 4713 | mutex_lock(&ftrace_lock); |
Steven Rostedt (VMware) | e16b35d | 2017-04-04 14:46:56 -0400 | [diff] [blame] | 4714 | ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4715 | mutex_unlock(&ftrace_lock); |
| 4716 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4717 | out_regex_unlock: |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4718 | mutex_unlock(&ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4719 | |
| 4720 | free_ftrace_hash(hash); |
| 4721 | return ret; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4722 | } |
| 4723 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4724 | static int |
| 4725 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, |
| 4726 | int reset, int enable) |
| 4727 | { |
| 4728 | return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); |
| 4729 | } |
| 4730 | |
| 4731 | /** |
| 4732 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address |
| 4733 | * @ops - the ops to set the filter with |
| 4734 | * @ip - the address to add to or remove from the filter. |
| 4735 | * @remove - non zero to remove the ip from the filter |
| 4736 | * @reset - non zero to reset all filters before applying this filter. |
| 4737 | * |
| 4738 | * Filters denote which functions should be enabled when tracing is enabled |
| 4739 | * If @ip is NULL, it failes to update filter. |
| 4740 | */ |
| 4741 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, |
| 4742 | int remove, int reset) |
| 4743 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4744 | ftrace_ops_init(ops); |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4745 | return ftrace_set_addr(ops, ip, remove, reset, 1); |
| 4746 | } |
| 4747 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); |
| 4748 | |
Joel Fernandes | d032ae8 | 2016-11-15 12:31:20 -0800 | [diff] [blame] | 4749 | /** |
| 4750 | * ftrace_ops_set_global_filter - setup ops to use global filters |
| 4751 | * @ops - the ops which will use the global filters |
| 4752 | * |
| 4753 | * ftrace users who need global function trace filtering should call this. |
| 4754 | * It can set the global filter only if ops were not initialized before. |
| 4755 | */ |
| 4756 | void ftrace_ops_set_global_filter(struct ftrace_ops *ops) |
| 4757 | { |
| 4758 | if (ops->flags & FTRACE_OPS_FL_INITIALIZED) |
| 4759 | return; |
| 4760 | |
| 4761 | ftrace_ops_init(ops); |
| 4762 | ops->func_hash = &global_ops.local_hash; |
| 4763 | } |
| 4764 | EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter); |
| 4765 | |
Masami Hiramatsu | 647664e | 2012-06-05 19:28:08 +0900 | [diff] [blame] | 4766 | static int |
| 4767 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 4768 | int reset, int enable) |
| 4769 | { |
| 4770 | return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); |
| 4771 | } |
| 4772 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4773 | /** |
| 4774 | * ftrace_set_filter - set a function to filter on in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4775 | * @ops - the ops to set the filter with |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4776 | * @buf - the string that holds the function filter text. |
| 4777 | * @len - the length of the string. |
| 4778 | * @reset - non zero to reset all filters before applying this filter. |
| 4779 | * |
| 4780 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4781 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4782 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4783 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4784 | int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4785 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4786 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4787 | return ftrace_set_regex(ops, buf, len, reset, 1); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4788 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4789 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4790 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4791 | /** |
| 4792 | * ftrace_set_notrace - set a function to not trace in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4793 | * @ops - the ops to set the notrace filter with |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4794 | * @buf - the string that holds the function notrace text. |
| 4795 | * @len - the length of the string. |
| 4796 | * @reset - non zero to reset all filters before applying this filter. |
| 4797 | * |
| 4798 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4799 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4800 | * for tracing. |
| 4801 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4802 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4803 | int len, int reset) |
| 4804 | { |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4805 | ftrace_ops_init(ops); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 4806 | return ftrace_set_regex(ops, buf, len, reset, 0); |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4807 | } |
| 4808 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 4809 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4810 | * 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] | 4811 | * @buf - the string that holds the function filter text. |
| 4812 | * @len - the length of the string. |
| 4813 | * @reset - non zero to reset all filters before applying this filter. |
| 4814 | * |
| 4815 | * Filters denote which functions should be enabled when tracing is enabled. |
| 4816 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 4817 | */ |
| 4818 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 4819 | { |
| 4820 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
| 4821 | } |
| 4822 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 4823 | |
| 4824 | /** |
Jiaxing Wang | 8d1b065 | 2014-04-20 23:10:44 +0800 | [diff] [blame] | 4825 | * 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] | 4826 | * @buf - the string that holds the function notrace text. |
| 4827 | * @len - the length of the string. |
| 4828 | * @reset - non zero to reset all filters before applying this filter. |
| 4829 | * |
| 4830 | * Notrace Filters denote which functions should not be enabled when tracing |
| 4831 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 4832 | * for tracing. |
| 4833 | */ |
| 4834 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 4835 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4836 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4837 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 4838 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4839 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4840 | /* |
| 4841 | * command line interface to allow users to set filters on boot up. |
| 4842 | */ |
| 4843 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 4844 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4845 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 4846 | |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4847 | /* Used by function selftest to not test if filter is set */ |
| 4848 | bool ftrace_filter_param __initdata; |
| 4849 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4850 | static int __init set_ftrace_notrace(char *str) |
| 4851 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4852 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4853 | strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4854 | return 1; |
| 4855 | } |
| 4856 | __setup("ftrace_notrace=", set_ftrace_notrace); |
| 4857 | |
| 4858 | static int __init set_ftrace_filter(char *str) |
| 4859 | { |
Steven Rostedt (Red Hat) | f1ed7c7 | 2013-06-27 22:18:06 -0400 | [diff] [blame] | 4860 | ftrace_filter_param = true; |
Chen Gang | 75761cc | 2013-04-08 12:12:39 +0800 | [diff] [blame] | 4861 | strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4862 | return 1; |
| 4863 | } |
| 4864 | __setup("ftrace_filter=", set_ftrace_filter); |
| 4865 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4866 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Lai Jiangshan | f6060f4 | 2009-11-05 11:16:17 +0800 | [diff] [blame] | 4867 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4868 | static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4869 | static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer); |
Steven Rostedt | 801c29f | 2010-03-05 20:02:19 -0500 | [diff] [blame] | 4870 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4871 | static int __init set_graph_function(char *str) |
| 4872 | { |
Frederic Weisbecker | 06f43d6 | 2009-10-14 20:43:39 +0200 | [diff] [blame] | 4873 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4874 | return 1; |
| 4875 | } |
| 4876 | __setup("ftrace_graph_filter=", set_graph_function); |
| 4877 | |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4878 | static int __init set_graph_notrace_function(char *str) |
| 4879 | { |
| 4880 | strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 4881 | return 1; |
| 4882 | } |
| 4883 | __setup("ftrace_graph_notrace=", set_graph_notrace_function); |
| 4884 | |
Todd Brandt | 65a50c6 | 2017-03-02 16:12:15 -0800 | [diff] [blame] | 4885 | static int __init set_graph_max_depth_function(char *str) |
| 4886 | { |
| 4887 | if (!str) |
| 4888 | return 0; |
| 4889 | fgraph_max_depth = simple_strtoul(str, NULL, 0); |
| 4890 | return 1; |
| 4891 | } |
| 4892 | __setup("ftrace_graph_max_depth=", set_graph_max_depth_function); |
| 4893 | |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4894 | static void __init set_ftrace_early_graph(char *buf, int enable) |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4895 | { |
| 4896 | int ret; |
| 4897 | char *func; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4898 | struct ftrace_hash *hash; |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4899 | |
Steven Rostedt (VMware) | 92ad18e | 2017-03-02 12:53:26 -0500 | [diff] [blame] | 4900 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 4901 | if (WARN_ON(!hash)) |
| 4902 | return; |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4903 | |
| 4904 | while (buf) { |
| 4905 | func = strsep(&buf, ","); |
| 4906 | /* we allow only one expression at a time */ |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 4907 | ret = ftrace_graph_set_hash(hash, func); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4908 | if (ret) |
| 4909 | printk(KERN_DEBUG "ftrace: function %s not " |
| 4910 | "traceable\n", func); |
| 4911 | } |
Steven Rostedt (VMware) | 92ad18e | 2017-03-02 12:53:26 -0500 | [diff] [blame] | 4912 | |
| 4913 | if (enable) |
| 4914 | ftrace_graph_hash = hash; |
| 4915 | else |
| 4916 | ftrace_graph_notrace_hash = hash; |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4917 | } |
| 4918 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 4919 | |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4920 | void __init |
| 4921 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4922 | { |
| 4923 | char *func; |
| 4924 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 4925 | ftrace_ops_init(ops); |
| 4926 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4927 | while (buf) { |
| 4928 | func = strsep(&buf, ","); |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 4929 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4930 | } |
| 4931 | } |
| 4932 | |
| 4933 | static void __init set_ftrace_early_filters(void) |
| 4934 | { |
| 4935 | if (ftrace_filter_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4936 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4937 | if (ftrace_notrace_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 4938 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4939 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4940 | if (ftrace_graph_buf[0]) |
Namhyung Kim | 0d7d9a1 | 2014-06-13 01:23:50 +0900 | [diff] [blame] | 4941 | set_ftrace_early_graph(ftrace_graph_buf, 1); |
| 4942 | if (ftrace_graph_notrace_buf[0]) |
| 4943 | set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 4944 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 4945 | } |
| 4946 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 4947 | int ftrace_regex_release(struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4948 | { |
| 4949 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 4950 | struct ftrace_iterator *iter; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4951 | struct ftrace_hash **orig_hash; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4952 | struct trace_parser *parser; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4953 | int filter_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4954 | int ret; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4955 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4956 | if (file->f_mode & FMODE_READ) { |
| 4957 | iter = m->private; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4958 | seq_release(inode, file); |
| 4959 | } else |
| 4960 | iter = file->private_data; |
| 4961 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4962 | parser = &iter->parser; |
| 4963 | if (trace_parser_loaded(parser)) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 4964 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4965 | } |
| 4966 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4967 | trace_parser_put(parser); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 4968 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4969 | mutex_lock(&iter->ops->func_hash->regex_lock); |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4970 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4971 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 4972 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 4973 | |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 4974 | if (filter_hash) { |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4975 | orig_hash = &iter->ops->func_hash->filter_hash; |
Steven Rostedt (VMware) | 69d7187 | 2017-07-05 09:45:43 -0400 | [diff] [blame] | 4976 | if (iter->tr && !list_empty(&iter->tr->mod_trace)) |
Steven Rostedt (VMware) | 8c08f0d | 2017-06-26 11:47:31 -0400 | [diff] [blame] | 4977 | iter->hash->flags |= FTRACE_HASH_FL_MOD; |
| 4978 | } else |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4979 | orig_hash = &iter->ops->func_hash->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4980 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4981 | mutex_lock(&ftrace_lock); |
Steven Rostedt (VMware) | e16b35d | 2017-04-04 14:46:56 -0400 | [diff] [blame] | 4982 | ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash, |
| 4983 | iter->hash, filter_hash); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4984 | mutex_unlock(&ftrace_lock); |
Steven Rostedt (VMware) | c20489d | 2017-03-29 14:55:49 -0400 | [diff] [blame] | 4985 | } else { |
| 4986 | /* For read only, the hash is the ops hash */ |
| 4987 | iter->hash = NULL; |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4988 | } |
Masami Hiramatsu | 3f2367b | 2013-05-09 14:44:21 +0900 | [diff] [blame] | 4989 | |
Steven Rostedt (Red Hat) | 33b7f99 | 2014-08-15 17:23:02 -0400 | [diff] [blame] | 4990 | mutex_unlock(&iter->ops->func_hash->regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 4991 | free_ftrace_hash(iter->hash); |
| 4992 | kfree(iter); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 4993 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4994 | return 0; |
| 4995 | } |
| 4996 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4997 | static const struct file_operations ftrace_avail_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4998 | .open = ftrace_avail_open, |
| 4999 | .read = seq_read, |
| 5000 | .llseek = seq_lseek, |
Li Zefan | 3be04b4 | 2009-08-17 16:54:03 +0800 | [diff] [blame] | 5001 | .release = seq_release_private, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5002 | }; |
| 5003 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 5004 | static const struct file_operations ftrace_enabled_fops = { |
| 5005 | .open = ftrace_enabled_open, |
| 5006 | .read = seq_read, |
| 5007 | .llseek = seq_lseek, |
| 5008 | .release = seq_release_private, |
| 5009 | }; |
| 5010 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 5011 | static const struct file_operations ftrace_filter_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5012 | .open = ftrace_filter_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 5013 | .read = seq_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5014 | .write = ftrace_filter_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5015 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 5016 | .release = ftrace_regex_release, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5017 | }; |
| 5018 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 5019 | static const struct file_operations ftrace_notrace_fops = { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 5020 | .open = ftrace_notrace_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 5021 | .read = seq_read, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 5022 | .write = ftrace_notrace_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5023 | .llseek = tracing_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 5024 | .release = ftrace_regex_release, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 5025 | }; |
| 5026 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5027 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 5028 | |
| 5029 | static DEFINE_MUTEX(graph_lock); |
| 5030 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5031 | struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH; |
| 5032 | struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH; |
| 5033 | |
| 5034 | enum graph_filter_type { |
| 5035 | GRAPH_FILTER_NOTRACE = 0, |
| 5036 | GRAPH_FILTER_FUNCTION, |
| 5037 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5038 | |
Steven Rostedt (VMware) | 555fc78 | 2017-02-02 10:15:22 -0500 | [diff] [blame] | 5039 | #define FTRACE_GRAPH_EMPTY ((void *)1) |
| 5040 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5041 | struct ftrace_graph_data { |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5042 | struct ftrace_hash *hash; |
| 5043 | struct ftrace_func_entry *entry; |
| 5044 | int idx; /* for hash table iteration */ |
| 5045 | enum graph_filter_type type; |
| 5046 | struct ftrace_hash *new_hash; |
| 5047 | const struct seq_operations *seq_ops; |
| 5048 | struct trace_parser parser; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5049 | }; |
| 5050 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5051 | static void * |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 5052 | __g_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5053 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5054 | struct ftrace_graph_data *fgd = m->private; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5055 | struct ftrace_func_entry *entry = fgd->entry; |
| 5056 | struct hlist_head *head; |
| 5057 | int i, idx = fgd->idx; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5058 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5059 | if (*pos >= fgd->hash->count) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5060 | return NULL; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5061 | |
| 5062 | if (entry) { |
| 5063 | hlist_for_each_entry_continue(entry, hlist) { |
| 5064 | fgd->entry = entry; |
| 5065 | return entry; |
| 5066 | } |
| 5067 | |
| 5068 | idx++; |
| 5069 | } |
| 5070 | |
| 5071 | for (i = idx; i < 1 << fgd->hash->size_bits; i++) { |
| 5072 | head = &fgd->hash->buckets[i]; |
| 5073 | hlist_for_each_entry(entry, head, hlist) { |
| 5074 | fgd->entry = entry; |
| 5075 | fgd->idx = i; |
| 5076 | return entry; |
| 5077 | } |
| 5078 | } |
| 5079 | return NULL; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 5080 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5081 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 5082 | static void * |
| 5083 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 5084 | { |
| 5085 | (*pos)++; |
| 5086 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 5090 | { |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5091 | struct ftrace_graph_data *fgd = m->private; |
| 5092 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5093 | mutex_lock(&graph_lock); |
| 5094 | |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 5095 | if (fgd->type == GRAPH_FILTER_FUNCTION) |
| 5096 | fgd->hash = rcu_dereference_protected(ftrace_graph_hash, |
| 5097 | lockdep_is_held(&graph_lock)); |
| 5098 | else |
| 5099 | fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 5100 | lockdep_is_held(&graph_lock)); |
| 5101 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 5102 | /* Nothing, tell g_show to print all functions are enabled */ |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5103 | if (ftrace_hash_empty(fgd->hash) && !*pos) |
Steven Rostedt (VMware) | 555fc78 | 2017-02-02 10:15:22 -0500 | [diff] [blame] | 5104 | return FTRACE_GRAPH_EMPTY; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 5105 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5106 | fgd->idx = 0; |
| 5107 | fgd->entry = NULL; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 5108 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | static void g_stop(struct seq_file *m, void *p) |
| 5112 | { |
| 5113 | mutex_unlock(&graph_lock); |
| 5114 | } |
| 5115 | |
| 5116 | static int g_show(struct seq_file *m, void *v) |
| 5117 | { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5118 | struct ftrace_func_entry *entry = v; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5119 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5120 | if (!entry) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5121 | return 0; |
| 5122 | |
Steven Rostedt (VMware) | 555fc78 | 2017-02-02 10:15:22 -0500 | [diff] [blame] | 5123 | if (entry == FTRACE_GRAPH_EMPTY) { |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 5124 | struct ftrace_graph_data *fgd = m->private; |
| 5125 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5126 | if (fgd->type == GRAPH_FILTER_FUNCTION) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 5127 | seq_puts(m, "#### all functions enabled ####\n"); |
Namhyung Kim | 280d142 | 2014-06-13 01:23:51 +0900 | [diff] [blame] | 5128 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 5129 | seq_puts(m, "#### no functions disabled ####\n"); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 5130 | return 0; |
| 5131 | } |
| 5132 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5133 | seq_printf(m, "%ps\n", (void *)entry->ip); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5134 | |
| 5135 | return 0; |
| 5136 | } |
| 5137 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 5138 | static const struct seq_operations ftrace_graph_seq_ops = { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5139 | .start = g_start, |
| 5140 | .next = g_next, |
| 5141 | .stop = g_stop, |
| 5142 | .show = g_show, |
| 5143 | }; |
| 5144 | |
| 5145 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5146 | __ftrace_graph_open(struct inode *inode, struct file *file, |
| 5147 | struct ftrace_graph_data *fgd) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5148 | { |
| 5149 | int ret = 0; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5150 | struct ftrace_hash *new_hash = NULL; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5151 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5152 | if (file->f_mode & FMODE_WRITE) { |
| 5153 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 5154 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5155 | if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX)) |
| 5156 | return -ENOMEM; |
| 5157 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5158 | if (file->f_flags & O_TRUNC) |
| 5159 | new_hash = alloc_ftrace_hash(size_bits); |
| 5160 | else |
| 5161 | new_hash = alloc_and_copy_ftrace_hash(size_bits, |
| 5162 | fgd->hash); |
| 5163 | if (!new_hash) { |
| 5164 | ret = -ENOMEM; |
| 5165 | goto out; |
| 5166 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5167 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5168 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5169 | if (file->f_mode & FMODE_READ) { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5170 | ret = seq_open(file, &ftrace_graph_seq_ops); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5171 | if (!ret) { |
| 5172 | struct seq_file *m = file->private_data; |
| 5173 | m->private = fgd; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5174 | } else { |
| 5175 | /* Failed */ |
| 5176 | free_ftrace_hash(new_hash); |
| 5177 | new_hash = NULL; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5178 | } |
| 5179 | } else |
| 5180 | file->private_data = fgd; |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 5181 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5182 | out: |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5183 | if (ret < 0 && file->f_mode & FMODE_WRITE) |
| 5184 | trace_parser_put(&fgd->parser); |
| 5185 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5186 | fgd->new_hash = new_hash; |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 5187 | |
| 5188 | /* |
| 5189 | * All uses of fgd->hash must be taken with the graph_lock |
| 5190 | * held. The graph_lock is going to be released, so force |
| 5191 | * fgd->hash to be reinitialized when it is taken again. |
| 5192 | */ |
| 5193 | fgd->hash = NULL; |
| 5194 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5195 | return ret; |
| 5196 | } |
| 5197 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5198 | static int |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5199 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 5200 | { |
| 5201 | struct ftrace_graph_data *fgd; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5202 | int ret; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5203 | |
| 5204 | if (unlikely(ftrace_disabled)) |
| 5205 | return -ENODEV; |
| 5206 | |
| 5207 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 5208 | if (fgd == NULL) |
| 5209 | return -ENOMEM; |
| 5210 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5211 | mutex_lock(&graph_lock); |
| 5212 | |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 5213 | fgd->hash = rcu_dereference_protected(ftrace_graph_hash, |
| 5214 | lockdep_is_held(&graph_lock)); |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5215 | fgd->type = GRAPH_FILTER_FUNCTION; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5216 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 5217 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5218 | ret = __ftrace_graph_open(inode, file, fgd); |
| 5219 | if (ret < 0) |
| 5220 | kfree(fgd); |
| 5221 | |
| 5222 | mutex_unlock(&graph_lock); |
| 5223 | return ret; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | static int |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5227 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) |
| 5228 | { |
| 5229 | struct ftrace_graph_data *fgd; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5230 | int ret; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5231 | |
| 5232 | if (unlikely(ftrace_disabled)) |
| 5233 | return -ENODEV; |
| 5234 | |
| 5235 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 5236 | if (fgd == NULL) |
| 5237 | return -ENOMEM; |
| 5238 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5239 | mutex_lock(&graph_lock); |
| 5240 | |
Steven Rostedt (VMware) | 649b988 | 2017-02-02 20:16:29 -0500 | [diff] [blame] | 5241 | fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 5242 | lockdep_is_held(&graph_lock)); |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5243 | fgd->type = GRAPH_FILTER_NOTRACE; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5244 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 5245 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5246 | ret = __ftrace_graph_open(inode, file, fgd); |
| 5247 | if (ret < 0) |
| 5248 | kfree(fgd); |
| 5249 | |
| 5250 | mutex_unlock(&graph_lock); |
| 5251 | return ret; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | static int |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 5255 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 5256 | { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5257 | struct ftrace_graph_data *fgd; |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5258 | struct ftrace_hash *old_hash, *new_hash; |
| 5259 | struct trace_parser *parser; |
| 5260 | int ret = 0; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5261 | |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5262 | if (file->f_mode & FMODE_READ) { |
| 5263 | struct seq_file *m = file->private_data; |
| 5264 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5265 | fgd = m->private; |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 5266 | seq_release(inode, file); |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5267 | } else { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5268 | fgd = file->private_data; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5269 | } |
| 5270 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5271 | |
| 5272 | if (file->f_mode & FMODE_WRITE) { |
| 5273 | |
| 5274 | parser = &fgd->parser; |
| 5275 | |
| 5276 | if (trace_parser_loaded((parser))) { |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5277 | ret = ftrace_graph_set_hash(fgd->new_hash, |
| 5278 | parser->buffer); |
| 5279 | } |
| 5280 | |
| 5281 | trace_parser_put(parser); |
| 5282 | |
| 5283 | new_hash = __ftrace_hash_move(fgd->new_hash); |
| 5284 | if (!new_hash) { |
| 5285 | ret = -ENOMEM; |
| 5286 | goto out; |
| 5287 | } |
| 5288 | |
| 5289 | mutex_lock(&graph_lock); |
| 5290 | |
| 5291 | if (fgd->type == GRAPH_FILTER_FUNCTION) { |
| 5292 | old_hash = rcu_dereference_protected(ftrace_graph_hash, |
| 5293 | lockdep_is_held(&graph_lock)); |
| 5294 | rcu_assign_pointer(ftrace_graph_hash, new_hash); |
| 5295 | } else { |
| 5296 | old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 5297 | lockdep_is_held(&graph_lock)); |
| 5298 | rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash); |
| 5299 | } |
| 5300 | |
| 5301 | mutex_unlock(&graph_lock); |
| 5302 | |
| 5303 | /* Wait till all users are no longer using the old hash */ |
| 5304 | synchronize_sched(); |
| 5305 | |
| 5306 | free_ftrace_hash(old_hash); |
| 5307 | } |
| 5308 | |
| 5309 | out: |
Luis Henriques | f9797c2 | 2017-05-25 16:20:38 +0100 | [diff] [blame] | 5310 | free_ftrace_hash(fgd->new_hash); |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5311 | kfree(fgd); |
| 5312 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5313 | return ret; |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | static int |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5317 | ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5318 | { |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 5319 | struct ftrace_glob func_g; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5320 | struct dyn_ftrace *rec; |
| 5321 | struct ftrace_page *pg; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5322 | struct ftrace_func_entry *entry; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5323 | int fail = 1; |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 5324 | int not; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5325 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 5326 | /* decode regex */ |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 5327 | func_g.type = filter_parse_regex(buffer, strlen(buffer), |
| 5328 | &func_g.search, ¬); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 5329 | |
Dmitry Safonov | 3ba0092 | 2015-09-29 19:46:14 +0300 | [diff] [blame] | 5330 | func_g.len = strlen(func_g.search); |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 5331 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 5332 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5333 | |
| 5334 | if (unlikely(ftrace_disabled)) { |
| 5335 | mutex_unlock(&ftrace_lock); |
| 5336 | return -ENODEV; |
| 5337 | } |
| 5338 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 5339 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5340 | |
Steven Rostedt (Red Hat) | 546fece | 2016-11-14 16:31:49 -0500 | [diff] [blame] | 5341 | if (rec->flags & FTRACE_FL_DISABLED) |
| 5342 | continue; |
| 5343 | |
Dmitry Safonov | 0b507e1 | 2015-09-29 19:46:15 +0300 | [diff] [blame] | 5344 | if (ftrace_match_record(rec, &func_g, NULL, 0)) { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5345 | entry = ftrace_lookup_ip(hash, rec->ip); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5346 | |
| 5347 | if (!not) { |
| 5348 | fail = 0; |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5349 | |
| 5350 | if (entry) |
| 5351 | continue; |
| 5352 | if (add_hash_entry(hash, rec->ip) < 0) |
| 5353 | goto out; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5354 | } else { |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5355 | if (entry) { |
| 5356 | free_hash_entry(hash, entry); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5357 | fail = 0; |
| 5358 | } |
| 5359 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5360 | } |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 5361 | } while_for_each_ftrace_rec(); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5362 | out: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 5363 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5364 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5365 | if (fail) |
| 5366 | return -EINVAL; |
| 5367 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5368 | return 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | static ssize_t |
| 5372 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 5373 | size_t cnt, loff_t *ppos) |
| 5374 | { |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 5375 | ssize_t read, ret = 0; |
Namhyung Kim | faf982a | 2013-10-14 17:24:24 +0900 | [diff] [blame] | 5376 | struct ftrace_graph_data *fgd = file->private_data; |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5377 | struct trace_parser *parser; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5378 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 5379 | if (!cnt) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5380 | return 0; |
| 5381 | |
Steven Rostedt (VMware) | ae98d27 | 2017-02-02 16:59:06 -0500 | [diff] [blame] | 5382 | /* Read mode uses seq functions */ |
| 5383 | if (file->f_mode & FMODE_READ) { |
| 5384 | struct seq_file *m = file->private_data; |
| 5385 | fgd = m->private; |
| 5386 | } |
| 5387 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5388 | parser = &fgd->parser; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 5389 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5390 | read = trace_get_user(parser, ubuf, cnt, ppos); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 5391 | |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5392 | if (read >= 0 && trace_parser_loaded(parser) && |
| 5393 | !trace_parser_cont(parser)) { |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 5394 | |
Namhyung Kim | b9b0c831 | 2017-01-20 11:44:47 +0900 | [diff] [blame] | 5395 | ret = ftrace_graph_set_hash(fgd->new_hash, |
Steven Rostedt (VMware) | e704eff | 2017-02-02 20:34:37 -0500 | [diff] [blame] | 5396 | parser->buffer); |
| 5397 | trace_parser_clear(parser); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5398 | } |
| 5399 | |
Namhyung Kim | 6a10108 | 2013-10-14 17:24:25 +0900 | [diff] [blame] | 5400 | if (!ret) |
| 5401 | ret = read; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 5402 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5403 | return ret; |
| 5404 | } |
| 5405 | |
| 5406 | static const struct file_operations ftrace_graph_fops = { |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 5407 | .open = ftrace_graph_open, |
| 5408 | .read = seq_read, |
| 5409 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5410 | .llseek = tracing_lseek, |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 5411 | .release = ftrace_graph_release, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5412 | }; |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5413 | |
| 5414 | static const struct file_operations ftrace_graph_notrace_fops = { |
| 5415 | .open = ftrace_graph_notrace_open, |
| 5416 | .read = seq_read, |
| 5417 | .write = ftrace_graph_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 5418 | .llseek = tracing_lseek, |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5419 | .release = ftrace_graph_release, |
| 5420 | }; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5421 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 5422 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 5423 | void ftrace_create_filter_files(struct ftrace_ops *ops, |
| 5424 | struct dentry *parent) |
| 5425 | { |
| 5426 | |
| 5427 | trace_create_file("set_ftrace_filter", 0644, parent, |
| 5428 | ops, &ftrace_filter_fops); |
| 5429 | |
| 5430 | trace_create_file("set_ftrace_notrace", 0644, parent, |
| 5431 | ops, &ftrace_notrace_fops); |
| 5432 | } |
| 5433 | |
| 5434 | /* |
| 5435 | * The name "destroy_filter_files" is really a misnomer. Although |
| 5436 | * in the future, it may actualy delete the files, but this is |
| 5437 | * really intended to make sure the ops passed in are disabled |
| 5438 | * and that when this function returns, the caller is free to |
| 5439 | * free the ops. |
| 5440 | * |
| 5441 | * The "destroy" name is only to match the "create" name that this |
| 5442 | * should be paired with. |
| 5443 | */ |
| 5444 | void ftrace_destroy_filter_files(struct ftrace_ops *ops) |
| 5445 | { |
| 5446 | mutex_lock(&ftrace_lock); |
| 5447 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 5448 | ftrace_shutdown(ops, 0); |
| 5449 | ops->flags |= FTRACE_OPS_FL_DELETED; |
| 5450 | mutex_unlock(&ftrace_lock); |
| 5451 | } |
| 5452 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 5453 | static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5454 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5455 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 5456 | trace_create_file("available_filter_functions", 0444, |
| 5457 | d_tracer, NULL, &ftrace_avail_fops); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5458 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 5459 | trace_create_file("enabled_functions", 0444, |
| 5460 | d_tracer, NULL, &ftrace_enabled_fops); |
| 5461 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 5462 | ftrace_create_filter_files(&global_ops, d_tracer); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 5463 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5464 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Chen LinX | 1ce0500 | 2014-09-03 14:31:09 +0800 | [diff] [blame] | 5465 | trace_create_file("set_graph_function", 0644, d_tracer, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5466 | NULL, |
| 5467 | &ftrace_graph_fops); |
Chen LinX | 1ce0500 | 2014-09-03 14:31:09 +0800 | [diff] [blame] | 5468 | trace_create_file("set_graph_notrace", 0644, d_tracer, |
Namhyung Kim | 29ad23b | 2013-10-14 17:24:26 +0900 | [diff] [blame] | 5469 | NULL, |
| 5470 | &ftrace_graph_notrace_fops); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 5471 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 5472 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 5473 | return 0; |
| 5474 | } |
| 5475 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5476 | static int ftrace_cmp_ips(const void *a, const void *b) |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 5477 | { |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5478 | const unsigned long *ipa = a; |
| 5479 | const unsigned long *ipb = b; |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 5480 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5481 | if (*ipa > *ipb) |
| 5482 | return 1; |
| 5483 | if (*ipa < *ipb) |
| 5484 | return -1; |
| 5485 | return 0; |
| 5486 | } |
| 5487 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 5488 | static int ftrace_process_locs(struct module *mod, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 5489 | unsigned long *start, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5490 | unsigned long *end) |
| 5491 | { |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5492 | struct ftrace_page *start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5493 | struct ftrace_page *pg; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5494 | struct dyn_ftrace *rec; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5495 | unsigned long count; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5496 | unsigned long *p; |
| 5497 | unsigned long addr; |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5498 | unsigned long flags = 0; /* Shut up gcc */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5499 | int ret = -ENOMEM; |
| 5500 | |
| 5501 | count = end - start; |
| 5502 | |
| 5503 | if (!count) |
| 5504 | return 0; |
| 5505 | |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5506 | sort(start, count, sizeof(*start), |
Rasmus Villemoes | 6db0290 | 2015-09-09 23:27:02 +0200 | [diff] [blame] | 5507 | ftrace_cmp_ips, NULL); |
Steven Rostedt | 9fd4932 | 2012-04-24 22:32:06 -0400 | [diff] [blame] | 5508 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5509 | start_pg = ftrace_allocate_pages(count); |
| 5510 | if (!start_pg) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5511 | return -ENOMEM; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5512 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5513 | mutex_lock(&ftrace_lock); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5514 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5515 | /* |
| 5516 | * Core and each module needs their own pages, as |
| 5517 | * modules will free them when they are removed. |
| 5518 | * Force a new page to be allocated for modules. |
| 5519 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5520 | if (!mod) { |
| 5521 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 5522 | /* First initialization */ |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5523 | ftrace_pages = ftrace_pages_start = start_pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5524 | } else { |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5525 | if (!ftrace_pages) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5526 | goto out; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5527 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5528 | if (WARN_ON(ftrace_pages->next)) { |
| 5529 | /* Hmm, we have free pages? */ |
| 5530 | while (ftrace_pages->next) |
| 5531 | ftrace_pages = ftrace_pages->next; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5532 | } |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5533 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5534 | ftrace_pages->next = start_pg; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5535 | } |
| 5536 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5537 | p = start; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5538 | pg = start_pg; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5539 | while (p < end) { |
| 5540 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 5541 | /* |
| 5542 | * Some architecture linkers will pad between |
| 5543 | * the different mcount_loc sections of different |
| 5544 | * object files to satisfy alignments. |
| 5545 | * Skip any NULL pointers. |
| 5546 | */ |
| 5547 | if (!addr) |
| 5548 | continue; |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5549 | |
| 5550 | if (pg->index == pg->size) { |
| 5551 | /* We should have allocated enough */ |
| 5552 | if (WARN_ON(!pg->next)) |
| 5553 | break; |
| 5554 | pg = pg->next; |
| 5555 | } |
| 5556 | |
| 5557 | rec = &pg->records[pg->index++]; |
| 5558 | rec->ip = addr; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5559 | } |
| 5560 | |
Steven Rostedt | 706c81f | 2012-04-24 23:45:26 -0400 | [diff] [blame] | 5561 | /* We should have used all pages */ |
| 5562 | WARN_ON(pg->next); |
| 5563 | |
| 5564 | /* Assign the last page to ftrace_pages */ |
| 5565 | ftrace_pages = pg; |
| 5566 | |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 5567 | /* |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5568 | * We only need to disable interrupts on start up |
| 5569 | * because we are modifying code that an interrupt |
| 5570 | * may execute, and the modification is not atomic. |
| 5571 | * But for modules, nothing runs the code we modify |
| 5572 | * until we are finished with it, and there's no |
| 5573 | * reason to cause large interrupt latencies while we do it. |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 5574 | */ |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5575 | if (!mod) |
| 5576 | local_irq_save(flags); |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 5577 | ftrace_update_code(mod, start_pg); |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 5578 | if (!mod) |
| 5579 | local_irq_restore(flags); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5580 | ret = 0; |
| 5581 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 5582 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5583 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5584 | return ret; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 5585 | } |
| 5586 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5587 | struct ftrace_mod_func { |
| 5588 | struct list_head list; |
| 5589 | char *name; |
| 5590 | unsigned long ip; |
| 5591 | unsigned int size; |
| 5592 | }; |
| 5593 | |
| 5594 | struct ftrace_mod_map { |
Steven Rostedt (VMware) | 6aa6978 | 2017-09-05 19:20:16 -0400 | [diff] [blame] | 5595 | struct rcu_head rcu; |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5596 | struct list_head list; |
| 5597 | struct module *mod; |
| 5598 | unsigned long start_addr; |
| 5599 | unsigned long end_addr; |
| 5600 | struct list_head funcs; |
Steven Rostedt (VMware) | 6171a03 | 2017-09-06 08:40:41 -0400 | [diff] [blame] | 5601 | unsigned int num_funcs; |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5602 | }; |
| 5603 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5604 | #ifdef CONFIG_MODULES |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5605 | |
| 5606 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 5607 | |
Steven Rostedt (VMware) | 6aa6978 | 2017-09-05 19:20:16 -0400 | [diff] [blame] | 5608 | static LIST_HEAD(ftrace_mod_maps); |
| 5609 | |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5610 | static int referenced_filters(struct dyn_ftrace *rec) |
| 5611 | { |
| 5612 | struct ftrace_ops *ops; |
| 5613 | int cnt = 0; |
| 5614 | |
| 5615 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { |
| 5616 | if (ops_references_rec(ops, rec)) |
| 5617 | cnt++; |
| 5618 | } |
| 5619 | |
| 5620 | return cnt; |
| 5621 | } |
| 5622 | |
Steven Rostedt (VMware) | 2a5bfe4 | 2017-08-31 17:36:51 -0400 | [diff] [blame] | 5623 | static void |
| 5624 | clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash) |
| 5625 | { |
| 5626 | struct ftrace_func_entry *entry; |
| 5627 | struct dyn_ftrace *rec; |
| 5628 | int i; |
| 5629 | |
| 5630 | if (ftrace_hash_empty(hash)) |
| 5631 | return; |
| 5632 | |
| 5633 | for (i = 0; i < pg->index; i++) { |
| 5634 | rec = &pg->records[i]; |
| 5635 | entry = __ftrace_lookup_ip(hash, rec->ip); |
| 5636 | /* |
| 5637 | * Do not allow this rec to match again. |
| 5638 | * Yeah, it may waste some memory, but will be removed |
| 5639 | * if/when the hash is modified again. |
| 5640 | */ |
| 5641 | if (entry) |
| 5642 | entry->ip = 0; |
| 5643 | } |
| 5644 | } |
| 5645 | |
| 5646 | /* Clear any records from hashs */ |
| 5647 | static void clear_mod_from_hashes(struct ftrace_page *pg) |
| 5648 | { |
| 5649 | struct trace_array *tr; |
| 5650 | |
| 5651 | mutex_lock(&trace_types_lock); |
| 5652 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 5653 | if (!tr->ops || !tr->ops->func_hash) |
| 5654 | continue; |
| 5655 | mutex_lock(&tr->ops->func_hash->regex_lock); |
| 5656 | clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash); |
| 5657 | clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash); |
| 5658 | mutex_unlock(&tr->ops->func_hash->regex_lock); |
| 5659 | } |
| 5660 | mutex_unlock(&trace_types_lock); |
| 5661 | } |
| 5662 | |
Steven Rostedt (VMware) | 6aa6978 | 2017-09-05 19:20:16 -0400 | [diff] [blame] | 5663 | static void ftrace_free_mod_map(struct rcu_head *rcu) |
| 5664 | { |
| 5665 | struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu); |
| 5666 | struct ftrace_mod_func *mod_func; |
| 5667 | struct ftrace_mod_func *n; |
| 5668 | |
| 5669 | /* All the contents of mod_map are now not visible to readers */ |
| 5670 | list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) { |
| 5671 | kfree(mod_func->name); |
| 5672 | list_del(&mod_func->list); |
| 5673 | kfree(mod_func); |
| 5674 | } |
| 5675 | |
| 5676 | kfree(mod_map); |
| 5677 | } |
| 5678 | |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 5679 | void ftrace_release_mod(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5680 | { |
Steven Rostedt (VMware) | 6aa6978 | 2017-09-05 19:20:16 -0400 | [diff] [blame] | 5681 | struct ftrace_mod_map *mod_map; |
| 5682 | struct ftrace_mod_map *n; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5683 | struct dyn_ftrace *rec; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5684 | struct ftrace_page **last_pg; |
Steven Rostedt (VMware) | 2a5bfe4 | 2017-08-31 17:36:51 -0400 | [diff] [blame] | 5685 | struct ftrace_page *tmp_page = NULL; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5686 | struct ftrace_page *pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 5687 | int order; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5688 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5689 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5690 | |
| 5691 | if (ftrace_disabled) |
| 5692 | goto out_unlock; |
| 5693 | |
Steven Rostedt (VMware) | 6aa6978 | 2017-09-05 19:20:16 -0400 | [diff] [blame] | 5694 | list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) { |
| 5695 | if (mod_map->mod == mod) { |
| 5696 | list_del_rcu(&mod_map->list); |
| 5697 | call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map); |
| 5698 | break; |
| 5699 | } |
| 5700 | } |
| 5701 | |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5702 | /* |
| 5703 | * Each module has its own ftrace_pages, remove |
| 5704 | * them from the list. |
| 5705 | */ |
| 5706 | last_pg = &ftrace_pages_start; |
| 5707 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 5708 | rec = &pg->records[0]; |
Steven Rostedt (VMware) | 3e23428 | 2017-03-03 18:00:22 -0500 | [diff] [blame] | 5709 | if (within_module_core(rec->ip, mod) || |
| 5710 | within_module_init(rec->ip, mod)) { |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5711 | /* |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5712 | * As core pages are first, the first |
| 5713 | * page should never be a module page. |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5714 | */ |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5715 | if (WARN_ON(pg == ftrace_pages_start)) |
| 5716 | goto out_unlock; |
| 5717 | |
| 5718 | /* Check if we are deleting the last page */ |
| 5719 | if (pg == ftrace_pages) |
| 5720 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 5721 | |
Steven Rostedt (VMware) | 83dd149 | 2017-06-27 11:04:40 -0400 | [diff] [blame] | 5722 | ftrace_update_tot_cnt -= pg->index; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5723 | *last_pg = pg->next; |
Steven Rostedt (VMware) | 2a5bfe4 | 2017-08-31 17:36:51 -0400 | [diff] [blame] | 5724 | |
| 5725 | pg->next = tmp_page; |
| 5726 | tmp_page = pg; |
Steven Rostedt | 32082309 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 5727 | } else |
| 5728 | last_pg = &pg->next; |
| 5729 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 5730 | out_unlock: |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5731 | mutex_unlock(&ftrace_lock); |
Steven Rostedt (VMware) | 2a5bfe4 | 2017-08-31 17:36:51 -0400 | [diff] [blame] | 5732 | |
| 5733 | for (pg = tmp_page; pg; pg = tmp_page) { |
| 5734 | |
| 5735 | /* Needs to be called outside of ftrace_lock */ |
| 5736 | clear_mod_from_hashes(pg); |
| 5737 | |
| 5738 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 5739 | free_pages((unsigned long)pg->records, order); |
| 5740 | tmp_page = pg->next; |
| 5741 | kfree(pg); |
| 5742 | } |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5743 | } |
| 5744 | |
Jessica Yu | 7dcd182 | 2016-02-16 17:32:33 -0500 | [diff] [blame] | 5745 | void ftrace_module_enable(struct module *mod) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5746 | { |
| 5747 | struct dyn_ftrace *rec; |
| 5748 | struct ftrace_page *pg; |
| 5749 | |
| 5750 | mutex_lock(&ftrace_lock); |
| 5751 | |
| 5752 | if (ftrace_disabled) |
| 5753 | goto out_unlock; |
| 5754 | |
| 5755 | /* |
| 5756 | * If the tracing is enabled, go ahead and enable the record. |
| 5757 | * |
| 5758 | * The reason not to enable the record immediatelly is the |
| 5759 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 5760 | * correct previous instructions. Making first the NOP |
| 5761 | * conversion puts the module to the correct state, thus |
| 5762 | * passing the ftrace_make_call check. |
| 5763 | * |
| 5764 | * We also delay this to after the module code already set the |
| 5765 | * text to read-only, as we now need to set it back to read-write |
| 5766 | * so that we can modify the text. |
| 5767 | */ |
| 5768 | if (ftrace_start_up) |
| 5769 | ftrace_arch_code_modify_prepare(); |
| 5770 | |
| 5771 | do_for_each_ftrace_rec(pg, rec) { |
| 5772 | int cnt; |
| 5773 | /* |
| 5774 | * do_for_each_ftrace_rec() is a double loop. |
| 5775 | * module text shares the pg. If a record is |
| 5776 | * not part of this module, then skip this pg, |
| 5777 | * which the "break" will do. |
| 5778 | */ |
Steven Rostedt (VMware) | 3e23428 | 2017-03-03 18:00:22 -0500 | [diff] [blame] | 5779 | if (!within_module_core(rec->ip, mod) && |
| 5780 | !within_module_init(rec->ip, mod)) |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5781 | break; |
| 5782 | |
| 5783 | cnt = 0; |
| 5784 | |
| 5785 | /* |
| 5786 | * When adding a module, we need to check if tracers are |
| 5787 | * currently enabled and if they are, and can trace this record, |
| 5788 | * we need to enable the module functions as well as update the |
| 5789 | * reference counts for those function records. |
| 5790 | */ |
| 5791 | if (ftrace_start_up) |
| 5792 | cnt += referenced_filters(rec); |
| 5793 | |
| 5794 | /* This clears FTRACE_FL_DISABLED */ |
| 5795 | rec->flags = cnt; |
| 5796 | |
| 5797 | if (ftrace_start_up && cnt) { |
| 5798 | int failed = __ftrace_replace_code(rec, 1); |
| 5799 | if (failed) { |
| 5800 | ftrace_bug(failed, rec); |
| 5801 | goto out_loop; |
| 5802 | } |
| 5803 | } |
| 5804 | |
| 5805 | } while_for_each_ftrace_rec(); |
| 5806 | |
| 5807 | out_loop: |
| 5808 | if (ftrace_start_up) |
| 5809 | ftrace_arch_code_modify_post_process(); |
| 5810 | |
| 5811 | out_unlock: |
| 5812 | mutex_unlock(&ftrace_lock); |
Steven Rostedt (VMware) | d7fbf8d | 2017-06-26 10:57:21 -0400 | [diff] [blame] | 5813 | |
| 5814 | process_cached_mods(mod->name); |
Steven Rostedt (Red Hat) | b7ffffb | 2016-01-07 15:40:01 -0500 | [diff] [blame] | 5815 | } |
| 5816 | |
Steven Rostedt (Red Hat) | a949ae5 | 2014-04-24 10:40:12 -0400 | [diff] [blame] | 5817 | void ftrace_module_init(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5818 | { |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5819 | if (ftrace_disabled || !mod->num_ftrace_callsites) |
Abel Vesa | b6b71f6 | 2015-12-02 15:39:57 +0100 | [diff] [blame] | 5820 | return; |
| 5821 | |
Steven Rostedt (Red Hat) | 97e9b4f | 2015-12-23 12:12:22 -0500 | [diff] [blame] | 5822 | ftrace_process_locs(mod, mod->ftrace_callsites, |
| 5823 | mod->ftrace_callsites + mod->num_ftrace_callsites); |
Steven Rostedt (Red Hat) | 8c189ea | 2013-02-13 15:18:38 -0500 | [diff] [blame] | 5824 | } |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5825 | |
| 5826 | static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, |
| 5827 | struct dyn_ftrace *rec) |
| 5828 | { |
| 5829 | struct ftrace_mod_func *mod_func; |
| 5830 | unsigned long symsize; |
| 5831 | unsigned long offset; |
| 5832 | char str[KSYM_SYMBOL_LEN]; |
| 5833 | char *modname; |
| 5834 | const char *ret; |
| 5835 | |
| 5836 | ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str); |
| 5837 | if (!ret) |
| 5838 | return; |
| 5839 | |
| 5840 | mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL); |
| 5841 | if (!mod_func) |
| 5842 | return; |
| 5843 | |
| 5844 | mod_func->name = kstrdup(str, GFP_KERNEL); |
| 5845 | if (!mod_func->name) { |
| 5846 | kfree(mod_func); |
| 5847 | return; |
| 5848 | } |
| 5849 | |
| 5850 | mod_func->ip = rec->ip - offset; |
| 5851 | mod_func->size = symsize; |
| 5852 | |
Steven Rostedt (VMware) | 6171a03 | 2017-09-06 08:40:41 -0400 | [diff] [blame] | 5853 | mod_map->num_funcs++; |
| 5854 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5855 | list_add_rcu(&mod_func->list, &mod_map->funcs); |
| 5856 | } |
| 5857 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5858 | static struct ftrace_mod_map * |
| 5859 | allocate_ftrace_mod_map(struct module *mod, |
| 5860 | unsigned long start, unsigned long end) |
| 5861 | { |
| 5862 | struct ftrace_mod_map *mod_map; |
| 5863 | |
| 5864 | mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL); |
| 5865 | if (!mod_map) |
| 5866 | return NULL; |
| 5867 | |
| 5868 | mod_map->mod = mod; |
| 5869 | mod_map->start_addr = start; |
| 5870 | mod_map->end_addr = end; |
Steven Rostedt (VMware) | 6171a03 | 2017-09-06 08:40:41 -0400 | [diff] [blame] | 5871 | mod_map->num_funcs = 0; |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5872 | |
| 5873 | INIT_LIST_HEAD_RCU(&mod_map->funcs); |
| 5874 | |
| 5875 | list_add_rcu(&mod_map->list, &ftrace_mod_maps); |
| 5876 | |
| 5877 | return mod_map; |
| 5878 | } |
| 5879 | |
| 5880 | static const char * |
| 5881 | ftrace_func_address_lookup(struct ftrace_mod_map *mod_map, |
| 5882 | unsigned long addr, unsigned long *size, |
| 5883 | unsigned long *off, char *sym) |
| 5884 | { |
| 5885 | struct ftrace_mod_func *found_func = NULL; |
| 5886 | struct ftrace_mod_func *mod_func; |
| 5887 | |
| 5888 | list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { |
| 5889 | if (addr >= mod_func->ip && |
| 5890 | addr < mod_func->ip + mod_func->size) { |
| 5891 | found_func = mod_func; |
| 5892 | break; |
| 5893 | } |
| 5894 | } |
| 5895 | |
| 5896 | if (found_func) { |
| 5897 | if (size) |
| 5898 | *size = found_func->size; |
| 5899 | if (off) |
| 5900 | *off = addr - found_func->ip; |
| 5901 | if (sym) |
| 5902 | strlcpy(sym, found_func->name, KSYM_NAME_LEN); |
| 5903 | |
| 5904 | return found_func->name; |
| 5905 | } |
| 5906 | |
| 5907 | return NULL; |
| 5908 | } |
| 5909 | |
| 5910 | const char * |
| 5911 | ftrace_mod_address_lookup(unsigned long addr, unsigned long *size, |
| 5912 | unsigned long *off, char **modname, char *sym) |
| 5913 | { |
| 5914 | struct ftrace_mod_map *mod_map; |
| 5915 | const char *ret = NULL; |
| 5916 | |
Steven Rostedt (VMware) | 6aa6978 | 2017-09-05 19:20:16 -0400 | [diff] [blame] | 5917 | /* mod_map is freed via call_rcu_sched() */ |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5918 | preempt_disable(); |
| 5919 | list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { |
| 5920 | ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym); |
| 5921 | if (ret) { |
| 5922 | if (modname) |
| 5923 | *modname = mod_map->mod->name; |
| 5924 | break; |
| 5925 | } |
| 5926 | } |
| 5927 | preempt_enable(); |
| 5928 | |
| 5929 | return ret; |
| 5930 | } |
| 5931 | |
Steven Rostedt (VMware) | 6171a03 | 2017-09-06 08:40:41 -0400 | [diff] [blame] | 5932 | int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, |
| 5933 | char *type, char *name, |
| 5934 | char *module_name, int *exported) |
| 5935 | { |
| 5936 | struct ftrace_mod_map *mod_map; |
| 5937 | struct ftrace_mod_func *mod_func; |
| 5938 | |
| 5939 | preempt_disable(); |
| 5940 | list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { |
| 5941 | |
| 5942 | if (symnum >= mod_map->num_funcs) { |
| 5943 | symnum -= mod_map->num_funcs; |
| 5944 | continue; |
| 5945 | } |
| 5946 | |
| 5947 | list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { |
| 5948 | if (symnum > 1) { |
| 5949 | symnum--; |
| 5950 | continue; |
| 5951 | } |
| 5952 | |
| 5953 | *value = mod_func->ip; |
| 5954 | *type = 'T'; |
| 5955 | strlcpy(name, mod_func->name, KSYM_NAME_LEN); |
| 5956 | strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN); |
| 5957 | *exported = 1; |
| 5958 | preempt_enable(); |
| 5959 | return 0; |
| 5960 | } |
| 5961 | WARN_ON(1); |
| 5962 | break; |
| 5963 | } |
| 5964 | preempt_enable(); |
| 5965 | return -ERANGE; |
| 5966 | } |
| 5967 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 5968 | #else |
| 5969 | static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, |
| 5970 | struct dyn_ftrace *rec) { } |
| 5971 | static inline struct ftrace_mod_map * |
| 5972 | allocate_ftrace_mod_map(struct module *mod, |
| 5973 | unsigned long start, unsigned long end) |
| 5974 | { |
| 5975 | return NULL; |
| 5976 | } |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 5977 | #endif /* CONFIG_MODULES */ |
| 5978 | |
Joel Fernandes | 8715b10 | 2017-10-09 12:29:31 -0700 | [diff] [blame] | 5979 | struct ftrace_init_func { |
| 5980 | struct list_head list; |
| 5981 | unsigned long ip; |
| 5982 | }; |
| 5983 | |
| 5984 | /* Clear any init ips from hashes */ |
| 5985 | static void |
| 5986 | clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash) |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 5987 | { |
Joel Fernandes | 8715b10 | 2017-10-09 12:29:31 -0700 | [diff] [blame] | 5988 | struct ftrace_func_entry *entry; |
| 5989 | |
| 5990 | if (ftrace_hash_empty(hash)) |
| 5991 | return; |
| 5992 | |
| 5993 | entry = __ftrace_lookup_ip(hash, func->ip); |
| 5994 | |
| 5995 | /* |
| 5996 | * Do not allow this rec to match again. |
| 5997 | * Yeah, it may waste some memory, but will be removed |
| 5998 | * if/when the hash is modified again. |
| 5999 | */ |
| 6000 | if (entry) |
| 6001 | entry->ip = 0; |
| 6002 | } |
| 6003 | |
| 6004 | static void |
| 6005 | clear_func_from_hashes(struct ftrace_init_func *func) |
| 6006 | { |
| 6007 | struct trace_array *tr; |
| 6008 | |
| 6009 | mutex_lock(&trace_types_lock); |
| 6010 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 6011 | if (!tr->ops || !tr->ops->func_hash) |
| 6012 | continue; |
| 6013 | mutex_lock(&tr->ops->func_hash->regex_lock); |
| 6014 | clear_func_from_hash(func, tr->ops->func_hash->filter_hash); |
| 6015 | clear_func_from_hash(func, tr->ops->func_hash->notrace_hash); |
| 6016 | mutex_unlock(&tr->ops->func_hash->regex_lock); |
| 6017 | } |
| 6018 | mutex_unlock(&trace_types_lock); |
| 6019 | } |
| 6020 | |
| 6021 | static void add_to_clear_hash_list(struct list_head *clear_list, |
| 6022 | struct dyn_ftrace *rec) |
| 6023 | { |
| 6024 | struct ftrace_init_func *func; |
| 6025 | |
| 6026 | func = kmalloc(sizeof(*func), GFP_KERNEL); |
| 6027 | if (!func) { |
| 6028 | WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n"); |
| 6029 | return; |
| 6030 | } |
| 6031 | |
| 6032 | func->ip = rec->ip; |
| 6033 | list_add(&func->list, clear_list); |
| 6034 | } |
| 6035 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 6036 | void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6037 | { |
Steven Rostedt (VMware) | 6cafbe1 | 2017-06-20 10:44:58 -0400 | [diff] [blame] | 6038 | unsigned long start = (unsigned long)(start_ptr); |
| 6039 | unsigned long end = (unsigned long)(end_ptr); |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6040 | struct ftrace_page **last_pg = &ftrace_pages_start; |
| 6041 | struct ftrace_page *pg; |
| 6042 | struct dyn_ftrace *rec; |
| 6043 | struct dyn_ftrace key; |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 6044 | struct ftrace_mod_map *mod_map = NULL; |
Joel Fernandes | 8715b10 | 2017-10-09 12:29:31 -0700 | [diff] [blame] | 6045 | struct ftrace_init_func *func, *func_next; |
| 6046 | struct list_head clear_hash; |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6047 | int order; |
| 6048 | |
Joel Fernandes | 8715b10 | 2017-10-09 12:29:31 -0700 | [diff] [blame] | 6049 | INIT_LIST_HEAD(&clear_hash); |
| 6050 | |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6051 | key.ip = start; |
| 6052 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 6053 | |
| 6054 | mutex_lock(&ftrace_lock); |
| 6055 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 6056 | /* |
| 6057 | * If we are freeing module init memory, then check if |
| 6058 | * any tracer is active. If so, we need to save a mapping of |
| 6059 | * the module functions being freed with the address. |
| 6060 | */ |
| 6061 | if (mod && ftrace_ops_list != &ftrace_list_end) |
| 6062 | mod_map = allocate_ftrace_mod_map(mod, start, end); |
| 6063 | |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6064 | for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) { |
| 6065 | if (end < pg->records[0].ip || |
| 6066 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 6067 | continue; |
| 6068 | again: |
| 6069 | rec = bsearch(&key, pg->records, pg->index, |
| 6070 | sizeof(struct dyn_ftrace), |
| 6071 | ftrace_cmp_recs); |
| 6072 | if (!rec) |
| 6073 | continue; |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 6074 | |
Joel Fernandes | 8715b10 | 2017-10-09 12:29:31 -0700 | [diff] [blame] | 6075 | /* rec will be cleared from hashes after ftrace_lock unlock */ |
| 6076 | add_to_clear_hash_list(&clear_hash, rec); |
| 6077 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 6078 | if (mod_map) |
| 6079 | save_ftrace_mod_rec(mod_map, rec); |
| 6080 | |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6081 | pg->index--; |
Steven Rostedt (VMware) | 4ec7846 | 2017-06-28 11:57:03 -0400 | [diff] [blame] | 6082 | ftrace_update_tot_cnt--; |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6083 | if (!pg->index) { |
| 6084 | *last_pg = pg->next; |
| 6085 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 6086 | free_pages((unsigned long)pg->records, order); |
| 6087 | kfree(pg); |
| 6088 | pg = container_of(last_pg, struct ftrace_page, next); |
| 6089 | if (!(*last_pg)) |
| 6090 | ftrace_pages = pg; |
| 6091 | continue; |
| 6092 | } |
| 6093 | memmove(rec, rec + 1, |
| 6094 | (pg->index - (rec - pg->records)) * sizeof(*rec)); |
| 6095 | /* More than one function may be in this block */ |
| 6096 | goto again; |
| 6097 | } |
| 6098 | mutex_unlock(&ftrace_lock); |
Joel Fernandes | 8715b10 | 2017-10-09 12:29:31 -0700 | [diff] [blame] | 6099 | |
| 6100 | list_for_each_entry_safe(func, func_next, &clear_hash, list) { |
| 6101 | clear_func_from_hashes(func); |
| 6102 | kfree(func); |
| 6103 | } |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 6104 | } |
| 6105 | |
Steven Rostedt (VMware) | 6cafbe1 | 2017-06-20 10:44:58 -0400 | [diff] [blame] | 6106 | void __init ftrace_free_init_mem(void) |
| 6107 | { |
| 6108 | void *start = (void *)(&__init_begin); |
| 6109 | void *end = (void *)(&__init_end); |
| 6110 | |
Steven Rostedt (VMware) | aba4b5c | 2017-09-01 08:35:38 -0400 | [diff] [blame] | 6111 | ftrace_free_mem(NULL, start, end); |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 6112 | } |
| 6113 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6114 | void __init ftrace_init(void) |
| 6115 | { |
Jiri Slaby | 1dc43cf | 2014-02-24 19:59:56 +0100 | [diff] [blame] | 6116 | extern unsigned long __start_mcount_loc[]; |
| 6117 | extern unsigned long __stop_mcount_loc[]; |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 6118 | unsigned long count, flags; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6119 | int ret; |
| 6120 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6121 | local_irq_save(flags); |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 6122 | ret = ftrace_dyn_arch_init(); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6123 | local_irq_restore(flags); |
Jiri Slaby | af64a7c | 2014-02-24 19:59:58 +0100 | [diff] [blame] | 6124 | if (ret) |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6125 | goto failed; |
| 6126 | |
| 6127 | count = __stop_mcount_loc - __start_mcount_loc; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 6128 | if (!count) { |
| 6129 | pr_info("ftrace: No functions to be traced?\n"); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6130 | goto failed; |
Jiri Slaby | c867ccd | 2014-02-24 19:59:57 +0100 | [diff] [blame] | 6131 | } |
| 6132 | |
| 6133 | pr_info("ftrace: allocating %ld entries in %ld pages\n", |
| 6134 | count, count / ENTRIES_PER_PAGE + 1); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6135 | |
| 6136 | last_ftrace_enabled = ftrace_enabled = 1; |
| 6137 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 6138 | ret = ftrace_process_locs(NULL, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 6139 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6140 | __stop_mcount_loc); |
| 6141 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 6142 | set_ftrace_early_filters(); |
| 6143 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6144 | return; |
| 6145 | failed: |
| 6146 | ftrace_disabled = 1; |
| 6147 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 6148 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 6149 | /* Do nothing if arch does not support this */ |
| 6150 | void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) |
| 6151 | { |
| 6152 | } |
| 6153 | |
| 6154 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 6155 | { |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 6156 | arch_ftrace_update_trampoline(ops); |
| 6157 | } |
| 6158 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 6159 | void ftrace_init_trace_array(struct trace_array *tr) |
| 6160 | { |
| 6161 | INIT_LIST_HEAD(&tr->func_probes); |
Steven Rostedt (VMware) | 673feb9 | 2017-06-23 15:26:26 -0400 | [diff] [blame] | 6162 | INIT_LIST_HEAD(&tr->mod_trace); |
| 6163 | INIT_LIST_HEAD(&tr->mod_notrace); |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 6164 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6165 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 6166 | |
Steven Rostedt (VMware) | 3306fc4a | 2018-11-15 12:32:38 -0500 | [diff] [blame] | 6167 | struct ftrace_ops global_ops = { |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 6168 | .func = ftrace_stub, |
Steven Rostedt (Red Hat) | e3eea14 | 2015-07-24 10:38:12 -0400 | [diff] [blame] | 6169 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 6170 | FTRACE_OPS_FL_INITIALIZED | |
| 6171 | FTRACE_OPS_FL_PID, |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 6172 | }; |
| 6173 | |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 6174 | static int __init ftrace_nodyn_init(void) |
| 6175 | { |
| 6176 | ftrace_enabled = 1; |
| 6177 | return 0; |
| 6178 | } |
Steven Rostedt | 6f41567 | 2012-10-05 12:13:07 -0400 | [diff] [blame] | 6179 | core_initcall(ftrace_nodyn_init); |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 6180 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 6181 | 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] | 6182 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 6183 | static inline void ftrace_startup_all(int command) { } |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 6184 | |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 6185 | # define ftrace_startup_sysctl() do { } while (0) |
| 6186 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 6187 | |
Steven Rostedt (Red Hat) | f3bea49 | 2014-07-02 23:23:31 -0400 | [diff] [blame] | 6188 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 6189 | { |
| 6190 | } |
| 6191 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6192 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 6193 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 6194 | __init void ftrace_init_global_array_ops(struct trace_array *tr) |
| 6195 | { |
| 6196 | tr->ops = &global_ops; |
| 6197 | tr->ops->private = tr; |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 6198 | ftrace_init_trace_array(tr); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 6199 | } |
| 6200 | |
| 6201 | void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) |
| 6202 | { |
| 6203 | /* If we filter on pids, update to use the pid function */ |
| 6204 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { |
| 6205 | if (WARN_ON(tr->ops->func != ftrace_stub)) |
| 6206 | printk("ftrace ops had %pS for function\n", |
| 6207 | tr->ops->func); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 6208 | } |
| 6209 | tr->ops->func = func; |
| 6210 | tr->ops->private = tr; |
| 6211 | } |
| 6212 | |
| 6213 | void ftrace_reset_array_ops(struct trace_array *tr) |
| 6214 | { |
| 6215 | tr->ops->func = ftrace_stub; |
| 6216 | } |
| 6217 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 6218 | static inline void |
| 6219 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 6220 | struct ftrace_ops *ignored, struct pt_regs *regs) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 6221 | { |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 6222 | struct ftrace_ops *op; |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 6223 | int bit; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 6224 | |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 6225 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 6226 | if (bit < 0) |
| 6227 | return; |
Steven Rostedt | c29f122 | 2012-11-02 17:17:59 -0400 | [diff] [blame] | 6228 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 6229 | /* |
| 6230 | * Some of the ops may be dynamically allocated, |
| 6231 | * they must be freed after a synchronize_sched(). |
| 6232 | */ |
| 6233 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 6234 | |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 6235 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 6236 | /* |
| 6237 | * Check the following for each ops before calling their func: |
| 6238 | * if RCU flag is set, then rcu_is_watching() must be true |
| 6239 | * if PER_CPU is set, then ftrace_function_local_disable() |
| 6240 | * must be false |
| 6241 | * Otherwise test if the ip matches the ops filter |
| 6242 | * |
| 6243 | * If any of the above fails then the op->func() is not executed. |
| 6244 | */ |
| 6245 | if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) && |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 6246 | ftrace_ops_test(op, ip, regs)) { |
Steven Rostedt (Red Hat) | 1d48d59 | 2014-06-25 11:54:03 -0400 | [diff] [blame] | 6247 | if (FTRACE_WARN_ON(!op->func)) { |
| 6248 | pr_warn("op=%p %pS\n", op, op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 6249 | goto out; |
| 6250 | } |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 6251 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 6252 | } |
Steven Rostedt | 0a01640 | 2012-11-02 17:03:03 -0400 | [diff] [blame] | 6253 | } while_for_each_ftrace_op(op); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 6254 | out: |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 6255 | preempt_enable_notrace(); |
Steven Rostedt | edc15ca | 2012-11-02 17:47:21 -0400 | [diff] [blame] | 6256 | trace_clear_recursion(bit); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 6257 | } |
| 6258 | |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 6259 | /* |
| 6260 | * Some archs only support passing ip and parent_ip. Even though |
| 6261 | * the list function ignores the op parameter, we do not want any |
| 6262 | * C side effects, where a function is called without the caller |
| 6263 | * sending a third parameter. |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 6264 | * Archs are to support both the regs and ftrace_ops at the same time. |
| 6265 | * If they support ftrace_ops, it is assumed they support regs. |
| 6266 | * 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] | 6267 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
| 6268 | * 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] | 6269 | * An architecture can pass partial regs with ftrace_ops and still |
Li Bin | b8ec330 | 2015-11-30 18:23:36 +0800 | [diff] [blame] | 6270 | * set the ARCH_SUPPORTS_FTRACE_OPS. |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 6271 | */ |
| 6272 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 6273 | 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] | 6274 | struct ftrace_ops *op, struct pt_regs *regs) |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 6275 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 6276 | __ftrace_ops_list_func(ip, parent_ip, NULL, regs); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 6277 | } |
| 6278 | #else |
| 6279 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip) |
| 6280 | { |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 6281 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 6282 | } |
| 6283 | #endif |
| 6284 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 6285 | /* |
| 6286 | * 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] | 6287 | * recursion, needs RCU protection and/or requires per cpu handling, then |
| 6288 | * this function will be called by the mcount trampoline. |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 6289 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 6290 | 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] | 6291 | struct ftrace_ops *op, struct pt_regs *regs) |
| 6292 | { |
| 6293 | int bit; |
| 6294 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 6295 | if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching()) |
| 6296 | return; |
| 6297 | |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 6298 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
| 6299 | if (bit < 0) |
| 6300 | return; |
| 6301 | |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 6302 | preempt_disable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 6303 | |
Peter Zijlstra | b3a8880 | 2017-10-11 09:45:32 +0200 | [diff] [blame] | 6304 | op->func(ip, parent_ip, op, regs); |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 6305 | |
| 6306 | preempt_enable_notrace(); |
Steven Rostedt (Red Hat) | f1ff634 | 2014-07-22 20:16:57 -0400 | [diff] [blame] | 6307 | trace_clear_recursion(bit); |
| 6308 | } |
| 6309 | |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 6310 | /** |
| 6311 | * ftrace_ops_get_func - get the function a trampoline should call |
| 6312 | * @ops: the ops to get the function for |
| 6313 | * |
| 6314 | * Normally the mcount trampoline will call the ops->func, but there |
| 6315 | * are times that it should not. For example, if the ops does not |
| 6316 | * have its own recursion protection, then it should call the |
Chunyu Hu | 3a150df | 2017-02-22 08:29:26 +0800 | [diff] [blame] | 6317 | * ftrace_ops_assist_func() instead. |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 6318 | * |
| 6319 | * Returns the function that the trampoline should call for @ops. |
| 6320 | */ |
| 6321 | ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) |
| 6322 | { |
| 6323 | /* |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 6324 | * If the function does not handle recursion, needs to be RCU safe, |
| 6325 | * 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] | 6326 | */ |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 6327 | if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) || |
Peter Zijlstra | b3a8880 | 2017-10-11 09:45:32 +0200 | [diff] [blame] | 6328 | ops->flags & FTRACE_OPS_FL_RCU) |
Steven Rostedt (Red Hat) | c68c0fa | 2015-12-01 13:28:16 -0500 | [diff] [blame] | 6329 | return ftrace_ops_assist_func; |
Steven Rostedt (Red Hat) | 8735405 | 2014-07-22 20:41:42 -0400 | [diff] [blame] | 6330 | |
| 6331 | return ops->func; |
| 6332 | } |
| 6333 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6334 | static void |
| 6335 | ftrace_filter_pid_sched_switch_probe(void *data, bool preempt, |
| 6336 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 6337 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6338 | struct trace_array *tr = data; |
| 6339 | struct trace_pid_list *pid_list; |
| 6340 | |
| 6341 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 6342 | |
| 6343 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 6344 | trace_ignore_this_task(pid_list, next)); |
| 6345 | } |
| 6346 | |
Namhyung Kim | 1e10486 | 2017-04-17 11:44:28 +0900 | [diff] [blame] | 6347 | static void |
| 6348 | ftrace_pid_follow_sched_process_fork(void *data, |
| 6349 | struct task_struct *self, |
| 6350 | struct task_struct *task) |
| 6351 | { |
| 6352 | struct trace_pid_list *pid_list; |
| 6353 | struct trace_array *tr = data; |
| 6354 | |
| 6355 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 6356 | trace_filter_add_remove_task(pid_list, self, task); |
| 6357 | } |
| 6358 | |
| 6359 | static void |
| 6360 | ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task) |
| 6361 | { |
| 6362 | struct trace_pid_list *pid_list; |
| 6363 | struct trace_array *tr = data; |
| 6364 | |
| 6365 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 6366 | trace_filter_add_remove_task(pid_list, NULL, task); |
| 6367 | } |
| 6368 | |
| 6369 | void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) |
| 6370 | { |
| 6371 | if (enable) { |
| 6372 | register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork, |
| 6373 | tr); |
| 6374 | register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit, |
| 6375 | tr); |
| 6376 | } else { |
| 6377 | unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork, |
| 6378 | tr); |
| 6379 | unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit, |
| 6380 | tr); |
| 6381 | } |
| 6382 | } |
| 6383 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6384 | static void clear_ftrace_pids(struct trace_array *tr) |
| 6385 | { |
| 6386 | struct trace_pid_list *pid_list; |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 6387 | int cpu; |
| 6388 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6389 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 6390 | lockdep_is_held(&ftrace_lock)); |
| 6391 | if (!pid_list) |
| 6392 | return; |
| 6393 | |
| 6394 | unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 6395 | |
| 6396 | for_each_possible_cpu(cpu) |
| 6397 | per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false; |
| 6398 | |
| 6399 | rcu_assign_pointer(tr->function_pids, NULL); |
| 6400 | |
| 6401 | /* Wait till all users are no longer using pid filtering */ |
| 6402 | synchronize_sched(); |
| 6403 | |
| 6404 | trace_free_pid_list(pid_list); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 6405 | } |
| 6406 | |
Namhyung Kim | d879d0b | 2017-04-17 11:44:27 +0900 | [diff] [blame] | 6407 | void ftrace_clear_pids(struct trace_array *tr) |
| 6408 | { |
| 6409 | mutex_lock(&ftrace_lock); |
| 6410 | |
| 6411 | clear_ftrace_pids(tr); |
| 6412 | |
| 6413 | mutex_unlock(&ftrace_lock); |
| 6414 | } |
| 6415 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6416 | static void ftrace_pid_reset(struct trace_array *tr) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 6417 | { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6418 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6419 | clear_ftrace_pids(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6420 | |
| 6421 | ftrace_update_pid_func(); |
Steven Rostedt (Red Hat) | e1effa0 | 2014-08-05 17:19:38 -0400 | [diff] [blame] | 6422 | ftrace_startup_all(0); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6423 | |
| 6424 | mutex_unlock(&ftrace_lock); |
| 6425 | } |
| 6426 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6427 | /* Greater than any max PID */ |
| 6428 | #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1) |
| 6429 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6430 | 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] | 6431 | __acquires(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6432 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6433 | struct trace_pid_list *pid_list; |
| 6434 | struct trace_array *tr = m->private; |
| 6435 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6436 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6437 | rcu_read_lock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6438 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6439 | pid_list = rcu_dereference_sched(tr->function_pids); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6440 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6441 | if (!pid_list) |
| 6442 | return !(*pos) ? FTRACE_NO_PIDS : NULL; |
| 6443 | |
| 6444 | return trace_pid_start(pid_list, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 6448 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6449 | struct trace_array *tr = m->private; |
| 6450 | struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids); |
| 6451 | |
| 6452 | if (v == FTRACE_NO_PIDS) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6453 | return NULL; |
| 6454 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6455 | return trace_pid_next(pid_list, v, pos); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6456 | } |
| 6457 | |
| 6458 | static void fpid_stop(struct seq_file *m, void *p) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6459 | __releases(RCU) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6460 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6461 | rcu_read_unlock_sched(); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6462 | mutex_unlock(&ftrace_lock); |
| 6463 | } |
| 6464 | |
| 6465 | static int fpid_show(struct seq_file *m, void *v) |
| 6466 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6467 | if (v == FTRACE_NO_PIDS) { |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 6468 | seq_puts(m, "no pid\n"); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6469 | return 0; |
| 6470 | } |
| 6471 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6472 | return trace_pid_show(m, v); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6473 | } |
| 6474 | |
| 6475 | static const struct seq_operations ftrace_pid_sops = { |
| 6476 | .start = fpid_start, |
| 6477 | .next = fpid_next, |
| 6478 | .stop = fpid_stop, |
| 6479 | .show = fpid_show, |
| 6480 | }; |
| 6481 | |
| 6482 | static int |
| 6483 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 6484 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6485 | struct trace_array *tr = inode->i_private; |
| 6486 | struct seq_file *m; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6487 | int ret = 0; |
| 6488 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6489 | if (trace_array_get(tr) < 0) |
| 6490 | return -ENODEV; |
| 6491 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6492 | if ((file->f_mode & FMODE_WRITE) && |
| 6493 | (file->f_flags & O_TRUNC)) |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6494 | ftrace_pid_reset(tr); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6495 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6496 | ret = seq_open(file, &ftrace_pid_sops); |
| 6497 | if (ret < 0) { |
| 6498 | trace_array_put(tr); |
| 6499 | } else { |
| 6500 | m = file->private_data; |
| 6501 | /* copy tr over to seq ops */ |
| 6502 | m->private = tr; |
| 6503 | } |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6504 | |
| 6505 | return ret; |
| 6506 | } |
| 6507 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6508 | static void ignore_task_cpu(void *data) |
| 6509 | { |
| 6510 | struct trace_array *tr = data; |
| 6511 | struct trace_pid_list *pid_list; |
| 6512 | |
| 6513 | /* |
| 6514 | * This function is called by on_each_cpu() while the |
| 6515 | * event_mutex is held. |
| 6516 | */ |
| 6517 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 6518 | mutex_is_locked(&ftrace_lock)); |
| 6519 | |
| 6520 | this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid, |
| 6521 | trace_ignore_this_task(pid_list, current)); |
| 6522 | } |
| 6523 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6524 | static ssize_t |
| 6525 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 6526 | size_t cnt, loff_t *ppos) |
| 6527 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6528 | struct seq_file *m = filp->private_data; |
| 6529 | struct trace_array *tr = m->private; |
| 6530 | struct trace_pid_list *filtered_pids = NULL; |
| 6531 | struct trace_pid_list *pid_list; |
| 6532 | ssize_t ret; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6533 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6534 | if (!cnt) |
| 6535 | return 0; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6536 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6537 | mutex_lock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6538 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6539 | filtered_pids = rcu_dereference_protected(tr->function_pids, |
| 6540 | lockdep_is_held(&ftrace_lock)); |
| 6541 | |
| 6542 | ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); |
| 6543 | if (ret < 0) |
| 6544 | goto out; |
| 6545 | |
| 6546 | rcu_assign_pointer(tr->function_pids, pid_list); |
| 6547 | |
| 6548 | if (filtered_pids) { |
| 6549 | synchronize_sched(); |
| 6550 | trace_free_pid_list(filtered_pids); |
| 6551 | } else if (pid_list) { |
| 6552 | /* Register a probe to set whether to ignore the tracing of a task */ |
| 6553 | register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); |
| 6554 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6555 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6556 | /* |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6557 | * Ignoring of pids is done at task switch. But we have to |
| 6558 | * check for those tasks that are currently running. |
| 6559 | * Always do this in case a pid was appended or removed. |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6560 | */ |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6561 | on_each_cpu(ignore_task_cpu, tr, 1); |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6562 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6563 | ftrace_update_pid_func(); |
| 6564 | ftrace_startup_all(0); |
| 6565 | out: |
| 6566 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6567 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6568 | if (ret > 0) |
| 6569 | *ppos += ret; |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 6570 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6571 | return ret; |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6572 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6573 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6574 | static int |
| 6575 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 6576 | { |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6577 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6578 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6579 | trace_array_put(tr); |
| 6580 | |
| 6581 | return seq_release(inode, file); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6582 | } |
| 6583 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6584 | static const struct file_operations ftrace_pid_fops = { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6585 | .open = ftrace_pid_open, |
| 6586 | .write = ftrace_pid_write, |
| 6587 | .read = seq_read, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 6588 | .llseek = tracing_lseek, |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 6589 | .release = ftrace_pid_release, |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6590 | }; |
| 6591 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6592 | void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6593 | { |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 6594 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 6595 | tr, &ftrace_pid_fops); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6596 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 6597 | |
Steven Rostedt (Red Hat) | 501c237 | 2016-07-05 10:04:34 -0400 | [diff] [blame] | 6598 | void __init ftrace_init_tracefs_toplevel(struct trace_array *tr, |
| 6599 | struct dentry *d_tracer) |
| 6600 | { |
| 6601 | /* Only the top level directory has the dyn_tracefs and profile */ |
| 6602 | WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); |
| 6603 | |
| 6604 | ftrace_init_dyn_tracefs(d_tracer); |
| 6605 | ftrace_profile_tracefs(d_tracer); |
| 6606 | } |
| 6607 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6608 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 6609 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 6610 | * |
| 6611 | * This function should be used by panic code. It stops ftrace |
| 6612 | * but in a not so nice way. If you need to simply kill ftrace |
| 6613 | * from a non-atomic section, use ftrace_kill. |
| 6614 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 6615 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 6616 | { |
| 6617 | ftrace_disabled = 1; |
| 6618 | ftrace_enabled = 0; |
Yisheng Xie | 5ccba64 | 2018-02-02 10:14:49 +0800 | [diff] [blame] | 6619 | ftrace_trace_function = ftrace_stub; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | /** |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 6623 | * Test if ftrace is dead or not. |
| 6624 | */ |
| 6625 | int ftrace_is_dead(void) |
| 6626 | { |
| 6627 | return ftrace_disabled; |
| 6628 | } |
| 6629 | |
| 6630 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6631 | * register_ftrace_function - register a function for profiling |
| 6632 | * @ops - ops structure that holds the function for profiling. |
| 6633 | * |
| 6634 | * Register a function to be called by all functions in the |
| 6635 | * kernel. |
| 6636 | * |
| 6637 | * Note: @ops->func and all the functions it calls must be labeled |
| 6638 | * with "notrace", otherwise it will go into a |
| 6639 | * recursive loop. |
| 6640 | */ |
| 6641 | int register_ftrace_function(struct ftrace_ops *ops) |
| 6642 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 6643 | int ret = -1; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 6644 | |
Masami Hiramatsu | f04f24fb | 2013-05-09 14:44:17 +0900 | [diff] [blame] | 6645 | ftrace_ops_init(ops); |
| 6646 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6647 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 6648 | |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 6649 | ret = ftrace_startup(ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 6650 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6651 | mutex_unlock(&ftrace_lock); |
Borislav Petkov | 8d240dd | 2012-03-29 19:11:40 +0200 | [diff] [blame] | 6652 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6653 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6654 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 6655 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6656 | |
| 6657 | /** |
Uwe Kleine-Koenig | 3263292 | 2009-01-12 23:35:50 +0100 | [diff] [blame] | 6658 | * unregister_ftrace_function - unregister a function for profiling. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6659 | * @ops - ops structure that holds the function to unregister |
| 6660 | * |
| 6661 | * Unregister a function that was added to be called by ftrace profiling. |
| 6662 | */ |
| 6663 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 6664 | { |
| 6665 | int ret; |
| 6666 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6667 | mutex_lock(&ftrace_lock); |
Steven Rostedt (Red Hat) | 8a56d77 | 2013-11-25 20:59:46 -0500 | [diff] [blame] | 6668 | ret = ftrace_shutdown(ops, 0); |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6669 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6670 | |
| 6671 | return ret; |
| 6672 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 6673 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6674 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 6675 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6676 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 6677 | void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6678 | loff_t *ppos) |
| 6679 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 6680 | int ret = -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 6681 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6682 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6683 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 6684 | if (unlikely(ftrace_disabled)) |
| 6685 | goto out; |
| 6686 | |
| 6687 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6688 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 6689 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6690 | goto out; |
| 6691 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 6692 | last_ftrace_enabled = !!ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6693 | |
| 6694 | if (ftrace_enabled) { |
| 6695 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6696 | /* we are starting ftrace again */ |
Chunyan Zhang | f86f418 | 2017-06-07 16:12:51 +0800 | [diff] [blame] | 6697 | if (rcu_dereference_protected(ftrace_ops_list, |
| 6698 | lockdep_is_held(&ftrace_lock)) != &ftrace_list_end) |
Jan Kiszka | 5000c41 | 2013-03-26 17:53:03 +0100 | [diff] [blame] | 6699 | update_ftrace_function(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6700 | |
Steven Rostedt (Red Hat) | 524a386 | 2015-03-06 19:55:13 -0500 | [diff] [blame] | 6701 | ftrace_startup_sysctl(); |
| 6702 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 6703 | } else { |
| 6704 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 6705 | ftrace_trace_function = ftrace_stub; |
| 6706 | |
| 6707 | ftrace_shutdown_sysctl(); |
| 6708 | } |
| 6709 | |
| 6710 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 6711 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6712 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6713 | } |