2 * Copyright (C) 2008 Mathieu Desnoyers
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/types.h>
21 #include <linux/jhash.h>
22 #include <linux/list.h>
23 #include <linux/rcupdate.h>
24 #include <linux/tracepoint.h>
25 #include <linux/err.h>
26 #include <linux/slab.h>
28 extern struct tracepoint __start___tracepoints[];
29 extern struct tracepoint __stop___tracepoints[];
31 /* Set to 1 to enable tracepoint debug output */
32 static const int tracepoint_debug;
35 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
36 * builtin and module tracepoints and the hash table.
38 static DEFINE_MUTEX(tracepoints_mutex);
41 * Tracepoint hash table, containing the active tracepoints.
42 * Protected by tracepoints_mutex.
44 #define TRACEPOINT_HASH_BITS 6
45 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
46 static struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
50 * It is used to to delay the free of multiple probes array until a quiescent
52 * Tracepoint entries modifications are protected by the tracepoints_mutex.
54 struct tracepoint_entry {
55 struct hlist_node hlist;
57 int refcount; /* Number of times armed. 0 if disarmed. */
66 static inline void *allocate_probes(int count)
68 struct tp_probes *p = kmalloc(count * sizeof(void *)
69 + sizeof(struct tp_probes), GFP_KERNEL);
70 return p == NULL ? NULL : p->probes;
73 static void rcu_free_old_probes(struct rcu_head *head)
75 kfree(container_of(head, struct tp_probes, rcu));
78 static inline void release_probes(void *old)
81 struct tp_probes *tp_probes = container_of(old,
82 struct tp_probes, probes[0]);
83 call_rcu(&tp_probes->rcu, rcu_free_old_probes);
87 static void debug_print_probes(struct tracepoint_entry *entry)
91 if (!tracepoint_debug || !entry->funcs)
94 for (i = 0; entry->funcs[i]; i++)
95 printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i]);
99 tracepoint_entry_add_probe(struct tracepoint_entry *entry, void *probe)
106 debug_print_probes(entry);
109 /* (N -> N+1), (N != 0, 1) probes */
110 for (nr_probes = 0; old[nr_probes]; nr_probes++)
111 if (old[nr_probes] == probe)
112 return ERR_PTR(-EEXIST);
114 /* + 2 : one for new probe, one for NULL func */
115 new = allocate_probes(nr_probes + 2);
117 return ERR_PTR(-ENOMEM);
119 memcpy(new, old, nr_probes * sizeof(void *));
120 new[nr_probes] = probe;
121 new[nr_probes + 1] = NULL;
122 entry->refcount = nr_probes + 1;
124 debug_print_probes(entry);
129 tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe)
131 int nr_probes = 0, nr_del = 0, i;
137 return ERR_PTR(-ENOENT);
139 debug_print_probes(entry);
140 /* (N -> M), (N > 1, M >= 0) probes */
141 for (nr_probes = 0; old[nr_probes]; nr_probes++) {
142 if ((!probe || old[nr_probes] == probe))
146 if (nr_probes - nr_del == 0) {
147 /* N -> 0, (N > 1) */
150 debug_print_probes(entry);
154 /* N -> M, (N > 1, M > 0) */
156 new = allocate_probes(nr_probes - nr_del + 1);
158 return ERR_PTR(-ENOMEM);
159 for (i = 0; old[i]; i++)
160 if ((probe && old[i] != probe))
162 new[nr_probes - nr_del] = NULL;
163 entry->refcount = nr_probes - nr_del;
166 debug_print_probes(entry);
171 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
172 * Must be called with tracepoints_mutex held.
173 * Returns NULL if not present.
175 static struct tracepoint_entry *get_tracepoint(const char *name)
177 struct hlist_head *head;
178 struct hlist_node *node;
179 struct tracepoint_entry *e;
180 u32 hash = jhash(name, strlen(name), 0);
182 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
183 hlist_for_each_entry(e, node, head, hlist) {
184 if (!strcmp(name, e->name))
191 * Add the tracepoint to the tracepoint hash table. Must be called with
192 * tracepoints_mutex held.
194 static struct tracepoint_entry *add_tracepoint(const char *name)
196 struct hlist_head *head;
197 struct hlist_node *node;
198 struct tracepoint_entry *e;
199 size_t name_len = strlen(name) + 1;
200 u32 hash = jhash(name, name_len-1, 0);
202 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
203 hlist_for_each_entry(e, node, head, hlist) {
204 if (!strcmp(name, e->name)) {
206 "tracepoint %s busy\n", name);
207 return ERR_PTR(-EEXIST); /* Already there */
211 * Using kmalloc here to allocate a variable length element. Could
212 * cause some memory fragmentation if overused.
214 e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
216 return ERR_PTR(-ENOMEM);
217 memcpy(&e->name[0], name, name_len);
220 hlist_add_head(&e->hlist, head);
225 * Remove the tracepoint from the tracepoint hash table. Must be called with
228 static inline void remove_tracepoint(struct tracepoint_entry *e)
230 hlist_del(&e->hlist);
235 * Sets the probe callback corresponding to one tracepoint.
237 static void set_tracepoint(struct tracepoint_entry **entry,
238 struct tracepoint *elem, int active)
240 WARN_ON(strcmp((*entry)->name, elem->name) != 0);
243 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
244 * probe callbacks array is consistent before setting a pointer to it.
245 * This array is referenced by __DO_TRACE from
246 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
249 rcu_assign_pointer(elem->funcs, (*entry)->funcs);
250 elem->state = active;
254 * Disable a tracepoint and its probe callback.
255 * Note: only waiting an RCU period after setting elem->call to the empty
256 * function insures that the original callback is not used anymore. This insured
257 * by preempt_disable around the call site.
259 static void disable_tracepoint(struct tracepoint *elem)
265 * tracepoint_update_probe_range - Update a probe range
266 * @begin: beginning of the range
267 * @end: end of the range
269 * Updates the probe callback corresponding to a range of tracepoints.
271 void tracepoint_update_probe_range(struct tracepoint *begin,
272 struct tracepoint *end)
274 struct tracepoint *iter;
275 struct tracepoint_entry *mark_entry;
277 mutex_lock(&tracepoints_mutex);
278 for (iter = begin; iter < end; iter++) {
279 mark_entry = get_tracepoint(iter->name);
281 set_tracepoint(&mark_entry, iter,
282 !!mark_entry->refcount);
284 disable_tracepoint(iter);
287 mutex_unlock(&tracepoints_mutex);
291 * Update probes, removing the faulty probes.
293 static void tracepoint_update_probes(void)
295 /* Core kernel tracepoints */
296 tracepoint_update_probe_range(__start___tracepoints,
297 __stop___tracepoints);
298 /* tracepoints in modules. */
299 module_update_tracepoints();
303 * tracepoint_probe_register - Connect a probe to a tracepoint
304 * @name: tracepoint name
305 * @probe: probe handler
307 * Returns 0 if ok, error value on error.
308 * The probe address must at least be aligned on the architecture pointer size.
310 int tracepoint_probe_register(const char *name, void *probe)
312 struct tracepoint_entry *entry;
316 mutex_lock(&tracepoints_mutex);
317 entry = get_tracepoint(name);
319 entry = add_tracepoint(name);
321 ret = PTR_ERR(entry);
325 old = tracepoint_entry_add_probe(entry, probe);
327 if (!entry->refcount)
328 remove_tracepoint(entry);
332 mutex_unlock(&tracepoints_mutex);
333 tracepoint_update_probes(); /* may update entry */
337 mutex_unlock(&tracepoints_mutex);
340 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
343 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
344 * @name: tracepoint name
345 * @probe: probe function pointer
347 * We do not need to call a synchronize_sched to make sure the probes have
348 * finished running before doing a module unload, because the module unload
349 * itself uses stop_machine(), which insures that every preempt disabled section
352 int tracepoint_probe_unregister(const char *name, void *probe)
354 struct tracepoint_entry *entry;
358 mutex_lock(&tracepoints_mutex);
359 entry = get_tracepoint(name);
362 old = tracepoint_entry_remove_probe(entry, probe);
367 if (!entry->refcount)
368 remove_tracepoint(entry);
369 mutex_unlock(&tracepoints_mutex);
370 tracepoint_update_probes(); /* may update entry */
374 mutex_unlock(&tracepoints_mutex);
377 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
380 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
381 * @tracepoint: current tracepoints (in), next tracepoint (out)
382 * @begin: beginning of the range
383 * @end: end of the range
385 * Returns whether a next tracepoint has been found (1) or not (0).
386 * Will return the first tracepoint in the range if the input tracepoint is
389 int tracepoint_get_iter_range(struct tracepoint **tracepoint,
390 struct tracepoint *begin, struct tracepoint *end)
392 if (!*tracepoint && begin != end) {
396 if (*tracepoint >= begin && *tracepoint < end)
400 EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
402 static void tracepoint_get_iter(struct tracepoint_iter *iter)
406 /* Core kernel tracepoints */
408 found = tracepoint_get_iter_range(&iter->tracepoint,
409 __start___tracepoints, __stop___tracepoints);
413 /* tracepoints in modules. */
414 found = module_get_iter_tracepoints(iter);
417 tracepoint_iter_reset(iter);
420 void tracepoint_iter_start(struct tracepoint_iter *iter)
422 tracepoint_get_iter(iter);
424 EXPORT_SYMBOL_GPL(tracepoint_iter_start);
426 void tracepoint_iter_next(struct tracepoint_iter *iter)
430 * iter->tracepoint may be invalid because we blindly incremented it.
431 * Make sure it is valid by marshalling on the tracepoints, getting the
432 * tracepoints from following modules if necessary.
434 tracepoint_get_iter(iter);
436 EXPORT_SYMBOL_GPL(tracepoint_iter_next);
438 void tracepoint_iter_stop(struct tracepoint_iter *iter)
441 EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
443 void tracepoint_iter_reset(struct tracepoint_iter *iter)
446 iter->tracepoint = NULL;
448 EXPORT_SYMBOL_GPL(tracepoint_iter_reset);