2 * Copyright (C) 2008-2014 Mathieu Desnoyers
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.
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.
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.
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>
27 #include <linux/sched/signal.h>
28 #include <linux/sched/task.h>
29 #include <linux/static_key.h>
31 extern tracepoint_ptr_t __start___tracepoints_ptrs[];
32 extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
34 DEFINE_SRCU(tracepoint_srcu);
35 EXPORT_SYMBOL_GPL(tracepoint_srcu);
37 /* Set to 1 to enable tracepoint debug output */
38 static const int tracepoint_debug;
42 * Tracepoint module list mutex protects the local module list.
44 static DEFINE_MUTEX(tracepoint_module_list_mutex);
46 /* Local list of struct tp_module */
47 static LIST_HEAD(tracepoint_module_list);
48 #endif /* CONFIG_MODULES */
51 * tracepoints_mutex protects the builtin and module tracepoints.
52 * tracepoints_mutex nests inside tracepoint_module_list_mutex.
54 static DEFINE_MUTEX(tracepoints_mutex);
56 static struct rcu_head *early_probes;
57 static bool ok_to_free_tracepoints;
61 * It is used to delay the free of multiple probes array until a quiescent
66 struct tracepoint_func probes[0];
69 /* Called in removal of a func but failed to allocate a new tp_funcs */
70 static void tp_stub_func(void)
75 static inline void *allocate_probes(int count)
77 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
78 + sizeof(struct tp_probes), GFP_KERNEL);
79 return p == NULL ? NULL : p->probes;
82 static void srcu_free_old_probes(struct rcu_head *head)
84 kfree(container_of(head, struct tp_probes, rcu));
87 static void rcu_free_old_probes(struct rcu_head *head)
89 call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
92 static __init int release_early_probes(void)
96 ok_to_free_tracepoints = true;
98 while (early_probes) {
100 early_probes = tmp->next;
101 call_rcu_sched(tmp, rcu_free_old_probes);
107 /* SRCU is initialized at core_initcall */
108 postcore_initcall(release_early_probes);
110 static inline void release_probes(struct tracepoint_func *old)
113 struct tp_probes *tp_probes = container_of(old,
114 struct tp_probes, probes[0]);
117 * We can't free probes if SRCU is not initialized yet.
118 * Postpone the freeing till after SRCU is initialized.
120 if (unlikely(!ok_to_free_tracepoints)) {
121 tp_probes->rcu.next = early_probes;
122 early_probes = &tp_probes->rcu;
127 * Tracepoint probes are protected by both sched RCU and SRCU,
128 * by calling the SRCU callback in the sched RCU callback we
129 * cover both cases. So let us chain the SRCU and sched RCU
130 * callbacks to wait for both grace periods.
132 call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
136 static void debug_print_probes(struct tracepoint_func *funcs)
140 if (!tracepoint_debug || !funcs)
143 for (i = 0; funcs[i].func; i++)
144 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
147 static struct tracepoint_func *
148 func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
151 struct tracepoint_func *old, *new;
156 if (WARN_ON(!tp_func->func))
157 return ERR_PTR(-EINVAL);
159 debug_print_probes(*funcs);
162 /* (N -> N+1), (N != 0, 1) probes */
163 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
164 /* Insert before probes of lower priority */
165 if (pos < 0 && old[nr_probes].prio < prio)
167 if (old[nr_probes].func == tp_func->func &&
168 old[nr_probes].data == tp_func->data)
169 return ERR_PTR(-EEXIST);
170 if (old[nr_probes].func == tp_stub_func)
174 /* + 2 : one for new probe, one for NULL func - stub functions */
175 new = allocate_probes(nr_probes + 2 - stub_funcs);
177 return ERR_PTR(-ENOMEM);
180 /* Need to copy one at a time to remove stubs */
184 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
185 if (old[nr_probes].func == tp_stub_func)
187 if (pos < 0 && old[nr_probes].prio < prio)
189 new[probes++] = old[nr_probes];
195 nr_probes--; /* Account for insertion */
197 } else if (pos < 0) {
199 memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
201 /* Copy higher priority probes ahead of the new probe */
202 memcpy(new, old, pos * sizeof(struct tracepoint_func));
203 /* Copy the rest after it. */
204 memcpy(new + pos + 1, old + pos,
205 (nr_probes - pos) * sizeof(struct tracepoint_func));
210 new[nr_probes + 1].func = NULL;
212 debug_print_probes(*funcs);
216 static void *func_remove(struct tracepoint_func **funcs,
217 struct tracepoint_func *tp_func)
219 int nr_probes = 0, nr_del = 0, i;
220 struct tracepoint_func *old, *new;
225 return ERR_PTR(-ENOENT);
227 debug_print_probes(*funcs);
228 /* (N -> M), (N > 1, M >= 0) probes */
230 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
231 if ((old[nr_probes].func == tp_func->func &&
232 old[nr_probes].data == tp_func->data) ||
233 old[nr_probes].func == tp_stub_func)
239 * If probe is NULL, then nr_probes = nr_del = 0, and then the
240 * entire entry will be removed.
242 if (nr_probes - nr_del == 0) {
243 /* N -> 0, (N > 1) */
245 debug_print_probes(*funcs);
249 /* N -> M, (N > 1, M > 0) */
251 new = allocate_probes(nr_probes - nr_del + 1);
253 for (i = 0; old[i].func; i++)
254 if ((old[i].func != tp_func->func
255 || old[i].data != tp_func->data)
256 && old[i].func != tp_stub_func)
258 new[nr_probes - nr_del].func = NULL;
262 * Failed to allocate, replace the old function
263 * with calls to tp_stub_func.
265 for (i = 0; old[i].func; i++)
266 if (old[i].func == tp_func->func &&
267 old[i].data == tp_func->data) {
268 old[i].func = tp_stub_func;
269 /* Set the prio to the next event. */
279 debug_print_probes(*funcs);
284 * Add the probe function to a tracepoint.
286 static int tracepoint_add_func(struct tracepoint *tp,
287 struct tracepoint_func *func, int prio)
289 struct tracepoint_func *old, *tp_funcs;
292 if (tp->regfunc && !static_key_enabled(&tp->key)) {
298 tp_funcs = rcu_dereference_protected(tp->funcs,
299 lockdep_is_held(&tracepoints_mutex));
300 old = func_add(&tp_funcs, func, prio);
302 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
307 * rcu_assign_pointer has as smp_store_release() which makes sure
308 * that the new probe callbacks array is consistent before setting
309 * a pointer to it. This array is referenced by __DO_TRACE from
310 * include/linux/tracepoint.h using rcu_dereference_sched().
312 rcu_assign_pointer(tp->funcs, tp_funcs);
313 if (!static_key_enabled(&tp->key))
314 static_key_slow_inc(&tp->key);
320 * Remove a probe function from a tracepoint.
321 * Note: only waiting an RCU period after setting elem->call to the empty
322 * function insures that the original callback is not used anymore. This insured
323 * by preempt_disable around the call site.
325 static int tracepoint_remove_func(struct tracepoint *tp,
326 struct tracepoint_func *func)
328 struct tracepoint_func *old, *tp_funcs;
330 tp_funcs = rcu_dereference_protected(tp->funcs,
331 lockdep_is_held(&tracepoints_mutex));
332 old = func_remove(&tp_funcs, func);
333 if (WARN_ON_ONCE(IS_ERR(old)))
337 /* Failed allocating new tp_funcs, replaced func with stub */
341 /* Removed last function */
342 if (tp->unregfunc && static_key_enabled(&tp->key))
345 if (static_key_enabled(&tp->key))
346 static_key_slow_dec(&tp->key);
348 rcu_assign_pointer(tp->funcs, tp_funcs);
354 * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority
356 * @probe: probe handler
357 * @data: tracepoint data
358 * @prio: priority of this function over other registered functions
360 * Returns 0 if ok, error value on error.
361 * Note: if @tp is within a module, the caller is responsible for
362 * unregistering the probe before the module is gone. This can be
363 * performed either with a tracepoint module going notifier, or from
364 * within module exit functions.
366 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
367 void *data, int prio)
369 struct tracepoint_func tp_func;
372 mutex_lock(&tracepoints_mutex);
373 tp_func.func = probe;
376 ret = tracepoint_add_func(tp, &tp_func, prio);
377 mutex_unlock(&tracepoints_mutex);
380 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
383 * tracepoint_probe_register - Connect a probe to a tracepoint
385 * @probe: probe handler
386 * @data: tracepoint data
388 * Returns 0 if ok, error value on error.
389 * Note: if @tp is within a module, the caller is responsible for
390 * unregistering the probe before the module is gone. This can be
391 * performed either with a tracepoint module going notifier, or from
392 * within module exit functions.
394 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
396 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
398 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
401 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
403 * @probe: probe function pointer
404 * @data: tracepoint data
406 * Returns 0 if ok, error value on error.
408 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
410 struct tracepoint_func tp_func;
413 mutex_lock(&tracepoints_mutex);
414 tp_func.func = probe;
416 ret = tracepoint_remove_func(tp, &tp_func);
417 mutex_unlock(&tracepoints_mutex);
420 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
422 static void for_each_tracepoint_range(
423 tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
424 void (*fct)(struct tracepoint *tp, void *priv),
427 tracepoint_ptr_t *iter;
431 for (iter = begin; iter < end; iter++)
432 fct(tracepoint_ptr_deref(iter), priv);
435 #ifdef CONFIG_MODULES
436 bool trace_module_has_bad_taint(struct module *mod)
438 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
439 (1 << TAINT_UNSIGNED_MODULE));
442 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
445 * register_tracepoint_notifier - register tracepoint coming/going notifier
446 * @nb: notifier block
448 * Notifiers registered with this function are called on module
449 * coming/going with the tracepoint_module_list_mutex held.
450 * The notifier block callback should expect a "struct tp_module" data
453 int register_tracepoint_module_notifier(struct notifier_block *nb)
455 struct tp_module *tp_mod;
458 mutex_lock(&tracepoint_module_list_mutex);
459 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
462 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
463 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
465 mutex_unlock(&tracepoint_module_list_mutex);
468 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
471 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
472 * @nb: notifier block
474 * The notifier block callback should expect a "struct tp_module" data
477 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
479 struct tp_module *tp_mod;
482 mutex_lock(&tracepoint_module_list_mutex);
483 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
486 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
487 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
489 mutex_unlock(&tracepoint_module_list_mutex);
493 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
496 * Ensure the tracer unregistered the module's probes before the module
497 * teardown is performed. Prevents leaks of probe and data pointers.
499 static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
501 WARN_ON_ONCE(tp->funcs);
504 static int tracepoint_module_coming(struct module *mod)
506 struct tp_module *tp_mod;
509 if (!mod->num_tracepoints)
513 * We skip modules that taint the kernel, especially those with different
514 * module headers (for forced load), to make sure we don't cause a crash.
515 * Staging, out-of-tree, and unsigned GPL modules are fine.
517 if (trace_module_has_bad_taint(mod))
519 mutex_lock(&tracepoint_module_list_mutex);
520 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
526 list_add_tail(&tp_mod->list, &tracepoint_module_list);
527 blocking_notifier_call_chain(&tracepoint_notify_list,
528 MODULE_STATE_COMING, tp_mod);
530 mutex_unlock(&tracepoint_module_list_mutex);
534 static void tracepoint_module_going(struct module *mod)
536 struct tp_module *tp_mod;
538 if (!mod->num_tracepoints)
541 mutex_lock(&tracepoint_module_list_mutex);
542 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
543 if (tp_mod->mod == mod) {
544 blocking_notifier_call_chain(&tracepoint_notify_list,
545 MODULE_STATE_GOING, tp_mod);
546 list_del(&tp_mod->list);
549 * Called the going notifier before checking for
552 for_each_tracepoint_range(mod->tracepoints_ptrs,
553 mod->tracepoints_ptrs + mod->num_tracepoints,
554 tp_module_going_check_quiescent, NULL);
559 * In the case of modules that were tainted at "coming", we'll simply
560 * walk through the list without finding it. We cannot use the "tainted"
561 * flag on "going", in case a module taints the kernel only after being
564 mutex_unlock(&tracepoint_module_list_mutex);
567 static int tracepoint_module_notify(struct notifier_block *self,
568 unsigned long val, void *data)
570 struct module *mod = data;
574 case MODULE_STATE_COMING:
575 ret = tracepoint_module_coming(mod);
577 case MODULE_STATE_LIVE:
579 case MODULE_STATE_GOING:
580 tracepoint_module_going(mod);
582 case MODULE_STATE_UNFORMED:
588 static struct notifier_block tracepoint_module_nb = {
589 .notifier_call = tracepoint_module_notify,
593 static __init int init_tracepoints(void)
597 ret = register_module_notifier(&tracepoint_module_nb);
599 pr_warn("Failed to register tracepoint module enter notifier\n");
603 __initcall(init_tracepoints);
604 #endif /* CONFIG_MODULES */
607 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
609 * @priv: private data
611 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
614 for_each_tracepoint_range(__start___tracepoints_ptrs,
615 __stop___tracepoints_ptrs, fct, priv);
617 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
619 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
621 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
622 static int sys_tracepoint_refcount;
624 int syscall_regfunc(void)
626 struct task_struct *p, *t;
628 if (!sys_tracepoint_refcount) {
629 read_lock(&tasklist_lock);
630 for_each_process_thread(p, t) {
631 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
633 read_unlock(&tasklist_lock);
635 sys_tracepoint_refcount++;
640 void syscall_unregfunc(void)
642 struct task_struct *p, *t;
644 sys_tracepoint_refcount--;
645 if (!sys_tracepoint_refcount) {
646 read_lock(&tasklist_lock);
647 for_each_process_thread(p, t) {
648 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
650 read_unlock(&tasklist_lock);