Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux-2.6/btrfs-unstable.git] / kernel / tracepoint.c
blobbf2c06ef9afc3d5c2a5eecb620aa9acb07a7bba5
1 /*
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 struct tracepoint * const __start___tracepoints_ptrs[];
32 extern struct tracepoint * const __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;
40 #ifdef CONFIG_MODULES
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;
60 * Note about RCU :
61 * It is used to delay the free of multiple probes array until a quiescent
62 * state is reached.
64 struct tp_probes {
65 struct rcu_head rcu;
66 struct tracepoint_func probes[0];
69 static inline void *allocate_probes(int count)
71 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
72 + sizeof(struct tp_probes), GFP_KERNEL);
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)
88 struct rcu_head *tmp;
90 ok_to_free_tracepoints = true;
92 while (early_probes) {
93 tmp = early_probes;
94 early_probes = tmp->next;
95 call_rcu_sched(tmp, rcu_free_old_probes);
98 return 0;
101 /* SRCU is initialized at core_initcall */
102 postcore_initcall(release_early_probes);
104 static inline void release_probes(struct tracepoint_func *old)
106 if (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;
117 return;
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_sched(&tp_probes->rcu, rcu_free_old_probes);
130 static void debug_print_probes(struct tracepoint_func *funcs)
132 int i;
134 if (!tracepoint_debug || !funcs)
135 return;
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,
143 int prio)
145 struct tracepoint_func *old, *new;
146 int nr_probes = 0;
147 int pos = -1;
149 if (WARN_ON(!tp_func->func))
150 return ERR_PTR(-EINVAL);
152 debug_print_probes(*funcs);
153 old = *funcs;
154 if (old) {
155 /* (N -> N+1), (N != 0, 1) probes */
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;
160 if (old[nr_probes].func == tp_func->func &&
161 old[nr_probes].data == tp_func->data)
162 return ERR_PTR(-EEXIST);
165 /* + 2 : one for new probe, one for NULL func */
166 new = allocate_probes(nr_probes + 2);
167 if (new == NULL)
168 return ERR_PTR(-ENOMEM);
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));
180 } else
181 pos = 0;
182 new[pos] = *tp_func;
183 new[nr_probes + 1].func = NULL;
184 *funcs = new;
185 debug_print_probes(*funcs);
186 return old;
189 static void *func_remove(struct tracepoint_func **funcs,
190 struct tracepoint_func *tp_func)
192 int nr_probes = 0, nr_del = 0, i;
193 struct tracepoint_func *old, *new;
195 old = *funcs;
197 if (!old)
198 return ERR_PTR(-ENOENT);
200 debug_print_probes(*funcs);
201 /* (N -> M), (N > 1, M >= 0) probes */
202 if (tp_func->func) {
203 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
204 if (old[nr_probes].func == tp_func->func &&
205 old[nr_probes].data == tp_func->data)
206 nr_del++;
211 * If probe is NULL, then nr_probes = nr_del = 0, and then the
212 * entire entry will be removed.
214 if (nr_probes - nr_del == 0) {
215 /* N -> 0, (N > 1) */
216 *funcs = NULL;
217 debug_print_probes(*funcs);
218 return old;
219 } else {
220 int j = 0;
221 /* N -> M, (N > 1, M > 0) */
222 /* + 1 for NULL */
223 new = allocate_probes(nr_probes - nr_del + 1);
224 if (new == NULL)
225 return ERR_PTR(-ENOMEM);
226 for (i = 0; old[i].func; i++)
227 if (old[i].func != tp_func->func
228 || old[i].data != tp_func->data)
229 new[j++] = old[i];
230 new[nr_probes - nr_del].func = NULL;
231 *funcs = new;
233 debug_print_probes(*funcs);
234 return old;
238 * Add the probe function to a tracepoint.
240 static int tracepoint_add_func(struct tracepoint *tp,
241 struct tracepoint_func *func, int prio)
243 struct tracepoint_func *old, *tp_funcs;
244 int ret;
246 if (tp->regfunc && !static_key_enabled(&tp->key)) {
247 ret = tp->regfunc();
248 if (ret < 0)
249 return ret;
252 tp_funcs = rcu_dereference_protected(tp->funcs,
253 lockdep_is_held(&tracepoints_mutex));
254 old = func_add(&tp_funcs, func, prio);
255 if (IS_ERR(old)) {
256 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
257 return PTR_ERR(old);
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().
266 rcu_assign_pointer(tp->funcs, tp_funcs);
267 if (!static_key_enabled(&tp->key))
268 static_key_slow_inc(&tp->key);
269 release_probes(old);
270 return 0;
274 * Remove a probe function from a tracepoint.
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.
279 static int tracepoint_remove_func(struct tracepoint *tp,
280 struct tracepoint_func *func)
282 struct tracepoint_func *old, *tp_funcs;
284 tp_funcs = rcu_dereference_protected(tp->funcs,
285 lockdep_is_held(&tracepoints_mutex));
286 old = func_remove(&tp_funcs, func);
287 if (IS_ERR(old)) {
288 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
289 return PTR_ERR(old);
292 if (!tp_funcs) {
293 /* Removed last function */
294 if (tp->unregfunc && static_key_enabled(&tp->key))
295 tp->unregfunc();
297 if (static_key_enabled(&tp->key))
298 static_key_slow_dec(&tp->key);
300 rcu_assign_pointer(tp->funcs, tp_funcs);
301 release_probes(old);
302 return 0;
306 * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority
307 * @tp: tracepoint
308 * @probe: probe handler
309 * @data: tracepoint data
310 * @prio: priority of this function over other registered functions
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.
318 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
319 void *data, int prio)
321 struct tracepoint_func tp_func;
322 int ret;
324 mutex_lock(&tracepoints_mutex);
325 tp_func.func = probe;
326 tp_func.data = data;
327 tp_func.prio = prio;
328 ret = tracepoint_add_func(tp, &tp_func, prio);
329 mutex_unlock(&tracepoints_mutex);
330 return ret;
332 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
335 * tracepoint_probe_register - Connect a probe to a tracepoint
336 * @tp: tracepoint
337 * @probe: probe handler
338 * @data: tracepoint data
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.
346 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
348 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
350 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
353 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
354 * @tp: tracepoint
355 * @probe: probe function pointer
356 * @data: tracepoint data
358 * Returns 0 if ok, error value on error.
360 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
362 struct tracepoint_func tp_func;
363 int ret;
365 mutex_lock(&tracepoints_mutex);
366 tp_func.func = probe;
367 tp_func.data = data;
368 ret = tracepoint_remove_func(tp, &tp_func);
369 mutex_unlock(&tracepoints_mutex);
370 return ret;
372 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
374 static void for_each_tracepoint_range(struct tracepoint * const *begin,
375 struct tracepoint * const *end,
376 void (*fct)(struct tracepoint *tp, void *priv),
377 void *priv)
379 if (!begin)
380 return;
382 if (IS_ENABLED(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)) {
383 const int *iter;
385 for (iter = (const int *)begin; iter < (const int *)end; iter++)
386 fct(offset_to_ptr(iter), priv);
387 } else {
388 struct tracepoint * const *iter;
390 for (iter = begin; iter < end; iter++)
391 fct(*iter, priv);
395 #ifdef CONFIG_MODULES
396 bool trace_module_has_bad_taint(struct module *mod)
398 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
399 (1 << TAINT_UNSIGNED_MODULE));
402 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
405 * register_tracepoint_notifier - register tracepoint coming/going notifier
406 * @nb: notifier block
408 * Notifiers registered with this function are called on module
409 * coming/going with the tracepoint_module_list_mutex held.
410 * The notifier block callback should expect a "struct tp_module" data
411 * pointer.
413 int register_tracepoint_module_notifier(struct notifier_block *nb)
415 struct tp_module *tp_mod;
416 int ret;
418 mutex_lock(&tracepoint_module_list_mutex);
419 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
420 if (ret)
421 goto end;
422 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
423 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
424 end:
425 mutex_unlock(&tracepoint_module_list_mutex);
426 return ret;
428 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
431 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
432 * @nb: notifier block
434 * The notifier block callback should expect a "struct tp_module" data
435 * pointer.
437 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
439 struct tp_module *tp_mod;
440 int ret;
442 mutex_lock(&tracepoint_module_list_mutex);
443 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
444 if (ret)
445 goto end;
446 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
447 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
448 end:
449 mutex_unlock(&tracepoint_module_list_mutex);
450 return ret;
453 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
456 * Ensure the tracer unregistered the module's probes before the module
457 * teardown is performed. Prevents leaks of probe and data pointers.
459 static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
461 WARN_ON_ONCE(tp->funcs);
464 static int tracepoint_module_coming(struct module *mod)
466 struct tp_module *tp_mod;
467 int ret = 0;
469 if (!mod->num_tracepoints)
470 return 0;
473 * We skip modules that taint the kernel, especially those with different
474 * module headers (for forced load), to make sure we don't cause a crash.
475 * Staging, out-of-tree, and unsigned GPL modules are fine.
477 if (trace_module_has_bad_taint(mod))
478 return 0;
479 mutex_lock(&tracepoint_module_list_mutex);
480 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
481 if (!tp_mod) {
482 ret = -ENOMEM;
483 goto end;
485 tp_mod->mod = mod;
486 list_add_tail(&tp_mod->list, &tracepoint_module_list);
487 blocking_notifier_call_chain(&tracepoint_notify_list,
488 MODULE_STATE_COMING, tp_mod);
489 end:
490 mutex_unlock(&tracepoint_module_list_mutex);
491 return ret;
494 static void tracepoint_module_going(struct module *mod)
496 struct tp_module *tp_mod;
498 if (!mod->num_tracepoints)
499 return;
501 mutex_lock(&tracepoint_module_list_mutex);
502 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
503 if (tp_mod->mod == mod) {
504 blocking_notifier_call_chain(&tracepoint_notify_list,
505 MODULE_STATE_GOING, tp_mod);
506 list_del(&tp_mod->list);
507 kfree(tp_mod);
509 * Called the going notifier before checking for
510 * quiescence.
512 for_each_tracepoint_range(mod->tracepoints_ptrs,
513 mod->tracepoints_ptrs + mod->num_tracepoints,
514 tp_module_going_check_quiescent, NULL);
515 break;
519 * In the case of modules that were tainted at "coming", we'll simply
520 * walk through the list without finding it. We cannot use the "tainted"
521 * flag on "going", in case a module taints the kernel only after being
522 * loaded.
524 mutex_unlock(&tracepoint_module_list_mutex);
527 static int tracepoint_module_notify(struct notifier_block *self,
528 unsigned long val, void *data)
530 struct module *mod = data;
531 int ret = 0;
533 switch (val) {
534 case MODULE_STATE_COMING:
535 ret = tracepoint_module_coming(mod);
536 break;
537 case MODULE_STATE_LIVE:
538 break;
539 case MODULE_STATE_GOING:
540 tracepoint_module_going(mod);
541 break;
542 case MODULE_STATE_UNFORMED:
543 break;
545 return ret;
548 static struct notifier_block tracepoint_module_nb = {
549 .notifier_call = tracepoint_module_notify,
550 .priority = 0,
553 static __init int init_tracepoints(void)
555 int ret;
557 ret = register_module_notifier(&tracepoint_module_nb);
558 if (ret)
559 pr_warn("Failed to register tracepoint module enter notifier\n");
561 return ret;
563 __initcall(init_tracepoints);
564 #endif /* CONFIG_MODULES */
567 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
568 * @fct: callback
569 * @priv: private data
571 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
572 void *priv)
574 for_each_tracepoint_range(__start___tracepoints_ptrs,
575 __stop___tracepoints_ptrs, fct, priv);
577 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
579 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
581 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
582 static int sys_tracepoint_refcount;
584 int syscall_regfunc(void)
586 struct task_struct *p, *t;
588 if (!sys_tracepoint_refcount) {
589 read_lock(&tasklist_lock);
590 for_each_process_thread(p, t) {
591 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
593 read_unlock(&tasklist_lock);
595 sys_tracepoint_refcount++;
597 return 0;
600 void syscall_unregfunc(void)
602 struct task_struct *p, *t;
604 sys_tracepoint_refcount--;
605 if (!sys_tracepoint_refcount) {
606 read_lock(&tasklist_lock);
607 for_each_process_thread(p, t) {
608 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
610 read_unlock(&tasklist_lock);
613 #endif