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,
290 struct tracepoint_func *old, *tp_funcs;
293 if (tp->regfunc && !static_key_enabled(&tp->key)) {
299 tp_funcs = rcu_dereference_protected(tp->funcs,
300 lockdep_is_held(&tracepoints_mutex));
301 old = func_add(&tp_funcs, func, prio);
303 WARN_ON_ONCE(warn && PTR_ERR(old) != -ENOMEM);
308 * rcu_assign_pointer has as smp_store_release() which makes sure
309 * that the new probe callbacks array is consistent before setting
310 * a pointer to it. This array is referenced by __DO_TRACE from
311 * include/linux/tracepoint.h using rcu_dereference_sched().
313 rcu_assign_pointer(tp->funcs, tp_funcs);
314 if (!static_key_enabled(&tp->key))
315 static_key_slow_inc(&tp->key);
321 * Remove a probe function from a tracepoint.
322 * Note: only waiting an RCU period after setting elem->call to the empty
323 * function insures that the original callback is not used anymore. This insured
324 * by preempt_disable around the call site.
326 static int tracepoint_remove_func(struct tracepoint *tp,
327 struct tracepoint_func *func)
329 struct tracepoint_func *old, *tp_funcs;
331 tp_funcs = rcu_dereference_protected(tp->funcs,
332 lockdep_is_held(&tracepoints_mutex));
333 old = func_remove(&tp_funcs, func);
334 if (WARN_ON_ONCE(IS_ERR(old)))
338 /* Failed allocating new tp_funcs, replaced func with stub */
342 /* Removed last function */
343 if (tp->unregfunc && static_key_enabled(&tp->key))
346 if (static_key_enabled(&tp->key))
347 static_key_slow_dec(&tp->key);
349 rcu_assign_pointer(tp->funcs, tp_funcs);
355 * tracepoint_probe_register_prio_may_exist - Connect a probe to a tracepoint with priority
357 * @probe: probe handler
358 * @data: tracepoint data
359 * @prio: priority of this function over other registered functions
361 * Same as tracepoint_probe_register_prio() except that it will not warn
362 * if the tracepoint is already registered.
364 int tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe,
365 void *data, int prio)
367 struct tracepoint_func tp_func;
370 mutex_lock(&tracepoints_mutex);
371 tp_func.func = probe;
374 ret = tracepoint_add_func(tp, &tp_func, prio, false);
375 mutex_unlock(&tracepoints_mutex);
378 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio_may_exist);
381 * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority
383 * @probe: probe handler
384 * @data: tracepoint data
385 * @prio: priority of this function over other registered functions
387 * Returns 0 if ok, error value on error.
388 * Note: if @tp is within a module, the caller is responsible for
389 * unregistering the probe before the module is gone. This can be
390 * performed either with a tracepoint module going notifier, or from
391 * within module exit functions.
393 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
394 void *data, int prio)
396 struct tracepoint_func tp_func;
399 mutex_lock(&tracepoints_mutex);
400 tp_func.func = probe;
403 ret = tracepoint_add_func(tp, &tp_func, prio, true);
404 mutex_unlock(&tracepoints_mutex);
407 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
410 * tracepoint_probe_register - Connect a probe to a tracepoint
412 * @probe: probe handler
413 * @data: tracepoint data
415 * Returns 0 if ok, error value on error.
416 * Note: if @tp is within a module, the caller is responsible for
417 * unregistering the probe before the module is gone. This can be
418 * performed either with a tracepoint module going notifier, or from
419 * within module exit functions.
421 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
423 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
425 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
428 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
430 * @probe: probe function pointer
431 * @data: tracepoint data
433 * Returns 0 if ok, error value on error.
435 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
437 struct tracepoint_func tp_func;
440 mutex_lock(&tracepoints_mutex);
441 tp_func.func = probe;
443 ret = tracepoint_remove_func(tp, &tp_func);
444 mutex_unlock(&tracepoints_mutex);
447 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
449 static void for_each_tracepoint_range(
450 tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
451 void (*fct)(struct tracepoint *tp, void *priv),
454 tracepoint_ptr_t *iter;
458 for (iter = begin; iter < end; iter++)
459 fct(tracepoint_ptr_deref(iter), priv);
462 #ifdef CONFIG_MODULES
463 bool trace_module_has_bad_taint(struct module *mod)
465 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
466 (1 << TAINT_UNSIGNED_MODULE));
469 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
472 * register_tracepoint_notifier - register tracepoint coming/going notifier
473 * @nb: notifier block
475 * Notifiers registered with this function are called on module
476 * coming/going with the tracepoint_module_list_mutex held.
477 * The notifier block callback should expect a "struct tp_module" data
480 int register_tracepoint_module_notifier(struct notifier_block *nb)
482 struct tp_module *tp_mod;
485 mutex_lock(&tracepoint_module_list_mutex);
486 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
489 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
490 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
492 mutex_unlock(&tracepoint_module_list_mutex);
495 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
498 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
499 * @nb: notifier block
501 * The notifier block callback should expect a "struct tp_module" data
504 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
506 struct tp_module *tp_mod;
509 mutex_lock(&tracepoint_module_list_mutex);
510 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
513 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
514 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
516 mutex_unlock(&tracepoint_module_list_mutex);
520 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
523 * Ensure the tracer unregistered the module's probes before the module
524 * teardown is performed. Prevents leaks of probe and data pointers.
526 static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
528 WARN_ON_ONCE(tp->funcs);
531 static int tracepoint_module_coming(struct module *mod)
533 struct tp_module *tp_mod;
536 if (!mod->num_tracepoints)
540 * We skip modules that taint the kernel, especially those with different
541 * module headers (for forced load), to make sure we don't cause a crash.
542 * Staging, out-of-tree, and unsigned GPL modules are fine.
544 if (trace_module_has_bad_taint(mod))
546 mutex_lock(&tracepoint_module_list_mutex);
547 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
553 list_add_tail(&tp_mod->list, &tracepoint_module_list);
554 blocking_notifier_call_chain(&tracepoint_notify_list,
555 MODULE_STATE_COMING, tp_mod);
557 mutex_unlock(&tracepoint_module_list_mutex);
561 static void tracepoint_module_going(struct module *mod)
563 struct tp_module *tp_mod;
565 if (!mod->num_tracepoints)
568 mutex_lock(&tracepoint_module_list_mutex);
569 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
570 if (tp_mod->mod == mod) {
571 blocking_notifier_call_chain(&tracepoint_notify_list,
572 MODULE_STATE_GOING, tp_mod);
573 list_del(&tp_mod->list);
576 * Called the going notifier before checking for
579 for_each_tracepoint_range(mod->tracepoints_ptrs,
580 mod->tracepoints_ptrs + mod->num_tracepoints,
581 tp_module_going_check_quiescent, NULL);
586 * In the case of modules that were tainted at "coming", we'll simply
587 * walk through the list without finding it. We cannot use the "tainted"
588 * flag on "going", in case a module taints the kernel only after being
591 mutex_unlock(&tracepoint_module_list_mutex);
594 static int tracepoint_module_notify(struct notifier_block *self,
595 unsigned long val, void *data)
597 struct module *mod = data;
601 case MODULE_STATE_COMING:
602 ret = tracepoint_module_coming(mod);
604 case MODULE_STATE_LIVE:
606 case MODULE_STATE_GOING:
607 tracepoint_module_going(mod);
609 case MODULE_STATE_UNFORMED:
615 static struct notifier_block tracepoint_module_nb = {
616 .notifier_call = tracepoint_module_notify,
620 static __init int init_tracepoints(void)
624 ret = register_module_notifier(&tracepoint_module_nb);
626 pr_warn("Failed to register tracepoint module enter notifier\n");
630 __initcall(init_tracepoints);
631 #endif /* CONFIG_MODULES */
634 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
636 * @priv: private data
638 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
641 for_each_tracepoint_range(__start___tracepoints_ptrs,
642 __stop___tracepoints_ptrs, fct, priv);
644 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
646 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
648 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
649 static int sys_tracepoint_refcount;
651 int syscall_regfunc(void)
653 struct task_struct *p, *t;
655 if (!sys_tracepoint_refcount) {
656 read_lock(&tasklist_lock);
657 for_each_process_thread(p, t) {
658 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
660 read_unlock(&tasklist_lock);
662 sys_tracepoint_refcount++;
667 void syscall_unregfunc(void)
669 struct task_struct *p, *t;
671 sys_tracepoint_refcount--;
672 if (!sys_tracepoint_refcount) {
673 read_lock(&tasklist_lock);
674 for_each_process_thread(p, t) {
675 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
677 read_unlock(&tasklist_lock);