xref: /linux/kernel/tracepoint.c (revision 45ab2813d40d88fc575e753c38478de242d03f88)
197e1c18eSMathieu Desnoyers /*
297e1c18eSMathieu Desnoyers  * Copyright (C) 2008 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 
3697e1c18eSMathieu Desnoyers /*
37b75ef8b4SMathieu Desnoyers  * Tracepoints mutex protects the builtin and module tracepoints and the hash
38b75ef8b4SMathieu Desnoyers  * table, as well as the local module list.
3997e1c18eSMathieu Desnoyers  */
4097e1c18eSMathieu Desnoyers static DEFINE_MUTEX(tracepoints_mutex);
4197e1c18eSMathieu Desnoyers 
42b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES
43b75ef8b4SMathieu Desnoyers /* Local list of struct module */
44b75ef8b4SMathieu Desnoyers static LIST_HEAD(tracepoint_module_list);
45b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */
46b75ef8b4SMathieu Desnoyers 
4797e1c18eSMathieu Desnoyers /*
4897e1c18eSMathieu Desnoyers  * Tracepoint hash table, containing the active tracepoints.
4997e1c18eSMathieu Desnoyers  * Protected by tracepoints_mutex.
5097e1c18eSMathieu Desnoyers  */
5197e1c18eSMathieu Desnoyers #define TRACEPOINT_HASH_BITS 6
5297e1c18eSMathieu Desnoyers #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
5319dba33cSLai Jiangshan static struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
5497e1c18eSMathieu Desnoyers 
5597e1c18eSMathieu Desnoyers /*
5697e1c18eSMathieu Desnoyers  * Note about RCU :
57fd589a8fSAnand Gadiyar  * It is used to delay the free of multiple probes array until a quiescent
5897e1c18eSMathieu Desnoyers  * state is reached.
5997e1c18eSMathieu Desnoyers  * Tracepoint entries modifications are protected by the tracepoints_mutex.
6097e1c18eSMathieu Desnoyers  */
6197e1c18eSMathieu Desnoyers struct tracepoint_entry {
6297e1c18eSMathieu Desnoyers 	struct hlist_node hlist;
6338516ab5SSteven Rostedt 	struct tracepoint_func *funcs;
6497e1c18eSMathieu Desnoyers 	int refcount;	/* Number of times armed. 0 if disarmed. */
6597e1c18eSMathieu Desnoyers 	char name[0];
6697e1c18eSMathieu Desnoyers };
6797e1c18eSMathieu Desnoyers 
6819dba33cSLai Jiangshan struct tp_probes {
69127cafbbSLai Jiangshan 	union {
7019dba33cSLai Jiangshan 		struct rcu_head rcu;
71127cafbbSLai Jiangshan 		struct list_head list;
72127cafbbSLai Jiangshan 	} u;
7338516ab5SSteven Rostedt 	struct tracepoint_func probes[0];
7419dba33cSLai Jiangshan };
7597e1c18eSMathieu Desnoyers 
7619dba33cSLai Jiangshan static inline void *allocate_probes(int count)
7797e1c18eSMathieu Desnoyers {
7838516ab5SSteven Rostedt 	struct tp_probes *p  = kmalloc(count * sizeof(struct tracepoint_func)
7919dba33cSLai Jiangshan 			+ sizeof(struct tp_probes), GFP_KERNEL);
8019dba33cSLai Jiangshan 	return p == NULL ? NULL : p->probes;
8197e1c18eSMathieu Desnoyers }
8297e1c18eSMathieu Desnoyers 
8319dba33cSLai Jiangshan static void rcu_free_old_probes(struct rcu_head *head)
8497e1c18eSMathieu Desnoyers {
85127cafbbSLai Jiangshan 	kfree(container_of(head, struct tp_probes, u.rcu));
8619dba33cSLai Jiangshan }
8719dba33cSLai Jiangshan 
8838516ab5SSteven Rostedt static inline void release_probes(struct tracepoint_func *old)
8919dba33cSLai Jiangshan {
9019dba33cSLai Jiangshan 	if (old) {
9119dba33cSLai Jiangshan 		struct tp_probes *tp_probes = container_of(old,
9219dba33cSLai Jiangshan 			struct tp_probes, probes[0]);
93127cafbbSLai Jiangshan 		call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
9419dba33cSLai Jiangshan 	}
9597e1c18eSMathieu Desnoyers }
9697e1c18eSMathieu Desnoyers 
9797e1c18eSMathieu Desnoyers static void debug_print_probes(struct tracepoint_entry *entry)
9897e1c18eSMathieu Desnoyers {
9997e1c18eSMathieu Desnoyers 	int i;
10097e1c18eSMathieu Desnoyers 
10119dba33cSLai Jiangshan 	if (!tracepoint_debug || !entry->funcs)
10297e1c18eSMathieu Desnoyers 		return;
10397e1c18eSMathieu Desnoyers 
10438516ab5SSteven Rostedt 	for (i = 0; entry->funcs[i].func; i++)
10538516ab5SSteven Rostedt 		printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i].func);
10697e1c18eSMathieu Desnoyers }
10797e1c18eSMathieu Desnoyers 
10838516ab5SSteven Rostedt static struct tracepoint_func *
10938516ab5SSteven Rostedt tracepoint_entry_add_probe(struct tracepoint_entry *entry,
11038516ab5SSteven Rostedt 			   void *probe, void *data)
11197e1c18eSMathieu Desnoyers {
11297e1c18eSMathieu Desnoyers 	int nr_probes = 0;
11338516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
11497e1c18eSMathieu Desnoyers 
1154c69e6eaSSahara 	if (WARN_ON(!probe))
1164c69e6eaSSahara 		return ERR_PTR(-EINVAL);
11797e1c18eSMathieu Desnoyers 
11897e1c18eSMathieu Desnoyers 	debug_print_probes(entry);
11997e1c18eSMathieu Desnoyers 	old = entry->funcs;
12097e1c18eSMathieu Desnoyers 	if (old) {
12197e1c18eSMathieu Desnoyers 		/* (N -> N+1), (N != 0, 1) probes */
12238516ab5SSteven Rostedt 		for (nr_probes = 0; old[nr_probes].func; nr_probes++)
12338516ab5SSteven Rostedt 			if (old[nr_probes].func == probe &&
12438516ab5SSteven Rostedt 			    old[nr_probes].data == data)
12597e1c18eSMathieu Desnoyers 				return ERR_PTR(-EEXIST);
12697e1c18eSMathieu Desnoyers 	}
12797e1c18eSMathieu Desnoyers 	/* + 2 : one for new probe, one for NULL func */
12819dba33cSLai Jiangshan 	new = allocate_probes(nr_probes + 2);
12997e1c18eSMathieu Desnoyers 	if (new == NULL)
13097e1c18eSMathieu Desnoyers 		return ERR_PTR(-ENOMEM);
13197e1c18eSMathieu Desnoyers 	if (old)
13238516ab5SSteven Rostedt 		memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
13338516ab5SSteven Rostedt 	new[nr_probes].func = probe;
13438516ab5SSteven Rostedt 	new[nr_probes].data = data;
13538516ab5SSteven Rostedt 	new[nr_probes + 1].func = NULL;
13697e1c18eSMathieu Desnoyers 	entry->refcount = nr_probes + 1;
13797e1c18eSMathieu Desnoyers 	entry->funcs = new;
13897e1c18eSMathieu Desnoyers 	debug_print_probes(entry);
13997e1c18eSMathieu Desnoyers 	return old;
14097e1c18eSMathieu Desnoyers }
14197e1c18eSMathieu Desnoyers 
14297e1c18eSMathieu Desnoyers static void *
14338516ab5SSteven Rostedt tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
14438516ab5SSteven Rostedt 			      void *probe, void *data)
14597e1c18eSMathieu Desnoyers {
14697e1c18eSMathieu Desnoyers 	int nr_probes = 0, nr_del = 0, i;
14738516ab5SSteven Rostedt 	struct tracepoint_func *old, *new;
14897e1c18eSMathieu Desnoyers 
14997e1c18eSMathieu Desnoyers 	old = entry->funcs;
15097e1c18eSMathieu Desnoyers 
151f66af459SFrederic Weisbecker 	if (!old)
15219dba33cSLai Jiangshan 		return ERR_PTR(-ENOENT);
153f66af459SFrederic Weisbecker 
15497e1c18eSMathieu Desnoyers 	debug_print_probes(entry);
15597e1c18eSMathieu Desnoyers 	/* (N -> M), (N > 1, M >= 0) probes */
1564c69e6eaSSahara 	if (probe) {
15738516ab5SSteven Rostedt 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
1584c69e6eaSSahara 			if (old[nr_probes].func == probe &&
1594c69e6eaSSahara 			     old[nr_probes].data == 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) */
17097e1c18eSMathieu Desnoyers 		entry->funcs = NULL;
17197e1c18eSMathieu Desnoyers 		entry->refcount = 0;
17297e1c18eSMathieu Desnoyers 		debug_print_probes(entry);
17397e1c18eSMathieu Desnoyers 		return old;
17497e1c18eSMathieu Desnoyers 	} else {
17597e1c18eSMathieu Desnoyers 		int j = 0;
17697e1c18eSMathieu Desnoyers 		/* N -> M, (N > 1, M > 0) */
17797e1c18eSMathieu Desnoyers 		/* + 1 for NULL */
17819dba33cSLai Jiangshan 		new = allocate_probes(nr_probes - nr_del + 1);
17997e1c18eSMathieu Desnoyers 		if (new == NULL)
18097e1c18eSMathieu Desnoyers 			return ERR_PTR(-ENOMEM);
18138516ab5SSteven Rostedt 		for (i = 0; old[i].func; i++)
1824c69e6eaSSahara 			if (old[i].func != probe || old[i].data != data)
18397e1c18eSMathieu Desnoyers 				new[j++] = old[i];
18438516ab5SSteven Rostedt 		new[nr_probes - nr_del].func = NULL;
18597e1c18eSMathieu Desnoyers 		entry->refcount = nr_probes - nr_del;
18697e1c18eSMathieu Desnoyers 		entry->funcs = new;
18797e1c18eSMathieu Desnoyers 	}
18897e1c18eSMathieu Desnoyers 	debug_print_probes(entry);
18997e1c18eSMathieu Desnoyers 	return old;
19097e1c18eSMathieu Desnoyers }
19197e1c18eSMathieu Desnoyers 
19297e1c18eSMathieu Desnoyers /*
19397e1c18eSMathieu Desnoyers  * Get tracepoint if the tracepoint is present in the tracepoint hash table.
19497e1c18eSMathieu Desnoyers  * Must be called with tracepoints_mutex held.
19597e1c18eSMathieu Desnoyers  * Returns NULL if not present.
19697e1c18eSMathieu Desnoyers  */
19797e1c18eSMathieu Desnoyers static struct tracepoint_entry *get_tracepoint(const char *name)
19897e1c18eSMathieu Desnoyers {
19997e1c18eSMathieu Desnoyers 	struct hlist_head *head;
20097e1c18eSMathieu Desnoyers 	struct tracepoint_entry *e;
20197e1c18eSMathieu Desnoyers 	u32 hash = jhash(name, strlen(name), 0);
20297e1c18eSMathieu Desnoyers 
2039795302aSMathieu Desnoyers 	head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
204b67bfe0dSSasha Levin 	hlist_for_each_entry(e, head, hlist) {
20597e1c18eSMathieu Desnoyers 		if (!strcmp(name, e->name))
20697e1c18eSMathieu Desnoyers 			return e;
20797e1c18eSMathieu Desnoyers 	}
20897e1c18eSMathieu Desnoyers 	return NULL;
20997e1c18eSMathieu Desnoyers }
21097e1c18eSMathieu Desnoyers 
21197e1c18eSMathieu Desnoyers /*
21297e1c18eSMathieu Desnoyers  * Add the tracepoint to the tracepoint hash table. Must be called with
21397e1c18eSMathieu Desnoyers  * tracepoints_mutex held.
21497e1c18eSMathieu Desnoyers  */
21597e1c18eSMathieu Desnoyers static struct tracepoint_entry *add_tracepoint(const char *name)
21697e1c18eSMathieu Desnoyers {
21797e1c18eSMathieu Desnoyers 	struct hlist_head *head;
21897e1c18eSMathieu Desnoyers 	struct tracepoint_entry *e;
21997e1c18eSMathieu Desnoyers 	size_t name_len = strlen(name) + 1;
22097e1c18eSMathieu Desnoyers 	u32 hash = jhash(name, name_len-1, 0);
22197e1c18eSMathieu Desnoyers 
2229795302aSMathieu Desnoyers 	head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
223b67bfe0dSSasha Levin 	hlist_for_each_entry(e, head, hlist) {
22497e1c18eSMathieu Desnoyers 		if (!strcmp(name, e->name)) {
22597e1c18eSMathieu Desnoyers 			printk(KERN_NOTICE
22697e1c18eSMathieu Desnoyers 				"tracepoint %s busy\n", name);
22797e1c18eSMathieu Desnoyers 			return ERR_PTR(-EEXIST);	/* Already there */
22897e1c18eSMathieu Desnoyers 		}
22997e1c18eSMathieu Desnoyers 	}
23097e1c18eSMathieu Desnoyers 	/*
23197e1c18eSMathieu Desnoyers 	 * Using kmalloc here to allocate a variable length element. Could
23297e1c18eSMathieu Desnoyers 	 * cause some memory fragmentation if overused.
23397e1c18eSMathieu Desnoyers 	 */
23497e1c18eSMathieu Desnoyers 	e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
23597e1c18eSMathieu Desnoyers 	if (!e)
23697e1c18eSMathieu Desnoyers 		return ERR_PTR(-ENOMEM);
23797e1c18eSMathieu Desnoyers 	memcpy(&e->name[0], name, name_len);
23897e1c18eSMathieu Desnoyers 	e->funcs = NULL;
23997e1c18eSMathieu Desnoyers 	e->refcount = 0;
24097e1c18eSMathieu Desnoyers 	hlist_add_head(&e->hlist, head);
24197e1c18eSMathieu Desnoyers 	return e;
24297e1c18eSMathieu Desnoyers }
24397e1c18eSMathieu Desnoyers 
24497e1c18eSMathieu Desnoyers /*
24597e1c18eSMathieu Desnoyers  * Remove the tracepoint from the tracepoint hash table. Must be called with
24697e1c18eSMathieu Desnoyers  * mutex_lock held.
24797e1c18eSMathieu Desnoyers  */
24819dba33cSLai Jiangshan static inline void remove_tracepoint(struct tracepoint_entry *e)
24997e1c18eSMathieu Desnoyers {
25097e1c18eSMathieu Desnoyers 	hlist_del(&e->hlist);
25197e1c18eSMathieu Desnoyers 	kfree(e);
25297e1c18eSMathieu Desnoyers }
25397e1c18eSMathieu Desnoyers 
25497e1c18eSMathieu Desnoyers /*
25597e1c18eSMathieu Desnoyers  * Sets the probe callback corresponding to one tracepoint.
25697e1c18eSMathieu Desnoyers  */
25797e1c18eSMathieu Desnoyers static void set_tracepoint(struct tracepoint_entry **entry,
25897e1c18eSMathieu Desnoyers 	struct tracepoint *elem, int active)
25997e1c18eSMathieu Desnoyers {
26097e1c18eSMathieu Desnoyers 	WARN_ON(strcmp((*entry)->name, elem->name) != 0);
26197e1c18eSMathieu Desnoyers 
262c5905afbSIngo Molnar 	if (elem->regfunc && !static_key_enabled(&elem->key) && active)
26397419875SJosh Stone 		elem->regfunc();
264c5905afbSIngo Molnar 	else if (elem->unregfunc && static_key_enabled(&elem->key) && !active)
26597419875SJosh Stone 		elem->unregfunc();
26697419875SJosh Stone 
26797e1c18eSMathieu Desnoyers 	/*
26897e1c18eSMathieu Desnoyers 	 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
26997e1c18eSMathieu Desnoyers 	 * probe callbacks array is consistent before setting a pointer to it.
27097e1c18eSMathieu Desnoyers 	 * This array is referenced by __DO_TRACE from
27197e1c18eSMathieu Desnoyers 	 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
27297e1c18eSMathieu Desnoyers 	 * is used.
27397e1c18eSMathieu Desnoyers 	 */
27497e1c18eSMathieu Desnoyers 	rcu_assign_pointer(elem->funcs, (*entry)->funcs);
275c5905afbSIngo Molnar 	if (active && !static_key_enabled(&elem->key))
276c5905afbSIngo Molnar 		static_key_slow_inc(&elem->key);
277c5905afbSIngo Molnar 	else if (!active && static_key_enabled(&elem->key))
278c5905afbSIngo Molnar 		static_key_slow_dec(&elem->key);
27997e1c18eSMathieu Desnoyers }
28097e1c18eSMathieu Desnoyers 
28197e1c18eSMathieu Desnoyers /*
28297e1c18eSMathieu Desnoyers  * Disable a tracepoint and its probe callback.
28397e1c18eSMathieu Desnoyers  * Note: only waiting an RCU period after setting elem->call to the empty
28497e1c18eSMathieu Desnoyers  * function insures that the original callback is not used anymore. This insured
28597e1c18eSMathieu Desnoyers  * by preempt_disable around the call site.
28697e1c18eSMathieu Desnoyers  */
28797e1c18eSMathieu Desnoyers static void disable_tracepoint(struct tracepoint *elem)
28897e1c18eSMathieu Desnoyers {
289c5905afbSIngo Molnar 	if (elem->unregfunc && static_key_enabled(&elem->key))
29097419875SJosh Stone 		elem->unregfunc();
29197419875SJosh Stone 
292c5905afbSIngo Molnar 	if (static_key_enabled(&elem->key))
293c5905afbSIngo Molnar 		static_key_slow_dec(&elem->key);
294de0baf9aSMathieu Desnoyers 	rcu_assign_pointer(elem->funcs, NULL);
29597e1c18eSMathieu Desnoyers }
29697e1c18eSMathieu Desnoyers 
29797e1c18eSMathieu Desnoyers /**
29897e1c18eSMathieu Desnoyers  * tracepoint_update_probe_range - Update a probe range
29997e1c18eSMathieu Desnoyers  * @begin: beginning of the range
30097e1c18eSMathieu Desnoyers  * @end: end of the range
30197e1c18eSMathieu Desnoyers  *
30297e1c18eSMathieu Desnoyers  * Updates the probe callback corresponding to a range of tracepoints.
303b75ef8b4SMathieu Desnoyers  * Called with tracepoints_mutex held.
30497e1c18eSMathieu Desnoyers  */
305b75ef8b4SMathieu Desnoyers static void tracepoint_update_probe_range(struct tracepoint * const *begin,
30665498646SMathieu Desnoyers 					  struct tracepoint * const *end)
30797e1c18eSMathieu Desnoyers {
30865498646SMathieu Desnoyers 	struct tracepoint * const *iter;
30997e1c18eSMathieu Desnoyers 	struct tracepoint_entry *mark_entry;
31097e1c18eSMathieu Desnoyers 
311ec625cb2SIngo Molnar 	if (!begin)
31209933a10SJaswinder Singh Rajput 		return;
31309933a10SJaswinder Singh Rajput 
31497e1c18eSMathieu Desnoyers 	for (iter = begin; iter < end; iter++) {
31565498646SMathieu Desnoyers 		mark_entry = get_tracepoint((*iter)->name);
31697e1c18eSMathieu Desnoyers 		if (mark_entry) {
31765498646SMathieu Desnoyers 			set_tracepoint(&mark_entry, *iter,
31897e1c18eSMathieu Desnoyers 					!!mark_entry->refcount);
31997e1c18eSMathieu Desnoyers 		} else {
32065498646SMathieu Desnoyers 			disable_tracepoint(*iter);
32197e1c18eSMathieu Desnoyers 		}
32297e1c18eSMathieu Desnoyers 	}
32397e1c18eSMathieu Desnoyers }
32497e1c18eSMathieu Desnoyers 
325b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES
326b75ef8b4SMathieu Desnoyers void module_update_tracepoints(void)
327b75ef8b4SMathieu Desnoyers {
328b75ef8b4SMathieu Desnoyers 	struct tp_module *tp_mod;
329b75ef8b4SMathieu Desnoyers 
330b75ef8b4SMathieu Desnoyers 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
331b75ef8b4SMathieu Desnoyers 		tracepoint_update_probe_range(tp_mod->tracepoints_ptrs,
332b75ef8b4SMathieu Desnoyers 			tp_mod->tracepoints_ptrs + tp_mod->num_tracepoints);
333b75ef8b4SMathieu Desnoyers }
334b75ef8b4SMathieu Desnoyers #else /* CONFIG_MODULES */
335b75ef8b4SMathieu Desnoyers void module_update_tracepoints(void)
336b75ef8b4SMathieu Desnoyers {
337b75ef8b4SMathieu Desnoyers }
338b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */
339b75ef8b4SMathieu Desnoyers 
340b75ef8b4SMathieu Desnoyers 
34197e1c18eSMathieu Desnoyers /*
34297e1c18eSMathieu Desnoyers  * Update probes, removing the faulty probes.
343b75ef8b4SMathieu Desnoyers  * Called with tracepoints_mutex held.
34497e1c18eSMathieu Desnoyers  */
34597e1c18eSMathieu Desnoyers static void tracepoint_update_probes(void)
34697e1c18eSMathieu Desnoyers {
34797e1c18eSMathieu Desnoyers 	/* Core kernel tracepoints */
34865498646SMathieu Desnoyers 	tracepoint_update_probe_range(__start___tracepoints_ptrs,
34965498646SMathieu Desnoyers 		__stop___tracepoints_ptrs);
35097e1c18eSMathieu Desnoyers 	/* tracepoints in modules. */
35197e1c18eSMathieu Desnoyers 	module_update_tracepoints();
35297e1c18eSMathieu Desnoyers }
35397e1c18eSMathieu Desnoyers 
35438516ab5SSteven Rostedt static struct tracepoint_func *
35538516ab5SSteven Rostedt tracepoint_add_probe(const char *name, void *probe, void *data)
356127cafbbSLai Jiangshan {
357127cafbbSLai Jiangshan 	struct tracepoint_entry *entry;
35838516ab5SSteven Rostedt 	struct tracepoint_func *old;
359127cafbbSLai Jiangshan 
360127cafbbSLai Jiangshan 	entry = get_tracepoint(name);
361127cafbbSLai Jiangshan 	if (!entry) {
362127cafbbSLai Jiangshan 		entry = add_tracepoint(name);
363127cafbbSLai Jiangshan 		if (IS_ERR(entry))
36438516ab5SSteven Rostedt 			return (struct tracepoint_func *)entry;
365127cafbbSLai Jiangshan 	}
36638516ab5SSteven Rostedt 	old = tracepoint_entry_add_probe(entry, probe, data);
367127cafbbSLai Jiangshan 	if (IS_ERR(old) && !entry->refcount)
368127cafbbSLai Jiangshan 		remove_tracepoint(entry);
369127cafbbSLai Jiangshan 	return old;
370127cafbbSLai Jiangshan }
371127cafbbSLai Jiangshan 
37297e1c18eSMathieu Desnoyers /**
37397e1c18eSMathieu Desnoyers  * tracepoint_probe_register -  Connect a probe to a tracepoint
37497e1c18eSMathieu Desnoyers  * @name: tracepoint name
37597e1c18eSMathieu Desnoyers  * @probe: probe handler
37697e1c18eSMathieu Desnoyers  *
37797e1c18eSMathieu Desnoyers  * Returns 0 if ok, error value on error.
37897e1c18eSMathieu Desnoyers  * The probe address must at least be aligned on the architecture pointer size.
37997e1c18eSMathieu Desnoyers  */
38038516ab5SSteven Rostedt int tracepoint_probe_register(const char *name, void *probe, void *data)
38197e1c18eSMathieu Desnoyers {
38238516ab5SSteven Rostedt 	struct tracepoint_func *old;
38397e1c18eSMathieu Desnoyers 
38497e1c18eSMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
38538516ab5SSteven Rostedt 	old = tracepoint_add_probe(name, probe, data);
386b75ef8b4SMathieu Desnoyers 	if (IS_ERR(old)) {
38797e1c18eSMathieu Desnoyers 		mutex_unlock(&tracepoints_mutex);
388127cafbbSLai Jiangshan 		return PTR_ERR(old);
389b75ef8b4SMathieu Desnoyers 	}
39097e1c18eSMathieu Desnoyers 	tracepoint_update_probes();		/* may update entry */
391b75ef8b4SMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
39219dba33cSLai Jiangshan 	release_probes(old);
39319dba33cSLai Jiangshan 	return 0;
39497e1c18eSMathieu Desnoyers }
39597e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_register);
39697e1c18eSMathieu Desnoyers 
39738516ab5SSteven Rostedt static struct tracepoint_func *
39838516ab5SSteven Rostedt tracepoint_remove_probe(const char *name, void *probe, void *data)
399127cafbbSLai Jiangshan {
400127cafbbSLai Jiangshan 	struct tracepoint_entry *entry;
40138516ab5SSteven Rostedt 	struct tracepoint_func *old;
402127cafbbSLai Jiangshan 
403127cafbbSLai Jiangshan 	entry = get_tracepoint(name);
404127cafbbSLai Jiangshan 	if (!entry)
405127cafbbSLai Jiangshan 		return ERR_PTR(-ENOENT);
40638516ab5SSteven Rostedt 	old = tracepoint_entry_remove_probe(entry, probe, data);
407127cafbbSLai Jiangshan 	if (IS_ERR(old))
408127cafbbSLai Jiangshan 		return old;
409127cafbbSLai Jiangshan 	if (!entry->refcount)
410127cafbbSLai Jiangshan 		remove_tracepoint(entry);
411127cafbbSLai Jiangshan 	return old;
412127cafbbSLai Jiangshan }
413127cafbbSLai Jiangshan 
41497e1c18eSMathieu Desnoyers /**
41597e1c18eSMathieu Desnoyers  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
41697e1c18eSMathieu Desnoyers  * @name: tracepoint name
41797e1c18eSMathieu Desnoyers  * @probe: probe function pointer
41897e1c18eSMathieu Desnoyers  *
41997e1c18eSMathieu Desnoyers  * We do not need to call a synchronize_sched to make sure the probes have
42097e1c18eSMathieu Desnoyers  * finished running before doing a module unload, because the module unload
42197e1c18eSMathieu Desnoyers  * itself uses stop_machine(), which insures that every preempt disabled section
42297e1c18eSMathieu Desnoyers  * have finished.
42397e1c18eSMathieu Desnoyers  */
42438516ab5SSteven Rostedt int tracepoint_probe_unregister(const char *name, void *probe, void *data)
42597e1c18eSMathieu Desnoyers {
42638516ab5SSteven Rostedt 	struct tracepoint_func *old;
42797e1c18eSMathieu Desnoyers 
42897e1c18eSMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
42938516ab5SSteven Rostedt 	old = tracepoint_remove_probe(name, probe, data);
430b75ef8b4SMathieu Desnoyers 	if (IS_ERR(old)) {
43197e1c18eSMathieu Desnoyers 		mutex_unlock(&tracepoints_mutex);
432127cafbbSLai Jiangshan 		return PTR_ERR(old);
433b75ef8b4SMathieu Desnoyers 	}
43497e1c18eSMathieu Desnoyers 	tracepoint_update_probes();		/* may update entry */
435b75ef8b4SMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
43619dba33cSLai Jiangshan 	release_probes(old);
43719dba33cSLai Jiangshan 	return 0;
43897e1c18eSMathieu Desnoyers }
43997e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
44097e1c18eSMathieu Desnoyers 
441127cafbbSLai Jiangshan static LIST_HEAD(old_probes);
442127cafbbSLai Jiangshan static int need_update;
443127cafbbSLai Jiangshan 
444127cafbbSLai Jiangshan static void tracepoint_add_old_probes(void *old)
445127cafbbSLai Jiangshan {
446127cafbbSLai Jiangshan 	need_update = 1;
447127cafbbSLai Jiangshan 	if (old) {
448127cafbbSLai Jiangshan 		struct tp_probes *tp_probes = container_of(old,
449127cafbbSLai Jiangshan 			struct tp_probes, probes[0]);
450127cafbbSLai Jiangshan 		list_add(&tp_probes->u.list, &old_probes);
451127cafbbSLai Jiangshan 	}
452127cafbbSLai Jiangshan }
453127cafbbSLai Jiangshan 
454127cafbbSLai Jiangshan /**
455127cafbbSLai Jiangshan  * tracepoint_probe_register_noupdate -  register a probe but not connect
456127cafbbSLai Jiangshan  * @name: tracepoint name
457127cafbbSLai Jiangshan  * @probe: probe handler
458127cafbbSLai Jiangshan  *
459127cafbbSLai Jiangshan  * caller must call tracepoint_probe_update_all()
460127cafbbSLai Jiangshan  */
46138516ab5SSteven Rostedt int tracepoint_probe_register_noupdate(const char *name, void *probe,
46238516ab5SSteven Rostedt 				       void *data)
463127cafbbSLai Jiangshan {
46438516ab5SSteven Rostedt 	struct tracepoint_func *old;
465127cafbbSLai Jiangshan 
466127cafbbSLai Jiangshan 	mutex_lock(&tracepoints_mutex);
46738516ab5SSteven Rostedt 	old = tracepoint_add_probe(name, probe, data);
468127cafbbSLai Jiangshan 	if (IS_ERR(old)) {
469127cafbbSLai Jiangshan 		mutex_unlock(&tracepoints_mutex);
470127cafbbSLai Jiangshan 		return PTR_ERR(old);
471127cafbbSLai Jiangshan 	}
472127cafbbSLai Jiangshan 	tracepoint_add_old_probes(old);
473127cafbbSLai Jiangshan 	mutex_unlock(&tracepoints_mutex);
474127cafbbSLai Jiangshan 	return 0;
475127cafbbSLai Jiangshan }
476127cafbbSLai Jiangshan EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
477127cafbbSLai Jiangshan 
478127cafbbSLai Jiangshan /**
479127cafbbSLai Jiangshan  * tracepoint_probe_unregister_noupdate -  remove a probe but not disconnect
480127cafbbSLai Jiangshan  * @name: tracepoint name
481127cafbbSLai Jiangshan  * @probe: probe function pointer
482127cafbbSLai Jiangshan  *
483127cafbbSLai Jiangshan  * caller must call tracepoint_probe_update_all()
484127cafbbSLai Jiangshan  */
48538516ab5SSteven Rostedt int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
48638516ab5SSteven Rostedt 					 void *data)
487127cafbbSLai Jiangshan {
48838516ab5SSteven Rostedt 	struct tracepoint_func *old;
489127cafbbSLai Jiangshan 
490127cafbbSLai Jiangshan 	mutex_lock(&tracepoints_mutex);
49138516ab5SSteven Rostedt 	old = tracepoint_remove_probe(name, probe, data);
492127cafbbSLai Jiangshan 	if (IS_ERR(old)) {
493127cafbbSLai Jiangshan 		mutex_unlock(&tracepoints_mutex);
494127cafbbSLai Jiangshan 		return PTR_ERR(old);
495127cafbbSLai Jiangshan 	}
496127cafbbSLai Jiangshan 	tracepoint_add_old_probes(old);
497127cafbbSLai Jiangshan 	mutex_unlock(&tracepoints_mutex);
498127cafbbSLai Jiangshan 	return 0;
499127cafbbSLai Jiangshan }
500127cafbbSLai Jiangshan EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
501127cafbbSLai Jiangshan 
502127cafbbSLai Jiangshan /**
503127cafbbSLai Jiangshan  * tracepoint_probe_update_all -  update tracepoints
504127cafbbSLai Jiangshan  */
505127cafbbSLai Jiangshan void tracepoint_probe_update_all(void)
506127cafbbSLai Jiangshan {
507127cafbbSLai Jiangshan 	LIST_HEAD(release_probes);
508127cafbbSLai Jiangshan 	struct tp_probes *pos, *next;
509127cafbbSLai Jiangshan 
510127cafbbSLai Jiangshan 	mutex_lock(&tracepoints_mutex);
511127cafbbSLai Jiangshan 	if (!need_update) {
512127cafbbSLai Jiangshan 		mutex_unlock(&tracepoints_mutex);
513127cafbbSLai Jiangshan 		return;
514127cafbbSLai Jiangshan 	}
515127cafbbSLai Jiangshan 	if (!list_empty(&old_probes))
516127cafbbSLai Jiangshan 		list_replace_init(&old_probes, &release_probes);
517127cafbbSLai Jiangshan 	need_update = 0;
518127cafbbSLai Jiangshan 	tracepoint_update_probes();
519b75ef8b4SMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
520127cafbbSLai Jiangshan 	list_for_each_entry_safe(pos, next, &release_probes, u.list) {
521127cafbbSLai Jiangshan 		list_del(&pos->u.list);
522127cafbbSLai Jiangshan 		call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
523127cafbbSLai Jiangshan 	}
524127cafbbSLai Jiangshan }
525127cafbbSLai Jiangshan EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
526127cafbbSLai Jiangshan 
52797e1c18eSMathieu Desnoyers /**
52897e1c18eSMathieu Desnoyers  * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
52997e1c18eSMathieu Desnoyers  * @tracepoint: current tracepoints (in), next tracepoint (out)
53097e1c18eSMathieu Desnoyers  * @begin: beginning of the range
53197e1c18eSMathieu Desnoyers  * @end: end of the range
53297e1c18eSMathieu Desnoyers  *
53397e1c18eSMathieu Desnoyers  * Returns whether a next tracepoint has been found (1) or not (0).
53497e1c18eSMathieu Desnoyers  * Will return the first tracepoint in the range if the input tracepoint is
53597e1c18eSMathieu Desnoyers  * NULL.
53697e1c18eSMathieu Desnoyers  */
537b75ef8b4SMathieu Desnoyers static int tracepoint_get_iter_range(struct tracepoint * const **tracepoint,
53865498646SMathieu Desnoyers 	struct tracepoint * const *begin, struct tracepoint * const *end)
53997e1c18eSMathieu Desnoyers {
54097e1c18eSMathieu Desnoyers 	if (!*tracepoint && begin != end) {
54197e1c18eSMathieu Desnoyers 		*tracepoint = begin;
54297e1c18eSMathieu Desnoyers 		return 1;
54397e1c18eSMathieu Desnoyers 	}
54497e1c18eSMathieu Desnoyers 	if (*tracepoint >= begin && *tracepoint < end)
54597e1c18eSMathieu Desnoyers 		return 1;
54697e1c18eSMathieu Desnoyers 	return 0;
54797e1c18eSMathieu Desnoyers }
54897e1c18eSMathieu Desnoyers 
549b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES
55097e1c18eSMathieu Desnoyers static void tracepoint_get_iter(struct tracepoint_iter *iter)
55197e1c18eSMathieu Desnoyers {
55297e1c18eSMathieu Desnoyers 	int found = 0;
553b75ef8b4SMathieu Desnoyers 	struct tp_module *iter_mod;
55497e1c18eSMathieu Desnoyers 
55597e1c18eSMathieu Desnoyers 	/* Core kernel tracepoints */
55697e1c18eSMathieu Desnoyers 	if (!iter->module) {
55797e1c18eSMathieu Desnoyers 		found = tracepoint_get_iter_range(&iter->tracepoint,
55865498646SMathieu Desnoyers 				__start___tracepoints_ptrs,
55965498646SMathieu Desnoyers 				__stop___tracepoints_ptrs);
56097e1c18eSMathieu Desnoyers 		if (found)
56197e1c18eSMathieu Desnoyers 			goto end;
56297e1c18eSMathieu Desnoyers 	}
563b75ef8b4SMathieu Desnoyers 	/* Tracepoints in modules */
564b75ef8b4SMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
565b75ef8b4SMathieu Desnoyers 	list_for_each_entry(iter_mod, &tracepoint_module_list, list) {
566b75ef8b4SMathieu Desnoyers 		/*
567b75ef8b4SMathieu Desnoyers 		 * Sorted module list
568b75ef8b4SMathieu Desnoyers 		 */
569b75ef8b4SMathieu Desnoyers 		if (iter_mod < iter->module)
570b75ef8b4SMathieu Desnoyers 			continue;
571b75ef8b4SMathieu Desnoyers 		else if (iter_mod > iter->module)
572b75ef8b4SMathieu Desnoyers 			iter->tracepoint = NULL;
573b75ef8b4SMathieu Desnoyers 		found = tracepoint_get_iter_range(&iter->tracepoint,
574b75ef8b4SMathieu Desnoyers 			iter_mod->tracepoints_ptrs,
575b75ef8b4SMathieu Desnoyers 			iter_mod->tracepoints_ptrs
576b75ef8b4SMathieu Desnoyers 				+ iter_mod->num_tracepoints);
577b75ef8b4SMathieu Desnoyers 		if (found) {
578b75ef8b4SMathieu Desnoyers 			iter->module = iter_mod;
579b75ef8b4SMathieu Desnoyers 			break;
580b75ef8b4SMathieu Desnoyers 		}
581b75ef8b4SMathieu Desnoyers 	}
582b75ef8b4SMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
58397e1c18eSMathieu Desnoyers end:
58497e1c18eSMathieu Desnoyers 	if (!found)
58597e1c18eSMathieu Desnoyers 		tracepoint_iter_reset(iter);
58697e1c18eSMathieu Desnoyers }
587b75ef8b4SMathieu Desnoyers #else /* CONFIG_MODULES */
588b75ef8b4SMathieu Desnoyers static void tracepoint_get_iter(struct tracepoint_iter *iter)
589b75ef8b4SMathieu Desnoyers {
590b75ef8b4SMathieu Desnoyers 	int found = 0;
591b75ef8b4SMathieu Desnoyers 
592b75ef8b4SMathieu Desnoyers 	/* Core kernel tracepoints */
593b75ef8b4SMathieu Desnoyers 	found = tracepoint_get_iter_range(&iter->tracepoint,
594b75ef8b4SMathieu Desnoyers 			__start___tracepoints_ptrs,
595b75ef8b4SMathieu Desnoyers 			__stop___tracepoints_ptrs);
596b75ef8b4SMathieu Desnoyers 	if (!found)
597b75ef8b4SMathieu Desnoyers 		tracepoint_iter_reset(iter);
598b75ef8b4SMathieu Desnoyers }
599b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */
60097e1c18eSMathieu Desnoyers 
60197e1c18eSMathieu Desnoyers void tracepoint_iter_start(struct tracepoint_iter *iter)
60297e1c18eSMathieu Desnoyers {
60397e1c18eSMathieu Desnoyers 	tracepoint_get_iter(iter);
60497e1c18eSMathieu Desnoyers }
60597e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_start);
60697e1c18eSMathieu Desnoyers 
60797e1c18eSMathieu Desnoyers void tracepoint_iter_next(struct tracepoint_iter *iter)
60897e1c18eSMathieu Desnoyers {
60997e1c18eSMathieu Desnoyers 	iter->tracepoint++;
61097e1c18eSMathieu Desnoyers 	/*
61197e1c18eSMathieu Desnoyers 	 * iter->tracepoint may be invalid because we blindly incremented it.
61297e1c18eSMathieu Desnoyers 	 * Make sure it is valid by marshalling on the tracepoints, getting the
61397e1c18eSMathieu Desnoyers 	 * tracepoints from following modules if necessary.
61497e1c18eSMathieu Desnoyers 	 */
61597e1c18eSMathieu Desnoyers 	tracepoint_get_iter(iter);
61697e1c18eSMathieu Desnoyers }
61797e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_next);
61897e1c18eSMathieu Desnoyers 
61997e1c18eSMathieu Desnoyers void tracepoint_iter_stop(struct tracepoint_iter *iter)
62097e1c18eSMathieu Desnoyers {
62197e1c18eSMathieu Desnoyers }
62297e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
62397e1c18eSMathieu Desnoyers 
62497e1c18eSMathieu Desnoyers void tracepoint_iter_reset(struct tracepoint_iter *iter)
62597e1c18eSMathieu Desnoyers {
626b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES
62797e1c18eSMathieu Desnoyers 	iter->module = NULL;
628b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */
62997e1c18eSMathieu Desnoyers 	iter->tracepoint = NULL;
63097e1c18eSMathieu Desnoyers }
63197e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
63232f85742SMathieu Desnoyers 
633227a8375SIngo Molnar #ifdef CONFIG_MODULES
634*45ab2813SSteven Rostedt (Red Hat) bool trace_module_has_bad_taint(struct module *mod)
635*45ab2813SSteven Rostedt (Red Hat) {
636*45ab2813SSteven Rostedt (Red Hat) 	return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP));
637*45ab2813SSteven Rostedt (Red Hat) }
638*45ab2813SSteven Rostedt (Red Hat) 
639b75ef8b4SMathieu Desnoyers static int tracepoint_module_coming(struct module *mod)
640b75ef8b4SMathieu Desnoyers {
641b75ef8b4SMathieu Desnoyers 	struct tp_module *tp_mod, *iter;
642b75ef8b4SMathieu Desnoyers 	int ret = 0;
643b75ef8b4SMathieu Desnoyers 
644b75ef8b4SMathieu Desnoyers 	/*
645c10076c4SSteven Rostedt 	 * We skip modules that taint the kernel, especially those with different
646c10076c4SSteven Rostedt 	 * module headers (for forced load), to make sure we don't cause a crash.
647c10076c4SSteven Rostedt 	 * Staging and out-of-tree GPL modules are fine.
648b75ef8b4SMathieu Desnoyers 	 */
649*45ab2813SSteven Rostedt (Red Hat) 	if (trace_module_has_bad_taint(mod))
650b75ef8b4SMathieu Desnoyers 		return 0;
651b75ef8b4SMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
652b75ef8b4SMathieu Desnoyers 	tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
653b75ef8b4SMathieu Desnoyers 	if (!tp_mod) {
654b75ef8b4SMathieu Desnoyers 		ret = -ENOMEM;
655b75ef8b4SMathieu Desnoyers 		goto end;
656b75ef8b4SMathieu Desnoyers 	}
657b75ef8b4SMathieu Desnoyers 	tp_mod->num_tracepoints = mod->num_tracepoints;
658b75ef8b4SMathieu Desnoyers 	tp_mod->tracepoints_ptrs = mod->tracepoints_ptrs;
659b75ef8b4SMathieu Desnoyers 
660b75ef8b4SMathieu Desnoyers 	/*
661b75ef8b4SMathieu Desnoyers 	 * tracepoint_module_list is kept sorted by struct module pointer
662b75ef8b4SMathieu Desnoyers 	 * address for iteration on tracepoints from a seq_file that can release
663b75ef8b4SMathieu Desnoyers 	 * the mutex between calls.
664b75ef8b4SMathieu Desnoyers 	 */
665b75ef8b4SMathieu Desnoyers 	list_for_each_entry_reverse(iter, &tracepoint_module_list, list) {
666b75ef8b4SMathieu Desnoyers 		BUG_ON(iter == tp_mod);	/* Should never be in the list twice */
667b75ef8b4SMathieu Desnoyers 		if (iter < tp_mod) {
668b75ef8b4SMathieu Desnoyers 			/* We belong to the location right after iter. */
669b75ef8b4SMathieu Desnoyers 			list_add(&tp_mod->list, &iter->list);
670b75ef8b4SMathieu Desnoyers 			goto module_added;
671b75ef8b4SMathieu Desnoyers 		}
672b75ef8b4SMathieu Desnoyers 	}
673b75ef8b4SMathieu Desnoyers 	/* We belong to the beginning of the list */
674b75ef8b4SMathieu Desnoyers 	list_add(&tp_mod->list, &tracepoint_module_list);
675b75ef8b4SMathieu Desnoyers module_added:
676b75ef8b4SMathieu Desnoyers 	tracepoint_update_probe_range(mod->tracepoints_ptrs,
677b75ef8b4SMathieu Desnoyers 		mod->tracepoints_ptrs + mod->num_tracepoints);
678b75ef8b4SMathieu Desnoyers end:
679b75ef8b4SMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
680b75ef8b4SMathieu Desnoyers 	return ret;
681b75ef8b4SMathieu Desnoyers }
682b75ef8b4SMathieu Desnoyers 
683b75ef8b4SMathieu Desnoyers static int tracepoint_module_going(struct module *mod)
684b75ef8b4SMathieu Desnoyers {
685b75ef8b4SMathieu Desnoyers 	struct tp_module *pos;
686b75ef8b4SMathieu Desnoyers 
687b75ef8b4SMathieu Desnoyers 	mutex_lock(&tracepoints_mutex);
688b75ef8b4SMathieu Desnoyers 	tracepoint_update_probe_range(mod->tracepoints_ptrs,
689b75ef8b4SMathieu Desnoyers 		mod->tracepoints_ptrs + mod->num_tracepoints);
690b75ef8b4SMathieu Desnoyers 	list_for_each_entry(pos, &tracepoint_module_list, list) {
691b75ef8b4SMathieu Desnoyers 		if (pos->tracepoints_ptrs == mod->tracepoints_ptrs) {
692b75ef8b4SMathieu Desnoyers 			list_del(&pos->list);
693b75ef8b4SMathieu Desnoyers 			kfree(pos);
694b75ef8b4SMathieu Desnoyers 			break;
695b75ef8b4SMathieu Desnoyers 		}
696b75ef8b4SMathieu Desnoyers 	}
697b75ef8b4SMathieu Desnoyers 	/*
698b75ef8b4SMathieu Desnoyers 	 * In the case of modules that were tainted at "coming", we'll simply
699b75ef8b4SMathieu Desnoyers 	 * walk through the list without finding it. We cannot use the "tainted"
700b75ef8b4SMathieu Desnoyers 	 * flag on "going", in case a module taints the kernel only after being
701b75ef8b4SMathieu Desnoyers 	 * loaded.
702b75ef8b4SMathieu Desnoyers 	 */
703b75ef8b4SMathieu Desnoyers 	mutex_unlock(&tracepoints_mutex);
704b75ef8b4SMathieu Desnoyers 	return 0;
705b75ef8b4SMathieu Desnoyers }
706227a8375SIngo Molnar 
70732f85742SMathieu Desnoyers int tracepoint_module_notify(struct notifier_block *self,
70832f85742SMathieu Desnoyers 			     unsigned long val, void *data)
70932f85742SMathieu Desnoyers {
71032f85742SMathieu Desnoyers 	struct module *mod = data;
711b75ef8b4SMathieu Desnoyers 	int ret = 0;
71232f85742SMathieu Desnoyers 
71332f85742SMathieu Desnoyers 	switch (val) {
71432f85742SMathieu Desnoyers 	case MODULE_STATE_COMING:
715b75ef8b4SMathieu Desnoyers 		ret = tracepoint_module_coming(mod);
716b75ef8b4SMathieu Desnoyers 		break;
717b75ef8b4SMathieu Desnoyers 	case MODULE_STATE_LIVE:
718b75ef8b4SMathieu Desnoyers 		break;
71932f85742SMathieu Desnoyers 	case MODULE_STATE_GOING:
720b75ef8b4SMathieu Desnoyers 		ret = tracepoint_module_going(mod);
72132f85742SMathieu Desnoyers 		break;
72232f85742SMathieu Desnoyers 	}
723b75ef8b4SMathieu Desnoyers 	return ret;
72432f85742SMathieu Desnoyers }
72532f85742SMathieu Desnoyers 
72632f85742SMathieu Desnoyers struct notifier_block tracepoint_module_nb = {
72732f85742SMathieu Desnoyers 	.notifier_call = tracepoint_module_notify,
72832f85742SMathieu Desnoyers 	.priority = 0,
72932f85742SMathieu Desnoyers };
73032f85742SMathieu Desnoyers 
73132f85742SMathieu Desnoyers static int init_tracepoints(void)
73232f85742SMathieu Desnoyers {
73332f85742SMathieu Desnoyers 	return register_module_notifier(&tracepoint_module_nb);
73432f85742SMathieu Desnoyers }
73532f85742SMathieu Desnoyers __initcall(init_tracepoints);
736227a8375SIngo Molnar #endif /* CONFIG_MODULES */
737a871bd33SJason Baron 
7383d27d8cbSJosh Stone #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
73960d970c2SIngo Molnar 
74097419875SJosh Stone /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
741a871bd33SJason Baron static int sys_tracepoint_refcount;
742a871bd33SJason Baron 
743a871bd33SJason Baron void syscall_regfunc(void)
744a871bd33SJason Baron {
745a871bd33SJason Baron 	unsigned long flags;
746a871bd33SJason Baron 	struct task_struct *g, *t;
747a871bd33SJason Baron 
748a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
749a871bd33SJason Baron 		read_lock_irqsave(&tasklist_lock, flags);
750a871bd33SJason Baron 		do_each_thread(g, t) {
751cc3b13c1SHendrik Brueckner 			/* Skip kernel threads. */
752cc3b13c1SHendrik Brueckner 			if (t->mm)
75366700001SJosh Stone 				set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
754a871bd33SJason Baron 		} while_each_thread(g, t);
755a871bd33SJason Baron 		read_unlock_irqrestore(&tasklist_lock, flags);
756a871bd33SJason Baron 	}
757a871bd33SJason Baron 	sys_tracepoint_refcount++;
758a871bd33SJason Baron }
759a871bd33SJason Baron 
760a871bd33SJason Baron void syscall_unregfunc(void)
761a871bd33SJason Baron {
762a871bd33SJason Baron 	unsigned long flags;
763a871bd33SJason Baron 	struct task_struct *g, *t;
764a871bd33SJason Baron 
765a871bd33SJason Baron 	sys_tracepoint_refcount--;
766a871bd33SJason Baron 	if (!sys_tracepoint_refcount) {
767a871bd33SJason Baron 		read_lock_irqsave(&tasklist_lock, flags);
768a871bd33SJason Baron 		do_each_thread(g, t) {
76966700001SJosh Stone 			clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
770a871bd33SJason Baron 		} while_each_thread(g, t);
771a871bd33SJason Baron 		read_unlock_irqrestore(&tasklist_lock, flags);
772a871bd33SJason Baron 	}
773a871bd33SJason Baron }
77460d970c2SIngo Molnar #endif
775