]>
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 | |
9c0be3f6 MD |
31 | extern tracepoint_ptr_t __start___tracepoints_ptrs[]; |
32 | extern tracepoint_ptr_t __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 | 55 | |
f8a79d5c SRV |
56 | static struct rcu_head *early_probes; |
57 | static bool ok_to_free_tracepoints; | |
58 | ||
97e1c18e MD |
59 | /* |
60 | * Note about RCU : | |
fd589a8f | 61 | * It is used to delay the free of multiple probes array until a quiescent |
97e1c18e | 62 | * state is reached. |
97e1c18e | 63 | */ |
19dba33c | 64 | struct tp_probes { |
0dea6d52 | 65 | struct rcu_head rcu; |
38516ab5 | 66 | struct tracepoint_func probes[0]; |
19dba33c | 67 | }; |
97e1c18e | 68 | |
19dba33c | 69 | static inline void *allocate_probes(int count) |
97e1c18e | 70 | { |
38516ab5 | 71 | struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func) |
19dba33c LJ |
72 | + sizeof(struct tp_probes), GFP_KERNEL); |
73 | return p == NULL ? NULL : p->probes; | |
97e1c18e MD |
74 | } |
75 | ||
e6753f23 | 76 | static void srcu_free_old_probes(struct rcu_head *head) |
97e1c18e | 77 | { |
0dea6d52 | 78 | kfree(container_of(head, struct tp_probes, rcu)); |
19dba33c LJ |
79 | } |
80 | ||
e6753f23 JFG |
81 | static void rcu_free_old_probes(struct rcu_head *head) |
82 | { | |
83 | call_srcu(&tracepoint_srcu, head, srcu_free_old_probes); | |
84 | } | |
85 | ||
f8a79d5c SRV |
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 | ||
38516ab5 | 104 | static inline void release_probes(struct tracepoint_func *old) |
19dba33c LJ |
105 | { |
106 | if (old) { | |
107 | struct tp_probes *tp_probes = container_of(old, | |
108 | struct tp_probes, probes[0]); | |
f8a79d5c SRV |
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 | ||
e6753f23 JFG |
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 | */ | |
0dea6d52 | 126 | call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes); |
19dba33c | 127 | } |
97e1c18e MD |
128 | } |
129 | ||
de7b2973 | 130 | static void debug_print_probes(struct tracepoint_func *funcs) |
97e1c18e MD |
131 | { |
132 | int i; | |
133 | ||
de7b2973 | 134 | if (!tracepoint_debug || !funcs) |
97e1c18e MD |
135 | return; |
136 | ||
de7b2973 MD |
137 | for (i = 0; funcs[i].func; i++) |
138 | printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func); | |
97e1c18e MD |
139 | } |
140 | ||
7904b5c4 SRRH |
141 | static struct tracepoint_func * |
142 | func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func, | |
143 | int prio) | |
97e1c18e | 144 | { |
38516ab5 | 145 | struct tracepoint_func *old, *new; |
7904b5c4 SRRH |
146 | int nr_probes = 0; |
147 | int pos = -1; | |
97e1c18e | 148 | |
de7b2973 | 149 | if (WARN_ON(!tp_func->func)) |
4c69e6ea | 150 | return ERR_PTR(-EINVAL); |
97e1c18e | 151 | |
de7b2973 MD |
152 | debug_print_probes(*funcs); |
153 | old = *funcs; | |
97e1c18e MD |
154 | if (old) { |
155 | /* (N -> N+1), (N != 0, 1) probes */ | |
7904b5c4 SRRH |
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; | |
de7b2973 MD |
160 | if (old[nr_probes].func == tp_func->func && |
161 | old[nr_probes].data == tp_func->data) | |
97e1c18e | 162 | return ERR_PTR(-EEXIST); |
7904b5c4 | 163 | } |
97e1c18e MD |
164 | } |
165 | /* + 2 : one for new probe, one for NULL func */ | |
19dba33c | 166 | new = allocate_probes(nr_probes + 2); |
97e1c18e MD |
167 | if (new == NULL) |
168 | return ERR_PTR(-ENOMEM); | |
7904b5c4 SRRH |
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; | |
38516ab5 | 183 | new[nr_probes + 1].func = NULL; |
de7b2973 MD |
184 | *funcs = new; |
185 | debug_print_probes(*funcs); | |
97e1c18e MD |
186 | return old; |
187 | } | |
188 | ||
de7b2973 MD |
189 | static void *func_remove(struct tracepoint_func **funcs, |
190 | struct tracepoint_func *tp_func) | |
97e1c18e MD |
191 | { |
192 | int nr_probes = 0, nr_del = 0, i; | |
38516ab5 | 193 | struct tracepoint_func *old, *new; |
97e1c18e | 194 | |
de7b2973 | 195 | old = *funcs; |
97e1c18e | 196 | |
f66af459 | 197 | if (!old) |
19dba33c | 198 | return ERR_PTR(-ENOENT); |
f66af459 | 199 | |
de7b2973 | 200 | debug_print_probes(*funcs); |
97e1c18e | 201 | /* (N -> M), (N > 1, M >= 0) probes */ |
de7b2973 | 202 | if (tp_func->func) { |
4c69e6ea | 203 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
de7b2973 MD |
204 | if (old[nr_probes].func == tp_func->func && |
205 | old[nr_probes].data == tp_func->data) | |
4c69e6ea S |
206 | nr_del++; |
207 | } | |
97e1c18e MD |
208 | } |
209 | ||
4c69e6ea S |
210 | /* |
211 | * If probe is NULL, then nr_probes = nr_del = 0, and then the | |
212 | * entire entry will be removed. | |
213 | */ | |
97e1c18e MD |
214 | if (nr_probes - nr_del == 0) { |
215 | /* N -> 0, (N > 1) */ | |
de7b2973 MD |
216 | *funcs = NULL; |
217 | debug_print_probes(*funcs); | |
97e1c18e MD |
218 | return old; |
219 | } else { | |
220 | int j = 0; | |
221 | /* N -> M, (N > 1, M > 0) */ | |
222 | /* + 1 for NULL */ | |
19dba33c | 223 | new = allocate_probes(nr_probes - nr_del + 1); |
97e1c18e MD |
224 | if (new == NULL) |
225 | return ERR_PTR(-ENOMEM); | |
38516ab5 | 226 | for (i = 0; old[i].func; i++) |
de7b2973 MD |
227 | if (old[i].func != tp_func->func |
228 | || old[i].data != tp_func->data) | |
97e1c18e | 229 | new[j++] = old[i]; |
38516ab5 | 230 | new[nr_probes - nr_del].func = NULL; |
de7b2973 | 231 | *funcs = new; |
97e1c18e | 232 | } |
de7b2973 | 233 | debug_print_probes(*funcs); |
97e1c18e MD |
234 | return old; |
235 | } | |
236 | ||
237 | /* | |
de7b2973 | 238 | * Add the probe function to a tracepoint. |
97e1c18e | 239 | */ |
de7b2973 | 240 | static int tracepoint_add_func(struct tracepoint *tp, |
7904b5c4 | 241 | struct tracepoint_func *func, int prio) |
97e1c18e | 242 | { |
de7b2973 | 243 | struct tracepoint_func *old, *tp_funcs; |
8cf868af | 244 | int ret; |
97e1c18e | 245 | |
8cf868af SRRH |
246 | if (tp->regfunc && !static_key_enabled(&tp->key)) { |
247 | ret = tp->regfunc(); | |
248 | if (ret < 0) | |
249 | return ret; | |
250 | } | |
97e1c18e | 251 | |
b725dfea MD |
252 | tp_funcs = rcu_dereference_protected(tp->funcs, |
253 | lockdep_is_held(&tracepoints_mutex)); | |
7904b5c4 | 254 | old = func_add(&tp_funcs, func, prio); |
de7b2973 | 255 | if (IS_ERR(old)) { |
d66a270b | 256 | WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); |
de7b2973 MD |
257 | return PTR_ERR(old); |
258 | } | |
97419875 | 259 | |
97e1c18e | 260 | /* |
243d1a79 PM |
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(). | |
97e1c18e | 265 | */ |
de7b2973 MD |
266 | rcu_assign_pointer(tp->funcs, tp_funcs); |
267 | if (!static_key_enabled(&tp->key)) | |
268 | static_key_slow_inc(&tp->key); | |
8058bd0f | 269 | release_probes(old); |
de7b2973 | 270 | return 0; |
97e1c18e MD |
271 | } |
272 | ||
273 | /* | |
de7b2973 | 274 | * Remove a probe function from a tracepoint. |
97e1c18e MD |
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 | */ | |
de7b2973 MD |
279 | static int tracepoint_remove_func(struct tracepoint *tp, |
280 | struct tracepoint_func *func) | |
97e1c18e | 281 | { |
de7b2973 | 282 | struct tracepoint_func *old, *tp_funcs; |
97e1c18e | 283 | |
b725dfea MD |
284 | tp_funcs = rcu_dereference_protected(tp->funcs, |
285 | lockdep_is_held(&tracepoints_mutex)); | |
de7b2973 MD |
286 | old = func_remove(&tp_funcs, func); |
287 | if (IS_ERR(old)) { | |
d66a270b | 288 | WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); |
de7b2973 | 289 | return PTR_ERR(old); |
97e1c18e | 290 | } |
b75ef8b4 | 291 | |
de7b2973 MD |
292 | if (!tp_funcs) { |
293 | /* Removed last function */ | |
294 | if (tp->unregfunc && static_key_enabled(&tp->key)) | |
295 | tp->unregfunc(); | |
b75ef8b4 | 296 | |
de7b2973 MD |
297 | if (static_key_enabled(&tp->key)) |
298 | static_key_slow_dec(&tp->key); | |
127cafbb | 299 | } |
de7b2973 | 300 | rcu_assign_pointer(tp->funcs, tp_funcs); |
8058bd0f | 301 | release_probes(old); |
de7b2973 | 302 | return 0; |
127cafbb LJ |
303 | } |
304 | ||
97e1c18e | 305 | /** |
f39e2391 | 306 | * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority |
de7b2973 | 307 | * @tp: tracepoint |
97e1c18e | 308 | * @probe: probe handler |
cac92ba7 | 309 | * @data: tracepoint data |
7904b5c4 | 310 | * @prio: priority of this function over other registered functions |
97e1c18e | 311 | * |
de7b2973 MD |
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. | |
97e1c18e | 317 | */ |
7904b5c4 SRRH |
318 | int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe, |
319 | void *data, int prio) | |
97e1c18e | 320 | { |
de7b2973 MD |
321 | struct tracepoint_func tp_func; |
322 | int ret; | |
97e1c18e MD |
323 | |
324 | mutex_lock(&tracepoints_mutex); | |
de7b2973 MD |
325 | tp_func.func = probe; |
326 | tp_func.data = data; | |
7904b5c4 SRRH |
327 | tp_func.prio = prio; |
328 | ret = tracepoint_add_func(tp, &tp_func, prio); | |
b75ef8b4 | 329 | mutex_unlock(&tracepoints_mutex); |
b196e2b9 | 330 | return ret; |
97e1c18e | 331 | } |
7904b5c4 SRRH |
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 | |
7904b5c4 SRRH |
339 | * |
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. | |
345 | */ | |
346 | int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data) | |
347 | { | |
348 | return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO); | |
349 | } | |
97e1c18e MD |
350 | EXPORT_SYMBOL_GPL(tracepoint_probe_register); |
351 | ||
352 | /** | |
353 | * tracepoint_probe_unregister - Disconnect a probe from a tracepoint | |
de7b2973 | 354 | * @tp: tracepoint |
97e1c18e | 355 | * @probe: probe function pointer |
cac92ba7 | 356 | * @data: tracepoint data |
97e1c18e | 357 | * |
de7b2973 | 358 | * Returns 0 if ok, error value on error. |
97e1c18e | 359 | */ |
de7b2973 | 360 | int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data) |
97e1c18e | 361 | { |
de7b2973 MD |
362 | struct tracepoint_func tp_func; |
363 | int ret; | |
97e1c18e MD |
364 | |
365 | mutex_lock(&tracepoints_mutex); | |
de7b2973 MD |
366 | tp_func.func = probe; |
367 | tp_func.data = data; | |
368 | ret = tracepoint_remove_func(tp, &tp_func); | |
b75ef8b4 | 369 | mutex_unlock(&tracepoints_mutex); |
de7b2973 | 370 | return ret; |
97e1c18e MD |
371 | } |
372 | EXPORT_SYMBOL_GPL(tracepoint_probe_unregister); | |
373 | ||
9c0be3f6 MD |
374 | static void for_each_tracepoint_range( |
375 | tracepoint_ptr_t *begin, tracepoint_ptr_t *end, | |
46e0c9be AB |
376 | void (*fct)(struct tracepoint *tp, void *priv), |
377 | void *priv) | |
378 | { | |
9c0be3f6 MD |
379 | tracepoint_ptr_t *iter; |
380 | ||
46e0c9be AB |
381 | if (!begin) |
382 | return; | |
9c0be3f6 MD |
383 | for (iter = begin; iter < end; iter++) |
384 | fct(tracepoint_ptr_deref(iter), priv); | |
46e0c9be AB |
385 | } |
386 | ||
227a8375 | 387 | #ifdef CONFIG_MODULES |
45ab2813 SRRH |
388 | bool trace_module_has_bad_taint(struct module *mod) |
389 | { | |
66cc69e3 MD |
390 | return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) | |
391 | (1 << TAINT_UNSIGNED_MODULE)); | |
45ab2813 SRRH |
392 | } |
393 | ||
de7b2973 MD |
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 | */ | |
46e0c9be | 451 | static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv) |
de7b2973 | 452 | { |
46e0c9be | 453 | WARN_ON_ONCE(tp->funcs); |
de7b2973 MD |
454 | } |
455 | ||
b75ef8b4 MD |
456 | static int tracepoint_module_coming(struct module *mod) |
457 | { | |
0dea6d52 | 458 | struct tp_module *tp_mod; |
b75ef8b4 MD |
459 | int ret = 0; |
460 | ||
7dec935a SRRH |
461 | if (!mod->num_tracepoints) |
462 | return 0; | |
463 | ||
b75ef8b4 | 464 | /* |
c10076c4 SR |
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. | |
66cc69e3 | 467 | * Staging, out-of-tree, and unsigned GPL modules are fine. |
b75ef8b4 | 468 | */ |
45ab2813 | 469 | if (trace_module_has_bad_taint(mod)) |
b75ef8b4 | 470 | return 0; |
de7b2973 | 471 | mutex_lock(&tracepoint_module_list_mutex); |
b75ef8b4 MD |
472 | tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL); |
473 | if (!tp_mod) { | |
474 | ret = -ENOMEM; | |
475 | goto end; | |
476 | } | |
eb7d035c | 477 | tp_mod->mod = mod; |
0dea6d52 | 478 | list_add_tail(&tp_mod->list, &tracepoint_module_list); |
de7b2973 MD |
479 | blocking_notifier_call_chain(&tracepoint_notify_list, |
480 | MODULE_STATE_COMING, tp_mod); | |
b75ef8b4 | 481 | end: |
de7b2973 | 482 | mutex_unlock(&tracepoint_module_list_mutex); |
b75ef8b4 MD |
483 | return ret; |
484 | } | |
485 | ||
de7b2973 | 486 | static void tracepoint_module_going(struct module *mod) |
b75ef8b4 | 487 | { |
de7b2973 | 488 | struct tp_module *tp_mod; |
b75ef8b4 | 489 | |
7dec935a | 490 | if (!mod->num_tracepoints) |
de7b2973 | 491 | return; |
7dec935a | 492 | |
de7b2973 MD |
493 | mutex_lock(&tracepoint_module_list_mutex); |
494 | list_for_each_entry(tp_mod, &tracepoint_module_list, list) { | |
eb7d035c | 495 | if (tp_mod->mod == mod) { |
de7b2973 MD |
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 | */ | |
46e0c9be AB |
504 | for_each_tracepoint_range(mod->tracepoints_ptrs, |
505 | mod->tracepoints_ptrs + mod->num_tracepoints, | |
506 | tp_module_going_check_quiescent, NULL); | |
b75ef8b4 MD |
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 | */ | |
de7b2973 | 516 | mutex_unlock(&tracepoint_module_list_mutex); |
b75ef8b4 | 517 | } |
227a8375 | 518 | |
de7b2973 MD |
519 | static int tracepoint_module_notify(struct notifier_block *self, |
520 | unsigned long val, void *data) | |
32f85742 MD |
521 | { |
522 | struct module *mod = data; | |
b75ef8b4 | 523 | int ret = 0; |
32f85742 MD |
524 | |
525 | switch (val) { | |
526 | case MODULE_STATE_COMING: | |
b75ef8b4 MD |
527 | ret = tracepoint_module_coming(mod); |
528 | break; | |
529 | case MODULE_STATE_LIVE: | |
530 | break; | |
32f85742 | 531 | case MODULE_STATE_GOING: |
de7b2973 MD |
532 | tracepoint_module_going(mod); |
533 | break; | |
534 | case MODULE_STATE_UNFORMED: | |
32f85742 MD |
535 | break; |
536 | } | |
b75ef8b4 | 537 | return ret; |
32f85742 MD |
538 | } |
539 | ||
de7b2973 | 540 | static struct notifier_block tracepoint_module_nb = { |
32f85742 MD |
541 | .notifier_call = tracepoint_module_notify, |
542 | .priority = 0, | |
543 | }; | |
544 | ||
de7b2973 | 545 | static __init int init_tracepoints(void) |
32f85742 | 546 | { |
de7b2973 MD |
547 | int ret; |
548 | ||
549 | ret = register_module_notifier(&tracepoint_module_nb); | |
eb7d035c | 550 | if (ret) |
a395d6a7 | 551 | pr_warn("Failed to register tracepoint module enter notifier\n"); |
eb7d035c | 552 | |
de7b2973 | 553 | return ret; |
32f85742 MD |
554 | } |
555 | __initcall(init_tracepoints); | |
227a8375 | 556 | #endif /* CONFIG_MODULES */ |
a871bd33 | 557 | |
de7b2973 MD |
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 | ||
3d27d8cb | 571 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
60d970c2 | 572 | |
97419875 | 573 | /* NB: reg/unreg are called while guarded with the tracepoints_mutex */ |
a871bd33 JB |
574 | static int sys_tracepoint_refcount; |
575 | ||
8cf868af | 576 | int syscall_regfunc(void) |
a871bd33 | 577 | { |
8063e41d | 578 | struct task_struct *p, *t; |
a871bd33 | 579 | |
a871bd33 | 580 | if (!sys_tracepoint_refcount) { |
8063e41d ON |
581 | read_lock(&tasklist_lock); |
582 | for_each_process_thread(p, t) { | |
ea73c79e | 583 | set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); |
8063e41d ON |
584 | } |
585 | read_unlock(&tasklist_lock); | |
a871bd33 JB |
586 | } |
587 | sys_tracepoint_refcount++; | |
8cf868af SRRH |
588 | |
589 | return 0; | |
a871bd33 JB |
590 | } |
591 | ||
592 | void syscall_unregfunc(void) | |
593 | { | |
8063e41d | 594 | struct task_struct *p, *t; |
a871bd33 | 595 | |
a871bd33 JB |
596 | sys_tracepoint_refcount--; |
597 | if (!sys_tracepoint_refcount) { | |
8063e41d ON |
598 | read_lock(&tasklist_lock); |
599 | for_each_process_thread(p, t) { | |
66700001 | 600 | clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); |
8063e41d ON |
601 | } |
602 | read_unlock(&tasklist_lock); | |
a871bd33 | 603 | } |
a871bd33 | 604 | } |
60d970c2 | 605 | #endif |