1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2008-2014 Mathieu Desnoyers
5 #include <linux/module.h>
6 #include <linux/mutex.h>
7 #include <linux/types.h>
8 #include <linux/jhash.h>
9 #include <linux/list.h>
10 #include <linux/rcupdate.h>
11 #include <linux/tracepoint.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/sched/signal.h>
15 #include <linux/sched/task.h>
16 #include <linux/static_key.h>
25 extern tracepoint_ptr_t __start___tracepoints_ptrs[];
26 extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
28 DEFINE_SRCU(tracepoint_srcu);
29 EXPORT_SYMBOL_GPL(tracepoint_srcu);
31 /* Set to 1 to enable tracepoint debug output */
32 static const int tracepoint_debug;
36 * Tracepoint module list mutex protects the local module list.
38 static DEFINE_MUTEX(tracepoint_module_list_mutex);
40 /* Local list of struct tp_module */
41 static LIST_HEAD(tracepoint_module_list);
42 #endif /* CONFIG_MODULES */
45 * tracepoints_mutex protects the builtin and module tracepoints.
46 * tracepoints_mutex nests inside tracepoint_module_list_mutex.
48 static DEFINE_MUTEX(tracepoints_mutex);
50 static struct rcu_head *early_probes;
51 static bool ok_to_free_tracepoints;
55 * It is used to delay the free of multiple probes array until a quiescent
60 struct tracepoint_func probes[];
63 /* Called in removal of a func but failed to allocate a new tp_funcs */
64 static void tp_stub_func(void)
69 static inline void *allocate_probes(int count)
71 struct tp_probes *p = kmalloc(struct_size(p, probes, count),
73 return p == NULL ? NULL : p->probes;
76 static void srcu_free_old_probes(struct rcu_head *head)
78 kfree(container_of(head, struct tp_probes, rcu));
81 static void rcu_free_old_probes(struct rcu_head *head)
83 call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
86 static __init int release_early_probes(void)
90 ok_to_free_tracepoints = true;
92 while (early_probes) {
94 early_probes = tmp->next;
95 call_rcu(tmp, rcu_free_old_probes);
101 /* SRCU is initialized at core_initcall */
102 postcore_initcall(release_early_probes);
104 static inline void release_probes(struct tracepoint_func *old)
107 struct tp_probes *tp_probes = container_of(old,
108 struct tp_probes, probes[0]);
111 * We can't free probes if SRCU is not initialized yet.
112 * Postpone the freeing till after SRCU is initialized.
114 if (unlikely(!ok_to_free_tracepoints)) {
115 tp_probes->rcu.next = early_probes;
116 early_probes = &tp_probes->rcu;
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.
126 call_rcu(&tp_probes->rcu, rcu_free_old_probes);
130 static void debug_print_probes(struct tracepoint_func *funcs)
134 if (!tracepoint_debug || !funcs)
137 for (i = 0; funcs[i].func; i++)
138 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
141 static struct tracepoint_func *
142 func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
145 struct tracepoint_func *old, *new;
146 int iter_probes; /* Iterate over old probe array. */
147 int nr_probes = 0; /* Counter for probes */
148 int pos = -1; /* Insertion position into new array */
150 if (WARN_ON(!tp_func->func))
151 return ERR_PTR(-EINVAL);
153 debug_print_probes(*funcs);
156 /* (N -> N+1), (N != 0, 1) probes */
157 for (iter_probes = 0; old[iter_probes].func; iter_probes++) {
158 if (old[iter_probes].func == tp_stub_func)
159 continue; /* Skip stub functions. */
160 if (old[iter_probes].func == tp_func->func &&
161 old[iter_probes].data == tp_func->data)
162 return ERR_PTR(-EEXIST);
166 /* + 2 : one for new probe, one for NULL func */
167 new = allocate_probes(nr_probes + 2);
169 return ERR_PTR(-ENOMEM);
172 for (iter_probes = 0; old[iter_probes].func; iter_probes++) {
173 if (old[iter_probes].func == tp_stub_func)
175 /* Insert before probes of lower priority */
176 if (pos < 0 && old[iter_probes].prio < prio)
178 new[nr_probes++] = old[iter_probes];
182 /* nr_probes now points to the end of the new array */
185 nr_probes = 1; /* must point at end of array */
188 new[nr_probes].func = NULL;
190 debug_print_probes(*funcs);
194 static void *func_remove(struct tracepoint_func **funcs,
195 struct tracepoint_func *tp_func)
197 int nr_probes = 0, nr_del = 0, i;
198 struct tracepoint_func *old, *new;
203 return ERR_PTR(-ENOENT);
205 debug_print_probes(*funcs);
206 /* (N -> M), (N > 1, M >= 0) probes */
208 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
209 if ((old[nr_probes].func == tp_func->func &&
210 old[nr_probes].data == tp_func->data) ||
211 old[nr_probes].func == tp_stub_func)
217 * If probe is NULL, then nr_probes = nr_del = 0, and then the
218 * entire entry will be removed.
220 if (nr_probes - nr_del == 0) {
221 /* N -> 0, (N > 1) */
223 debug_print_probes(*funcs);
227 /* N -> M, (N > 1, M > 0) */
229 new = allocate_probes(nr_probes - nr_del + 1);
231 for (i = 0; old[i].func; i++) {
232 if ((old[i].func != tp_func->func ||
233 old[i].data != tp_func->data) &&
234 old[i].func != tp_stub_func)
237 new[nr_probes - nr_del].func = NULL;
241 * Failed to allocate, replace the old function
242 * with calls to tp_stub_func.
244 for (i = 0; old[i].func; i++) {
245 if (old[i].func == tp_func->func &&
246 old[i].data == tp_func->data)
247 WRITE_ONCE(old[i].func, tp_stub_func);
252 debug_print_probes(*funcs);
257 * Count the number of functions (enum tp_func_state) in a tp_funcs array.
259 static enum tp_func_state nr_func_state(const struct tracepoint_func *tp_funcs)
263 if (!tp_funcs[1].func)
265 if (!tp_funcs[2].func)
267 return TP_FUNC_N; /* 3 or more */
270 static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func *tp_funcs)
272 void *func = tp->iterator;
274 /* Synthetic events do not have static call sites */
275 if (!tp->static_call_key)
277 if (nr_func_state(tp_funcs) == TP_FUNC_1)
278 func = tp_funcs[0].func;
279 __static_call_update(tp->static_call_key, tp->static_call_tramp, func);
283 * Add the probe function to a tracepoint.
285 static int tracepoint_add_func(struct tracepoint *tp,
286 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(warn && 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 switch (nr_func_state(tp_funcs)) {
313 case TP_FUNC_1: /* 0->1 */
314 /* Set static call to first function */
315 tracepoint_update_call(tp, tp_funcs);
316 /* Both iterator and static call handle NULL tp->funcs */
317 rcu_assign_pointer(tp->funcs, tp_funcs);
318 static_key_enable(&tp->key);
320 case TP_FUNC_2: /* 1->2 */
321 /* Set iterator static call */
322 tracepoint_update_call(tp, tp_funcs);
324 * Iterator callback installed before updating tp->funcs.
325 * Requires ordering between RCU assign/dereference and
326 * static call update/call.
328 rcu_assign_pointer(tp->funcs, tp_funcs);
330 case TP_FUNC_N: /* N->N+1 (N>1) */
331 rcu_assign_pointer(tp->funcs, tp_funcs);
343 * Remove a probe function from a tracepoint.
344 * Note: only waiting an RCU period after setting elem->call to the empty
345 * function insures that the original callback is not used anymore. This insured
346 * by preempt_disable around the call site.
348 static int tracepoint_remove_func(struct tracepoint *tp,
349 struct tracepoint_func *func)
351 struct tracepoint_func *old, *tp_funcs;
353 tp_funcs = rcu_dereference_protected(tp->funcs,
354 lockdep_is_held(&tracepoints_mutex));
355 old = func_remove(&tp_funcs, func);
356 if (WARN_ON_ONCE(IS_ERR(old)))
360 /* Failed allocating new tp_funcs, replaced func with stub */
363 switch (nr_func_state(tp_funcs)) {
364 case TP_FUNC_0: /* 1->0 */
365 /* Removed last function */
366 if (tp->unregfunc && static_key_enabled(&tp->key))
369 static_key_disable(&tp->key);
370 /* Set iterator static call */
371 tracepoint_update_call(tp, tp_funcs);
372 /* Both iterator and static call handle NULL tp->funcs */
373 rcu_assign_pointer(tp->funcs, NULL);
375 * Make sure new func never uses old data after a 1->0->1
376 * transition sequence.
377 * Considering that transition 0->1 is the common case
378 * and don't have rcu-sync, issue rcu-sync after
379 * transition 1->0 to break that sequence by waiting for
380 * readers to be quiescent.
382 tracepoint_synchronize_unregister();
384 case TP_FUNC_1: /* 2->1 */
385 rcu_assign_pointer(tp->funcs, tp_funcs);
387 * On 2->1 transition, RCU sync is needed before setting
388 * static call to first callback, because the observer
389 * may have loaded any prior tp->funcs after the last one
390 * associated with an rcu-sync.
392 tracepoint_synchronize_unregister();
393 /* Set static call to first function */
394 tracepoint_update_call(tp, tp_funcs);
396 case TP_FUNC_2: /* N->N-1 (N>2) */
399 rcu_assign_pointer(tp->funcs, tp_funcs);
410 * tracepoint_probe_register_prio_may_exist - Connect a probe to a tracepoint with priority
412 * @probe: probe handler
413 * @data: tracepoint data
414 * @prio: priority of this function over other registered functions
416 * Same as tracepoint_probe_register_prio() except that it will not warn
417 * if the tracepoint is already registered.
419 int tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe,
420 void *data, int prio)
422 struct tracepoint_func tp_func;
425 mutex_lock(&tracepoints_mutex);
426 tp_func.func = probe;
429 ret = tracepoint_add_func(tp, &tp_func, prio, false);
430 mutex_unlock(&tracepoints_mutex);
433 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio_may_exist);
436 * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority
438 * @probe: probe handler
439 * @data: tracepoint data
440 * @prio: priority of this function over other registered functions
442 * Returns 0 if ok, error value on error.
443 * Note: if @tp is within a module, the caller is responsible for
444 * unregistering the probe before the module is gone. This can be
445 * performed either with a tracepoint module going notifier, or from
446 * within module exit functions.
448 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
449 void *data, int prio)
451 struct tracepoint_func tp_func;
454 mutex_lock(&tracepoints_mutex);
455 tp_func.func = probe;
458 ret = tracepoint_add_func(tp, &tp_func, prio, true);
459 mutex_unlock(&tracepoints_mutex);
462 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
465 * tracepoint_probe_register - Connect a probe to a tracepoint
467 * @probe: probe handler
468 * @data: tracepoint data
470 * Returns 0 if ok, error value on error.
471 * Note: if @tp is within a module, the caller is responsible for
472 * unregistering the probe before the module is gone. This can be
473 * performed either with a tracepoint module going notifier, or from
474 * within module exit functions.
476 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
478 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
480 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
483 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
485 * @probe: probe function pointer
486 * @data: tracepoint data
488 * Returns 0 if ok, error value on error.
490 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
492 struct tracepoint_func tp_func;
495 mutex_lock(&tracepoints_mutex);
496 tp_func.func = probe;
498 ret = tracepoint_remove_func(tp, &tp_func);
499 mutex_unlock(&tracepoints_mutex);
502 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
504 static void for_each_tracepoint_range(
505 tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
506 void (*fct)(struct tracepoint *tp, void *priv),
509 tracepoint_ptr_t *iter;
513 for (iter = begin; iter < end; iter++)
514 fct(tracepoint_ptr_deref(iter), priv);
517 #ifdef CONFIG_MODULES
518 bool trace_module_has_bad_taint(struct module *mod)
520 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
521 (1 << TAINT_UNSIGNED_MODULE));
524 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
527 * register_tracepoint_notifier - register tracepoint coming/going notifier
528 * @nb: notifier block
530 * Notifiers registered with this function are called on module
531 * coming/going with the tracepoint_module_list_mutex held.
532 * The notifier block callback should expect a "struct tp_module" data
535 int register_tracepoint_module_notifier(struct notifier_block *nb)
537 struct tp_module *tp_mod;
540 mutex_lock(&tracepoint_module_list_mutex);
541 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
544 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
545 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
547 mutex_unlock(&tracepoint_module_list_mutex);
550 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
553 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
554 * @nb: notifier block
556 * The notifier block callback should expect a "struct tp_module" data
559 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
561 struct tp_module *tp_mod;
564 mutex_lock(&tracepoint_module_list_mutex);
565 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
568 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
569 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
571 mutex_unlock(&tracepoint_module_list_mutex);
575 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
578 * Ensure the tracer unregistered the module's probes before the module
579 * teardown is performed. Prevents leaks of probe and data pointers.
581 static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
583 WARN_ON_ONCE(tp->funcs);
586 static int tracepoint_module_coming(struct module *mod)
588 struct tp_module *tp_mod;
591 if (!mod->num_tracepoints)
595 * We skip modules that taint the kernel, especially those with different
596 * module headers (for forced load), to make sure we don't cause a crash.
597 * Staging, out-of-tree, and unsigned GPL modules are fine.
599 if (trace_module_has_bad_taint(mod))
601 mutex_lock(&tracepoint_module_list_mutex);
602 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
608 list_add_tail(&tp_mod->list, &tracepoint_module_list);
609 blocking_notifier_call_chain(&tracepoint_notify_list,
610 MODULE_STATE_COMING, tp_mod);
612 mutex_unlock(&tracepoint_module_list_mutex);
616 static void tracepoint_module_going(struct module *mod)
618 struct tp_module *tp_mod;
620 if (!mod->num_tracepoints)
623 mutex_lock(&tracepoint_module_list_mutex);
624 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
625 if (tp_mod->mod == mod) {
626 blocking_notifier_call_chain(&tracepoint_notify_list,
627 MODULE_STATE_GOING, tp_mod);
628 list_del(&tp_mod->list);
631 * Called the going notifier before checking for
634 for_each_tracepoint_range(mod->tracepoints_ptrs,
635 mod->tracepoints_ptrs + mod->num_tracepoints,
636 tp_module_going_check_quiescent, NULL);
641 * In the case of modules that were tainted at "coming", we'll simply
642 * walk through the list without finding it. We cannot use the "tainted"
643 * flag on "going", in case a module taints the kernel only after being
646 mutex_unlock(&tracepoint_module_list_mutex);
649 static int tracepoint_module_notify(struct notifier_block *self,
650 unsigned long val, void *data)
652 struct module *mod = data;
656 case MODULE_STATE_COMING:
657 ret = tracepoint_module_coming(mod);
659 case MODULE_STATE_LIVE:
661 case MODULE_STATE_GOING:
662 tracepoint_module_going(mod);
664 case MODULE_STATE_UNFORMED:
667 return notifier_from_errno(ret);
670 static struct notifier_block tracepoint_module_nb = {
671 .notifier_call = tracepoint_module_notify,
675 static __init int init_tracepoints(void)
679 ret = register_module_notifier(&tracepoint_module_nb);
681 pr_warn("Failed to register tracepoint module enter notifier\n");
685 __initcall(init_tracepoints);
686 #endif /* CONFIG_MODULES */
689 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
691 * @priv: private data
693 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
696 for_each_tracepoint_range(__start___tracepoints_ptrs,
697 __stop___tracepoints_ptrs, fct, priv);
699 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
701 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
703 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
704 static int sys_tracepoint_refcount;
706 int syscall_regfunc(void)
708 struct task_struct *p, *t;
710 if (!sys_tracepoint_refcount) {
711 read_lock(&tasklist_lock);
712 for_each_process_thread(p, t) {
713 set_task_syscall_work(t, SYSCALL_TRACEPOINT);
715 read_unlock(&tasklist_lock);
717 sys_tracepoint_refcount++;
722 void syscall_unregfunc(void)
724 struct task_struct *p, *t;
726 sys_tracepoint_refcount--;
727 if (!sys_tracepoint_refcount) {
728 read_lock(&tasklist_lock);
729 for_each_process_thread(p, t) {
730 clear_task_syscall_work(t, SYSCALL_TRACEPOINT);
732 read_unlock(&tasklist_lock);