xref: /linux/kernel/tracepoint.c (revision 7440172974e85b1828bdd84ac6b23b5bcad9c5eb)
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 
319c0be3f6SMathieu Desnoyers extern tracepoint_ptr_t __start___tracepoints_ptrs[];
329c0be3f6SMathieu Desnoyers extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
3397e1c18eSMathieu Desnoyers 
34e6753f23SJoel Fernandes (Google) DEFINE_SRCU(tracepoint_srcu);
35e6753f23SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(tracepoint_srcu);
36e6753f23SJoel Fernandes (Google) 
3797e1c18eSMathieu Desnoyers /* Set to 1 to enable tracepoint debug output */
3897e1c18eSMathieu Desnoyers static const int tracepoint_debug;
3997e1c18eSMathieu Desnoyers 
40b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES
41de7b2973SMathieu Desnoyers /*
42de7b2973SMathieu Desnoyers  * Tracepoint module list mutex protects the local module list.
43de7b2973SMathieu Desnoyers  */
44de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoint_module_list_mutex);
45de7b2973SMathieu Desnoyers 
46de7b2973SMathieu Desnoyers /* Local list of struct tp_module */
47b75ef8b4SMathieu Desnoyers static LIST_HEAD(tracepoint_module_list);
48b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */
49b75ef8b4SMathieu Desnoyers 
5097e1c18eSMathieu Desnoyers /*
51de7b2973SMathieu Desnoyers  * tracepoints_mutex protects the builtin and module tracepoints.
52de7b2973SMathieu Desnoyers  * tracepoints_mutex nests inside tracepoint_module_list_mutex.
5397e1c18eSMathieu Desnoyers  */
54de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoints_mutex);
5597e1c18eSMathieu Desnoyers 
56f8a79d5cSSteven Rostedt (VMware) static struct rcu_head *early_probes;
57f8a79d5cSSteven Rostedt (VMware) static bool ok_to_free_tracepoints;
58f8a79d5cSSteven Rostedt (VMware) 
5997e1c18eSMathieu Desnoyers /*
6097e1c18eSMathieu Desnoyers  * Note about RCU :
61fd589a8fSAnand Gadiyar  * It is used to delay the free of multiple probes array until a quiescent
6297e1c18eSMathieu Desnoyers  * state is reached.
6397e1c18eSMathieu Desnoyers  */
6419dba33cSLai Jiangshan struct tp_probes {
6519dba33cSLai Jiangshan 	struct rcu_head rcu;
6638516ab5SSteven Rostedt 	struct tracepoint_func probes[0];
6719dba33cSLai Jiangshan };
6897e1c18eSMathieu Desnoyers 
6919dba33cSLai Jiangshan static inline void *allocate_probes(int count)
7097e1c18eSMathieu Desnoyers {
7138516ab5SSteven Rostedt 	struct tp_probes *p  = kmalloc(count * sizeof(struct tracepoint_func)
7219dba33cSLai Jiangshan 			+ sizeof(struct tp_probes), GFP_KERNEL);
7319dba33cSLai Jiangshan 	return p == NULL ? NULL : p->probes;
7497e1c18eSMathieu Desnoyers }
7597e1c18eSMathieu Desnoyers 
76e6753f23SJoel Fernandes (Google) static void srcu_free_old_probes(struct rcu_head *head)
7797e1c18eSMathieu Desnoyers {
780dea6d52SMathieu Desnoyers 	kfree(container_of(head, struct tp_probes, rcu));
7919dba33cSLai Jiangshan }
8019dba33cSLai Jiangshan 
81e6753f23SJoel Fernandes (Google) static void rcu_free_old_probes(struct rcu_head *head)
82e6753f23SJoel Fernandes (Google) {
83e6753f23SJoel Fernandes (Google) 	call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
84e6753f23SJoel Fernandes (Google) }
85e6753f23SJoel Fernandes (Google) 
86f8a79d5cSSteven Rostedt (VMware) static __init int release_early_probes(void)
87f8a79d5cSSteven Rostedt (VMware) {
88f8a79d5cSSteven Rostedt (VMware) 	struct rcu_head *tmp;
89f8a79d5cSSteven Rostedt (VMware) 
90f8a79d5cSSteven Rostedt (VMware) 	ok_to_free_tracepoints = true;
91f8a79d5cSSteven Rostedt (VMware) 
92f8a79d5cSSteven Rostedt (VMware) 	while (early_probes) {
93f8a79d5cSSteven Rostedt (VMware) 		tmp = early_probes;
94f8a79d5cSSteven Rostedt (VMware) 		early_probes = tmp->next;
95*74401729SPaul E. McKenney 		call_rcu(tmp, rcu_free_old_probes);
96f8a79d5cSSteven Rostedt (VMware) 	}
97f8a79d5cSSteven Rostedt (VMware) 
98f8a79d5cSSteven Rostedt (VMware) 	return 0;
99f8a79d5cSSteven Rostedt (VMware) }
100f8a79d5cSSteven Rostedt (VMware) 
101f8a79d5cSSteven Rostedt (VMware) /* SRCU is initialized at core_initcall */
102f8a79d5cSSteven Rostedt (VMware) postcore_initcall(release_early_probes);
103f8a79d5cSSteven Rostedt (VMware) 
10438516ab5SSteven Rostedt static inline void release_probes(struct tracepoint_func *old)
10519dba33cSLai Jiangshan {
10619dba33cSLai Jiangshan 	if (old) {
10719dba33cSLai Jiangshan 		struct tp_probes *tp_probes = container_of(old,
10819dba33cSLai Jiangshan 			struct tp_probes, probes[0]);
109f8a79d5cSSteven Rostedt (VMware) 
110f8a79d5cSSteven Rostedt (VMware) 		/*
111f8a79d5cSSteven Rostedt (VMware) 		 * We can't free probes if SRCU is not initialized yet.
112f8a79d5cSSteven Rostedt (VMware) 		 * Postpone the freeing till after SRCU is initialized.
113f8a79d5cSSteven Rostedt (VMware) 		 */
114f8a79d5cSSteven Rostedt (VMware) 		if (unlikely(!ok_to_free_tracepoints)) {
115f8a79d5cSSteven Rostedt (VMware) 			tp_probes->rcu.next = early_probes;
116f8a79d5cSSteven Rostedt (VMware) 			early_probes = &tp_probes->rcu;
117f8a79d5cSSteven Rostedt (VMware) 			return;
118f8a79d5cSSteven Rostedt (VMware) 		}
119f8a79d5cSSteven Rostedt (VMware) 
120e6753f23SJoel Fernandes (Google) 		/*
121e6753f23SJoel Fernandes (Google) 		 * Tracepoint probes are protected by both sched RCU and SRCU,
122e6753f23SJoel Fernandes (Google) 		 * by calling the SRCU callback in the sched RCU callback we
123e6753f23SJoel Fernandes (Google) 		 * cover both cases. So let us chain the SRCU and sched RCU
124e6753f23SJoel Fernandes (Google) 		 * callbacks to wait for both grace periods.
125e6753f23SJoel Fernandes (Google) 		 */
126*74401729SPaul E. McKenney 		call_rcu(&tp_probes->rcu, rcu_free_old_probes);
12719dba33cSLai Jiangshan 	}
12897e1c18eSMathieu Desnoyers }
12997e1c18eSMathieu Desnoyers 
130de7b2973SMathieu Desnoyers static void debug_print_probes(struct tracepoint_func *funcs)
13197e1c18eSMathieu Desnoyers {
13297e1c18eSMathieu Desnoyers 	int i;
13397e1c18eSMathieu Desnoyers 
134de7b2973SMathieu Desnoyers 	if (!tracepoint_debug || !funcs)
13597e1c18eSMathieu Desnoyers 		return;
13697e1c18eSMathieu Desnoyers 
137de7b2973SMathieu Desnoyers 	for (i = 0; funcs[i].func; i++)
138de7b2973SMathieu Desnoyers 		printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
13997e1c18eSMathieu Desnoyers }
14097e1c18eSMathieu Desnoyers 
1417904b5c4SSteven Rostedt (Red Hat) static struct tracepoint_func *
1427904b5c4SSteven Rostedt (Red Hat) func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
1437904b5c4SSteven Rostedt (Red Hat) 	 int prio)
14497e1c18eSMathieu Desnoyers {
14538516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
1467904b5c4SSteven Rostedt (Red Hat) 	int nr_probes = 0;
1477904b5c4SSteven Rostedt (Red Hat) 	int pos = -1;
14897e1c18eSMathieu Desnoyers 
149de7b2973SMathieu Desnoyers 	if (WARN_ON(!tp_func->func))
1504c69e6eaSSahara 		return ERR_PTR(-EINVAL);
15197e1c18eSMathieu Desnoyers 
152de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
153de7b2973SMathieu Desnoyers 	old = *funcs;
15497e1c18eSMathieu Desnoyers 	if (old) {
15597e1c18eSMathieu Desnoyers 		/* (N -> N+1), (N != 0, 1) probes */
1567904b5c4SSteven Rostedt (Red Hat) 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
1577904b5c4SSteven Rostedt (Red Hat) 			/* Insert before probes of lower priority */
1587904b5c4SSteven Rostedt (Red Hat) 			if (pos < 0 && old[nr_probes].prio < prio)
1597904b5c4SSteven Rostedt (Red Hat) 				pos = nr_probes;
160de7b2973SMathieu Desnoyers 			if (old[nr_probes].func == tp_func->func &&
161de7b2973SMathieu Desnoyers 			    old[nr_probes].data == tp_func->data)
16297e1c18eSMathieu Desnoyers 				return ERR_PTR(-EEXIST);
16397e1c18eSMathieu Desnoyers 		}
1647904b5c4SSteven Rostedt (Red Hat) 	}
16597e1c18eSMathieu Desnoyers 	/* + 2 : one for new probe, one for NULL func */
16619dba33cSLai Jiangshan 	new = allocate_probes(nr_probes + 2);
16797e1c18eSMathieu Desnoyers 	if (new == NULL)
16897e1c18eSMathieu Desnoyers 		return ERR_PTR(-ENOMEM);
1697904b5c4SSteven Rostedt (Red Hat) 	if (old) {
1707904b5c4SSteven Rostedt (Red Hat) 		if (pos < 0) {
1717904b5c4SSteven Rostedt (Red Hat) 			pos = nr_probes;
17238516ab5SSteven Rostedt 			memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
1737904b5c4SSteven Rostedt (Red Hat) 		} else {
1747904b5c4SSteven Rostedt (Red Hat) 			/* Copy higher priority probes ahead of the new probe */
1757904b5c4SSteven Rostedt (Red Hat) 			memcpy(new, old, pos * sizeof(struct tracepoint_func));
1767904b5c4SSteven Rostedt (Red Hat) 			/* Copy the rest after it. */
1777904b5c4SSteven Rostedt (Red Hat) 			memcpy(new + pos + 1, old + pos,
1787904b5c4SSteven Rostedt (Red Hat) 			       (nr_probes - pos) * sizeof(struct tracepoint_func));
1797904b5c4SSteven Rostedt (Red Hat) 		}
1807904b5c4SSteven Rostedt (Red Hat) 	} else
1817904b5c4SSteven Rostedt (Red Hat) 		pos = 0;
1827904b5c4SSteven Rostedt (Red Hat) 	new[pos] = *tp_func;
18338516ab5SSteven Rostedt 	new[nr_probes + 1].func = NULL;
184de7b2973SMathieu Desnoyers 	*funcs = new;
185de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
18697e1c18eSMathieu Desnoyers 	return old;
18797e1c18eSMathieu Desnoyers }
18897e1c18eSMathieu Desnoyers 
189de7b2973SMathieu Desnoyers static void *func_remove(struct tracepoint_func **funcs,
190de7b2973SMathieu Desnoyers 		struct tracepoint_func *tp_func)
19197e1c18eSMathieu Desnoyers {
19297e1c18eSMathieu Desnoyers 	int nr_probes = 0, nr_del = 0, i;
19338516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
19497e1c18eSMathieu Desnoyers 
195de7b2973SMathieu Desnoyers 	old = *funcs;
19697e1c18eSMathieu Desnoyers 
197f66af459SFrederic Weisbecker 	if (!old)
19819dba33cSLai Jiangshan 		return ERR_PTR(-ENOENT);
199f66af459SFrederic Weisbecker 
200de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
20197e1c18eSMathieu Desnoyers 	/* (N -> M), (N > 1, M >= 0) probes */
202de7b2973SMathieu Desnoyers 	if (tp_func->func) {
20338516ab5SSteven Rostedt 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
204de7b2973SMathieu Desnoyers 			if (old[nr_probes].func == tp_func->func &&
205de7b2973SMathieu Desnoyers 			     old[nr_probes].data == tp_func->data)
20697e1c18eSMathieu Desnoyers 				nr_del++;
20797e1c18eSMathieu Desnoyers 		}
2084c69e6eaSSahara 	}
20997e1c18eSMathieu Desnoyers 
2104c69e6eaSSahara 	/*
2114c69e6eaSSahara 	 * If probe is NULL, then nr_probes = nr_del = 0, and then the
2124c69e6eaSSahara 	 * entire entry will be removed.
2134c69e6eaSSahara 	 */
21497e1c18eSMathieu Desnoyers 	if (nr_probes - nr_del == 0) {
21597e1c18eSMathieu Desnoyers 		/* N -> 0, (N > 1) */
216de7b2973SMathieu Desnoyers 		*funcs = NULL;
217de7b2973SMathieu Desnoyers 		debug_print_probes(*funcs);
21897e1c18eSMathieu Desnoyers 		return old;
21997e1c18eSMathieu Desnoyers 	} else {
22097e1c18eSMathieu Desnoyers 		int j = 0;
22197e1c18eSMathieu Desnoyers 		/* N -> M, (N > 1, M > 0) */
22297e1c18eSMathieu Desnoyers 		/* + 1 for NULL */
22319dba33cSLai Jiangshan 		new = allocate_probes(nr_probes - nr_del + 1);
22497e1c18eSMathieu Desnoyers 		if (new == NULL)
22597e1c18eSMathieu Desnoyers 			return ERR_PTR(-ENOMEM);
22638516ab5SSteven Rostedt 		for (i = 0; old[i].func; i++)
227de7b2973SMathieu Desnoyers 			if (old[i].func != tp_func->func
228de7b2973SMathieu Desnoyers 					|| old[i].data != tp_func->data)
22997e1c18eSMathieu Desnoyers 				new[j++] = old[i];
23038516ab5SSteven Rostedt 		new[nr_probes - nr_del].func = NULL;
231de7b2973SMathieu Desnoyers 		*funcs = new;
23297e1c18eSMathieu Desnoyers 	}
233de7b2973SMathieu Desnoyers 	debug_print_probes(*funcs);
23497e1c18eSMathieu Desnoyers 	return old;
23597e1c18eSMathieu Desnoyers }
23697e1c18eSMathieu Desnoyers 
23797e1c18eSMathieu Desnoyers /*
238de7b2973SMathieu Desnoyers  * Add the probe function to a tracepoint.
23997e1c18eSMathieu Desnoyers  */
240de7b2973SMathieu Desnoyers static int tracepoint_add_func(struct tracepoint *tp,
2417904b5c4SSteven Rostedt (Red Hat) 			       struct tracepoint_func *func, int prio)
24297e1c18eSMathieu Desnoyers {
243de7b2973SMathieu Desnoyers 	struct tracepoint_func *old, *tp_funcs;
2448cf868afSSteven Rostedt (Red Hat) 	int ret;
24597e1c18eSMathieu Desnoyers 
2468cf868afSSteven Rostedt (Red Hat) 	if (tp->regfunc && !static_key_enabled(&tp->key)) {
2478cf868afSSteven Rostedt (Red Hat) 		ret = tp->regfunc();
2488cf868afSSteven Rostedt (Red Hat) 		if (ret < 0)
2498cf868afSSteven Rostedt (Red Hat) 			return ret;
2508cf868afSSteven Rostedt (Red Hat) 	}
25197e1c18eSMathieu Desnoyers 
252b725dfeaSMathieu Desnoyers 	tp_funcs = rcu_dereference_protected(tp->funcs,
253b725dfeaSMathieu Desnoyers 			lockdep_is_held(&tracepoints_mutex));
2547904b5c4SSteven Rostedt (Red Hat) 	old = func_add(&tp_funcs, func, prio);
255de7b2973SMathieu Desnoyers 	if (IS_ERR(old)) {
256d66a270bSMathieu Desnoyers 		WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
257de7b2973SMathieu Desnoyers 		return PTR_ERR(old);
25897e1c18eSMathieu Desnoyers 	}
25997419875SJosh Stone 
26097e1c18eSMathieu Desnoyers 	/*
261243d1a79SPaul E. McKenney 	 * rcu_assign_pointer has as smp_store_release() which makes sure
262243d1a79SPaul E. McKenney 	 * that the new probe callbacks array is consistent before setting
263243d1a79SPaul E. McKenney 	 * a pointer to it.  This array is referenced by __DO_TRACE from
264243d1a79SPaul E. McKenney 	 * include/linux/tracepoint.h using rcu_dereference_sched().
26597e1c18eSMathieu Desnoyers 	 */
266de7b2973SMathieu Desnoyers 	rcu_assign_pointer(tp->funcs, tp_funcs);
267de7b2973SMathieu Desnoyers 	if (!static_key_enabled(&tp->key))
268de7b2973SMathieu Desnoyers 		static_key_slow_inc(&tp->key);
2698058bd0fSMathieu Desnoyers 	release_probes(old);
270de7b2973SMathieu Desnoyers 	return 0;
27197e1c18eSMathieu Desnoyers }
27297e1c18eSMathieu Desnoyers 
27397e1c18eSMathieu Desnoyers /*
274de7b2973SMathieu Desnoyers  * Remove a probe function from a tracepoint.
27597e1c18eSMathieu Desnoyers  * Note: only waiting an RCU period after setting elem->call to the empty
27697e1c18eSMathieu Desnoyers  * function insures that the original callback is not used anymore. This insured
27797e1c18eSMathieu Desnoyers  * by preempt_disable around the call site.
27897e1c18eSMathieu Desnoyers  */
279de7b2973SMathieu Desnoyers static int tracepoint_remove_func(struct tracepoint *tp,
280de7b2973SMathieu Desnoyers 		struct tracepoint_func *func)
28197e1c18eSMathieu Desnoyers {
282de7b2973SMathieu Desnoyers 	struct tracepoint_func *old, *tp_funcs;
28397419875SJosh Stone 
284b725dfeaSMathieu Desnoyers 	tp_funcs = rcu_dereference_protected(tp->funcs,
285b725dfeaSMathieu Desnoyers 			lockdep_is_held(&tracepoints_mutex));
286de7b2973SMathieu Desnoyers 	old = func_remove(&tp_funcs, func);
287de7b2973SMathieu Desnoyers 	if (IS_ERR(old)) {
288d66a270bSMathieu Desnoyers 		WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
289de7b2973SMathieu Desnoyers 		return PTR_ERR(old);
29097e1c18eSMathieu Desnoyers 	}
29197e1c18eSMathieu Desnoyers 
292de7b2973SMathieu Desnoyers 	if (!tp_funcs) {
293de7b2973SMathieu Desnoyers 		/* Removed last function */
294de7b2973SMathieu Desnoyers 		if (tp->unregfunc && static_key_enabled(&tp->key))
295de7b2973SMathieu Desnoyers 			tp->unregfunc();
29697e1c18eSMathieu Desnoyers 
297de7b2973SMathieu Desnoyers 		if (static_key_enabled(&tp->key))
298de7b2973SMathieu Desnoyers 			static_key_slow_dec(&tp->key);
29997e1c18eSMathieu Desnoyers 	}
300de7b2973SMathieu Desnoyers 	rcu_assign_pointer(tp->funcs, tp_funcs);
3018058bd0fSMathieu Desnoyers 	release_probes(old);
302de7b2973SMathieu Desnoyers 	return 0;
303127cafbbSLai Jiangshan }
304127cafbbSLai Jiangshan 
30597e1c18eSMathieu Desnoyers /**
306f39e2391SLee, Chun-Yi  * tracepoint_probe_register_prio -  Connect a probe to a tracepoint with priority
307de7b2973SMathieu Desnoyers  * @tp: tracepoint
30897e1c18eSMathieu Desnoyers  * @probe: probe handler
309cac92ba7SFabian Frederick  * @data: tracepoint data
3107904b5c4SSteven Rostedt (Red Hat)  * @prio: priority of this function over other registered functions
3117904b5c4SSteven Rostedt (Red Hat)  *
3127904b5c4SSteven Rostedt (Red Hat)  * Returns 0 if ok, error value on error.
3137904b5c4SSteven Rostedt (Red Hat)  * Note: if @tp is within a module, the caller is responsible for
3147904b5c4SSteven Rostedt (Red Hat)  * unregistering the probe before the module is gone. This can be
3157904b5c4SSteven Rostedt (Red Hat)  * performed either with a tracepoint module going notifier, or from
3167904b5c4SSteven Rostedt (Red Hat)  * within module exit functions.
3177904b5c4SSteven Rostedt (Red Hat)  */
3187904b5c4SSteven Rostedt (Red Hat) int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
3197904b5c4SSteven Rostedt (Red Hat) 				   void *data, int prio)
3207904b5c4SSteven Rostedt (Red Hat) {
3217904b5c4SSteven Rostedt (Red Hat) 	struct tracepoint_func tp_func;
3227904b5c4SSteven Rostedt (Red Hat) 	int ret;
3237904b5c4SSteven Rostedt (Red Hat) 
3247904b5c4SSteven Rostedt (Red Hat) 	mutex_lock(&tracepoints_mutex);
3257904b5c4SSteven Rostedt (Red Hat) 	tp_func.func = probe;
3267904b5c4SSteven Rostedt (Red Hat) 	tp_func.data = data;
3277904b5c4SSteven Rostedt (Red Hat) 	tp_func.prio = prio;
3287904b5c4SSteven Rostedt (Red Hat) 	ret = tracepoint_add_func(tp, &tp_func, prio);
3297904b5c4SSteven Rostedt (Red Hat) 	mutex_unlock(&tracepoints_mutex);
3307904b5c4SSteven Rostedt (Red Hat) 	return ret;
3317904b5c4SSteven Rostedt (Red Hat) }
3327904b5c4SSteven Rostedt (Red Hat) EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
3337904b5c4SSteven Rostedt (Red Hat) 
3347904b5c4SSteven Rostedt (Red Hat) /**
3357904b5c4SSteven Rostedt (Red Hat)  * tracepoint_probe_register -  Connect a probe to a tracepoint
3367904b5c4SSteven Rostedt (Red Hat)  * @tp: tracepoint
3377904b5c4SSteven Rostedt (Red Hat)  * @probe: probe handler
3387904b5c4SSteven Rostedt (Red Hat)  * @data: tracepoint data
33997e1c18eSMathieu Desnoyers  *
340de7b2973SMathieu Desnoyers  * Returns 0 if ok, error value on error.
341de7b2973SMathieu Desnoyers  * Note: if @tp is within a module, the caller is responsible for
342de7b2973SMathieu Desnoyers  * unregistering the probe before the module is gone. This can be
343de7b2973SMathieu Desnoyers  * performed either with a tracepoint module going notifier, or from
344de7b2973SMathieu Desnoyers  * within module exit functions.
34597e1c18eSMathieu Desnoyers  */
346de7b2973SMathieu Desnoyers int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
34797e1c18eSMathieu Desnoyers {
3487904b5c4SSteven Rostedt (Red Hat) 	return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
34997e1c18eSMathieu Desnoyers }
35097e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_register);
35197e1c18eSMathieu Desnoyers 
35297e1c18eSMathieu Desnoyers /**
35397e1c18eSMathieu Desnoyers  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
354de7b2973SMathieu Desnoyers  * @tp: tracepoint
35597e1c18eSMathieu Desnoyers  * @probe: probe function pointer
356cac92ba7SFabian Frederick  * @data: tracepoint data
35797e1c18eSMathieu Desnoyers  *
358de7b2973SMathieu Desnoyers  * Returns 0 if ok, error value on error.
35997e1c18eSMathieu Desnoyers  */
360de7b2973SMathieu Desnoyers int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
36197e1c18eSMathieu Desnoyers {
362de7b2973SMathieu Desnoyers 	struct tracepoint_func tp_func;
363de7b2973SMathieu Desnoyers 	int ret;
36497e1c18eSMathieu Desnoyers 
36597e1c18eSMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
366de7b2973SMathieu Desnoyers 	tp_func.func = probe;
367de7b2973SMathieu Desnoyers 	tp_func.data = data;
368de7b2973SMathieu Desnoyers 	ret = tracepoint_remove_func(tp, &tp_func);
36997e1c18eSMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
370de7b2973SMathieu Desnoyers 	return ret;
37197e1c18eSMathieu Desnoyers }
37297e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
37397e1c18eSMathieu Desnoyers 
3749c0be3f6SMathieu Desnoyers static void for_each_tracepoint_range(
3759c0be3f6SMathieu Desnoyers 		tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
37646e0c9beSArd Biesheuvel 		void (*fct)(struct tracepoint *tp, void *priv),
37746e0c9beSArd Biesheuvel 		void *priv)
37846e0c9beSArd Biesheuvel {
3799c0be3f6SMathieu Desnoyers 	tracepoint_ptr_t *iter;
3809c0be3f6SMathieu Desnoyers 
38146e0c9beSArd Biesheuvel 	if (!begin)
38246e0c9beSArd Biesheuvel 		return;
38346e0c9beSArd Biesheuvel 	for (iter = begin; iter < end; iter++)
3849c0be3f6SMathieu Desnoyers 		fct(tracepoint_ptr_deref(iter), priv);
38546e0c9beSArd Biesheuvel }
38646e0c9beSArd Biesheuvel 
387227a8375SIngo Molnar #ifdef CONFIG_MODULES
38845ab2813SSteven Rostedt (Red Hat) bool trace_module_has_bad_taint(struct module *mod)
38945ab2813SSteven Rostedt (Red Hat) {
39066cc69e3SMathieu Desnoyers 	return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
39166cc69e3SMathieu Desnoyers 			       (1 << TAINT_UNSIGNED_MODULE));
39245ab2813SSteven Rostedt (Red Hat) }
39345ab2813SSteven Rostedt (Red Hat) 
394de7b2973SMathieu Desnoyers static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
395de7b2973SMathieu Desnoyers 
396de7b2973SMathieu Desnoyers /**
397de7b2973SMathieu Desnoyers  * register_tracepoint_notifier - register tracepoint coming/going notifier
398de7b2973SMathieu Desnoyers  * @nb: notifier block
399de7b2973SMathieu Desnoyers  *
400de7b2973SMathieu Desnoyers  * Notifiers registered with this function are called on module
401de7b2973SMathieu Desnoyers  * coming/going with the tracepoint_module_list_mutex held.
402de7b2973SMathieu Desnoyers  * The notifier block callback should expect a "struct tp_module" data
403de7b2973SMathieu Desnoyers  * pointer.
404de7b2973SMathieu Desnoyers  */
405de7b2973SMathieu Desnoyers int register_tracepoint_module_notifier(struct notifier_block *nb)
406de7b2973SMathieu Desnoyers {
407de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
408de7b2973SMathieu Desnoyers 	int ret;
409de7b2973SMathieu Desnoyers 
410de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
411de7b2973SMathieu Desnoyers 	ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
412de7b2973SMathieu Desnoyers 	if (ret)
413de7b2973SMathieu Desnoyers 		goto end;
414de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
415de7b2973SMathieu Desnoyers 		(void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
416de7b2973SMathieu Desnoyers end:
417de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
418de7b2973SMathieu Desnoyers 	return ret;
419de7b2973SMathieu Desnoyers }
420de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
421de7b2973SMathieu Desnoyers 
422de7b2973SMathieu Desnoyers /**
423de7b2973SMathieu Desnoyers  * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
424de7b2973SMathieu Desnoyers  * @nb: notifier block
425de7b2973SMathieu Desnoyers  *
426de7b2973SMathieu Desnoyers  * The notifier block callback should expect a "struct tp_module" data
427de7b2973SMathieu Desnoyers  * pointer.
428de7b2973SMathieu Desnoyers  */
429de7b2973SMathieu Desnoyers int unregister_tracepoint_module_notifier(struct notifier_block *nb)
430de7b2973SMathieu Desnoyers {
431de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
432de7b2973SMathieu Desnoyers 	int ret;
433de7b2973SMathieu Desnoyers 
434de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
435de7b2973SMathieu Desnoyers 	ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
436de7b2973SMathieu Desnoyers 	if (ret)
437de7b2973SMathieu Desnoyers 		goto end;
438de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
439de7b2973SMathieu Desnoyers 		(void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
440de7b2973SMathieu Desnoyers end:
441de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
442de7b2973SMathieu Desnoyers 	return ret;
443de7b2973SMathieu Desnoyers 
444de7b2973SMathieu Desnoyers }
445de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
446de7b2973SMathieu Desnoyers 
447de7b2973SMathieu Desnoyers /*
448de7b2973SMathieu Desnoyers  * Ensure the tracer unregistered the module's probes before the module
449de7b2973SMathieu Desnoyers  * teardown is performed. Prevents leaks of probe and data pointers.
450de7b2973SMathieu Desnoyers  */
45146e0c9beSArd Biesheuvel static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
452de7b2973SMathieu Desnoyers {
45346e0c9beSArd Biesheuvel 	WARN_ON_ONCE(tp->funcs);
454de7b2973SMathieu Desnoyers }
455de7b2973SMathieu Desnoyers 
456b75ef8b4SMathieu Desnoyers static int tracepoint_module_coming(struct module *mod)
457b75ef8b4SMathieu Desnoyers {
4580dea6d52SMathieu Desnoyers 	struct tp_module *tp_mod;
459b75ef8b4SMathieu Desnoyers 	int ret = 0;
460b75ef8b4SMathieu Desnoyers 
4617dec935aSSteven Rostedt (Red Hat) 	if (!mod->num_tracepoints)
4627dec935aSSteven Rostedt (Red Hat) 		return 0;
4637dec935aSSteven Rostedt (Red Hat) 
464b75ef8b4SMathieu Desnoyers 	/*
465c10076c4SSteven Rostedt 	 * We skip modules that taint the kernel, especially those with different
466c10076c4SSteven Rostedt 	 * module headers (for forced load), to make sure we don't cause a crash.
46766cc69e3SMathieu Desnoyers 	 * Staging, out-of-tree, and unsigned GPL modules are fine.
468b75ef8b4SMathieu Desnoyers 	 */
46945ab2813SSteven Rostedt (Red Hat) 	if (trace_module_has_bad_taint(mod))
470b75ef8b4SMathieu Desnoyers 		return 0;
471de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
472b75ef8b4SMathieu Desnoyers 	tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
473b75ef8b4SMathieu Desnoyers 	if (!tp_mod) {
474b75ef8b4SMathieu Desnoyers 		ret = -ENOMEM;
475b75ef8b4SMathieu Desnoyers 		goto end;
476b75ef8b4SMathieu Desnoyers 	}
477eb7d035cSSteven Rostedt (Red Hat) 	tp_mod->mod = mod;
4780dea6d52SMathieu Desnoyers 	list_add_tail(&tp_mod->list, &tracepoint_module_list);
479de7b2973SMathieu Desnoyers 	blocking_notifier_call_chain(&tracepoint_notify_list,
480de7b2973SMathieu Desnoyers 			MODULE_STATE_COMING, tp_mod);
481b75ef8b4SMathieu Desnoyers end:
482de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
483b75ef8b4SMathieu Desnoyers 	return ret;
484b75ef8b4SMathieu Desnoyers }
485b75ef8b4SMathieu Desnoyers 
486de7b2973SMathieu Desnoyers static void tracepoint_module_going(struct module *mod)
487b75ef8b4SMathieu Desnoyers {
488de7b2973SMathieu Desnoyers 	struct tp_module *tp_mod;
489b75ef8b4SMathieu Desnoyers 
4907dec935aSSteven Rostedt (Red Hat) 	if (!mod->num_tracepoints)
491de7b2973SMathieu Desnoyers 		return;
4927dec935aSSteven Rostedt (Red Hat) 
493de7b2973SMathieu Desnoyers 	mutex_lock(&tracepoint_module_list_mutex);
494de7b2973SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
495eb7d035cSSteven Rostedt (Red Hat) 		if (tp_mod->mod == mod) {
496de7b2973SMathieu Desnoyers 			blocking_notifier_call_chain(&tracepoint_notify_list,
497de7b2973SMathieu Desnoyers 					MODULE_STATE_GOING, tp_mod);
498de7b2973SMathieu Desnoyers 			list_del(&tp_mod->list);
499de7b2973SMathieu Desnoyers 			kfree(tp_mod);
500de7b2973SMathieu Desnoyers 			/*
501de7b2973SMathieu Desnoyers 			 * Called the going notifier before checking for
502de7b2973SMathieu Desnoyers 			 * quiescence.
503de7b2973SMathieu Desnoyers 			 */
50446e0c9beSArd Biesheuvel 			for_each_tracepoint_range(mod->tracepoints_ptrs,
50546e0c9beSArd Biesheuvel 				mod->tracepoints_ptrs + mod->num_tracepoints,
50646e0c9beSArd Biesheuvel 				tp_module_going_check_quiescent, NULL);
507b75ef8b4SMathieu Desnoyers 			break;
508b75ef8b4SMathieu Desnoyers 		}
509b75ef8b4SMathieu Desnoyers 	}
510b75ef8b4SMathieu Desnoyers 	/*
511b75ef8b4SMathieu Desnoyers 	 * In the case of modules that were tainted at "coming", we'll simply
512b75ef8b4SMathieu Desnoyers 	 * walk through the list without finding it. We cannot use the "tainted"
513b75ef8b4SMathieu Desnoyers 	 * flag on "going", in case a module taints the kernel only after being
514b75ef8b4SMathieu Desnoyers 	 * loaded.
515b75ef8b4SMathieu Desnoyers 	 */
516de7b2973SMathieu Desnoyers 	mutex_unlock(&tracepoint_module_list_mutex);
517b75ef8b4SMathieu Desnoyers }
518227a8375SIngo Molnar 
519de7b2973SMathieu Desnoyers static int tracepoint_module_notify(struct notifier_block *self,
52032f85742SMathieu Desnoyers 		unsigned long val, void *data)
52132f85742SMathieu Desnoyers {
52232f85742SMathieu Desnoyers 	struct module *mod = data;
523b75ef8b4SMathieu Desnoyers 	int ret = 0;
52432f85742SMathieu Desnoyers 
52532f85742SMathieu Desnoyers 	switch (val) {
52632f85742SMathieu Desnoyers 	case MODULE_STATE_COMING:
527b75ef8b4SMathieu Desnoyers 		ret = tracepoint_module_coming(mod);
528b75ef8b4SMathieu Desnoyers 		break;
529b75ef8b4SMathieu Desnoyers 	case MODULE_STATE_LIVE:
530b75ef8b4SMathieu Desnoyers 		break;
53132f85742SMathieu Desnoyers 	case MODULE_STATE_GOING:
532de7b2973SMathieu Desnoyers 		tracepoint_module_going(mod);
533de7b2973SMathieu Desnoyers 		break;
534de7b2973SMathieu Desnoyers 	case MODULE_STATE_UNFORMED:
53532f85742SMathieu Desnoyers 		break;
53632f85742SMathieu Desnoyers 	}
537b75ef8b4SMathieu Desnoyers 	return ret;
53832f85742SMathieu Desnoyers }
53932f85742SMathieu Desnoyers 
540de7b2973SMathieu Desnoyers static struct notifier_block tracepoint_module_nb = {
54132f85742SMathieu Desnoyers 	.notifier_call = tracepoint_module_notify,
54232f85742SMathieu Desnoyers 	.priority = 0,
54332f85742SMathieu Desnoyers };
54432f85742SMathieu Desnoyers 
545de7b2973SMathieu Desnoyers static __init int init_tracepoints(void)
54632f85742SMathieu Desnoyers {
547de7b2973SMathieu Desnoyers 	int ret;
548de7b2973SMathieu Desnoyers 
549de7b2973SMathieu Desnoyers 	ret = register_module_notifier(&tracepoint_module_nb);
550eb7d035cSSteven Rostedt (Red Hat) 	if (ret)
551a395d6a7SJoe Perches 		pr_warn("Failed to register tracepoint module enter notifier\n");
552eb7d035cSSteven Rostedt (Red Hat) 
553de7b2973SMathieu Desnoyers 	return ret;
55432f85742SMathieu Desnoyers }
55532f85742SMathieu Desnoyers __initcall(init_tracepoints);
556227a8375SIngo Molnar #endif /* CONFIG_MODULES */
557a871bd33SJason Baron 
558de7b2973SMathieu Desnoyers /**
559de7b2973SMathieu Desnoyers  * for_each_kernel_tracepoint - iteration on all kernel tracepoints
560de7b2973SMathieu Desnoyers  * @fct: callback
561de7b2973SMathieu Desnoyers  * @priv: private data
562de7b2973SMathieu Desnoyers  */
563de7b2973SMathieu Desnoyers void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
564de7b2973SMathieu Desnoyers 		void *priv)
565de7b2973SMathieu Desnoyers {
566de7b2973SMathieu Desnoyers 	for_each_tracepoint_range(__start___tracepoints_ptrs,
567de7b2973SMathieu Desnoyers 		__stop___tracepoints_ptrs, fct, priv);
568de7b2973SMathieu Desnoyers }
569de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
570de7b2973SMathieu Desnoyers 
5713d27d8cbSJosh Stone #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
57260d970c2SIngo Molnar 
57397419875SJosh Stone /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
574a871bd33SJason Baron static int sys_tracepoint_refcount;
575a871bd33SJason Baron 
5768cf868afSSteven Rostedt (Red Hat) int syscall_regfunc(void)
577a871bd33SJason Baron {
5788063e41dSOleg Nesterov 	struct task_struct *p, *t;
579a871bd33SJason Baron 
580a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
5818063e41dSOleg Nesterov 		read_lock(&tasklist_lock);
5828063e41dSOleg Nesterov 		for_each_process_thread(p, t) {
58366700001SJosh Stone 			set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
5848063e41dSOleg Nesterov 		}
5858063e41dSOleg Nesterov 		read_unlock(&tasklist_lock);
586a871bd33SJason Baron 	}
587a871bd33SJason Baron 	sys_tracepoint_refcount++;
5888cf868afSSteven Rostedt (Red Hat) 
5898cf868afSSteven Rostedt (Red Hat) 	return 0;
590a871bd33SJason Baron }
591a871bd33SJason Baron 
592a871bd33SJason Baron void syscall_unregfunc(void)
593a871bd33SJason Baron {
5948063e41dSOleg Nesterov 	struct task_struct *p, *t;
595a871bd33SJason Baron 
596a871bd33SJason Baron 	sys_tracepoint_refcount--;
597a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
5988063e41dSOleg Nesterov 		read_lock(&tasklist_lock);
5998063e41dSOleg Nesterov 		for_each_process_thread(p, t) {
60066700001SJosh Stone 			clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
6018063e41dSOleg Nesterov 		}
6028063e41dSOleg Nesterov 		read_unlock(&tasklist_lock);
603a871bd33SJason Baron 	}
604a871bd33SJason Baron }
60560d970c2SIngo Molnar #endif
606