xref: /linux/kernel/tracepoint.c (revision a395d6a7e3d6e3d1d316376db0c4c8b5d2995930)
197e1c18eSMathieu Desnoyers /*
2de7b2973SMathieu Desnoyers  * Copyright (C) 2008-2014 Mathieu Desnoyers
397e1c18eSMathieu Desnoyers  *
497e1c18eSMathieu Desnoyers  * This program is free software; you can redistribute it and/or modify
597e1c18eSMathieu Desnoyers  * it under the terms of the GNU General Public License as published by
697e1c18eSMathieu Desnoyers  * the Free Software Foundation; either version 2 of the License, or
797e1c18eSMathieu Desnoyers  * (at your option) any later version.
897e1c18eSMathieu Desnoyers  *
997e1c18eSMathieu Desnoyers  * This program is distributed in the hope that it will be useful,
1097e1c18eSMathieu Desnoyers  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1197e1c18eSMathieu Desnoyers  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1297e1c18eSMathieu Desnoyers  * GNU General Public License for more details.
1397e1c18eSMathieu Desnoyers  *
1497e1c18eSMathieu Desnoyers  * You should have received a copy of the GNU General Public License
1597e1c18eSMathieu Desnoyers  * along with this program; if not, write to the Free Software
1697e1c18eSMathieu Desnoyers  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1797e1c18eSMathieu Desnoyers  */
1897e1c18eSMathieu Desnoyers #include <linux/module.h>
1997e1c18eSMathieu Desnoyers #include <linux/mutex.h>
2097e1c18eSMathieu Desnoyers #include <linux/types.h>
2197e1c18eSMathieu Desnoyers #include <linux/jhash.h>
2297e1c18eSMathieu Desnoyers #include <linux/list.h>
2397e1c18eSMathieu Desnoyers #include <linux/rcupdate.h>
2497e1c18eSMathieu Desnoyers #include <linux/tracepoint.h>
2597e1c18eSMathieu Desnoyers #include <linux/err.h>
2697e1c18eSMathieu Desnoyers #include <linux/slab.h>
27a871bd33SJason Baron #include <linux/sched.h>
28c5905afbSIngo Molnar #include <linux/static_key.h>
2997e1c18eSMathieu Desnoyers 
3065498646SMathieu Desnoyers extern struct tracepoint * const __start___tracepoints_ptrs[];
3165498646SMathieu Desnoyers extern struct tracepoint * const __stop___tracepoints_ptrs[];
3297e1c18eSMathieu Desnoyers 
3397e1c18eSMathieu Desnoyers /* Set to 1 to enable tracepoint debug output */
3497e1c18eSMathieu Desnoyers static const int tracepoint_debug;
3597e1c18eSMathieu Desnoyers 
36b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES
37de7b2973SMathieu Desnoyers /*
38de7b2973SMathieu Desnoyers  * Tracepoint module list mutex protects the local module list.
39de7b2973SMathieu Desnoyers  */
40de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoint_module_list_mutex);
41de7b2973SMathieu Desnoyers 
42de7b2973SMathieu Desnoyers /* Local list of struct tp_module */
43b75ef8b4SMathieu Desnoyers static LIST_HEAD(tracepoint_module_list);
44b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */
45b75ef8b4SMathieu Desnoyers 
4697e1c18eSMathieu Desnoyers /*
47de7b2973SMathieu Desnoyers  * tracepoints_mutex protects the builtin and module tracepoints.
48de7b2973SMathieu Desnoyers  * tracepoints_mutex nests inside tracepoint_module_list_mutex.
4997e1c18eSMathieu Desnoyers  */
50de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoints_mutex);
5197e1c18eSMathieu Desnoyers 
5297e1c18eSMathieu Desnoyers /*
5397e1c18eSMathieu Desnoyers  * Note about RCU :
54fd589a8fSAnand Gadiyar  * It is used to delay the free of multiple probes array until a quiescent
5597e1c18eSMathieu Desnoyers  * state is reached.
5697e1c18eSMathieu Desnoyers  */
5719dba33cSLai Jiangshan struct tp_probes {
5819dba33cSLai Jiangshan 	struct rcu_head rcu;
5938516ab5SSteven Rostedt 	struct tracepoint_func probes[0];
6019dba33cSLai Jiangshan };
6197e1c18eSMathieu Desnoyers 
6219dba33cSLai Jiangshan static inline void *allocate_probes(int count)
6397e1c18eSMathieu Desnoyers {
6438516ab5SSteven Rostedt 	struct tp_probes *p  = kmalloc(count * sizeof(struct tracepoint_func)
6519dba33cSLai Jiangshan 			+ sizeof(struct tp_probes), GFP_KERNEL);
6619dba33cSLai Jiangshan 	return p == NULL ? NULL : p->probes;
6797e1c18eSMathieu Desnoyers }
6897e1c18eSMathieu Desnoyers 
6919dba33cSLai Jiangshan static void rcu_free_old_probes(struct rcu_head *head)
7097e1c18eSMathieu Desnoyers {
710dea6d52SMathieu Desnoyers 	kfree(container_of(head, struct tp_probes, rcu));
7219dba33cSLai Jiangshan }
7319dba33cSLai Jiangshan 
7438516ab5SSteven Rostedt static inline void release_probes(struct tracepoint_func *old)
7519dba33cSLai Jiangshan {
7619dba33cSLai Jiangshan 	if (old) {
7719dba33cSLai Jiangshan 		struct tp_probes *tp_probes = container_of(old,
7819dba33cSLai Jiangshan 			struct tp_probes, probes[0]);
790dea6d52SMathieu Desnoyers 		call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
8019dba33cSLai Jiangshan 	}
8197e1c18eSMathieu Desnoyers }
8297e1c18eSMathieu Desnoyers 
83de7b2973SMathieu Desnoyers static void debug_print_probes(struct tracepoint_func *funcs)
8497e1c18eSMathieu Desnoyers {
8597e1c18eSMathieu Desnoyers 	int i;
8697e1c18eSMathieu Desnoyers 
87de7b2973SMathieu Desnoyers 	if (!tracepoint_debug || !funcs)
8897e1c18eSMathieu Desnoyers 		return;
8997e1c18eSMathieu Desnoyers 
90de7b2973SMathieu Desnoyers 	for (i = 0; funcs[i].func; i++)
91de7b2973SMathieu Desnoyers 		printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
9297e1c18eSMathieu Desnoyers }
9397e1c18eSMathieu Desnoyers 
947904b5c4SSteven Rostedt (Red Hat) static struct tracepoint_func *
957904b5c4SSteven Rostedt (Red Hat) func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
967904b5c4SSteven Rostedt (Red Hat) 	 int prio)
9797e1c18eSMathieu Desnoyers {
9838516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
997904b5c4SSteven Rostedt (Red Hat) 	int nr_probes = 0;
1007904b5c4SSteven Rostedt (Red Hat) 	int pos = -1;
10197e1c18eSMathieu Desnoyers 
102de7b2973SMathieu Desnoyers 	if (WARN_ON(!tp_func->func))
1034c69e6eaSSahara 		return ERR_PTR(-EINVAL);
10497e1c18eSMathieu Desnoyers 
105de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
106de7b2973SMathieu Desnoyers 	old = *funcs;
10797e1c18eSMathieu Desnoyers 	if (old) {
10897e1c18eSMathieu Desnoyers 		/* (N -> N+1), (N != 0, 1) probes */
1097904b5c4SSteven Rostedt (Red Hat) 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
1107904b5c4SSteven Rostedt (Red Hat) 			/* Insert before probes of lower priority */
1117904b5c4SSteven Rostedt (Red Hat) 			if (pos < 0 && old[nr_probes].prio < prio)
1127904b5c4SSteven Rostedt (Red Hat) 				pos = nr_probes;
113de7b2973SMathieu Desnoyers 			if (old[nr_probes].func == tp_func->func &&
114de7b2973SMathieu Desnoyers 			    old[nr_probes].data == tp_func->data)
11597e1c18eSMathieu Desnoyers 				return ERR_PTR(-EEXIST);
11697e1c18eSMathieu Desnoyers 		}
1177904b5c4SSteven Rostedt (Red Hat) 	}
11897e1c18eSMathieu Desnoyers 	/* + 2 : one for new probe, one for NULL func */
11919dba33cSLai Jiangshan 	new = allocate_probes(nr_probes + 2);
12097e1c18eSMathieu Desnoyers 	if (new == NULL)
12197e1c18eSMathieu Desnoyers 		return ERR_PTR(-ENOMEM);
1227904b5c4SSteven Rostedt (Red Hat) 	if (old) {
1237904b5c4SSteven Rostedt (Red Hat) 		if (pos < 0) {
1247904b5c4SSteven Rostedt (Red Hat) 			pos = nr_probes;
12538516ab5SSteven Rostedt 			memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
1267904b5c4SSteven Rostedt (Red Hat) 		} else {
1277904b5c4SSteven Rostedt (Red Hat) 			/* Copy higher priority probes ahead of the new probe */
1287904b5c4SSteven Rostedt (Red Hat) 			memcpy(new, old, pos * sizeof(struct tracepoint_func));
1297904b5c4SSteven Rostedt (Red Hat) 			/* Copy the rest after it. */
1307904b5c4SSteven Rostedt (Red Hat) 			memcpy(new + pos + 1, old + pos,
1317904b5c4SSteven Rostedt (Red Hat) 			       (nr_probes - pos) * sizeof(struct tracepoint_func));
1327904b5c4SSteven Rostedt (Red Hat) 		}
1337904b5c4SSteven Rostedt (Red Hat) 	} else
1347904b5c4SSteven Rostedt (Red Hat) 		pos = 0;
1357904b5c4SSteven Rostedt (Red Hat) 	new[pos] = *tp_func;
13638516ab5SSteven Rostedt 	new[nr_probes + 1].func = NULL;
137de7b2973SMathieu Desnoyers 	*funcs = new;
138de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
13997e1c18eSMathieu Desnoyers 	return old;
14097e1c18eSMathieu Desnoyers }
14197e1c18eSMathieu Desnoyers 
142de7b2973SMathieu Desnoyers static void *func_remove(struct tracepoint_func **funcs,
143de7b2973SMathieu Desnoyers 		struct tracepoint_func *tp_func)
14497e1c18eSMathieu Desnoyers {
14597e1c18eSMathieu Desnoyers 	int nr_probes = 0, nr_del = 0, i;
14638516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
14797e1c18eSMathieu Desnoyers 
148de7b2973SMathieu Desnoyers 	old = *funcs;
14997e1c18eSMathieu Desnoyers 
150f66af459SFrederic Weisbecker 	if (!old)
15119dba33cSLai Jiangshan 		return ERR_PTR(-ENOENT);
152f66af459SFrederic Weisbecker 
153de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
15497e1c18eSMathieu Desnoyers 	/* (N -> M), (N > 1, M >= 0) probes */
155de7b2973SMathieu Desnoyers 	if (tp_func->func) {
15638516ab5SSteven Rostedt 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
157de7b2973SMathieu Desnoyers 			if (old[nr_probes].func == tp_func->func &&
158de7b2973SMathieu Desnoyers 			     old[nr_probes].data == tp_func->data)
15997e1c18eSMathieu Desnoyers 				nr_del++;
16097e1c18eSMathieu Desnoyers 		}
1614c69e6eaSSahara 	}
16297e1c18eSMathieu Desnoyers 
1634c69e6eaSSahara 	/*
1644c69e6eaSSahara 	 * If probe is NULL, then nr_probes = nr_del = 0, and then the
1654c69e6eaSSahara 	 * entire entry will be removed.
1664c69e6eaSSahara 	 */
16797e1c18eSMathieu Desnoyers 	if (nr_probes - nr_del == 0) {
16897e1c18eSMathieu Desnoyers 		/* N -> 0, (N > 1) */
169de7b2973SMathieu Desnoyers 		*funcs = NULL;
170de7b2973SMathieu Desnoyers 		debug_print_probes(*funcs);
17197e1c18eSMathieu Desnoyers 		return old;
17297e1c18eSMathieu Desnoyers 	} else {
17397e1c18eSMathieu Desnoyers 		int j = 0;
17497e1c18eSMathieu Desnoyers 		/* N -> M, (N > 1, M > 0) */
17597e1c18eSMathieu Desnoyers 		/* + 1 for NULL */
17619dba33cSLai Jiangshan 		new = allocate_probes(nr_probes - nr_del + 1);
17797e1c18eSMathieu Desnoyers 		if (new == NULL)
17897e1c18eSMathieu Desnoyers 			return ERR_PTR(-ENOMEM);
17938516ab5SSteven Rostedt 		for (i = 0; old[i].func; i++)
180de7b2973SMathieu Desnoyers 			if (old[i].func != tp_func->func
181de7b2973SMathieu Desnoyers 					|| old[i].data != tp_func->data)
18297e1c18eSMathieu Desnoyers 				new[j++] = old[i];
18338516ab5SSteven Rostedt 		new[nr_probes - nr_del].func = NULL;
184de7b2973SMathieu Desnoyers 		*funcs = new;
18597e1c18eSMathieu Desnoyers 	}
186de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
18797e1c18eSMathieu Desnoyers 	return old;
18897e1c18eSMathieu Desnoyers }
18997e1c18eSMathieu Desnoyers 
19097e1c18eSMathieu Desnoyers /*
191de7b2973SMathieu Desnoyers  * Add the probe function to a tracepoint.
19297e1c18eSMathieu Desnoyers  */
193de7b2973SMathieu Desnoyers static int tracepoint_add_func(struct tracepoint *tp,
1947904b5c4SSteven Rostedt (Red Hat) 			       struct tracepoint_func *func, int prio)
19597e1c18eSMathieu Desnoyers {
196de7b2973SMathieu Desnoyers 	struct tracepoint_func *old, *tp_funcs;
19797e1c18eSMathieu Desnoyers 
198de7b2973SMathieu Desnoyers 	if (tp->regfunc && !static_key_enabled(&tp->key))
199de7b2973SMathieu Desnoyers 		tp->regfunc();
20097e1c18eSMathieu Desnoyers 
201b725dfeaSMathieu Desnoyers 	tp_funcs = rcu_dereference_protected(tp->funcs,
202b725dfeaSMathieu Desnoyers 			lockdep_is_held(&tracepoints_mutex));
2037904b5c4SSteven Rostedt (Red Hat) 	old = func_add(&tp_funcs, func, prio);
204de7b2973SMathieu Desnoyers 	if (IS_ERR(old)) {
205de7b2973SMathieu Desnoyers 		WARN_ON_ONCE(1);
206de7b2973SMathieu Desnoyers 		return PTR_ERR(old);
20797e1c18eSMathieu Desnoyers 	}
20897419875SJosh Stone 
20997e1c18eSMathieu Desnoyers 	/*
21097e1c18eSMathieu Desnoyers 	 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
21197e1c18eSMathieu Desnoyers 	 * probe callbacks array is consistent before setting a pointer to it.
21297e1c18eSMathieu Desnoyers 	 * This array is referenced by __DO_TRACE from
21397e1c18eSMathieu Desnoyers 	 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
21497e1c18eSMathieu Desnoyers 	 * is used.
21597e1c18eSMathieu Desnoyers 	 */
216de7b2973SMathieu Desnoyers 	rcu_assign_pointer(tp->funcs, tp_funcs);
217de7b2973SMathieu Desnoyers 	if (!static_key_enabled(&tp->key))
218de7b2973SMathieu Desnoyers 		static_key_slow_inc(&tp->key);
2198058bd0fSMathieu Desnoyers 	release_probes(old);
220de7b2973SMathieu Desnoyers 	return 0;
22197e1c18eSMathieu Desnoyers }
22297e1c18eSMathieu Desnoyers 
22397e1c18eSMathieu Desnoyers /*
224de7b2973SMathieu Desnoyers  * Remove a probe function from a tracepoint.
22597e1c18eSMathieu Desnoyers  * Note: only waiting an RCU period after setting elem->call to the empty
22697e1c18eSMathieu Desnoyers  * function insures that the original callback is not used anymore. This insured
22797e1c18eSMathieu Desnoyers  * by preempt_disable around the call site.
22897e1c18eSMathieu Desnoyers  */
229de7b2973SMathieu Desnoyers static int tracepoint_remove_func(struct tracepoint *tp,
230de7b2973SMathieu Desnoyers 		struct tracepoint_func *func)
23197e1c18eSMathieu Desnoyers {
232de7b2973SMathieu Desnoyers 	struct tracepoint_func *old, *tp_funcs;
23397419875SJosh Stone 
234b725dfeaSMathieu Desnoyers 	tp_funcs = rcu_dereference_protected(tp->funcs,
235b725dfeaSMathieu Desnoyers 			lockdep_is_held(&tracepoints_mutex));
236de7b2973SMathieu Desnoyers 	old = func_remove(&tp_funcs, func);
237de7b2973SMathieu Desnoyers 	if (IS_ERR(old)) {
238de7b2973SMathieu Desnoyers 		WARN_ON_ONCE(1);
239de7b2973SMathieu Desnoyers 		return PTR_ERR(old);
24097e1c18eSMathieu Desnoyers 	}
24197e1c18eSMathieu Desnoyers 
242de7b2973SMathieu Desnoyers 	if (!tp_funcs) {
243de7b2973SMathieu Desnoyers 		/* Removed last function */
244de7b2973SMathieu Desnoyers 		if (tp->unregfunc && static_key_enabled(&tp->key))
245de7b2973SMathieu Desnoyers 			tp->unregfunc();
24697e1c18eSMathieu Desnoyers 
247de7b2973SMathieu Desnoyers 		if (static_key_enabled(&tp->key))
248de7b2973SMathieu Desnoyers 			static_key_slow_dec(&tp->key);
24997e1c18eSMathieu Desnoyers 	}
250de7b2973SMathieu Desnoyers 	rcu_assign_pointer(tp->funcs, tp_funcs);
2518058bd0fSMathieu Desnoyers 	release_probes(old);
252de7b2973SMathieu Desnoyers 	return 0;
253127cafbbSLai Jiangshan }
254127cafbbSLai Jiangshan 
25597e1c18eSMathieu Desnoyers /**
25697e1c18eSMathieu Desnoyers  * tracepoint_probe_register -  Connect a probe to a tracepoint
257de7b2973SMathieu Desnoyers  * @tp: tracepoint
25897e1c18eSMathieu Desnoyers  * @probe: probe handler
259cac92ba7SFabian Frederick  * @data: tracepoint data
2607904b5c4SSteven Rostedt (Red Hat)  * @prio: priority of this function over other registered functions
2617904b5c4SSteven Rostedt (Red Hat)  *
2627904b5c4SSteven Rostedt (Red Hat)  * Returns 0 if ok, error value on error.
2637904b5c4SSteven Rostedt (Red Hat)  * Note: if @tp is within a module, the caller is responsible for
2647904b5c4SSteven Rostedt (Red Hat)  * unregistering the probe before the module is gone. This can be
2657904b5c4SSteven Rostedt (Red Hat)  * performed either with a tracepoint module going notifier, or from
2667904b5c4SSteven Rostedt (Red Hat)  * within module exit functions.
2677904b5c4SSteven Rostedt (Red Hat)  */
2687904b5c4SSteven Rostedt (Red Hat) int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
2697904b5c4SSteven Rostedt (Red Hat) 				   void *data, int prio)
2707904b5c4SSteven Rostedt (Red Hat) {
2717904b5c4SSteven Rostedt (Red Hat) 	struct tracepoint_func tp_func;
2727904b5c4SSteven Rostedt (Red Hat) 	int ret;
2737904b5c4SSteven Rostedt (Red Hat) 
2747904b5c4SSteven Rostedt (Red Hat) 	mutex_lock(&tracepoints_mutex);
2757904b5c4SSteven Rostedt (Red Hat) 	tp_func.func = probe;
2767904b5c4SSteven Rostedt (Red Hat) 	tp_func.data = data;
2777904b5c4SSteven Rostedt (Red Hat) 	tp_func.prio = prio;
2787904b5c4SSteven Rostedt (Red Hat) 	ret = tracepoint_add_func(tp, &tp_func, prio);
2797904b5c4SSteven Rostedt (Red Hat) 	mutex_unlock(&tracepoints_mutex);
2807904b5c4SSteven Rostedt (Red Hat) 	return ret;
2817904b5c4SSteven Rostedt (Red Hat) }
2827904b5c4SSteven Rostedt (Red Hat) EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
2837904b5c4SSteven Rostedt (Red Hat) 
2847904b5c4SSteven Rostedt (Red Hat) /**
2857904b5c4SSteven Rostedt (Red Hat)  * tracepoint_probe_register -  Connect a probe to a tracepoint
2867904b5c4SSteven Rostedt (Red Hat)  * @tp: tracepoint
2877904b5c4SSteven Rostedt (Red Hat)  * @probe: probe handler
2887904b5c4SSteven Rostedt (Red Hat)  * @data: tracepoint data
2897904b5c4SSteven Rostedt (Red Hat)  * @prio: priority of this function over other registered functions
29097e1c18eSMathieu Desnoyers  *
291de7b2973SMathieu Desnoyers  * Returns 0 if ok, error value on error.
292de7b2973SMathieu Desnoyers  * Note: if @tp is within a module, the caller is responsible for
293de7b2973SMathieu Desnoyers  * unregistering the probe before the module is gone. This can be
294de7b2973SMathieu Desnoyers  * performed either with a tracepoint module going notifier, or from
295de7b2973SMathieu Desnoyers  * within module exit functions.
29697e1c18eSMathieu Desnoyers  */
297de7b2973SMathieu Desnoyers int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
29897e1c18eSMathieu Desnoyers {
2997904b5c4SSteven Rostedt (Red Hat) 	return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
30097e1c18eSMathieu Desnoyers }
30197e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_register);
30297e1c18eSMathieu Desnoyers 
30397e1c18eSMathieu Desnoyers /**
30497e1c18eSMathieu Desnoyers  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
305de7b2973SMathieu Desnoyers  * @tp: tracepoint
30697e1c18eSMathieu Desnoyers  * @probe: probe function pointer
307cac92ba7SFabian Frederick  * @data: tracepoint data
30897e1c18eSMathieu Desnoyers  *
309de7b2973SMathieu Desnoyers  * Returns 0 if ok, error value on error.
31097e1c18eSMathieu Desnoyers  */
311de7b2973SMathieu Desnoyers int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
31297e1c18eSMathieu Desnoyers {
313de7b2973SMathieu Desnoyers 	struct tracepoint_func tp_func;
314de7b2973SMathieu Desnoyers 	int ret;
31597e1c18eSMathieu Desnoyers 
31697e1c18eSMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
317de7b2973SMathieu Desnoyers 	tp_func.func = probe;
318de7b2973SMathieu Desnoyers 	tp_func.data = data;
319de7b2973SMathieu Desnoyers 	ret = tracepoint_remove_func(tp, &tp_func);
32097e1c18eSMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
321de7b2973SMathieu Desnoyers 	return ret;
32297e1c18eSMathieu Desnoyers }
32397e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
32497e1c18eSMathieu Desnoyers 
325227a8375SIngo Molnar #ifdef CONFIG_MODULES
32645ab2813SSteven Rostedt (Red Hat) bool trace_module_has_bad_taint(struct module *mod)
32745ab2813SSteven Rostedt (Red Hat) {
32866cc69e3SMathieu Desnoyers 	return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
32966cc69e3SMathieu Desnoyers 			       (1 << TAINT_UNSIGNED_MODULE));
33045ab2813SSteven Rostedt (Red Hat) }
33145ab2813SSteven Rostedt (Red Hat) 
332de7b2973SMathieu Desnoyers static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
333de7b2973SMathieu Desnoyers 
334de7b2973SMathieu Desnoyers /**
335de7b2973SMathieu Desnoyers  * register_tracepoint_notifier - register tracepoint coming/going notifier
336de7b2973SMathieu Desnoyers  * @nb: notifier block
337de7b2973SMathieu Desnoyers  *
338de7b2973SMathieu Desnoyers  * Notifiers registered with this function are called on module
339de7b2973SMathieu Desnoyers  * coming/going with the tracepoint_module_list_mutex held.
340de7b2973SMathieu Desnoyers  * The notifier block callback should expect a "struct tp_module" data
341de7b2973SMathieu Desnoyers  * pointer.
342de7b2973SMathieu Desnoyers  */
343de7b2973SMathieu Desnoyers int register_tracepoint_module_notifier(struct notifier_block *nb)
344de7b2973SMathieu Desnoyers {
345de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
346de7b2973SMathieu Desnoyers 	int ret;
347de7b2973SMathieu Desnoyers 
348de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
349de7b2973SMathieu Desnoyers 	ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
350de7b2973SMathieu Desnoyers 	if (ret)
351de7b2973SMathieu Desnoyers 		goto end;
352de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
353de7b2973SMathieu Desnoyers 		(void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
354de7b2973SMathieu Desnoyers end:
355de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
356de7b2973SMathieu Desnoyers 	return ret;
357de7b2973SMathieu Desnoyers }
358de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
359de7b2973SMathieu Desnoyers 
360de7b2973SMathieu Desnoyers /**
361de7b2973SMathieu Desnoyers  * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
362de7b2973SMathieu Desnoyers  * @nb: notifier block
363de7b2973SMathieu Desnoyers  *
364de7b2973SMathieu Desnoyers  * The notifier block callback should expect a "struct tp_module" data
365de7b2973SMathieu Desnoyers  * pointer.
366de7b2973SMathieu Desnoyers  */
367de7b2973SMathieu Desnoyers int unregister_tracepoint_module_notifier(struct notifier_block *nb)
368de7b2973SMathieu Desnoyers {
369de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
370de7b2973SMathieu Desnoyers 	int ret;
371de7b2973SMathieu Desnoyers 
372de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
373de7b2973SMathieu Desnoyers 	ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
374de7b2973SMathieu Desnoyers 	if (ret)
375de7b2973SMathieu Desnoyers 		goto end;
376de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
377de7b2973SMathieu Desnoyers 		(void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
378de7b2973SMathieu Desnoyers end:
379de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
380de7b2973SMathieu Desnoyers 	return ret;
381de7b2973SMathieu Desnoyers 
382de7b2973SMathieu Desnoyers }
383de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
384de7b2973SMathieu Desnoyers 
385de7b2973SMathieu Desnoyers /*
386de7b2973SMathieu Desnoyers  * Ensure the tracer unregistered the module's probes before the module
387de7b2973SMathieu Desnoyers  * teardown is performed. Prevents leaks of probe and data pointers.
388de7b2973SMathieu Desnoyers  */
389de7b2973SMathieu Desnoyers static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
390de7b2973SMathieu Desnoyers 		struct tracepoint * const *end)
391de7b2973SMathieu Desnoyers {
392de7b2973SMathieu Desnoyers 	struct tracepoint * const *iter;
393de7b2973SMathieu Desnoyers 
394de7b2973SMathieu Desnoyers 	if (!begin)
395de7b2973SMathieu Desnoyers 		return;
396de7b2973SMathieu Desnoyers 	for (iter = begin; iter < end; iter++)
397de7b2973SMathieu Desnoyers 		WARN_ON_ONCE((*iter)->funcs);
398de7b2973SMathieu Desnoyers }
399de7b2973SMathieu Desnoyers 
400b75ef8b4SMathieu Desnoyers static int tracepoint_module_coming(struct module *mod)
401b75ef8b4SMathieu Desnoyers {
4020dea6d52SMathieu Desnoyers 	struct tp_module *tp_mod;
403b75ef8b4SMathieu Desnoyers 	int ret = 0;
404b75ef8b4SMathieu Desnoyers 
4057dec935aSSteven Rostedt (Red Hat) 	if (!mod->num_tracepoints)
4067dec935aSSteven Rostedt (Red Hat) 		return 0;
4077dec935aSSteven Rostedt (Red Hat) 
408b75ef8b4SMathieu Desnoyers 	/*
409c10076c4SSteven Rostedt 	 * We skip modules that taint the kernel, especially those with different
410c10076c4SSteven Rostedt 	 * module headers (for forced load), to make sure we don't cause a crash.
41166cc69e3SMathieu Desnoyers 	 * Staging, out-of-tree, and unsigned GPL modules are fine.
412b75ef8b4SMathieu Desnoyers 	 */
41345ab2813SSteven Rostedt (Red Hat) 	if (trace_module_has_bad_taint(mod))
414b75ef8b4SMathieu Desnoyers 		return 0;
415de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
416b75ef8b4SMathieu Desnoyers 	tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
417b75ef8b4SMathieu Desnoyers 	if (!tp_mod) {
418b75ef8b4SMathieu Desnoyers 		ret = -ENOMEM;
419b75ef8b4SMathieu Desnoyers 		goto end;
420b75ef8b4SMathieu Desnoyers 	}
421eb7d035cSSteven Rostedt (Red Hat) 	tp_mod->mod = mod;
4220dea6d52SMathieu Desnoyers 	list_add_tail(&tp_mod->list, &tracepoint_module_list);
423de7b2973SMathieu Desnoyers 	blocking_notifier_call_chain(&tracepoint_notify_list,
424de7b2973SMathieu Desnoyers 			MODULE_STATE_COMING, tp_mod);
425b75ef8b4SMathieu Desnoyers end:
426de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
427b75ef8b4SMathieu Desnoyers 	return ret;
428b75ef8b4SMathieu Desnoyers }
429b75ef8b4SMathieu Desnoyers 
430de7b2973SMathieu Desnoyers static void tracepoint_module_going(struct module *mod)
431b75ef8b4SMathieu Desnoyers {
432de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
433b75ef8b4SMathieu Desnoyers 
4347dec935aSSteven Rostedt (Red Hat) 	if (!mod->num_tracepoints)
435de7b2973SMathieu Desnoyers 		return;
4367dec935aSSteven Rostedt (Red Hat) 
437de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
438de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
439eb7d035cSSteven Rostedt (Red Hat) 		if (tp_mod->mod == mod) {
440de7b2973SMathieu Desnoyers 			blocking_notifier_call_chain(&tracepoint_notify_list,
441de7b2973SMathieu Desnoyers 					MODULE_STATE_GOING, tp_mod);
442de7b2973SMathieu Desnoyers 			list_del(&tp_mod->list);
443de7b2973SMathieu Desnoyers 			kfree(tp_mod);
444de7b2973SMathieu Desnoyers 			/*
445de7b2973SMathieu Desnoyers 			 * Called the going notifier before checking for
446de7b2973SMathieu Desnoyers 			 * quiescence.
447de7b2973SMathieu Desnoyers 			 */
448de7b2973SMathieu Desnoyers 			tp_module_going_check_quiescent(mod->tracepoints_ptrs,
449b75ef8b4SMathieu Desnoyers 				mod->tracepoints_ptrs + mod->num_tracepoints);
450b75ef8b4SMathieu Desnoyers 			break;
451b75ef8b4SMathieu Desnoyers 		}
452b75ef8b4SMathieu Desnoyers 	}
453b75ef8b4SMathieu Desnoyers 	/*
454b75ef8b4SMathieu Desnoyers 	 * In the case of modules that were tainted at "coming", we'll simply
455b75ef8b4SMathieu Desnoyers 	 * walk through the list without finding it. We cannot use the "tainted"
456b75ef8b4SMathieu Desnoyers 	 * flag on "going", in case a module taints the kernel only after being
457b75ef8b4SMathieu Desnoyers 	 * loaded.
458b75ef8b4SMathieu Desnoyers 	 */
459de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
460b75ef8b4SMathieu Desnoyers }
461227a8375SIngo Molnar 
462de7b2973SMathieu Desnoyers static int tracepoint_module_notify(struct notifier_block *self,
46332f85742SMathieu Desnoyers 		unsigned long val, void *data)
46432f85742SMathieu Desnoyers {
46532f85742SMathieu Desnoyers 	struct module *mod = data;
466b75ef8b4SMathieu Desnoyers 	int ret = 0;
46732f85742SMathieu Desnoyers 
46832f85742SMathieu Desnoyers 	switch (val) {
46932f85742SMathieu Desnoyers 	case MODULE_STATE_COMING:
470b75ef8b4SMathieu Desnoyers 		ret = tracepoint_module_coming(mod);
471b75ef8b4SMathieu Desnoyers 		break;
472b75ef8b4SMathieu Desnoyers 	case MODULE_STATE_LIVE:
473b75ef8b4SMathieu Desnoyers 		break;
47432f85742SMathieu Desnoyers 	case MODULE_STATE_GOING:
475de7b2973SMathieu Desnoyers 		tracepoint_module_going(mod);
476de7b2973SMathieu Desnoyers 		break;
477de7b2973SMathieu Desnoyers 	case MODULE_STATE_UNFORMED:
47832f85742SMathieu Desnoyers 		break;
47932f85742SMathieu Desnoyers 	}
480b75ef8b4SMathieu Desnoyers 	return ret;
48132f85742SMathieu Desnoyers }
48232f85742SMathieu Desnoyers 
483de7b2973SMathieu Desnoyers static struct notifier_block tracepoint_module_nb = {
48432f85742SMathieu Desnoyers 	.notifier_call = tracepoint_module_notify,
48532f85742SMathieu Desnoyers 	.priority = 0,
48632f85742SMathieu Desnoyers };
48732f85742SMathieu Desnoyers 
488de7b2973SMathieu Desnoyers static __init int init_tracepoints(void)
48932f85742SMathieu Desnoyers {
490de7b2973SMathieu Desnoyers 	int ret;
491de7b2973SMathieu Desnoyers 
492de7b2973SMathieu Desnoyers 	ret = register_module_notifier(&tracepoint_module_nb);
493eb7d035cSSteven Rostedt (Red Hat) 	if (ret)
494*a395d6a7SJoe Perches 		pr_warn("Failed to register tracepoint module enter notifier\n");
495eb7d035cSSteven Rostedt (Red Hat) 
496de7b2973SMathieu Desnoyers 	return ret;
49732f85742SMathieu Desnoyers }
49832f85742SMathieu Desnoyers __initcall(init_tracepoints);
499227a8375SIngo Molnar #endif /* CONFIG_MODULES */
500a871bd33SJason Baron 
501de7b2973SMathieu Desnoyers static void for_each_tracepoint_range(struct tracepoint * const *begin,
502de7b2973SMathieu Desnoyers 		struct tracepoint * const *end,
503de7b2973SMathieu Desnoyers 		void (*fct)(struct tracepoint *tp, void *priv),
504de7b2973SMathieu Desnoyers 		void *priv)
505de7b2973SMathieu Desnoyers {
506de7b2973SMathieu Desnoyers 	struct tracepoint * const *iter;
507de7b2973SMathieu Desnoyers 
508de7b2973SMathieu Desnoyers 	if (!begin)
509de7b2973SMathieu Desnoyers 		return;
510de7b2973SMathieu Desnoyers 	for (iter = begin; iter < end; iter++)
511de7b2973SMathieu Desnoyers 		fct(*iter, priv);
512de7b2973SMathieu Desnoyers }
513de7b2973SMathieu Desnoyers 
514de7b2973SMathieu Desnoyers /**
515de7b2973SMathieu Desnoyers  * for_each_kernel_tracepoint - iteration on all kernel tracepoints
516de7b2973SMathieu Desnoyers  * @fct: callback
517de7b2973SMathieu Desnoyers  * @priv: private data
518de7b2973SMathieu Desnoyers  */
519de7b2973SMathieu Desnoyers void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
520de7b2973SMathieu Desnoyers 		void *priv)
521de7b2973SMathieu Desnoyers {
522de7b2973SMathieu Desnoyers 	for_each_tracepoint_range(__start___tracepoints_ptrs,
523de7b2973SMathieu Desnoyers 		__stop___tracepoints_ptrs, fct, priv);
524de7b2973SMathieu Desnoyers }
525de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
526de7b2973SMathieu Desnoyers 
5273d27d8cbSJosh Stone #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
52860d970c2SIngo Molnar 
52997419875SJosh Stone /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
530a871bd33SJason Baron static int sys_tracepoint_refcount;
531a871bd33SJason Baron 
532a871bd33SJason Baron void syscall_regfunc(void)
533a871bd33SJason Baron {
5348063e41dSOleg Nesterov 	struct task_struct *p, *t;
535a871bd33SJason Baron 
536a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
5378063e41dSOleg Nesterov 		read_lock(&tasklist_lock);
5388063e41dSOleg Nesterov 		for_each_process_thread(p, t) {
53966700001SJosh Stone 			set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
5408063e41dSOleg Nesterov 		}
5418063e41dSOleg Nesterov 		read_unlock(&tasklist_lock);
542a871bd33SJason Baron 	}
543a871bd33SJason Baron 	sys_tracepoint_refcount++;
544a871bd33SJason Baron }
545a871bd33SJason Baron 
546a871bd33SJason Baron void syscall_unregfunc(void)
547a871bd33SJason Baron {
5488063e41dSOleg Nesterov 	struct task_struct *p, *t;
549a871bd33SJason Baron 
550a871bd33SJason Baron 	sys_tracepoint_refcount--;
551a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
5528063e41dSOleg Nesterov 		read_lock(&tasklist_lock);
5538063e41dSOleg Nesterov 		for_each_process_thread(p, t) {
55466700001SJosh Stone 			clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
5558063e41dSOleg Nesterov 		}
5568063e41dSOleg Nesterov 		read_unlock(&tasklist_lock);
557a871bd33SJason Baron 	}
558a871bd33SJason Baron }
55960d970c2SIngo Molnar #endif
560