tracepoints: do not put arguments in name
[linux-2.6-block.git] / kernel / tracepoint.c
CommitLineData
97e1c18e
MD
1/*
2 * Copyright (C) 2008 Mathieu Desnoyers
3 *
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.
8 *
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.
13 *
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.
17 */
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>
27
28extern struct tracepoint __start___tracepoints[];
29extern struct tracepoint __stop___tracepoints[];
30
31/* Set to 1 to enable tracepoint debug output */
32static const int tracepoint_debug;
33
34/*
35 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
36 * builtin and module tracepoints and the hash table.
37 */
38static DEFINE_MUTEX(tracepoints_mutex);
39
40/*
41 * Tracepoint hash table, containing the active tracepoints.
42 * Protected by tracepoints_mutex.
43 */
44#define TRACEPOINT_HASH_BITS 6
45#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
19dba33c 46static struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
97e1c18e
MD
47
48/*
49 * Note about RCU :
50 * It is used to to delay the free of multiple probes array until a quiescent
51 * state is reached.
52 * Tracepoint entries modifications are protected by the tracepoints_mutex.
53 */
54struct tracepoint_entry {
55 struct hlist_node hlist;
56 void **funcs;
57 int refcount; /* Number of times armed. 0 if disarmed. */
97e1c18e
MD
58 char name[0];
59};
60
19dba33c 61struct tp_probes {
127cafbb
LJ
62 union {
63 struct rcu_head rcu;
64 struct list_head list;
65 } u;
19dba33c
LJ
66 void *probes[0];
67};
97e1c18e 68
19dba33c 69static inline void *allocate_probes(int count)
97e1c18e 70{
19dba33c
LJ
71 struct tp_probes *p = kmalloc(count * sizeof(void *)
72 + sizeof(struct tp_probes), GFP_KERNEL);
73 return p == NULL ? NULL : p->probes;
97e1c18e
MD
74}
75
19dba33c 76static void rcu_free_old_probes(struct rcu_head *head)
97e1c18e 77{
127cafbb 78 kfree(container_of(head, struct tp_probes, u.rcu));
19dba33c
LJ
79}
80
81static inline void release_probes(void *old)
82{
83 if (old) {
84 struct tp_probes *tp_probes = container_of(old,
85 struct tp_probes, probes[0]);
127cafbb 86 call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
19dba33c 87 }
97e1c18e
MD
88}
89
90static void debug_print_probes(struct tracepoint_entry *entry)
91{
92 int i;
93
19dba33c 94 if (!tracepoint_debug || !entry->funcs)
97e1c18e
MD
95 return;
96
97 for (i = 0; entry->funcs[i]; i++)
98 printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i]);
99}
100
101static void *
102tracepoint_entry_add_probe(struct tracepoint_entry *entry, void *probe)
103{
104 int nr_probes = 0;
105 void **old, **new;
106
107 WARN_ON(!probe);
108
109 debug_print_probes(entry);
110 old = entry->funcs;
111 if (old) {
112 /* (N -> N+1), (N != 0, 1) probes */
113 for (nr_probes = 0; old[nr_probes]; nr_probes++)
114 if (old[nr_probes] == probe)
115 return ERR_PTR(-EEXIST);
116 }
117 /* + 2 : one for new probe, one for NULL func */
19dba33c 118 new = allocate_probes(nr_probes + 2);
97e1c18e
MD
119 if (new == NULL)
120 return ERR_PTR(-ENOMEM);
121 if (old)
122 memcpy(new, old, nr_probes * sizeof(void *));
123 new[nr_probes] = probe;
19dba33c 124 new[nr_probes + 1] = NULL;
97e1c18e
MD
125 entry->refcount = nr_probes + 1;
126 entry->funcs = new;
127 debug_print_probes(entry);
128 return old;
129}
130
131static void *
132tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe)
133{
134 int nr_probes = 0, nr_del = 0, i;
135 void **old, **new;
136
137 old = entry->funcs;
138
f66af459 139 if (!old)
19dba33c 140 return ERR_PTR(-ENOENT);
f66af459 141
97e1c18e
MD
142 debug_print_probes(entry);
143 /* (N -> M), (N > 1, M >= 0) probes */
144 for (nr_probes = 0; old[nr_probes]; nr_probes++) {
145 if ((!probe || old[nr_probes] == probe))
146 nr_del++;
147 }
148
149 if (nr_probes - nr_del == 0) {
150 /* N -> 0, (N > 1) */
151 entry->funcs = NULL;
152 entry->refcount = 0;
153 debug_print_probes(entry);
154 return old;
155 } else {
156 int j = 0;
157 /* N -> M, (N > 1, M > 0) */
158 /* + 1 for NULL */
19dba33c 159 new = allocate_probes(nr_probes - nr_del + 1);
97e1c18e
MD
160 if (new == NULL)
161 return ERR_PTR(-ENOMEM);
162 for (i = 0; old[i]; i++)
163 if ((probe && old[i] != probe))
164 new[j++] = old[i];
19dba33c 165 new[nr_probes - nr_del] = NULL;
97e1c18e
MD
166 entry->refcount = nr_probes - nr_del;
167 entry->funcs = new;
168 }
169 debug_print_probes(entry);
170 return old;
171}
172
173/*
174 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
175 * Must be called with tracepoints_mutex held.
176 * Returns NULL if not present.
177 */
178static struct tracepoint_entry *get_tracepoint(const char *name)
179{
180 struct hlist_head *head;
181 struct hlist_node *node;
182 struct tracepoint_entry *e;
183 u32 hash = jhash(name, strlen(name), 0);
184
9795302a 185 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
97e1c18e
MD
186 hlist_for_each_entry(e, node, head, hlist) {
187 if (!strcmp(name, e->name))
188 return e;
189 }
190 return NULL;
191}
192
193/*
194 * Add the tracepoint to the tracepoint hash table. Must be called with
195 * tracepoints_mutex held.
196 */
197static struct tracepoint_entry *add_tracepoint(const char *name)
198{
199 struct hlist_head *head;
200 struct hlist_node *node;
201 struct tracepoint_entry *e;
202 size_t name_len = strlen(name) + 1;
203 u32 hash = jhash(name, name_len-1, 0);
204
9795302a 205 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
97e1c18e
MD
206 hlist_for_each_entry(e, node, head, hlist) {
207 if (!strcmp(name, e->name)) {
208 printk(KERN_NOTICE
209 "tracepoint %s busy\n", name);
210 return ERR_PTR(-EEXIST); /* Already there */
211 }
212 }
213 /*
214 * Using kmalloc here to allocate a variable length element. Could
215 * cause some memory fragmentation if overused.
216 */
217 e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
218 if (!e)
219 return ERR_PTR(-ENOMEM);
220 memcpy(&e->name[0], name, name_len);
221 e->funcs = NULL;
222 e->refcount = 0;
97e1c18e
MD
223 hlist_add_head(&e->hlist, head);
224 return e;
225}
226
227/*
228 * Remove the tracepoint from the tracepoint hash table. Must be called with
229 * mutex_lock held.
230 */
19dba33c 231static inline void remove_tracepoint(struct tracepoint_entry *e)
97e1c18e 232{
97e1c18e 233 hlist_del(&e->hlist);
97e1c18e 234 kfree(e);
97e1c18e
MD
235}
236
237/*
238 * Sets the probe callback corresponding to one tracepoint.
239 */
240static void set_tracepoint(struct tracepoint_entry **entry,
241 struct tracepoint *elem, int active)
242{
243 WARN_ON(strcmp((*entry)->name, elem->name) != 0);
244
245 /*
246 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
247 * probe callbacks array is consistent before setting a pointer to it.
248 * This array is referenced by __DO_TRACE from
249 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
250 * is used.
251 */
252 rcu_assign_pointer(elem->funcs, (*entry)->funcs);
253 elem->state = active;
254}
255
256/*
257 * Disable a tracepoint and its probe callback.
258 * Note: only waiting an RCU period after setting elem->call to the empty
259 * function insures that the original callback is not used anymore. This insured
260 * by preempt_disable around the call site.
261 */
262static void disable_tracepoint(struct tracepoint *elem)
263{
264 elem->state = 0;
de0baf9a 265 rcu_assign_pointer(elem->funcs, NULL);
97e1c18e
MD
266}
267
268/**
269 * tracepoint_update_probe_range - Update a probe range
270 * @begin: beginning of the range
271 * @end: end of the range
272 *
273 * Updates the probe callback corresponding to a range of tracepoints.
274 */
275void tracepoint_update_probe_range(struct tracepoint *begin,
276 struct tracepoint *end)
277{
278 struct tracepoint *iter;
279 struct tracepoint_entry *mark_entry;
280
281 mutex_lock(&tracepoints_mutex);
282 for (iter = begin; iter < end; iter++) {
283 mark_entry = get_tracepoint(iter->name);
284 if (mark_entry) {
285 set_tracepoint(&mark_entry, iter,
286 !!mark_entry->refcount);
287 } else {
288 disable_tracepoint(iter);
289 }
290 }
291 mutex_unlock(&tracepoints_mutex);
292}
293
294/*
295 * Update probes, removing the faulty probes.
296 */
297static void tracepoint_update_probes(void)
298{
299 /* Core kernel tracepoints */
300 tracepoint_update_probe_range(__start___tracepoints,
301 __stop___tracepoints);
302 /* tracepoints in modules. */
303 module_update_tracepoints();
304}
305
127cafbb
LJ
306static void *tracepoint_add_probe(const char *name, void *probe)
307{
308 struct tracepoint_entry *entry;
309 void *old;
310
311 entry = get_tracepoint(name);
312 if (!entry) {
313 entry = add_tracepoint(name);
314 if (IS_ERR(entry))
315 return entry;
316 }
317 old = tracepoint_entry_add_probe(entry, probe);
318 if (IS_ERR(old) && !entry->refcount)
319 remove_tracepoint(entry);
320 return old;
321}
322
97e1c18e
MD
323/**
324 * tracepoint_probe_register - Connect a probe to a tracepoint
325 * @name: tracepoint name
326 * @probe: probe handler
327 *
328 * Returns 0 if ok, error value on error.
329 * The probe address must at least be aligned on the architecture pointer size.
330 */
331int tracepoint_probe_register(const char *name, void *probe)
332{
97e1c18e
MD
333 void *old;
334
335 mutex_lock(&tracepoints_mutex);
127cafbb 336 old = tracepoint_add_probe(name, probe);
97e1c18e 337 mutex_unlock(&tracepoints_mutex);
127cafbb
LJ
338 if (IS_ERR(old))
339 return PTR_ERR(old);
340
97e1c18e 341 tracepoint_update_probes(); /* may update entry */
19dba33c
LJ
342 release_probes(old);
343 return 0;
97e1c18e
MD
344}
345EXPORT_SYMBOL_GPL(tracepoint_probe_register);
346
127cafbb
LJ
347static void *tracepoint_remove_probe(const char *name, void *probe)
348{
349 struct tracepoint_entry *entry;
350 void *old;
351
352 entry = get_tracepoint(name);
353 if (!entry)
354 return ERR_PTR(-ENOENT);
355 old = tracepoint_entry_remove_probe(entry, probe);
356 if (IS_ERR(old))
357 return old;
358 if (!entry->refcount)
359 remove_tracepoint(entry);
360 return old;
361}
362
97e1c18e
MD
363/**
364 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
365 * @name: tracepoint name
366 * @probe: probe function pointer
367 *
368 * We do not need to call a synchronize_sched to make sure the probes have
369 * finished running before doing a module unload, because the module unload
370 * itself uses stop_machine(), which insures that every preempt disabled section
371 * have finished.
372 */
373int tracepoint_probe_unregister(const char *name, void *probe)
374{
97e1c18e 375 void *old;
97e1c18e
MD
376
377 mutex_lock(&tracepoints_mutex);
127cafbb 378 old = tracepoint_remove_probe(name, probe);
97e1c18e 379 mutex_unlock(&tracepoints_mutex);
127cafbb
LJ
380 if (IS_ERR(old))
381 return PTR_ERR(old);
382
97e1c18e 383 tracepoint_update_probes(); /* may update entry */
19dba33c
LJ
384 release_probes(old);
385 return 0;
97e1c18e
MD
386}
387EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
388
127cafbb
LJ
389static LIST_HEAD(old_probes);
390static int need_update;
391
392static void tracepoint_add_old_probes(void *old)
393{
394 need_update = 1;
395 if (old) {
396 struct tp_probes *tp_probes = container_of(old,
397 struct tp_probes, probes[0]);
398 list_add(&tp_probes->u.list, &old_probes);
399 }
400}
401
402/**
403 * tracepoint_probe_register_noupdate - register a probe but not connect
404 * @name: tracepoint name
405 * @probe: probe handler
406 *
407 * caller must call tracepoint_probe_update_all()
408 */
409int tracepoint_probe_register_noupdate(const char *name, void *probe)
410{
411 void *old;
412
413 mutex_lock(&tracepoints_mutex);
414 old = tracepoint_add_probe(name, probe);
415 if (IS_ERR(old)) {
416 mutex_unlock(&tracepoints_mutex);
417 return PTR_ERR(old);
418 }
419 tracepoint_add_old_probes(old);
420 mutex_unlock(&tracepoints_mutex);
421 return 0;
422}
423EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
424
425/**
426 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
427 * @name: tracepoint name
428 * @probe: probe function pointer
429 *
430 * caller must call tracepoint_probe_update_all()
431 */
432int tracepoint_probe_unregister_noupdate(const char *name, void *probe)
433{
434 void *old;
435
436 mutex_lock(&tracepoints_mutex);
437 old = tracepoint_remove_probe(name, probe);
438 if (IS_ERR(old)) {
439 mutex_unlock(&tracepoints_mutex);
440 return PTR_ERR(old);
441 }
442 tracepoint_add_old_probes(old);
443 mutex_unlock(&tracepoints_mutex);
444 return 0;
445}
446EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
447
448/**
449 * tracepoint_probe_update_all - update tracepoints
450 */
451void tracepoint_probe_update_all(void)
452{
453 LIST_HEAD(release_probes);
454 struct tp_probes *pos, *next;
455
456 mutex_lock(&tracepoints_mutex);
457 if (!need_update) {
458 mutex_unlock(&tracepoints_mutex);
459 return;
460 }
461 if (!list_empty(&old_probes))
462 list_replace_init(&old_probes, &release_probes);
463 need_update = 0;
464 mutex_unlock(&tracepoints_mutex);
465
466 tracepoint_update_probes();
467 list_for_each_entry_safe(pos, next, &release_probes, u.list) {
468 list_del(&pos->u.list);
469 call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
470 }
471}
472EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
473
97e1c18e
MD
474/**
475 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
476 * @tracepoint: current tracepoints (in), next tracepoint (out)
477 * @begin: beginning of the range
478 * @end: end of the range
479 *
480 * Returns whether a next tracepoint has been found (1) or not (0).
481 * Will return the first tracepoint in the range if the input tracepoint is
482 * NULL.
483 */
484int tracepoint_get_iter_range(struct tracepoint **tracepoint,
485 struct tracepoint *begin, struct tracepoint *end)
486{
487 if (!*tracepoint && begin != end) {
488 *tracepoint = begin;
489 return 1;
490 }
491 if (*tracepoint >= begin && *tracepoint < end)
492 return 1;
493 return 0;
494}
495EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
496
497static void tracepoint_get_iter(struct tracepoint_iter *iter)
498{
499 int found = 0;
500
501 /* Core kernel tracepoints */
502 if (!iter->module) {
503 found = tracepoint_get_iter_range(&iter->tracepoint,
504 __start___tracepoints, __stop___tracepoints);
505 if (found)
506 goto end;
507 }
508 /* tracepoints in modules. */
509 found = module_get_iter_tracepoints(iter);
510end:
511 if (!found)
512 tracepoint_iter_reset(iter);
513}
514
515void tracepoint_iter_start(struct tracepoint_iter *iter)
516{
517 tracepoint_get_iter(iter);
518}
519EXPORT_SYMBOL_GPL(tracepoint_iter_start);
520
521void tracepoint_iter_next(struct tracepoint_iter *iter)
522{
523 iter->tracepoint++;
524 /*
525 * iter->tracepoint may be invalid because we blindly incremented it.
526 * Make sure it is valid by marshalling on the tracepoints, getting the
527 * tracepoints from following modules if necessary.
528 */
529 tracepoint_get_iter(iter);
530}
531EXPORT_SYMBOL_GPL(tracepoint_iter_next);
532
533void tracepoint_iter_stop(struct tracepoint_iter *iter)
534{
535}
536EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
537
538void tracepoint_iter_reset(struct tracepoint_iter *iter)
539{
540 iter->module = NULL;
541 iter->tracepoint = NULL;
542}
543EXPORT_SYMBOL_GPL(tracepoint_iter_reset);