xref: /linux/kernel/tracepoint.c (revision 243d1a7977ae0814aa1ccb8bb87f8a4e0822ca8f)
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>
273f07c014SIngo Molnar #include <linux/sched/signal.h>
2829930025SIngo Molnar #include <linux/sched/task.h>
29c5905afbSIngo Molnar #include <linux/static_key.h>
3097e1c18eSMathieu Desnoyers 
3165498646SMathieu Desnoyers extern struct tracepoint * const __start___tracepoints_ptrs[];
3265498646SMathieu Desnoyers extern struct tracepoint * const __stop___tracepoints_ptrs[];
3397e1c18eSMathieu Desnoyers 
3497e1c18eSMathieu Desnoyers /* Set to 1 to enable tracepoint debug output */
3597e1c18eSMathieu Desnoyers static const int tracepoint_debug;
3697e1c18eSMathieu Desnoyers 
37b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES
38de7b2973SMathieu Desnoyers /*
39de7b2973SMathieu Desnoyers  * Tracepoint module list mutex protects the local module list.
40de7b2973SMathieu Desnoyers  */
41de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoint_module_list_mutex);
42de7b2973SMathieu Desnoyers 
43de7b2973SMathieu Desnoyers /* Local list of struct tp_module */
44b75ef8b4SMathieu Desnoyers static LIST_HEAD(tracepoint_module_list);
45b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */
46b75ef8b4SMathieu Desnoyers 
4797e1c18eSMathieu Desnoyers /*
48de7b2973SMathieu Desnoyers  * tracepoints_mutex protects the builtin and module tracepoints.
49de7b2973SMathieu Desnoyers  * tracepoints_mutex nests inside tracepoint_module_list_mutex.
5097e1c18eSMathieu Desnoyers  */
51de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoints_mutex);
5297e1c18eSMathieu Desnoyers 
5397e1c18eSMathieu Desnoyers /*
5497e1c18eSMathieu Desnoyers  * Note about RCU :
55fd589a8fSAnand Gadiyar  * It is used to delay the free of multiple probes array until a quiescent
5697e1c18eSMathieu Desnoyers  * state is reached.
5797e1c18eSMathieu Desnoyers  */
5819dba33cSLai Jiangshan struct tp_probes {
5919dba33cSLai Jiangshan 	struct rcu_head rcu;
6038516ab5SSteven Rostedt 	struct tracepoint_func probes[0];
6119dba33cSLai Jiangshan };
6297e1c18eSMathieu Desnoyers 
6319dba33cSLai Jiangshan static inline void *allocate_probes(int count)
6497e1c18eSMathieu Desnoyers {
6538516ab5SSteven Rostedt 	struct tp_probes *p  = kmalloc(count * sizeof(struct tracepoint_func)
6619dba33cSLai Jiangshan 			+ sizeof(struct tp_probes), GFP_KERNEL);
6719dba33cSLai Jiangshan 	return p == NULL ? NULL : p->probes;
6897e1c18eSMathieu Desnoyers }
6997e1c18eSMathieu Desnoyers 
7019dba33cSLai Jiangshan static void rcu_free_old_probes(struct rcu_head *head)
7197e1c18eSMathieu Desnoyers {
720dea6d52SMathieu Desnoyers 	kfree(container_of(head, struct tp_probes, rcu));
7319dba33cSLai Jiangshan }
7419dba33cSLai Jiangshan 
7538516ab5SSteven Rostedt static inline void release_probes(struct tracepoint_func *old)
7619dba33cSLai Jiangshan {
7719dba33cSLai Jiangshan 	if (old) {
7819dba33cSLai Jiangshan 		struct tp_probes *tp_probes = container_of(old,
7919dba33cSLai Jiangshan 			struct tp_probes, probes[0]);
800dea6d52SMathieu Desnoyers 		call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
8119dba33cSLai Jiangshan 	}
8297e1c18eSMathieu Desnoyers }
8397e1c18eSMathieu Desnoyers 
84de7b2973SMathieu Desnoyers static void debug_print_probes(struct tracepoint_func *funcs)
8597e1c18eSMathieu Desnoyers {
8697e1c18eSMathieu Desnoyers 	int i;
8797e1c18eSMathieu Desnoyers 
88de7b2973SMathieu Desnoyers 	if (!tracepoint_debug || !funcs)
8997e1c18eSMathieu Desnoyers 		return;
9097e1c18eSMathieu Desnoyers 
91de7b2973SMathieu Desnoyers 	for (i = 0; funcs[i].func; i++)
92de7b2973SMathieu Desnoyers 		printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
9397e1c18eSMathieu Desnoyers }
9497e1c18eSMathieu Desnoyers 
957904b5c4SSteven Rostedt (Red Hat) static struct tracepoint_func *
967904b5c4SSteven Rostedt (Red Hat) func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
977904b5c4SSteven Rostedt (Red Hat) 	 int prio)
9897e1c18eSMathieu Desnoyers {
9938516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
1007904b5c4SSteven Rostedt (Red Hat) 	int nr_probes = 0;
1017904b5c4SSteven Rostedt (Red Hat) 	int pos = -1;
10297e1c18eSMathieu Desnoyers 
103de7b2973SMathieu Desnoyers 	if (WARN_ON(!tp_func->func))
1044c69e6eaSSahara 		return ERR_PTR(-EINVAL);
10597e1c18eSMathieu Desnoyers 
106de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
107de7b2973SMathieu Desnoyers 	old = *funcs;
10897e1c18eSMathieu Desnoyers 	if (old) {
10997e1c18eSMathieu Desnoyers 		/* (N -> N+1), (N != 0, 1) probes */
1107904b5c4SSteven Rostedt (Red Hat) 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
1117904b5c4SSteven Rostedt (Red Hat) 			/* Insert before probes of lower priority */
1127904b5c4SSteven Rostedt (Red Hat) 			if (pos < 0 && old[nr_probes].prio < prio)
1137904b5c4SSteven Rostedt (Red Hat) 				pos = nr_probes;
114de7b2973SMathieu Desnoyers 			if (old[nr_probes].func == tp_func->func &&
115de7b2973SMathieu Desnoyers 			    old[nr_probes].data == tp_func->data)
11697e1c18eSMathieu Desnoyers 				return ERR_PTR(-EEXIST);
11797e1c18eSMathieu Desnoyers 		}
1187904b5c4SSteven Rostedt (Red Hat) 	}
11997e1c18eSMathieu Desnoyers 	/* + 2 : one for new probe, one for NULL func */
12019dba33cSLai Jiangshan 	new = allocate_probes(nr_probes + 2);
12197e1c18eSMathieu Desnoyers 	if (new == NULL)
12297e1c18eSMathieu Desnoyers 		return ERR_PTR(-ENOMEM);
1237904b5c4SSteven Rostedt (Red Hat) 	if (old) {
1247904b5c4SSteven Rostedt (Red Hat) 		if (pos < 0) {
1257904b5c4SSteven Rostedt (Red Hat) 			pos = nr_probes;
12638516ab5SSteven Rostedt 			memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
1277904b5c4SSteven Rostedt (Red Hat) 		} else {
1287904b5c4SSteven Rostedt (Red Hat) 			/* Copy higher priority probes ahead of the new probe */
1297904b5c4SSteven Rostedt (Red Hat) 			memcpy(new, old, pos * sizeof(struct tracepoint_func));
1307904b5c4SSteven Rostedt (Red Hat) 			/* Copy the rest after it. */
1317904b5c4SSteven Rostedt (Red Hat) 			memcpy(new + pos + 1, old + pos,
1327904b5c4SSteven Rostedt (Red Hat) 			       (nr_probes - pos) * sizeof(struct tracepoint_func));
1337904b5c4SSteven Rostedt (Red Hat) 		}
1347904b5c4SSteven Rostedt (Red Hat) 	} else
1357904b5c4SSteven Rostedt (Red Hat) 		pos = 0;
1367904b5c4SSteven Rostedt (Red Hat) 	new[pos] = *tp_func;
13738516ab5SSteven Rostedt 	new[nr_probes + 1].func = NULL;
138de7b2973SMathieu Desnoyers 	*funcs = new;
139de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
14097e1c18eSMathieu Desnoyers 	return old;
14197e1c18eSMathieu Desnoyers }
14297e1c18eSMathieu Desnoyers 
143de7b2973SMathieu Desnoyers static void *func_remove(struct tracepoint_func **funcs,
144de7b2973SMathieu Desnoyers 		struct tracepoint_func *tp_func)
14597e1c18eSMathieu Desnoyers {
14697e1c18eSMathieu Desnoyers 	int nr_probes = 0, nr_del = 0, i;
14738516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
14897e1c18eSMathieu Desnoyers 
149de7b2973SMathieu Desnoyers 	old = *funcs;
15097e1c18eSMathieu Desnoyers 
151f66af459SFrederic Weisbecker 	if (!old)
15219dba33cSLai Jiangshan 		return ERR_PTR(-ENOENT);
153f66af459SFrederic Weisbecker 
154de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
15597e1c18eSMathieu Desnoyers 	/* (N -> M), (N > 1, M >= 0) probes */
156de7b2973SMathieu Desnoyers 	if (tp_func->func) {
15738516ab5SSteven Rostedt 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
158de7b2973SMathieu Desnoyers 			if (old[nr_probes].func == tp_func->func &&
159de7b2973SMathieu Desnoyers 			     old[nr_probes].data == tp_func->data)
16097e1c18eSMathieu Desnoyers 				nr_del++;
16197e1c18eSMathieu Desnoyers 		}
1624c69e6eaSSahara 	}
16397e1c18eSMathieu Desnoyers 
1644c69e6eaSSahara 	/*
1654c69e6eaSSahara 	 * If probe is NULL, then nr_probes = nr_del = 0, and then the
1664c69e6eaSSahara 	 * entire entry will be removed.
1674c69e6eaSSahara 	 */
16897e1c18eSMathieu Desnoyers 	if (nr_probes - nr_del == 0) {
16997e1c18eSMathieu Desnoyers 		/* N -> 0, (N > 1) */
170de7b2973SMathieu Desnoyers 		*funcs = NULL;
171de7b2973SMathieu Desnoyers 		debug_print_probes(*funcs);
17297e1c18eSMathieu Desnoyers 		return old;
17397e1c18eSMathieu Desnoyers 	} else {
17497e1c18eSMathieu Desnoyers 		int j = 0;
17597e1c18eSMathieu Desnoyers 		/* N -> M, (N > 1, M > 0) */
17697e1c18eSMathieu Desnoyers 		/* + 1 for NULL */
17719dba33cSLai Jiangshan 		new = allocate_probes(nr_probes - nr_del + 1);
17897e1c18eSMathieu Desnoyers 		if (new == NULL)
17997e1c18eSMathieu Desnoyers 			return ERR_PTR(-ENOMEM);
18038516ab5SSteven Rostedt 		for (i = 0; old[i].func; i++)
181de7b2973SMathieu Desnoyers 			if (old[i].func != tp_func->func
182de7b2973SMathieu Desnoyers 					|| old[i].data != tp_func->data)
18397e1c18eSMathieu Desnoyers 				new[j++] = old[i];
18438516ab5SSteven Rostedt 		new[nr_probes - nr_del].func = NULL;
185de7b2973SMathieu Desnoyers 		*funcs = new;
18697e1c18eSMathieu Desnoyers 	}
187de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
18897e1c18eSMathieu Desnoyers 	return old;
18997e1c18eSMathieu Desnoyers }
19097e1c18eSMathieu Desnoyers 
19197e1c18eSMathieu Desnoyers /*
192de7b2973SMathieu Desnoyers  * Add the probe function to a tracepoint.
19397e1c18eSMathieu Desnoyers  */
194de7b2973SMathieu Desnoyers static int tracepoint_add_func(struct tracepoint *tp,
1957904b5c4SSteven Rostedt (Red Hat) 			       struct tracepoint_func *func, int prio)
19697e1c18eSMathieu Desnoyers {
197de7b2973SMathieu Desnoyers 	struct tracepoint_func *old, *tp_funcs;
1988cf868afSSteven Rostedt (Red Hat) 	int ret;
19997e1c18eSMathieu Desnoyers 
2008cf868afSSteven Rostedt (Red Hat) 	if (tp->regfunc && !static_key_enabled(&tp->key)) {
2018cf868afSSteven Rostedt (Red Hat) 		ret = tp->regfunc();
2028cf868afSSteven Rostedt (Red Hat) 		if (ret < 0)
2038cf868afSSteven Rostedt (Red Hat) 			return ret;
2048cf868afSSteven Rostedt (Red Hat) 	}
20597e1c18eSMathieu Desnoyers 
206b725dfeaSMathieu Desnoyers 	tp_funcs = rcu_dereference_protected(tp->funcs,
207b725dfeaSMathieu Desnoyers 			lockdep_is_held(&tracepoints_mutex));
2087904b5c4SSteven Rostedt (Red Hat) 	old = func_add(&tp_funcs, func, prio);
209de7b2973SMathieu Desnoyers 	if (IS_ERR(old)) {
210de7b2973SMathieu Desnoyers 		WARN_ON_ONCE(1);
211de7b2973SMathieu Desnoyers 		return PTR_ERR(old);
21297e1c18eSMathieu Desnoyers 	}
21397419875SJosh Stone 
21497e1c18eSMathieu Desnoyers 	/*
215*243d1a79SPaul E. McKenney 	 * rcu_assign_pointer has as smp_store_release() which makes sure
216*243d1a79SPaul E. McKenney 	 * that the new probe callbacks array is consistent before setting
217*243d1a79SPaul E. McKenney 	 * a pointer to it.  This array is referenced by __DO_TRACE from
218*243d1a79SPaul E. McKenney 	 * include/linux/tracepoint.h using rcu_dereference_sched().
21997e1c18eSMathieu Desnoyers 	 */
220de7b2973SMathieu Desnoyers 	rcu_assign_pointer(tp->funcs, tp_funcs);
221de7b2973SMathieu Desnoyers 	if (!static_key_enabled(&tp->key))
222de7b2973SMathieu Desnoyers 		static_key_slow_inc(&tp->key);
2238058bd0fSMathieu Desnoyers 	release_probes(old);
224de7b2973SMathieu Desnoyers 	return 0;
22597e1c18eSMathieu Desnoyers }
22697e1c18eSMathieu Desnoyers 
22797e1c18eSMathieu Desnoyers /*
228de7b2973SMathieu Desnoyers  * Remove a probe function from a tracepoint.
22997e1c18eSMathieu Desnoyers  * Note: only waiting an RCU period after setting elem->call to the empty
23097e1c18eSMathieu Desnoyers  * function insures that the original callback is not used anymore. This insured
23197e1c18eSMathieu Desnoyers  * by preempt_disable around the call site.
23297e1c18eSMathieu Desnoyers  */
233de7b2973SMathieu Desnoyers static int tracepoint_remove_func(struct tracepoint *tp,
234de7b2973SMathieu Desnoyers 		struct tracepoint_func *func)
23597e1c18eSMathieu Desnoyers {
236de7b2973SMathieu Desnoyers 	struct tracepoint_func *old, *tp_funcs;
23797419875SJosh Stone 
238b725dfeaSMathieu Desnoyers 	tp_funcs = rcu_dereference_protected(tp->funcs,
239b725dfeaSMathieu Desnoyers 			lockdep_is_held(&tracepoints_mutex));
240de7b2973SMathieu Desnoyers 	old = func_remove(&tp_funcs, func);
241de7b2973SMathieu Desnoyers 	if (IS_ERR(old)) {
242de7b2973SMathieu Desnoyers 		WARN_ON_ONCE(1);
243de7b2973SMathieu Desnoyers 		return PTR_ERR(old);
24497e1c18eSMathieu Desnoyers 	}
24597e1c18eSMathieu Desnoyers 
246de7b2973SMathieu Desnoyers 	if (!tp_funcs) {
247de7b2973SMathieu Desnoyers 		/* Removed last function */
248de7b2973SMathieu Desnoyers 		if (tp->unregfunc && static_key_enabled(&tp->key))
249de7b2973SMathieu Desnoyers 			tp->unregfunc();
25097e1c18eSMathieu Desnoyers 
251de7b2973SMathieu Desnoyers 		if (static_key_enabled(&tp->key))
252de7b2973SMathieu Desnoyers 			static_key_slow_dec(&tp->key);
25397e1c18eSMathieu Desnoyers 	}
254de7b2973SMathieu Desnoyers 	rcu_assign_pointer(tp->funcs, tp_funcs);
2558058bd0fSMathieu Desnoyers 	release_probes(old);
256de7b2973SMathieu Desnoyers 	return 0;
257127cafbbSLai Jiangshan }
258127cafbbSLai Jiangshan 
25997e1c18eSMathieu Desnoyers /**
26097e1c18eSMathieu Desnoyers  * tracepoint_probe_register -  Connect a probe to a tracepoint
261de7b2973SMathieu Desnoyers  * @tp: tracepoint
26297e1c18eSMathieu Desnoyers  * @probe: probe handler
263cac92ba7SFabian Frederick  * @data: tracepoint data
2647904b5c4SSteven Rostedt (Red Hat)  * @prio: priority of this function over other registered functions
2657904b5c4SSteven Rostedt (Red Hat)  *
2667904b5c4SSteven Rostedt (Red Hat)  * Returns 0 if ok, error value on error.
2677904b5c4SSteven Rostedt (Red Hat)  * Note: if @tp is within a module, the caller is responsible for
2687904b5c4SSteven Rostedt (Red Hat)  * unregistering the probe before the module is gone. This can be
2697904b5c4SSteven Rostedt (Red Hat)  * performed either with a tracepoint module going notifier, or from
2707904b5c4SSteven Rostedt (Red Hat)  * within module exit functions.
2717904b5c4SSteven Rostedt (Red Hat)  */
2727904b5c4SSteven Rostedt (Red Hat) int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
2737904b5c4SSteven Rostedt (Red Hat) 				   void *data, int prio)
2747904b5c4SSteven Rostedt (Red Hat) {
2757904b5c4SSteven Rostedt (Red Hat) 	struct tracepoint_func tp_func;
2767904b5c4SSteven Rostedt (Red Hat) 	int ret;
2777904b5c4SSteven Rostedt (Red Hat) 
2787904b5c4SSteven Rostedt (Red Hat) 	mutex_lock(&tracepoints_mutex);
2797904b5c4SSteven Rostedt (Red Hat) 	tp_func.func = probe;
2807904b5c4SSteven Rostedt (Red Hat) 	tp_func.data = data;
2817904b5c4SSteven Rostedt (Red Hat) 	tp_func.prio = prio;
2827904b5c4SSteven Rostedt (Red Hat) 	ret = tracepoint_add_func(tp, &tp_func, prio);
2837904b5c4SSteven Rostedt (Red Hat) 	mutex_unlock(&tracepoints_mutex);
2847904b5c4SSteven Rostedt (Red Hat) 	return ret;
2857904b5c4SSteven Rostedt (Red Hat) }
2867904b5c4SSteven Rostedt (Red Hat) EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
2877904b5c4SSteven Rostedt (Red Hat) 
2887904b5c4SSteven Rostedt (Red Hat) /**
2897904b5c4SSteven Rostedt (Red Hat)  * tracepoint_probe_register -  Connect a probe to a tracepoint
2907904b5c4SSteven Rostedt (Red Hat)  * @tp: tracepoint
2917904b5c4SSteven Rostedt (Red Hat)  * @probe: probe handler
2927904b5c4SSteven Rostedt (Red Hat)  * @data: tracepoint data
2937904b5c4SSteven Rostedt (Red Hat)  * @prio: priority of this function over other registered functions
29497e1c18eSMathieu Desnoyers  *
295de7b2973SMathieu Desnoyers  * Returns 0 if ok, error value on error.
296de7b2973SMathieu Desnoyers  * Note: if @tp is within a module, the caller is responsible for
297de7b2973SMathieu Desnoyers  * unregistering the probe before the module is gone. This can be
298de7b2973SMathieu Desnoyers  * performed either with a tracepoint module going notifier, or from
299de7b2973SMathieu Desnoyers  * within module exit functions.
30097e1c18eSMathieu Desnoyers  */
301de7b2973SMathieu Desnoyers int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
30297e1c18eSMathieu Desnoyers {
3037904b5c4SSteven Rostedt (Red Hat) 	return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
30497e1c18eSMathieu Desnoyers }
30597e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_register);
30697e1c18eSMathieu Desnoyers 
30797e1c18eSMathieu Desnoyers /**
30897e1c18eSMathieu Desnoyers  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
309de7b2973SMathieu Desnoyers  * @tp: tracepoint
31097e1c18eSMathieu Desnoyers  * @probe: probe function pointer
311cac92ba7SFabian Frederick  * @data: tracepoint data
31297e1c18eSMathieu Desnoyers  *
313de7b2973SMathieu Desnoyers  * Returns 0 if ok, error value on error.
31497e1c18eSMathieu Desnoyers  */
315de7b2973SMathieu Desnoyers int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
31697e1c18eSMathieu Desnoyers {
317de7b2973SMathieu Desnoyers 	struct tracepoint_func tp_func;
318de7b2973SMathieu Desnoyers 	int ret;
31997e1c18eSMathieu Desnoyers 
32097e1c18eSMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
321de7b2973SMathieu Desnoyers 	tp_func.func = probe;
322de7b2973SMathieu Desnoyers 	tp_func.data = data;
323de7b2973SMathieu Desnoyers 	ret = tracepoint_remove_func(tp, &tp_func);
32497e1c18eSMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
325de7b2973SMathieu Desnoyers 	return ret;
32697e1c18eSMathieu Desnoyers }
32797e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
32897e1c18eSMathieu Desnoyers 
329227a8375SIngo Molnar #ifdef CONFIG_MODULES
33045ab2813SSteven Rostedt (Red Hat) bool trace_module_has_bad_taint(struct module *mod)
33145ab2813SSteven Rostedt (Red Hat) {
33266cc69e3SMathieu Desnoyers 	return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
33366cc69e3SMathieu Desnoyers 			       (1 << TAINT_UNSIGNED_MODULE));
33445ab2813SSteven Rostedt (Red Hat) }
33545ab2813SSteven Rostedt (Red Hat) 
336de7b2973SMathieu Desnoyers static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
337de7b2973SMathieu Desnoyers 
338de7b2973SMathieu Desnoyers /**
339de7b2973SMathieu Desnoyers  * register_tracepoint_notifier - register tracepoint coming/going notifier
340de7b2973SMathieu Desnoyers  * @nb: notifier block
341de7b2973SMathieu Desnoyers  *
342de7b2973SMathieu Desnoyers  * Notifiers registered with this function are called on module
343de7b2973SMathieu Desnoyers  * coming/going with the tracepoint_module_list_mutex held.
344de7b2973SMathieu Desnoyers  * The notifier block callback should expect a "struct tp_module" data
345de7b2973SMathieu Desnoyers  * pointer.
346de7b2973SMathieu Desnoyers  */
347de7b2973SMathieu Desnoyers int register_tracepoint_module_notifier(struct notifier_block *nb)
348de7b2973SMathieu Desnoyers {
349de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
350de7b2973SMathieu Desnoyers 	int ret;
351de7b2973SMathieu Desnoyers 
352de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
353de7b2973SMathieu Desnoyers 	ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
354de7b2973SMathieu Desnoyers 	if (ret)
355de7b2973SMathieu Desnoyers 		goto end;
356de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
357de7b2973SMathieu Desnoyers 		(void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
358de7b2973SMathieu Desnoyers end:
359de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
360de7b2973SMathieu Desnoyers 	return ret;
361de7b2973SMathieu Desnoyers }
362de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
363de7b2973SMathieu Desnoyers 
364de7b2973SMathieu Desnoyers /**
365de7b2973SMathieu Desnoyers  * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
366de7b2973SMathieu Desnoyers  * @nb: notifier block
367de7b2973SMathieu Desnoyers  *
368de7b2973SMathieu Desnoyers  * The notifier block callback should expect a "struct tp_module" data
369de7b2973SMathieu Desnoyers  * pointer.
370de7b2973SMathieu Desnoyers  */
371de7b2973SMathieu Desnoyers int unregister_tracepoint_module_notifier(struct notifier_block *nb)
372de7b2973SMathieu Desnoyers {
373de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
374de7b2973SMathieu Desnoyers 	int ret;
375de7b2973SMathieu Desnoyers 
376de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
377de7b2973SMathieu Desnoyers 	ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
378de7b2973SMathieu Desnoyers 	if (ret)
379de7b2973SMathieu Desnoyers 		goto end;
380de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
381de7b2973SMathieu Desnoyers 		(void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
382de7b2973SMathieu Desnoyers end:
383de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
384de7b2973SMathieu Desnoyers 	return ret;
385de7b2973SMathieu Desnoyers 
386de7b2973SMathieu Desnoyers }
387de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
388de7b2973SMathieu Desnoyers 
389de7b2973SMathieu Desnoyers /*
390de7b2973SMathieu Desnoyers  * Ensure the tracer unregistered the module's probes before the module
391de7b2973SMathieu Desnoyers  * teardown is performed. Prevents leaks of probe and data pointers.
392de7b2973SMathieu Desnoyers  */
393de7b2973SMathieu Desnoyers static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
394de7b2973SMathieu Desnoyers 		struct tracepoint * const *end)
395de7b2973SMathieu Desnoyers {
396de7b2973SMathieu Desnoyers 	struct tracepoint * const *iter;
397de7b2973SMathieu Desnoyers 
398de7b2973SMathieu Desnoyers 	if (!begin)
399de7b2973SMathieu Desnoyers 		return;
400de7b2973SMathieu Desnoyers 	for (iter = begin; iter < end; iter++)
401de7b2973SMathieu Desnoyers 		WARN_ON_ONCE((*iter)->funcs);
402de7b2973SMathieu Desnoyers }
403de7b2973SMathieu Desnoyers 
404b75ef8b4SMathieu Desnoyers static int tracepoint_module_coming(struct module *mod)
405b75ef8b4SMathieu Desnoyers {
4060dea6d52SMathieu Desnoyers 	struct tp_module *tp_mod;
407b75ef8b4SMathieu Desnoyers 	int ret = 0;
408b75ef8b4SMathieu Desnoyers 
4097dec935aSSteven Rostedt (Red Hat) 	if (!mod->num_tracepoints)
4107dec935aSSteven Rostedt (Red Hat) 		return 0;
4117dec935aSSteven Rostedt (Red Hat) 
412b75ef8b4SMathieu Desnoyers 	/*
413c10076c4SSteven Rostedt 	 * We skip modules that taint the kernel, especially those with different
414c10076c4SSteven Rostedt 	 * module headers (for forced load), to make sure we don't cause a crash.
41566cc69e3SMathieu Desnoyers 	 * Staging, out-of-tree, and unsigned GPL modules are fine.
416b75ef8b4SMathieu Desnoyers 	 */
41745ab2813SSteven Rostedt (Red Hat) 	if (trace_module_has_bad_taint(mod))
418b75ef8b4SMathieu Desnoyers 		return 0;
419de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
420b75ef8b4SMathieu Desnoyers 	tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
421b75ef8b4SMathieu Desnoyers 	if (!tp_mod) {
422b75ef8b4SMathieu Desnoyers 		ret = -ENOMEM;
423b75ef8b4SMathieu Desnoyers 		goto end;
424b75ef8b4SMathieu Desnoyers 	}
425eb7d035cSSteven Rostedt (Red Hat) 	tp_mod->mod = mod;
4260dea6d52SMathieu Desnoyers 	list_add_tail(&tp_mod->list, &tracepoint_module_list);
427de7b2973SMathieu Desnoyers 	blocking_notifier_call_chain(&tracepoint_notify_list,
428de7b2973SMathieu Desnoyers 			MODULE_STATE_COMING, tp_mod);
429b75ef8b4SMathieu Desnoyers end:
430de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
431b75ef8b4SMathieu Desnoyers 	return ret;
432b75ef8b4SMathieu Desnoyers }
433b75ef8b4SMathieu Desnoyers 
434de7b2973SMathieu Desnoyers static void tracepoint_module_going(struct module *mod)
435b75ef8b4SMathieu Desnoyers {
436de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
437b75ef8b4SMathieu Desnoyers 
4387dec935aSSteven Rostedt (Red Hat) 	if (!mod->num_tracepoints)
439de7b2973SMathieu Desnoyers 		return;
4407dec935aSSteven Rostedt (Red Hat) 
441de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
442de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
443eb7d035cSSteven Rostedt (Red Hat) 		if (tp_mod->mod == mod) {
444de7b2973SMathieu Desnoyers 			blocking_notifier_call_chain(&tracepoint_notify_list,
445de7b2973SMathieu Desnoyers 					MODULE_STATE_GOING, tp_mod);
446de7b2973SMathieu Desnoyers 			list_del(&tp_mod->list);
447de7b2973SMathieu Desnoyers 			kfree(tp_mod);
448de7b2973SMathieu Desnoyers 			/*
449de7b2973SMathieu Desnoyers 			 * Called the going notifier before checking for
450de7b2973SMathieu Desnoyers 			 * quiescence.
451de7b2973SMathieu Desnoyers 			 */
452de7b2973SMathieu Desnoyers 			tp_module_going_check_quiescent(mod->tracepoints_ptrs,
453b75ef8b4SMathieu Desnoyers 				mod->tracepoints_ptrs + mod->num_tracepoints);
454b75ef8b4SMathieu Desnoyers 			break;
455b75ef8b4SMathieu Desnoyers 		}
456b75ef8b4SMathieu Desnoyers 	}
457b75ef8b4SMathieu Desnoyers 	/*
458b75ef8b4SMathieu Desnoyers 	 * In the case of modules that were tainted at "coming", we'll simply
459b75ef8b4SMathieu Desnoyers 	 * walk through the list without finding it. We cannot use the "tainted"
460b75ef8b4SMathieu Desnoyers 	 * flag on "going", in case a module taints the kernel only after being
461b75ef8b4SMathieu Desnoyers 	 * loaded.
462b75ef8b4SMathieu Desnoyers 	 */
463de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
464b75ef8b4SMathieu Desnoyers }
465227a8375SIngo Molnar 
466de7b2973SMathieu Desnoyers static int tracepoint_module_notify(struct notifier_block *self,
46732f85742SMathieu Desnoyers 		unsigned long val, void *data)
46832f85742SMathieu Desnoyers {
46932f85742SMathieu Desnoyers 	struct module *mod = data;
470b75ef8b4SMathieu Desnoyers 	int ret = 0;
47132f85742SMathieu Desnoyers 
47232f85742SMathieu Desnoyers 	switch (val) {
47332f85742SMathieu Desnoyers 	case MODULE_STATE_COMING:
474b75ef8b4SMathieu Desnoyers 		ret = tracepoint_module_coming(mod);
475b75ef8b4SMathieu Desnoyers 		break;
476b75ef8b4SMathieu Desnoyers 	case MODULE_STATE_LIVE:
477b75ef8b4SMathieu Desnoyers 		break;
47832f85742SMathieu Desnoyers 	case MODULE_STATE_GOING:
479de7b2973SMathieu Desnoyers 		tracepoint_module_going(mod);
480de7b2973SMathieu Desnoyers 		break;
481de7b2973SMathieu Desnoyers 	case MODULE_STATE_UNFORMED:
48232f85742SMathieu Desnoyers 		break;
48332f85742SMathieu Desnoyers 	}
484b75ef8b4SMathieu Desnoyers 	return ret;
48532f85742SMathieu Desnoyers }
48632f85742SMathieu Desnoyers 
487de7b2973SMathieu Desnoyers static struct notifier_block tracepoint_module_nb = {
48832f85742SMathieu Desnoyers 	.notifier_call = tracepoint_module_notify,
48932f85742SMathieu Desnoyers 	.priority = 0,
49032f85742SMathieu Desnoyers };
49132f85742SMathieu Desnoyers 
492de7b2973SMathieu Desnoyers static __init int init_tracepoints(void)
49332f85742SMathieu Desnoyers {
494de7b2973SMathieu Desnoyers 	int ret;
495de7b2973SMathieu Desnoyers 
496de7b2973SMathieu Desnoyers 	ret = register_module_notifier(&tracepoint_module_nb);
497eb7d035cSSteven Rostedt (Red Hat) 	if (ret)
498a395d6a7SJoe Perches 		pr_warn("Failed to register tracepoint module enter notifier\n");
499eb7d035cSSteven Rostedt (Red Hat) 
500de7b2973SMathieu Desnoyers 	return ret;
50132f85742SMathieu Desnoyers }
50232f85742SMathieu Desnoyers __initcall(init_tracepoints);
503227a8375SIngo Molnar #endif /* CONFIG_MODULES */
504a871bd33SJason Baron 
505de7b2973SMathieu Desnoyers static void for_each_tracepoint_range(struct tracepoint * const *begin,
506de7b2973SMathieu Desnoyers 		struct tracepoint * const *end,
507de7b2973SMathieu Desnoyers 		void (*fct)(struct tracepoint *tp, void *priv),
508de7b2973SMathieu Desnoyers 		void *priv)
509de7b2973SMathieu Desnoyers {
510de7b2973SMathieu Desnoyers 	struct tracepoint * const *iter;
511de7b2973SMathieu Desnoyers 
512de7b2973SMathieu Desnoyers 	if (!begin)
513de7b2973SMathieu Desnoyers 		return;
514de7b2973SMathieu Desnoyers 	for (iter = begin; iter < end; iter++)
515de7b2973SMathieu Desnoyers 		fct(*iter, priv);
516de7b2973SMathieu Desnoyers }
517de7b2973SMathieu Desnoyers 
518de7b2973SMathieu Desnoyers /**
519de7b2973SMathieu Desnoyers  * for_each_kernel_tracepoint - iteration on all kernel tracepoints
520de7b2973SMathieu Desnoyers  * @fct: callback
521de7b2973SMathieu Desnoyers  * @priv: private data
522de7b2973SMathieu Desnoyers  */
523de7b2973SMathieu Desnoyers void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
524de7b2973SMathieu Desnoyers 		void *priv)
525de7b2973SMathieu Desnoyers {
526de7b2973SMathieu Desnoyers 	for_each_tracepoint_range(__start___tracepoints_ptrs,
527de7b2973SMathieu Desnoyers 		__stop___tracepoints_ptrs, fct, priv);
528de7b2973SMathieu Desnoyers }
529de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
530de7b2973SMathieu Desnoyers 
5313d27d8cbSJosh Stone #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
53260d970c2SIngo Molnar 
53397419875SJosh Stone /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
534a871bd33SJason Baron static int sys_tracepoint_refcount;
535a871bd33SJason Baron 
5368cf868afSSteven Rostedt (Red Hat) int syscall_regfunc(void)
537a871bd33SJason Baron {
5388063e41dSOleg Nesterov 	struct task_struct *p, *t;
539a871bd33SJason Baron 
540a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
5418063e41dSOleg Nesterov 		read_lock(&tasklist_lock);
5428063e41dSOleg Nesterov 		for_each_process_thread(p, t) {
54366700001SJosh Stone 			set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
5448063e41dSOleg Nesterov 		}
5458063e41dSOleg Nesterov 		read_unlock(&tasklist_lock);
546a871bd33SJason Baron 	}
547a871bd33SJason Baron 	sys_tracepoint_refcount++;
5488cf868afSSteven Rostedt (Red Hat) 
5498cf868afSSteven Rostedt (Red Hat) 	return 0;
550a871bd33SJason Baron }
551a871bd33SJason Baron 
552a871bd33SJason Baron void syscall_unregfunc(void)
553a871bd33SJason Baron {
5548063e41dSOleg Nesterov 	struct task_struct *p, *t;
555a871bd33SJason Baron 
556a871bd33SJason Baron 	sys_tracepoint_refcount--;
557a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
5588063e41dSOleg Nesterov 		read_lock(&tasklist_lock);
5598063e41dSOleg Nesterov 		for_each_process_thread(p, t) {
56066700001SJosh Stone 			clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
5618063e41dSOleg Nesterov 		}
5628063e41dSOleg Nesterov 		read_unlock(&tasklist_lock);
563a871bd33SJason Baron 	}
564a871bd33SJason Baron }
56560d970c2SIngo Molnar #endif
566