Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 1 | /* |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 2 | * Copyright (C) 2008-2014 Mathieu Desnoyers |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/jhash.h> |
| 22 | #include <linux/list.h> |
| 23 | #include <linux/rcupdate.h> |
| 24 | #include <linux/tracepoint.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 27 | #include <linux/sched/signal.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 28 | #include <linux/sched/task.h> |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 29 | #include <linux/static_key.h> |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 30 | |
Mathieu Desnoyers | 9c0be3f | 2018-10-13 15:10:50 -0400 | [diff] [blame] | 31 | extern tracepoint_ptr_t __start___tracepoints_ptrs[]; |
| 32 | extern tracepoint_ptr_t __stop___tracepoints_ptrs[]; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 33 | |
Joel Fernandes (Google) | e6753f2 | 2018-07-30 15:24:22 -0700 | [diff] [blame] | 34 | DEFINE_SRCU(tracepoint_srcu); |
| 35 | EXPORT_SYMBOL_GPL(tracepoint_srcu); |
| 36 | |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 37 | /* Set to 1 to enable tracepoint debug output */ |
| 38 | static const int tracepoint_debug; |
| 39 | |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 40 | #ifdef CONFIG_MODULES |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 41 | /* |
| 42 | * Tracepoint module list mutex protects the local module list. |
| 43 | */ |
| 44 | static DEFINE_MUTEX(tracepoint_module_list_mutex); |
| 45 | |
| 46 | /* Local list of struct tp_module */ |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 47 | static LIST_HEAD(tracepoint_module_list); |
| 48 | #endif /* CONFIG_MODULES */ |
| 49 | |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 50 | /* |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 51 | * tracepoints_mutex protects the builtin and module tracepoints. |
| 52 | * tracepoints_mutex nests inside tracepoint_module_list_mutex. |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 53 | */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 54 | static DEFINE_MUTEX(tracepoints_mutex); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 55 | |
Steven Rostedt (VMware) | f8a79d5 | 2018-08-10 12:17:50 -0400 | [diff] [blame] | 56 | static struct rcu_head *early_probes; |
| 57 | static bool ok_to_free_tracepoints; |
| 58 | |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 59 | /* |
| 60 | * Note about RCU : |
Anand Gadiyar | fd589a8 | 2009-07-16 17:13:03 +0200 | [diff] [blame] | 61 | * It is used to delay the free of multiple probes array until a quiescent |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 62 | * state is reached. |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 63 | */ |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 64 | struct tp_probes { |
Mathieu Desnoyers | 0dea6d5 | 2014-03-21 01:19:01 -0400 | [diff] [blame] | 65 | struct rcu_head rcu; |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 66 | struct tracepoint_func probes[0]; |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 67 | }; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 68 | |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 69 | static inline void *allocate_probes(int count) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 70 | { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 71 | struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func) |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 72 | + sizeof(struct tp_probes), GFP_KERNEL); |
| 73 | return p == NULL ? NULL : p->probes; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Joel Fernandes (Google) | e6753f2 | 2018-07-30 15:24:22 -0700 | [diff] [blame] | 76 | static void srcu_free_old_probes(struct rcu_head *head) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 77 | { |
Mathieu Desnoyers | 0dea6d5 | 2014-03-21 01:19:01 -0400 | [diff] [blame] | 78 | kfree(container_of(head, struct tp_probes, rcu)); |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Joel Fernandes (Google) | e6753f2 | 2018-07-30 15:24:22 -0700 | [diff] [blame] | 81 | static void rcu_free_old_probes(struct rcu_head *head) |
| 82 | { |
| 83 | call_srcu(&tracepoint_srcu, head, srcu_free_old_probes); |
| 84 | } |
| 85 | |
Steven Rostedt (VMware) | f8a79d5 | 2018-08-10 12:17:50 -0400 | [diff] [blame] | 86 | static __init int release_early_probes(void) |
| 87 | { |
| 88 | struct rcu_head *tmp; |
| 89 | |
| 90 | ok_to_free_tracepoints = true; |
| 91 | |
| 92 | while (early_probes) { |
| 93 | tmp = early_probes; |
| 94 | early_probes = tmp->next; |
| 95 | call_rcu_sched(tmp, rcu_free_old_probes); |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /* SRCU is initialized at core_initcall */ |
| 102 | postcore_initcall(release_early_probes); |
| 103 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 104 | static inline void release_probes(struct tracepoint_func *old) |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 105 | { |
| 106 | if (old) { |
| 107 | struct tp_probes *tp_probes = container_of(old, |
| 108 | struct tp_probes, probes[0]); |
Steven Rostedt (VMware) | f8a79d5 | 2018-08-10 12:17:50 -0400 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * We can't free probes if SRCU is not initialized yet. |
| 112 | * Postpone the freeing till after SRCU is initialized. |
| 113 | */ |
| 114 | if (unlikely(!ok_to_free_tracepoints)) { |
| 115 | tp_probes->rcu.next = early_probes; |
| 116 | early_probes = &tp_probes->rcu; |
| 117 | return; |
| 118 | } |
| 119 | |
Joel Fernandes (Google) | e6753f2 | 2018-07-30 15:24:22 -0700 | [diff] [blame] | 120 | /* |
| 121 | * Tracepoint probes are protected by both sched RCU and SRCU, |
| 122 | * by calling the SRCU callback in the sched RCU callback we |
| 123 | * cover both cases. So let us chain the SRCU and sched RCU |
| 124 | * callbacks to wait for both grace periods. |
| 125 | */ |
Mathieu Desnoyers | 0dea6d5 | 2014-03-21 01:19:01 -0400 | [diff] [blame] | 126 | call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes); |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 127 | } |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 130 | static void debug_print_probes(struct tracepoint_func *funcs) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 131 | { |
| 132 | int i; |
| 133 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 134 | if (!tracepoint_debug || !funcs) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 135 | return; |
| 136 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 137 | for (i = 0; funcs[i].func; i++) |
| 138 | printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 141 | static struct tracepoint_func * |
| 142 | func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func, |
| 143 | int prio) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 144 | { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 145 | struct tracepoint_func *old, *new; |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 146 | int nr_probes = 0; |
| 147 | int pos = -1; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 148 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 149 | if (WARN_ON(!tp_func->func)) |
Sahara | 4c69e6e | 2013-04-15 11:13:15 +0900 | [diff] [blame] | 150 | return ERR_PTR(-EINVAL); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 151 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 152 | debug_print_probes(*funcs); |
| 153 | old = *funcs; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 154 | if (old) { |
| 155 | /* (N -> N+1), (N != 0, 1) probes */ |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 156 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
| 157 | /* Insert before probes of lower priority */ |
| 158 | if (pos < 0 && old[nr_probes].prio < prio) |
| 159 | pos = nr_probes; |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 160 | if (old[nr_probes].func == tp_func->func && |
| 161 | old[nr_probes].data == tp_func->data) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 162 | return ERR_PTR(-EEXIST); |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 163 | } |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 164 | } |
| 165 | /* + 2 : one for new probe, one for NULL func */ |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 166 | new = allocate_probes(nr_probes + 2); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 167 | if (new == NULL) |
| 168 | return ERR_PTR(-ENOMEM); |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 169 | if (old) { |
| 170 | if (pos < 0) { |
| 171 | pos = nr_probes; |
| 172 | memcpy(new, old, nr_probes * sizeof(struct tracepoint_func)); |
| 173 | } else { |
| 174 | /* Copy higher priority probes ahead of the new probe */ |
| 175 | memcpy(new, old, pos * sizeof(struct tracepoint_func)); |
| 176 | /* Copy the rest after it. */ |
| 177 | memcpy(new + pos + 1, old + pos, |
| 178 | (nr_probes - pos) * sizeof(struct tracepoint_func)); |
| 179 | } |
| 180 | } else |
| 181 | pos = 0; |
| 182 | new[pos] = *tp_func; |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 183 | new[nr_probes + 1].func = NULL; |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 184 | *funcs = new; |
| 185 | debug_print_probes(*funcs); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 186 | return old; |
| 187 | } |
| 188 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 189 | static void *func_remove(struct tracepoint_func **funcs, |
| 190 | struct tracepoint_func *tp_func) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 191 | { |
| 192 | int nr_probes = 0, nr_del = 0, i; |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 193 | struct tracepoint_func *old, *new; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 194 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 195 | old = *funcs; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 196 | |
Frederic Weisbecker | f66af45 | 2008-10-22 19:14:55 +0200 | [diff] [blame] | 197 | if (!old) |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 198 | return ERR_PTR(-ENOENT); |
Frederic Weisbecker | f66af45 | 2008-10-22 19:14:55 +0200 | [diff] [blame] | 199 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 200 | debug_print_probes(*funcs); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 201 | /* (N -> M), (N > 1, M >= 0) probes */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 202 | if (tp_func->func) { |
Sahara | 4c69e6e | 2013-04-15 11:13:15 +0900 | [diff] [blame] | 203 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 204 | if (old[nr_probes].func == tp_func->func && |
| 205 | old[nr_probes].data == tp_func->data) |
Sahara | 4c69e6e | 2013-04-15 11:13:15 +0900 | [diff] [blame] | 206 | nr_del++; |
| 207 | } |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Sahara | 4c69e6e | 2013-04-15 11:13:15 +0900 | [diff] [blame] | 210 | /* |
| 211 | * If probe is NULL, then nr_probes = nr_del = 0, and then the |
| 212 | * entire entry will be removed. |
| 213 | */ |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 214 | if (nr_probes - nr_del == 0) { |
| 215 | /* N -> 0, (N > 1) */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 216 | *funcs = NULL; |
| 217 | debug_print_probes(*funcs); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 218 | return old; |
| 219 | } else { |
| 220 | int j = 0; |
| 221 | /* N -> M, (N > 1, M > 0) */ |
| 222 | /* + 1 for NULL */ |
Lai Jiangshan | 19dba33 | 2008-10-28 10:51:49 +0800 | [diff] [blame] | 223 | new = allocate_probes(nr_probes - nr_del + 1); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 224 | if (new == NULL) |
| 225 | return ERR_PTR(-ENOMEM); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 226 | for (i = 0; old[i].func; i++) |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 227 | if (old[i].func != tp_func->func |
| 228 | || old[i].data != tp_func->data) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 229 | new[j++] = old[i]; |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 230 | new[nr_probes - nr_del].func = NULL; |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 231 | *funcs = new; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 232 | } |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 233 | debug_print_probes(*funcs); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 234 | return old; |
| 235 | } |
| 236 | |
| 237 | /* |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 238 | * Add the probe function to a tracepoint. |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 239 | */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 240 | static int tracepoint_add_func(struct tracepoint *tp, |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 241 | struct tracepoint_func *func, int prio) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 242 | { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 243 | struct tracepoint_func *old, *tp_funcs; |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 244 | int ret; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 245 | |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 246 | if (tp->regfunc && !static_key_enabled(&tp->key)) { |
| 247 | ret = tp->regfunc(); |
| 248 | if (ret < 0) |
| 249 | return ret; |
| 250 | } |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 251 | |
Mathieu Desnoyers | b725dfe | 2014-04-09 09:24:43 -0400 | [diff] [blame] | 252 | tp_funcs = rcu_dereference_protected(tp->funcs, |
| 253 | lockdep_is_held(&tracepoints_mutex)); |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 254 | old = func_add(&tp_funcs, func, prio); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 255 | if (IS_ERR(old)) { |
Mathieu Desnoyers | d66a270 | 2018-03-15 08:44:24 -0400 | [diff] [blame] | 256 | WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 257 | return PTR_ERR(old); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 258 | } |
Josh Stone | 9741987 | 2009-08-24 14:43:13 -0700 | [diff] [blame] | 259 | |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 260 | /* |
Paul E. McKenney | 243d1a7 | 2017-10-09 11:30:11 -0700 | [diff] [blame] | 261 | * rcu_assign_pointer has as smp_store_release() which makes sure |
| 262 | * that the new probe callbacks array is consistent before setting |
| 263 | * a pointer to it. This array is referenced by __DO_TRACE from |
| 264 | * include/linux/tracepoint.h using rcu_dereference_sched(). |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 265 | */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 266 | rcu_assign_pointer(tp->funcs, tp_funcs); |
| 267 | if (!static_key_enabled(&tp->key)) |
| 268 | static_key_slow_inc(&tp->key); |
Mathieu Desnoyers | 8058bd0 | 2014-05-08 07:47:49 -0400 | [diff] [blame] | 269 | release_probes(old); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 270 | return 0; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 274 | * Remove a probe function from a tracepoint. |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 275 | * Note: only waiting an RCU period after setting elem->call to the empty |
| 276 | * function insures that the original callback is not used anymore. This insured |
| 277 | * by preempt_disable around the call site. |
| 278 | */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 279 | static int tracepoint_remove_func(struct tracepoint *tp, |
| 280 | struct tracepoint_func *func) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 281 | { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 282 | struct tracepoint_func *old, *tp_funcs; |
Josh Stone | 9741987 | 2009-08-24 14:43:13 -0700 | [diff] [blame] | 283 | |
Mathieu Desnoyers | b725dfe | 2014-04-09 09:24:43 -0400 | [diff] [blame] | 284 | tp_funcs = rcu_dereference_protected(tp->funcs, |
| 285 | lockdep_is_held(&tracepoints_mutex)); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 286 | old = func_remove(&tp_funcs, func); |
| 287 | if (IS_ERR(old)) { |
Mathieu Desnoyers | d66a270 | 2018-03-15 08:44:24 -0400 | [diff] [blame] | 288 | WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 289 | return PTR_ERR(old); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 290 | } |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 291 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 292 | if (!tp_funcs) { |
| 293 | /* Removed last function */ |
| 294 | if (tp->unregfunc && static_key_enabled(&tp->key)) |
| 295 | tp->unregfunc(); |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 296 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 297 | if (static_key_enabled(&tp->key)) |
| 298 | static_key_slow_dec(&tp->key); |
Lai Jiangshan | 127cafb | 2008-10-28 10:51:53 +0800 | [diff] [blame] | 299 | } |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 300 | rcu_assign_pointer(tp->funcs, tp_funcs); |
Mathieu Desnoyers | 8058bd0 | 2014-05-08 07:47:49 -0400 | [diff] [blame] | 301 | release_probes(old); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 302 | return 0; |
Lai Jiangshan | 127cafb | 2008-10-28 10:51:53 +0800 | [diff] [blame] | 303 | } |
| 304 | |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 305 | /** |
Lee, Chun-Yi | f39e239 | 2017-06-16 16:26:43 +0800 | [diff] [blame] | 306 | * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 307 | * @tp: tracepoint |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 308 | * @probe: probe handler |
Fabian Frederick | cac92ba | 2014-06-04 16:11:23 -0700 | [diff] [blame] | 309 | * @data: tracepoint data |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 310 | * @prio: priority of this function over other registered functions |
| 311 | * |
| 312 | * Returns 0 if ok, error value on error. |
| 313 | * Note: if @tp is within a module, the caller is responsible for |
| 314 | * unregistering the probe before the module is gone. This can be |
| 315 | * performed either with a tracepoint module going notifier, or from |
| 316 | * within module exit functions. |
| 317 | */ |
| 318 | int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe, |
| 319 | void *data, int prio) |
| 320 | { |
| 321 | struct tracepoint_func tp_func; |
| 322 | int ret; |
| 323 | |
| 324 | mutex_lock(&tracepoints_mutex); |
| 325 | tp_func.func = probe; |
| 326 | tp_func.data = data; |
| 327 | tp_func.prio = prio; |
| 328 | ret = tracepoint_add_func(tp, &tp_func, prio); |
| 329 | mutex_unlock(&tracepoints_mutex); |
| 330 | return ret; |
| 331 | } |
| 332 | EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio); |
| 333 | |
| 334 | /** |
| 335 | * tracepoint_probe_register - Connect a probe to a tracepoint |
| 336 | * @tp: tracepoint |
| 337 | * @probe: probe handler |
| 338 | * @data: tracepoint data |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 339 | * |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 340 | * Returns 0 if ok, error value on error. |
| 341 | * Note: if @tp is within a module, the caller is responsible for |
| 342 | * unregistering the probe before the module is gone. This can be |
| 343 | * performed either with a tracepoint module going notifier, or from |
| 344 | * within module exit functions. |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 345 | */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 346 | int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 347 | { |
Steven Rostedt (Red Hat) | 7904b5c | 2015-09-22 17:13:19 -0400 | [diff] [blame] | 348 | return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO); |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 349 | } |
| 350 | EXPORT_SYMBOL_GPL(tracepoint_probe_register); |
| 351 | |
| 352 | /** |
| 353 | * tracepoint_probe_unregister - Disconnect a probe from a tracepoint |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 354 | * @tp: tracepoint |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 355 | * @probe: probe function pointer |
Fabian Frederick | cac92ba | 2014-06-04 16:11:23 -0700 | [diff] [blame] | 356 | * @data: tracepoint data |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 357 | * |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 358 | * Returns 0 if ok, error value on error. |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 359 | */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 360 | int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data) |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 361 | { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 362 | struct tracepoint_func tp_func; |
| 363 | int ret; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 364 | |
| 365 | mutex_lock(&tracepoints_mutex); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 366 | tp_func.func = probe; |
| 367 | tp_func.data = data; |
| 368 | ret = tracepoint_remove_func(tp, &tp_func); |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 369 | mutex_unlock(&tracepoints_mutex); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 370 | return ret; |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 371 | } |
| 372 | EXPORT_SYMBOL_GPL(tracepoint_probe_unregister); |
| 373 | |
Mathieu Desnoyers | 9c0be3f | 2018-10-13 15:10:50 -0400 | [diff] [blame] | 374 | static void for_each_tracepoint_range( |
| 375 | tracepoint_ptr_t *begin, tracepoint_ptr_t *end, |
Ard Biesheuvel | 46e0c9b | 2018-08-21 21:56:22 -0700 | [diff] [blame] | 376 | void (*fct)(struct tracepoint *tp, void *priv), |
| 377 | void *priv) |
| 378 | { |
Mathieu Desnoyers | 9c0be3f | 2018-10-13 15:10:50 -0400 | [diff] [blame] | 379 | tracepoint_ptr_t *iter; |
| 380 | |
Ard Biesheuvel | 46e0c9b | 2018-08-21 21:56:22 -0700 | [diff] [blame] | 381 | if (!begin) |
| 382 | return; |
Mathieu Desnoyers | 9c0be3f | 2018-10-13 15:10:50 -0400 | [diff] [blame] | 383 | for (iter = begin; iter < end; iter++) |
| 384 | fct(tracepoint_ptr_deref(iter), priv); |
Ard Biesheuvel | 46e0c9b | 2018-08-21 21:56:22 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Ingo Molnar | 227a837 | 2008-11-16 09:50:34 +0100 | [diff] [blame] | 387 | #ifdef CONFIG_MODULES |
Steven Rostedt (Red Hat) | 45ab281 | 2014-02-26 13:37:38 -0500 | [diff] [blame] | 388 | bool trace_module_has_bad_taint(struct module *mod) |
| 389 | { |
Mathieu Desnoyers | 66cc69e | 2014-03-13 12:11:30 +1030 | [diff] [blame] | 390 | return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) | |
| 391 | (1 << TAINT_UNSIGNED_MODULE)); |
Steven Rostedt (Red Hat) | 45ab281 | 2014-02-26 13:37:38 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 394 | static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list); |
| 395 | |
| 396 | /** |
| 397 | * register_tracepoint_notifier - register tracepoint coming/going notifier |
| 398 | * @nb: notifier block |
| 399 | * |
| 400 | * Notifiers registered with this function are called on module |
| 401 | * coming/going with the tracepoint_module_list_mutex held. |
| 402 | * The notifier block callback should expect a "struct tp_module" data |
| 403 | * pointer. |
| 404 | */ |
| 405 | int register_tracepoint_module_notifier(struct notifier_block *nb) |
| 406 | { |
| 407 | struct tp_module *tp_mod; |
| 408 | int ret; |
| 409 | |
| 410 | mutex_lock(&tracepoint_module_list_mutex); |
| 411 | ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb); |
| 412 | if (ret) |
| 413 | goto end; |
| 414 | list_for_each_entry(tp_mod, &tracepoint_module_list, list) |
| 415 | (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod); |
| 416 | end: |
| 417 | mutex_unlock(&tracepoint_module_list_mutex); |
| 418 | return ret; |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier); |
| 421 | |
| 422 | /** |
| 423 | * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier |
| 424 | * @nb: notifier block |
| 425 | * |
| 426 | * The notifier block callback should expect a "struct tp_module" data |
| 427 | * pointer. |
| 428 | */ |
| 429 | int unregister_tracepoint_module_notifier(struct notifier_block *nb) |
| 430 | { |
| 431 | struct tp_module *tp_mod; |
| 432 | int ret; |
| 433 | |
| 434 | mutex_lock(&tracepoint_module_list_mutex); |
| 435 | ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb); |
| 436 | if (ret) |
| 437 | goto end; |
| 438 | list_for_each_entry(tp_mod, &tracepoint_module_list, list) |
| 439 | (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod); |
| 440 | end: |
| 441 | mutex_unlock(&tracepoint_module_list_mutex); |
| 442 | return ret; |
| 443 | |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier); |
| 446 | |
| 447 | /* |
| 448 | * Ensure the tracer unregistered the module's probes before the module |
| 449 | * teardown is performed. Prevents leaks of probe and data pointers. |
| 450 | */ |
Ard Biesheuvel | 46e0c9b | 2018-08-21 21:56:22 -0700 | [diff] [blame] | 451 | static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv) |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 452 | { |
Ard Biesheuvel | 46e0c9b | 2018-08-21 21:56:22 -0700 | [diff] [blame] | 453 | WARN_ON_ONCE(tp->funcs); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 454 | } |
| 455 | |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 456 | static int tracepoint_module_coming(struct module *mod) |
| 457 | { |
Mathieu Desnoyers | 0dea6d5 | 2014-03-21 01:19:01 -0400 | [diff] [blame] | 458 | struct tp_module *tp_mod; |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 459 | int ret = 0; |
| 460 | |
Steven Rostedt (Red Hat) | 7dec935 | 2014-02-26 10:54:36 -0500 | [diff] [blame] | 461 | if (!mod->num_tracepoints) |
| 462 | return 0; |
| 463 | |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 464 | /* |
Steven Rostedt | c10076c | 2012-01-13 21:40:59 -0500 | [diff] [blame] | 465 | * We skip modules that taint the kernel, especially those with different |
| 466 | * module headers (for forced load), to make sure we don't cause a crash. |
Mathieu Desnoyers | 66cc69e | 2014-03-13 12:11:30 +1030 | [diff] [blame] | 467 | * Staging, out-of-tree, and unsigned GPL modules are fine. |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 468 | */ |
Steven Rostedt (Red Hat) | 45ab281 | 2014-02-26 13:37:38 -0500 | [diff] [blame] | 469 | if (trace_module_has_bad_taint(mod)) |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 470 | return 0; |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 471 | mutex_lock(&tracepoint_module_list_mutex); |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 472 | tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL); |
| 473 | if (!tp_mod) { |
| 474 | ret = -ENOMEM; |
| 475 | goto end; |
| 476 | } |
Steven Rostedt (Red Hat) | eb7d035 | 2014-04-08 20:09:40 -0400 | [diff] [blame] | 477 | tp_mod->mod = mod; |
Mathieu Desnoyers | 0dea6d5 | 2014-03-21 01:19:01 -0400 | [diff] [blame] | 478 | list_add_tail(&tp_mod->list, &tracepoint_module_list); |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 479 | blocking_notifier_call_chain(&tracepoint_notify_list, |
| 480 | MODULE_STATE_COMING, tp_mod); |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 481 | end: |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 482 | mutex_unlock(&tracepoint_module_list_mutex); |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 483 | return ret; |
| 484 | } |
| 485 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 486 | static void tracepoint_module_going(struct module *mod) |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 487 | { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 488 | struct tp_module *tp_mod; |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 489 | |
Steven Rostedt (Red Hat) | 7dec935 | 2014-02-26 10:54:36 -0500 | [diff] [blame] | 490 | if (!mod->num_tracepoints) |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 491 | return; |
Steven Rostedt (Red Hat) | 7dec935 | 2014-02-26 10:54:36 -0500 | [diff] [blame] | 492 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 493 | mutex_lock(&tracepoint_module_list_mutex); |
| 494 | list_for_each_entry(tp_mod, &tracepoint_module_list, list) { |
Steven Rostedt (Red Hat) | eb7d035 | 2014-04-08 20:09:40 -0400 | [diff] [blame] | 495 | if (tp_mod->mod == mod) { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 496 | blocking_notifier_call_chain(&tracepoint_notify_list, |
| 497 | MODULE_STATE_GOING, tp_mod); |
| 498 | list_del(&tp_mod->list); |
| 499 | kfree(tp_mod); |
| 500 | /* |
| 501 | * Called the going notifier before checking for |
| 502 | * quiescence. |
| 503 | */ |
Ard Biesheuvel | 46e0c9b | 2018-08-21 21:56:22 -0700 | [diff] [blame] | 504 | for_each_tracepoint_range(mod->tracepoints_ptrs, |
| 505 | mod->tracepoints_ptrs + mod->num_tracepoints, |
| 506 | tp_module_going_check_quiescent, NULL); |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | } |
| 510 | /* |
| 511 | * In the case of modules that were tainted at "coming", we'll simply |
| 512 | * walk through the list without finding it. We cannot use the "tainted" |
| 513 | * flag on "going", in case a module taints the kernel only after being |
| 514 | * loaded. |
| 515 | */ |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 516 | mutex_unlock(&tracepoint_module_list_mutex); |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 517 | } |
Ingo Molnar | 227a837 | 2008-11-16 09:50:34 +0100 | [diff] [blame] | 518 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 519 | static int tracepoint_module_notify(struct notifier_block *self, |
| 520 | unsigned long val, void *data) |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 521 | { |
| 522 | struct module *mod = data; |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 523 | int ret = 0; |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 524 | |
| 525 | switch (val) { |
| 526 | case MODULE_STATE_COMING: |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 527 | ret = tracepoint_module_coming(mod); |
| 528 | break; |
| 529 | case MODULE_STATE_LIVE: |
| 530 | break; |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 531 | case MODULE_STATE_GOING: |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 532 | tracepoint_module_going(mod); |
| 533 | break; |
| 534 | case MODULE_STATE_UNFORMED: |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 535 | break; |
| 536 | } |
Mathieu Desnoyers | b75ef8b | 2011-08-10 15:18:39 -0400 | [diff] [blame] | 537 | return ret; |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 538 | } |
| 539 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 540 | static struct notifier_block tracepoint_module_nb = { |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 541 | .notifier_call = tracepoint_module_notify, |
| 542 | .priority = 0, |
| 543 | }; |
| 544 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 545 | static __init int init_tracepoints(void) |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 546 | { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 547 | int ret; |
| 548 | |
| 549 | ret = register_module_notifier(&tracepoint_module_nb); |
Steven Rostedt (Red Hat) | eb7d035 | 2014-04-08 20:09:40 -0400 | [diff] [blame] | 550 | if (ret) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 551 | pr_warn("Failed to register tracepoint module enter notifier\n"); |
Steven Rostedt (Red Hat) | eb7d035 | 2014-04-08 20:09:40 -0400 | [diff] [blame] | 552 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 553 | return ret; |
Mathieu Desnoyers | 32f8574 | 2008-11-14 17:47:46 -0500 | [diff] [blame] | 554 | } |
| 555 | __initcall(init_tracepoints); |
Ingo Molnar | 227a837 | 2008-11-16 09:50:34 +0100 | [diff] [blame] | 556 | #endif /* CONFIG_MODULES */ |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 557 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 558 | /** |
| 559 | * for_each_kernel_tracepoint - iteration on all kernel tracepoints |
| 560 | * @fct: callback |
| 561 | * @priv: private data |
| 562 | */ |
| 563 | void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv), |
| 564 | void *priv) |
| 565 | { |
| 566 | for_each_tracepoint_range(__start___tracepoints_ptrs, |
| 567 | __stop___tracepoints_ptrs, fct, priv); |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint); |
| 570 | |
Josh Stone | 3d27d8cb | 2009-08-24 14:43:12 -0700 | [diff] [blame] | 571 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
Ingo Molnar | 60d970c | 2009-08-13 23:37:26 +0200 | [diff] [blame] | 572 | |
Josh Stone | 9741987 | 2009-08-24 14:43:13 -0700 | [diff] [blame] | 573 | /* NB: reg/unreg are called while guarded with the tracepoints_mutex */ |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 574 | static int sys_tracepoint_refcount; |
| 575 | |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 576 | int syscall_regfunc(void) |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 577 | { |
Oleg Nesterov | 8063e41 | 2014-04-13 20:59:18 +0200 | [diff] [blame] | 578 | struct task_struct *p, *t; |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 579 | |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 580 | if (!sys_tracepoint_refcount) { |
Oleg Nesterov | 8063e41 | 2014-04-13 20:59:18 +0200 | [diff] [blame] | 581 | read_lock(&tasklist_lock); |
| 582 | for_each_process_thread(p, t) { |
Oleg Nesterov | ea73c79 | 2014-04-13 20:59:38 +0200 | [diff] [blame] | 583 | set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); |
Oleg Nesterov | 8063e41 | 2014-04-13 20:59:18 +0200 | [diff] [blame] | 584 | } |
| 585 | read_unlock(&tasklist_lock); |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 586 | } |
| 587 | sys_tracepoint_refcount++; |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 588 | |
| 589 | return 0; |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void syscall_unregfunc(void) |
| 593 | { |
Oleg Nesterov | 8063e41 | 2014-04-13 20:59:18 +0200 | [diff] [blame] | 594 | struct task_struct *p, *t; |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 595 | |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 596 | sys_tracepoint_refcount--; |
| 597 | if (!sys_tracepoint_refcount) { |
Oleg Nesterov | 8063e41 | 2014-04-13 20:59:18 +0200 | [diff] [blame] | 598 | read_lock(&tasklist_lock); |
| 599 | for_each_process_thread(p, t) { |
Josh Stone | 6670000 | 2009-08-24 14:43:11 -0700 | [diff] [blame] | 600 | clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); |
Oleg Nesterov | 8063e41 | 2014-04-13 20:59:18 +0200 | [diff] [blame] | 601 | } |
| 602 | read_unlock(&tasklist_lock); |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 603 | } |
Jason Baron | a871bd3 | 2009-08-10 16:52:31 -0400 | [diff] [blame] | 604 | } |
Ingo Molnar | 60d970c | 2009-08-13 23:37:26 +0200 | [diff] [blame] | 605 | #endif |