]>
Commit | Line | Data |
---|---|---|
97e1c18e | 1 | /* |
de7b2973 | 2 | * Copyright (C) 2008-2014 Mathieu Desnoyers |
97e1c18e MD |
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> | |
3f07c014 | 27 | #include <linux/sched/signal.h> |
29930025 | 28 | #include <linux/sched/task.h> |
c5905afb | 29 | #include <linux/static_key.h> |
97e1c18e | 30 | |
65498646 MD |
31 | extern struct tracepoint * const __start___tracepoints_ptrs[]; |
32 | extern struct tracepoint * const __stop___tracepoints_ptrs[]; | |
97e1c18e | 33 | |
e6753f23 JFG |
34 | DEFINE_SRCU(tracepoint_srcu); |
35 | EXPORT_SYMBOL_GPL(tracepoint_srcu); | |
36 | ||
97e1c18e MD |
37 | /* Set to 1 to enable tracepoint debug output */ |
38 | static const int tracepoint_debug; | |
39 | ||
de7b2973 | 40 | #ifdef CONFIG_MODULES |
97e1c18e | 41 | /* |
de7b2973 | 42 | * Tracepoint module list mutex protects the local module list. |
97e1c18e | 43 | */ |
de7b2973 | 44 | static DEFINE_MUTEX(tracepoint_module_list_mutex); |
97e1c18e | 45 | |
de7b2973 | 46 | /* Local list of struct tp_module */ |
b75ef8b4 MD |
47 | static LIST_HEAD(tracepoint_module_list); |
48 | #endif /* CONFIG_MODULES */ | |
49 | ||
97e1c18e | 50 | /* |
de7b2973 MD |
51 | * tracepoints_mutex protects the builtin and module tracepoints. |
52 | * tracepoints_mutex nests inside tracepoint_module_list_mutex. | |
97e1c18e | 53 | */ |
de7b2973 | 54 | static DEFINE_MUTEX(tracepoints_mutex); |
97e1c18e MD |
55 | |
56 | /* | |
57 | * Note about RCU : | |
fd589a8f | 58 | * It is used to delay the free of multiple probes array until a quiescent |
97e1c18e | 59 | * state is reached. |
97e1c18e | 60 | */ |
19dba33c | 61 | struct tp_probes { |
0dea6d52 | 62 | struct rcu_head rcu; |
38516ab5 | 63 | struct tracepoint_func probes[0]; |
19dba33c | 64 | }; |
97e1c18e | 65 | |
19dba33c | 66 | static inline void *allocate_probes(int count) |
97e1c18e | 67 | { |
38516ab5 | 68 | struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func) |
19dba33c LJ |
69 | + sizeof(struct tp_probes), GFP_KERNEL); |
70 | return p == NULL ? NULL : p->probes; | |
97e1c18e MD |
71 | } |
72 | ||
e6753f23 | 73 | static void srcu_free_old_probes(struct rcu_head *head) |
97e1c18e | 74 | { |
0dea6d52 | 75 | kfree(container_of(head, struct tp_probes, rcu)); |
19dba33c LJ |
76 | } |
77 | ||
e6753f23 JFG |
78 | static void rcu_free_old_probes(struct rcu_head *head) |
79 | { | |
80 | call_srcu(&tracepoint_srcu, head, srcu_free_old_probes); | |
81 | } | |
82 | ||
38516ab5 | 83 | static inline void release_probes(struct tracepoint_func *old) |
19dba33c LJ |
84 | { |
85 | if (old) { | |
86 | struct tp_probes *tp_probes = container_of(old, | |
87 | struct tp_probes, probes[0]); | |
e6753f23 JFG |
88 | /* |
89 | * Tracepoint probes are protected by both sched RCU and SRCU, | |
90 | * by calling the SRCU callback in the sched RCU callback we | |
91 | * cover both cases. So let us chain the SRCU and sched RCU | |
92 | * callbacks to wait for both grace periods. | |
93 | */ | |
0dea6d52 | 94 | call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes); |
19dba33c | 95 | } |
97e1c18e MD |
96 | } |
97 | ||
de7b2973 | 98 | static void debug_print_probes(struct tracepoint_func *funcs) |
97e1c18e MD |
99 | { |
100 | int i; | |
101 | ||
de7b2973 | 102 | if (!tracepoint_debug || !funcs) |
97e1c18e MD |
103 | return; |
104 | ||
de7b2973 MD |
105 | for (i = 0; funcs[i].func; i++) |
106 | printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func); | |
97e1c18e MD |
107 | } |
108 | ||
7904b5c4 SRRH |
109 | static struct tracepoint_func * |
110 | func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func, | |
111 | int prio) | |
97e1c18e | 112 | { |
38516ab5 | 113 | struct tracepoint_func *old, *new; |
7904b5c4 SRRH |
114 | int nr_probes = 0; |
115 | int pos = -1; | |
97e1c18e | 116 | |
de7b2973 | 117 | if (WARN_ON(!tp_func->func)) |
4c69e6ea | 118 | return ERR_PTR(-EINVAL); |
97e1c18e | 119 | |
de7b2973 MD |
120 | debug_print_probes(*funcs); |
121 | old = *funcs; | |
97e1c18e MD |
122 | if (old) { |
123 | /* (N -> N+1), (N != 0, 1) probes */ | |
7904b5c4 SRRH |
124 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
125 | /* Insert before probes of lower priority */ | |
126 | if (pos < 0 && old[nr_probes].prio < prio) | |
127 | pos = nr_probes; | |
de7b2973 MD |
128 | if (old[nr_probes].func == tp_func->func && |
129 | old[nr_probes].data == tp_func->data) | |
97e1c18e | 130 | return ERR_PTR(-EEXIST); |
7904b5c4 | 131 | } |
97e1c18e MD |
132 | } |
133 | /* + 2 : one for new probe, one for NULL func */ | |
19dba33c | 134 | new = allocate_probes(nr_probes + 2); |
97e1c18e MD |
135 | if (new == NULL) |
136 | return ERR_PTR(-ENOMEM); | |
7904b5c4 SRRH |
137 | if (old) { |
138 | if (pos < 0) { | |
139 | pos = nr_probes; | |
140 | memcpy(new, old, nr_probes * sizeof(struct tracepoint_func)); | |
141 | } else { | |
142 | /* Copy higher priority probes ahead of the new probe */ | |
143 | memcpy(new, old, pos * sizeof(struct tracepoint_func)); | |
144 | /* Copy the rest after it. */ | |
145 | memcpy(new + pos + 1, old + pos, | |
146 | (nr_probes - pos) * sizeof(struct tracepoint_func)); | |
147 | } | |
148 | } else | |
149 | pos = 0; | |
150 | new[pos] = *tp_func; | |
38516ab5 | 151 | new[nr_probes + 1].func = NULL; |
de7b2973 MD |
152 | *funcs = new; |
153 | debug_print_probes(*funcs); | |
97e1c18e MD |
154 | return old; |
155 | } | |
156 | ||
de7b2973 MD |
157 | static void *func_remove(struct tracepoint_func **funcs, |
158 | struct tracepoint_func *tp_func) | |
97e1c18e MD |
159 | { |
160 | int nr_probes = 0, nr_del = 0, i; | |
38516ab5 | 161 | struct tracepoint_func *old, *new; |
97e1c18e | 162 | |
de7b2973 | 163 | old = *funcs; |
97e1c18e | 164 | |
f66af459 | 165 | if (!old) |
19dba33c | 166 | return ERR_PTR(-ENOENT); |
f66af459 | 167 | |
de7b2973 | 168 | debug_print_probes(*funcs); |
97e1c18e | 169 | /* (N -> M), (N > 1, M >= 0) probes */ |
de7b2973 | 170 | if (tp_func->func) { |
4c69e6ea | 171 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
de7b2973 MD |
172 | if (old[nr_probes].func == tp_func->func && |
173 | old[nr_probes].data == tp_func->data) | |
4c69e6ea S |
174 | nr_del++; |
175 | } | |
97e1c18e MD |
176 | } |
177 | ||
4c69e6ea S |
178 | /* |
179 | * If probe is NULL, then nr_probes = nr_del = 0, and then the | |
180 | * entire entry will be removed. | |
181 | */ | |
97e1c18e MD |
182 | if (nr_probes - nr_del == 0) { |
183 | /* N -> 0, (N > 1) */ | |
de7b2973 MD |
184 | *funcs = NULL; |
185 | debug_print_probes(*funcs); | |
97e1c18e MD |
186 | return old; |
187 | } else { | |
188 | int j = 0; | |
189 | /* N -> M, (N > 1, M > 0) */ | |
190 | /* + 1 for NULL */ | |
19dba33c | 191 | new = allocate_probes(nr_probes - nr_del + 1); |
97e1c18e MD |
192 | if (new == NULL) |
193 | return ERR_PTR(-ENOMEM); | |
38516ab5 | 194 | for (i = 0; old[i].func; i++) |
de7b2973 MD |
195 | if (old[i].func != tp_func->func |
196 | || old[i].data != tp_func->data) | |
97e1c18e | 197 | new[j++] = old[i]; |
38516ab5 | 198 | new[nr_probes - nr_del].func = NULL; |
de7b2973 | 199 | *funcs = new; |
97e1c18e | 200 | } |
de7b2973 | 201 | debug_print_probes(*funcs); |
97e1c18e MD |
202 | return old; |
203 | } | |
204 | ||
205 | /* | |
de7b2973 | 206 | * Add the probe function to a tracepoint. |
97e1c18e | 207 | */ |
de7b2973 | 208 | static int tracepoint_add_func(struct tracepoint *tp, |
7904b5c4 | 209 | struct tracepoint_func *func, int prio) |
97e1c18e | 210 | { |
de7b2973 | 211 | struct tracepoint_func *old, *tp_funcs; |
8cf868af | 212 | int ret; |
97e1c18e | 213 | |
8cf868af SRRH |
214 | if (tp->regfunc && !static_key_enabled(&tp->key)) { |
215 | ret = tp->regfunc(); | |
216 | if (ret < 0) | |
217 | return ret; | |
218 | } | |
97e1c18e | 219 | |
b725dfea MD |
220 | tp_funcs = rcu_dereference_protected(tp->funcs, |
221 | lockdep_is_held(&tracepoints_mutex)); | |
7904b5c4 | 222 | old = func_add(&tp_funcs, func, prio); |
de7b2973 | 223 | if (IS_ERR(old)) { |
d66a270b | 224 | WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); |
de7b2973 MD |
225 | return PTR_ERR(old); |
226 | } | |
97419875 | 227 | |
97e1c18e | 228 | /* |
243d1a79 PM |
229 | * rcu_assign_pointer has as smp_store_release() which makes sure |
230 | * that the new probe callbacks array is consistent before setting | |
231 | * a pointer to it. This array is referenced by __DO_TRACE from | |
232 | * include/linux/tracepoint.h using rcu_dereference_sched(). | |
97e1c18e | 233 | */ |
de7b2973 MD |
234 | rcu_assign_pointer(tp->funcs, tp_funcs); |
235 | if (!static_key_enabled(&tp->key)) | |
236 | static_key_slow_inc(&tp->key); | |
8058bd0f | 237 | release_probes(old); |
de7b2973 | 238 | return 0; |
97e1c18e MD |
239 | } |
240 | ||
241 | /* | |
de7b2973 | 242 | * Remove a probe function from a tracepoint. |
97e1c18e MD |
243 | * Note: only waiting an RCU period after setting elem->call to the empty |
244 | * function insures that the original callback is not used anymore. This insured | |
245 | * by preempt_disable around the call site. | |
246 | */ | |
de7b2973 MD |
247 | static int tracepoint_remove_func(struct tracepoint *tp, |
248 | struct tracepoint_func *func) | |
97e1c18e | 249 | { |
de7b2973 | 250 | struct tracepoint_func *old, *tp_funcs; |
97e1c18e | 251 | |
b725dfea MD |
252 | tp_funcs = rcu_dereference_protected(tp->funcs, |
253 | lockdep_is_held(&tracepoints_mutex)); | |
de7b2973 MD |
254 | old = func_remove(&tp_funcs, func); |
255 | if (IS_ERR(old)) { | |
d66a270b | 256 | WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); |
de7b2973 | 257 | return PTR_ERR(old); |
97e1c18e | 258 | } |
b75ef8b4 | 259 | |
de7b2973 MD |
260 | if (!tp_funcs) { |
261 | /* Removed last function */ | |
262 | if (tp->unregfunc && static_key_enabled(&tp->key)) | |
263 | tp->unregfunc(); | |
b75ef8b4 | 264 | |
de7b2973 MD |
265 | if (static_key_enabled(&tp->key)) |
266 | static_key_slow_dec(&tp->key); | |
127cafbb | 267 | } |
de7b2973 | 268 | rcu_assign_pointer(tp->funcs, tp_funcs); |
8058bd0f | 269 | release_probes(old); |
de7b2973 | 270 | return 0; |
127cafbb LJ |
271 | } |
272 | ||
97e1c18e | 273 | /** |
f39e2391 | 274 | * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority |
de7b2973 | 275 | * @tp: tracepoint |
97e1c18e | 276 | * @probe: probe handler |
cac92ba7 | 277 | * @data: tracepoint data |
7904b5c4 | 278 | * @prio: priority of this function over other registered functions |
97e1c18e | 279 | * |
de7b2973 MD |
280 | * Returns 0 if ok, error value on error. |
281 | * Note: if @tp is within a module, the caller is responsible for | |
282 | * unregistering the probe before the module is gone. This can be | |
283 | * performed either with a tracepoint module going notifier, or from | |
284 | * within module exit functions. | |
97e1c18e | 285 | */ |
7904b5c4 SRRH |
286 | int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe, |
287 | void *data, int prio) | |
97e1c18e | 288 | { |
de7b2973 MD |
289 | struct tracepoint_func tp_func; |
290 | int ret; | |
97e1c18e MD |
291 | |
292 | mutex_lock(&tracepoints_mutex); | |
de7b2973 MD |
293 | tp_func.func = probe; |
294 | tp_func.data = data; | |
7904b5c4 SRRH |
295 | tp_func.prio = prio; |
296 | ret = tracepoint_add_func(tp, &tp_func, prio); | |
b75ef8b4 | 297 | mutex_unlock(&tracepoints_mutex); |
b196e2b9 | 298 | return ret; |
97e1c18e | 299 | } |
7904b5c4 SRRH |
300 | EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio); |
301 | ||
302 | /** | |
303 | * tracepoint_probe_register - Connect a probe to a tracepoint | |
304 | * @tp: tracepoint | |
305 | * @probe: probe handler | |
306 | * @data: tracepoint data | |
7904b5c4 SRRH |
307 | * |
308 | * Returns 0 if ok, error value on error. | |
309 | * Note: if @tp is within a module, the caller is responsible for | |
310 | * unregistering the probe before the module is gone. This can be | |
311 | * performed either with a tracepoint module going notifier, or from | |
312 | * within module exit functions. | |
313 | */ | |
314 | int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data) | |
315 | { | |
316 | return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO); | |
317 | } | |
97e1c18e MD |
318 | EXPORT_SYMBOL_GPL(tracepoint_probe_register); |
319 | ||
320 | /** | |
321 | * tracepoint_probe_unregister - Disconnect a probe from a tracepoint | |
de7b2973 | 322 | * @tp: tracepoint |
97e1c18e | 323 | * @probe: probe function pointer |
cac92ba7 | 324 | * @data: tracepoint data |
97e1c18e | 325 | * |
de7b2973 | 326 | * Returns 0 if ok, error value on error. |
97e1c18e | 327 | */ |
de7b2973 | 328 | int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data) |
97e1c18e | 329 | { |
de7b2973 MD |
330 | struct tracepoint_func tp_func; |
331 | int ret; | |
97e1c18e MD |
332 | |
333 | mutex_lock(&tracepoints_mutex); | |
de7b2973 MD |
334 | tp_func.func = probe; |
335 | tp_func.data = data; | |
336 | ret = tracepoint_remove_func(tp, &tp_func); | |
b75ef8b4 | 337 | mutex_unlock(&tracepoints_mutex); |
de7b2973 | 338 | return ret; |
97e1c18e MD |
339 | } |
340 | EXPORT_SYMBOL_GPL(tracepoint_probe_unregister); | |
341 | ||
227a8375 | 342 | #ifdef CONFIG_MODULES |
45ab2813 SRRH |
343 | bool trace_module_has_bad_taint(struct module *mod) |
344 | { | |
66cc69e3 MD |
345 | return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) | |
346 | (1 << TAINT_UNSIGNED_MODULE)); | |
45ab2813 SRRH |
347 | } |
348 | ||
de7b2973 MD |
349 | static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list); |
350 | ||
351 | /** | |
352 | * register_tracepoint_notifier - register tracepoint coming/going notifier | |
353 | * @nb: notifier block | |
354 | * | |
355 | * Notifiers registered with this function are called on module | |
356 | * coming/going with the tracepoint_module_list_mutex held. | |
357 | * The notifier block callback should expect a "struct tp_module" data | |
358 | * pointer. | |
359 | */ | |
360 | int register_tracepoint_module_notifier(struct notifier_block *nb) | |
361 | { | |
362 | struct tp_module *tp_mod; | |
363 | int ret; | |
364 | ||
365 | mutex_lock(&tracepoint_module_list_mutex); | |
366 | ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb); | |
367 | if (ret) | |
368 | goto end; | |
369 | list_for_each_entry(tp_mod, &tracepoint_module_list, list) | |
370 | (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod); | |
371 | end: | |
372 | mutex_unlock(&tracepoint_module_list_mutex); | |
373 | return ret; | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier); | |
376 | ||
377 | /** | |
378 | * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier | |
379 | * @nb: notifier block | |
380 | * | |
381 | * The notifier block callback should expect a "struct tp_module" data | |
382 | * pointer. | |
383 | */ | |
384 | int unregister_tracepoint_module_notifier(struct notifier_block *nb) | |
385 | { | |
386 | struct tp_module *tp_mod; | |
387 | int ret; | |
388 | ||
389 | mutex_lock(&tracepoint_module_list_mutex); | |
390 | ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb); | |
391 | if (ret) | |
392 | goto end; | |
393 | list_for_each_entry(tp_mod, &tracepoint_module_list, list) | |
394 | (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod); | |
395 | end: | |
396 | mutex_unlock(&tracepoint_module_list_mutex); | |
397 | return ret; | |
398 | ||
399 | } | |
400 | EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier); | |
401 | ||
402 | /* | |
403 | * Ensure the tracer unregistered the module's probes before the module | |
404 | * teardown is performed. Prevents leaks of probe and data pointers. | |
405 | */ | |
406 | static void tp_module_going_check_quiescent(struct tracepoint * const *begin, | |
407 | struct tracepoint * const *end) | |
408 | { | |
409 | struct tracepoint * const *iter; | |
410 | ||
411 | if (!begin) | |
412 | return; | |
413 | for (iter = begin; iter < end; iter++) | |
414 | WARN_ON_ONCE((*iter)->funcs); | |
415 | } | |
416 | ||
b75ef8b4 MD |
417 | static int tracepoint_module_coming(struct module *mod) |
418 | { | |
0dea6d52 | 419 | struct tp_module *tp_mod; |
b75ef8b4 MD |
420 | int ret = 0; |
421 | ||
7dec935a SRRH |
422 | if (!mod->num_tracepoints) |
423 | return 0; | |
424 | ||
b75ef8b4 | 425 | /* |
c10076c4 SR |
426 | * We skip modules that taint the kernel, especially those with different |
427 | * module headers (for forced load), to make sure we don't cause a crash. | |
66cc69e3 | 428 | * Staging, out-of-tree, and unsigned GPL modules are fine. |
b75ef8b4 | 429 | */ |
45ab2813 | 430 | if (trace_module_has_bad_taint(mod)) |
b75ef8b4 | 431 | return 0; |
de7b2973 | 432 | mutex_lock(&tracepoint_module_list_mutex); |
b75ef8b4 MD |
433 | tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL); |
434 | if (!tp_mod) { | |
435 | ret = -ENOMEM; | |
436 | goto end; | |
437 | } | |
eb7d035c | 438 | tp_mod->mod = mod; |
0dea6d52 | 439 | list_add_tail(&tp_mod->list, &tracepoint_module_list); |
de7b2973 MD |
440 | blocking_notifier_call_chain(&tracepoint_notify_list, |
441 | MODULE_STATE_COMING, tp_mod); | |
b75ef8b4 | 442 | end: |
de7b2973 | 443 | mutex_unlock(&tracepoint_module_list_mutex); |
b75ef8b4 MD |
444 | return ret; |
445 | } | |
446 | ||
de7b2973 | 447 | static void tracepoint_module_going(struct module *mod) |
b75ef8b4 | 448 | { |
de7b2973 | 449 | struct tp_module *tp_mod; |
b75ef8b4 | 450 | |
7dec935a | 451 | if (!mod->num_tracepoints) |
de7b2973 | 452 | return; |
7dec935a | 453 | |
de7b2973 MD |
454 | mutex_lock(&tracepoint_module_list_mutex); |
455 | list_for_each_entry(tp_mod, &tracepoint_module_list, list) { | |
eb7d035c | 456 | if (tp_mod->mod == mod) { |
de7b2973 MD |
457 | blocking_notifier_call_chain(&tracepoint_notify_list, |
458 | MODULE_STATE_GOING, tp_mod); | |
459 | list_del(&tp_mod->list); | |
460 | kfree(tp_mod); | |
461 | /* | |
462 | * Called the going notifier before checking for | |
463 | * quiescence. | |
464 | */ | |
465 | tp_module_going_check_quiescent(mod->tracepoints_ptrs, | |
466 | mod->tracepoints_ptrs + mod->num_tracepoints); | |
b75ef8b4 MD |
467 | break; |
468 | } | |
469 | } | |
470 | /* | |
471 | * In the case of modules that were tainted at "coming", we'll simply | |
472 | * walk through the list without finding it. We cannot use the "tainted" | |
473 | * flag on "going", in case a module taints the kernel only after being | |
474 | * loaded. | |
475 | */ | |
de7b2973 | 476 | mutex_unlock(&tracepoint_module_list_mutex); |
b75ef8b4 | 477 | } |
227a8375 | 478 | |
de7b2973 MD |
479 | static int tracepoint_module_notify(struct notifier_block *self, |
480 | unsigned long val, void *data) | |
32f85742 MD |
481 | { |
482 | struct module *mod = data; | |
b75ef8b4 | 483 | int ret = 0; |
32f85742 MD |
484 | |
485 | switch (val) { | |
486 | case MODULE_STATE_COMING: | |
b75ef8b4 MD |
487 | ret = tracepoint_module_coming(mod); |
488 | break; | |
489 | case MODULE_STATE_LIVE: | |
490 | break; | |
32f85742 | 491 | case MODULE_STATE_GOING: |
de7b2973 MD |
492 | tracepoint_module_going(mod); |
493 | break; | |
494 | case MODULE_STATE_UNFORMED: | |
32f85742 MD |
495 | break; |
496 | } | |
b75ef8b4 | 497 | return ret; |
32f85742 MD |
498 | } |
499 | ||
de7b2973 | 500 | static struct notifier_block tracepoint_module_nb = { |
32f85742 MD |
501 | .notifier_call = tracepoint_module_notify, |
502 | .priority = 0, | |
503 | }; | |
504 | ||
de7b2973 | 505 | static __init int init_tracepoints(void) |
32f85742 | 506 | { |
de7b2973 MD |
507 | int ret; |
508 | ||
509 | ret = register_module_notifier(&tracepoint_module_nb); | |
eb7d035c | 510 | if (ret) |
a395d6a7 | 511 | pr_warn("Failed to register tracepoint module enter notifier\n"); |
eb7d035c | 512 | |
de7b2973 | 513 | return ret; |
32f85742 MD |
514 | } |
515 | __initcall(init_tracepoints); | |
227a8375 | 516 | #endif /* CONFIG_MODULES */ |
a871bd33 | 517 | |
de7b2973 MD |
518 | static void for_each_tracepoint_range(struct tracepoint * const *begin, |
519 | struct tracepoint * const *end, | |
520 | void (*fct)(struct tracepoint *tp, void *priv), | |
521 | void *priv) | |
522 | { | |
523 | struct tracepoint * const *iter; | |
524 | ||
525 | if (!begin) | |
526 | return; | |
527 | for (iter = begin; iter < end; iter++) | |
528 | fct(*iter, priv); | |
529 | } | |
530 | ||
531 | /** | |
532 | * for_each_kernel_tracepoint - iteration on all kernel tracepoints | |
533 | * @fct: callback | |
534 | * @priv: private data | |
535 | */ | |
536 | void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv), | |
537 | void *priv) | |
538 | { | |
539 | for_each_tracepoint_range(__start___tracepoints_ptrs, | |
540 | __stop___tracepoints_ptrs, fct, priv); | |
541 | } | |
542 | EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint); | |
543 | ||
3d27d8cb | 544 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
60d970c2 | 545 | |
97419875 | 546 | /* NB: reg/unreg are called while guarded with the tracepoints_mutex */ |
a871bd33 JB |
547 | static int sys_tracepoint_refcount; |
548 | ||
8cf868af | 549 | int syscall_regfunc(void) |
a871bd33 | 550 | { |
8063e41d | 551 | struct task_struct *p, *t; |
a871bd33 | 552 | |
a871bd33 | 553 | if (!sys_tracepoint_refcount) { |
8063e41d ON |
554 | read_lock(&tasklist_lock); |
555 | for_each_process_thread(p, t) { | |
ea73c79e | 556 | set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); |
8063e41d ON |
557 | } |
558 | read_unlock(&tasklist_lock); | |
a871bd33 JB |
559 | } |
560 | sys_tracepoint_refcount++; | |
8cf868af SRRH |
561 | |
562 | return 0; | |
a871bd33 JB |
563 | } |
564 | ||
565 | void syscall_unregfunc(void) | |
566 | { | |
8063e41d | 567 | struct task_struct *p, *t; |
a871bd33 | 568 | |
a871bd33 JB |
569 | sys_tracepoint_refcount--; |
570 | if (!sys_tracepoint_refcount) { | |
8063e41d ON |
571 | read_lock(&tasklist_lock); |
572 | for_each_process_thread(p, t) { | |
66700001 | 573 | clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); |
8063e41d ON |
574 | } |
575 | read_unlock(&tasklist_lock); | |
a871bd33 | 576 | } |
a871bd33 | 577 | } |
60d970c2 | 578 | #endif |