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.h>
28 #include <linux/static_key.h>
30 extern struct tracepoint * const __start___tracepoints_ptrs[];
31 extern struct tracepoint * const __stop___tracepoints_ptrs[];
33 /* Set to 1 to enable tracepoint debug output */
34 static const int tracepoint_debug;
38 * Tracepoint module list mutex protects the local module list.
40 static DEFINE_MUTEX(tracepoint_module_list_mutex);
42 /* Local list of struct tp_module */
43 static LIST_HEAD(tracepoint_module_list);
44 #endif /* CONFIG_MODULES */
47 * tracepoints_mutex protects the builtin and module tracepoints.
48 * tracepoints_mutex nests inside tracepoint_module_list_mutex.
50 static DEFINE_MUTEX(tracepoints_mutex);
54 * It is used to delay the free of multiple probes array until a quiescent
59 struct tracepoint_func probes[0];
62 /* Called in removal of a func but failed to allocate a new tp_funcs */
63 static void tp_stub_func(void)
68 static inline void *allocate_probes(int count)
70 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
71 + sizeof(struct tp_probes), GFP_KERNEL);
72 return p == NULL ? NULL : p->probes;
75 static void rcu_free_old_probes(struct rcu_head *head)
77 kfree(container_of(head, struct tp_probes, rcu));
80 static inline void release_probes(struct tracepoint_func *old)
83 struct tp_probes *tp_probes = container_of(old,
84 struct tp_probes, probes[0]);
85 call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
89 static void debug_print_probes(struct tracepoint_func *funcs)
93 if (!tracepoint_debug || !funcs)
96 for (i = 0; funcs[i].func; i++)
97 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
100 static struct tracepoint_func *
101 func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
104 struct tracepoint_func *old, *new;
109 if (WARN_ON(!tp_func->func))
110 return ERR_PTR(-EINVAL);
112 debug_print_probes(*funcs);
115 /* (N -> N+1), (N != 0, 1) probes */
116 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
117 /* Insert before probes of lower priority */
118 if (pos < 0 && old[nr_probes].prio < prio)
120 if (old[nr_probes].func == tp_func->func &&
121 old[nr_probes].data == tp_func->data)
122 return ERR_PTR(-EEXIST);
123 if (old[nr_probes].func == tp_stub_func)
127 /* + 2 : one for new probe, one for NULL func - stub functions */
128 new = allocate_probes(nr_probes + 2 - stub_funcs);
130 return ERR_PTR(-ENOMEM);
133 /* Need to copy one at a time to remove stubs */
137 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
138 if (old[nr_probes].func == tp_stub_func)
140 if (pos < 0 && old[nr_probes].prio < prio)
142 new[probes++] = old[nr_probes];
148 nr_probes--; /* Account for insertion */
150 } else if (pos < 0) {
152 memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
154 /* Copy higher priority probes ahead of the new probe */
155 memcpy(new, old, pos * sizeof(struct tracepoint_func));
156 /* Copy the rest after it. */
157 memcpy(new + pos + 1, old + pos,
158 (nr_probes - pos) * sizeof(struct tracepoint_func));
163 new[nr_probes + 1].func = NULL;
165 debug_print_probes(*funcs);
169 static void *func_remove(struct tracepoint_func **funcs,
170 struct tracepoint_func *tp_func)
172 int nr_probes = 0, nr_del = 0, i;
173 struct tracepoint_func *old, *new;
178 return ERR_PTR(-ENOENT);
180 debug_print_probes(*funcs);
181 /* (N -> M), (N > 1, M >= 0) probes */
183 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
184 if ((old[nr_probes].func == tp_func->func &&
185 old[nr_probes].data == tp_func->data) ||
186 old[nr_probes].func == tp_stub_func)
192 * If probe is NULL, then nr_probes = nr_del = 0, and then the
193 * entire entry will be removed.
195 if (nr_probes - nr_del == 0) {
196 /* N -> 0, (N > 1) */
198 debug_print_probes(*funcs);
202 /* N -> M, (N > 1, M > 0) */
204 new = allocate_probes(nr_probes - nr_del + 1);
206 for (i = 0; old[i].func; i++)
207 if ((old[i].func != tp_func->func
208 || old[i].data != tp_func->data)
209 && old[i].func != tp_stub_func)
211 new[nr_probes - nr_del].func = NULL;
215 * Failed to allocate, replace the old function
216 * with calls to tp_stub_func.
218 for (i = 0; old[i].func; i++)
219 if (old[i].func == tp_func->func &&
220 old[i].data == tp_func->data) {
221 old[i].func = tp_stub_func;
222 /* Set the prio to the next event. */
232 debug_print_probes(*funcs);
237 * Add the probe function to a tracepoint.
239 static int tracepoint_add_func(struct tracepoint *tp,
240 struct tracepoint_func *func, int prio)
242 struct tracepoint_func *old, *tp_funcs;
244 if (tp->regfunc && !static_key_enabled(&tp->key))
247 tp_funcs = rcu_dereference_protected(tp->funcs,
248 lockdep_is_held(&tracepoints_mutex));
249 old = func_add(&tp_funcs, func, prio);
251 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
256 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
257 * probe callbacks array is consistent before setting a pointer to it.
258 * This array is referenced by __DO_TRACE from
259 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
262 rcu_assign_pointer(tp->funcs, tp_funcs);
263 if (!static_key_enabled(&tp->key))
264 static_key_slow_inc(&tp->key);
270 * Remove a probe function from a tracepoint.
271 * Note: only waiting an RCU period after setting elem->call to the empty
272 * function insures that the original callback is not used anymore. This insured
273 * by preempt_disable around the call site.
275 static int tracepoint_remove_func(struct tracepoint *tp,
276 struct tracepoint_func *func)
278 struct tracepoint_func *old, *tp_funcs;
280 tp_funcs = rcu_dereference_protected(tp->funcs,
281 lockdep_is_held(&tracepoints_mutex));
282 old = func_remove(&tp_funcs, func);
283 if (WARN_ON_ONCE(IS_ERR(old)))
287 /* Failed allocating new tp_funcs, replaced func with stub */
291 /* Removed last function */
292 if (tp->unregfunc && static_key_enabled(&tp->key))
295 if (static_key_enabled(&tp->key))
296 static_key_slow_dec(&tp->key);
298 rcu_assign_pointer(tp->funcs, tp_funcs);
304 * tracepoint_probe_register - Connect a probe to a tracepoint
306 * @probe: probe handler
307 * @data: tracepoint data
308 * @prio: priority of this function over other registered functions
310 * Returns 0 if ok, error value on error.
311 * Note: if @tp is within a module, the caller is responsible for
312 * unregistering the probe before the module is gone. This can be
313 * performed either with a tracepoint module going notifier, or from
314 * within module exit functions.
316 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
317 void *data, int prio)
319 struct tracepoint_func tp_func;
322 mutex_lock(&tracepoints_mutex);
323 tp_func.func = probe;
326 ret = tracepoint_add_func(tp, &tp_func, prio);
327 mutex_unlock(&tracepoints_mutex);
330 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
333 * tracepoint_probe_register - Connect a probe to a tracepoint
335 * @probe: probe handler
336 * @data: tracepoint data
337 * @prio: priority of this function over other registered functions
339 * Returns 0 if ok, error value on error.
340 * Note: if @tp is within a module, the caller is responsible for
341 * unregistering the probe before the module is gone. This can be
342 * performed either with a tracepoint module going notifier, or from
343 * within module exit functions.
345 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
347 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
349 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
352 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
354 * @probe: probe function pointer
355 * @data: tracepoint data
357 * Returns 0 if ok, error value on error.
359 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
361 struct tracepoint_func tp_func;
364 mutex_lock(&tracepoints_mutex);
365 tp_func.func = probe;
367 ret = tracepoint_remove_func(tp, &tp_func);
368 mutex_unlock(&tracepoints_mutex);
371 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
373 #ifdef CONFIG_MODULES
374 bool trace_module_has_bad_taint(struct module *mod)
376 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
377 (1 << TAINT_UNSIGNED_MODULE));
380 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
383 * register_tracepoint_notifier - register tracepoint coming/going notifier
384 * @nb: notifier block
386 * Notifiers registered with this function are called on module
387 * coming/going with the tracepoint_module_list_mutex held.
388 * The notifier block callback should expect a "struct tp_module" data
391 int register_tracepoint_module_notifier(struct notifier_block *nb)
393 struct tp_module *tp_mod;
396 mutex_lock(&tracepoint_module_list_mutex);
397 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
400 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
401 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
403 mutex_unlock(&tracepoint_module_list_mutex);
406 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
409 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
410 * @nb: notifier block
412 * The notifier block callback should expect a "struct tp_module" data
415 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
417 struct tp_module *tp_mod;
420 mutex_lock(&tracepoint_module_list_mutex);
421 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
424 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
425 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
427 mutex_unlock(&tracepoint_module_list_mutex);
431 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
434 * Ensure the tracer unregistered the module's probes before the module
435 * teardown is performed. Prevents leaks of probe and data pointers.
437 static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
438 struct tracepoint * const *end)
440 struct tracepoint * const *iter;
444 for (iter = begin; iter < end; iter++)
445 WARN_ON_ONCE((*iter)->funcs);
448 static int tracepoint_module_coming(struct module *mod)
450 struct tp_module *tp_mod;
453 if (!mod->num_tracepoints)
457 * We skip modules that taint the kernel, especially those with different
458 * module headers (for forced load), to make sure we don't cause a crash.
459 * Staging, out-of-tree, and unsigned GPL modules are fine.
461 if (trace_module_has_bad_taint(mod))
463 mutex_lock(&tracepoint_module_list_mutex);
464 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
470 list_add_tail(&tp_mod->list, &tracepoint_module_list);
471 blocking_notifier_call_chain(&tracepoint_notify_list,
472 MODULE_STATE_COMING, tp_mod);
474 mutex_unlock(&tracepoint_module_list_mutex);
478 static void tracepoint_module_going(struct module *mod)
480 struct tp_module *tp_mod;
482 if (!mod->num_tracepoints)
485 mutex_lock(&tracepoint_module_list_mutex);
486 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
487 if (tp_mod->mod == mod) {
488 blocking_notifier_call_chain(&tracepoint_notify_list,
489 MODULE_STATE_GOING, tp_mod);
490 list_del(&tp_mod->list);
493 * Called the going notifier before checking for
496 tp_module_going_check_quiescent(mod->tracepoints_ptrs,
497 mod->tracepoints_ptrs + mod->num_tracepoints);
502 * In the case of modules that were tainted at "coming", we'll simply
503 * walk through the list without finding it. We cannot use the "tainted"
504 * flag on "going", in case a module taints the kernel only after being
507 mutex_unlock(&tracepoint_module_list_mutex);
510 static int tracepoint_module_notify(struct notifier_block *self,
511 unsigned long val, void *data)
513 struct module *mod = data;
517 case MODULE_STATE_COMING:
518 ret = tracepoint_module_coming(mod);
520 case MODULE_STATE_LIVE:
522 case MODULE_STATE_GOING:
523 tracepoint_module_going(mod);
525 case MODULE_STATE_UNFORMED:
531 static struct notifier_block tracepoint_module_nb = {
532 .notifier_call = tracepoint_module_notify,
536 static __init int init_tracepoints(void)
540 ret = register_module_notifier(&tracepoint_module_nb);
542 pr_warning("Failed to register tracepoint module enter notifier\n");
546 __initcall(init_tracepoints);
547 #endif /* CONFIG_MODULES */
549 static void for_each_tracepoint_range(struct tracepoint * const *begin,
550 struct tracepoint * const *end,
551 void (*fct)(struct tracepoint *tp, void *priv),
554 struct tracepoint * const *iter;
558 for (iter = begin; iter < end; iter++)
563 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
565 * @priv: private data
567 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
570 for_each_tracepoint_range(__start___tracepoints_ptrs,
571 __stop___tracepoints_ptrs, fct, priv);
573 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
575 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
577 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
578 static int sys_tracepoint_refcount;
580 void syscall_regfunc(void)
582 struct task_struct *p, *t;
584 if (!sys_tracepoint_refcount) {
585 read_lock(&tasklist_lock);
586 for_each_process_thread(p, t) {
587 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
589 read_unlock(&tasklist_lock);
591 sys_tracepoint_refcount++;
594 void syscall_unregfunc(void)
596 struct task_struct *p, *t;
598 sys_tracepoint_refcount--;
599 if (!sys_tracepoint_refcount) {
600 read_lock(&tasklist_lock);
601 for_each_process_thread(p, t) {
602 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
604 read_unlock(&tasklist_lock);