2 * Copyright (C) 2008 Mathieu Desnoyers
3 * Copyright (C) 2009 Pierre-Marc Fournier
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * Ported to userspace by Pierre-Marc Fournier.
22 //ust// #include <linux/module.h>
23 //ust// #include <linux/mutex.h>
24 //ust// #include <linux/types.h>
25 //ust// #include <linux/jhash.h>
26 //ust// #include <linux/list.h>
27 //ust// #include <linux/rcupdate.h>
28 //ust// #include <linux/tracepoint.h>
29 //ust// #include <linux/err.h>
30 //ust// #include <linux/slab.h>
31 //ust// #include <linux/immediate.h>
35 #include "kernelcompat.h"
36 #include "tracepoint.h"
41 //extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
42 //extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
44 /* Set to 1 to enable tracepoint debug output */
45 static const int tracepoint_debug
;
47 /* libraries that contain tracepoints (struct tracepoint_lib) */
48 static LIST_HEAD(libs
);
51 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
52 * builtin and module tracepoints and the hash table.
54 static DEFINE_MUTEX(tracepoints_mutex
);
57 * Tracepoint hash table, containing the active tracepoints.
58 * Protected by tracepoints_mutex.
60 #define TRACEPOINT_HASH_BITS 6
61 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
62 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
66 * It is used to to delay the free of multiple probes array until a quiescent
68 * Tracepoint entries modifications are protected by the tracepoints_mutex.
70 struct tracepoint_entry
{
71 struct hlist_node hlist
;
73 int refcount
; /* Number of times armed. 0 if disarmed. */
79 //ust// struct rcu_head rcu;
80 struct list_head list
;
85 static inline void *allocate_probes(int count
)
87 struct tp_probes
*p
= kmalloc(count
* sizeof(void *)
88 + sizeof(struct tp_probes
), GFP_KERNEL
);
89 return p
== NULL
? NULL
: p
->probes
;
92 //ust// static void rcu_free_old_probes(struct rcu_head *head)
94 //ust// kfree(container_of(head, struct tp_probes, u.rcu));
97 static inline void release_probes(void *old
)
100 struct tp_probes
*tp_probes
= container_of(old
,
101 struct tp_probes
, probes
[0]);
102 //ust// call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
108 static void debug_print_probes(struct tracepoint_entry
*entry
)
112 if (!tracepoint_debug
|| !entry
->funcs
)
115 for (i
= 0; entry
->funcs
[i
]; i
++)
116 printk(KERN_DEBUG
"Probe %d : %p\n", i
, entry
->funcs
[i
]);
120 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
127 debug_print_probes(entry
);
130 /* (N -> N+1), (N != 0, 1) probes */
131 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++)
132 if (old
[nr_probes
] == probe
)
133 return ERR_PTR(-EEXIST
);
135 /* + 2 : one for new probe, one for NULL func */
136 new = allocate_probes(nr_probes
+ 2);
138 return ERR_PTR(-ENOMEM
);
140 memcpy(new, old
, nr_probes
* sizeof(void *));
141 new[nr_probes
] = probe
;
142 new[nr_probes
+ 1] = NULL
;
143 entry
->refcount
= nr_probes
+ 1;
145 debug_print_probes(entry
);
150 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
152 int nr_probes
= 0, nr_del
= 0, i
;
158 return ERR_PTR(-ENOENT
);
160 debug_print_probes(entry
);
161 /* (N -> M), (N > 1, M >= 0) probes */
162 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++) {
163 if ((!probe
|| old
[nr_probes
] == probe
))
167 if (nr_probes
- nr_del
== 0) {
168 /* N -> 0, (N > 1) */
171 debug_print_probes(entry
);
175 /* N -> M, (N > 1, M > 0) */
177 new = allocate_probes(nr_probes
- nr_del
+ 1);
179 return ERR_PTR(-ENOMEM
);
180 for (i
= 0; old
[i
]; i
++)
181 if ((probe
&& old
[i
] != probe
))
183 new[nr_probes
- nr_del
] = NULL
;
184 entry
->refcount
= nr_probes
- nr_del
;
187 debug_print_probes(entry
);
192 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
193 * Must be called with tracepoints_mutex held.
194 * Returns NULL if not present.
196 static struct tracepoint_entry
*get_tracepoint(const char *name
)
198 struct hlist_head
*head
;
199 struct hlist_node
*node
;
200 struct tracepoint_entry
*e
;
201 u32 hash
= jhash(name
, strlen(name
), 0);
203 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
204 hlist_for_each_entry(e
, node
, head
, hlist
) {
205 if (!strcmp(name
, e
->name
))
212 * Add the tracepoint to the tracepoint hash table. Must be called with
213 * tracepoints_mutex held.
215 static struct tracepoint_entry
*add_tracepoint(const char *name
)
217 struct hlist_head
*head
;
218 struct hlist_node
*node
;
219 struct tracepoint_entry
*e
;
220 size_t name_len
= strlen(name
) + 1;
221 u32 hash
= jhash(name
, name_len
-1, 0);
223 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
224 hlist_for_each_entry(e
, node
, head
, hlist
) {
225 if (!strcmp(name
, e
->name
)) {
227 "tracepoint %s busy\n", name
);
228 return ERR_PTR(-EEXIST
); /* Already there */
232 * Using kmalloc here to allocate a variable length element. Could
233 * cause some memory fragmentation if overused.
235 e
= kmalloc(sizeof(struct tracepoint_entry
) + name_len
, GFP_KERNEL
);
237 return ERR_PTR(-ENOMEM
);
238 memcpy(&e
->name
[0], name
, name_len
);
241 hlist_add_head(&e
->hlist
, head
);
246 * Remove the tracepoint from the tracepoint hash table. Must be called with
249 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
251 hlist_del(&e
->hlist
);
256 * Sets the probe callback corresponding to one tracepoint.
258 static void set_tracepoint(struct tracepoint_entry
**entry
,
259 struct tracepoint
*elem
, int active
)
261 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
264 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
265 * probe callbacks array is consistent before setting a pointer to it.
266 * This array is referenced by __DO_TRACE from
267 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
270 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
271 elem
->state__imv
= active
;
275 * Disable a tracepoint and its probe callback.
276 * Note: only waiting an RCU period after setting elem->call to the empty
277 * function insures that the original callback is not used anymore. This insured
278 * by preempt_disable around the call site.
280 static void disable_tracepoint(struct tracepoint
*elem
)
282 elem
->state__imv
= 0;
283 rcu_assign_pointer(elem
->funcs
, NULL
);
287 * tracepoint_update_probe_range - Update a probe range
288 * @begin: beginning of the range
289 * @end: end of the range
291 * Updates the probe callback corresponding to a range of tracepoints.
293 void tracepoint_update_probe_range(struct tracepoint
*begin
,
294 struct tracepoint
*end
)
296 struct tracepoint
*iter
;
297 struct tracepoint_entry
*mark_entry
;
299 mutex_lock(&tracepoints_mutex
);
300 for (iter
= begin
; iter
< end
; iter
++) {
301 mark_entry
= get_tracepoint(iter
->name
);
303 set_tracepoint(&mark_entry
, iter
,
304 !!mark_entry
->refcount
);
306 disable_tracepoint(iter
);
309 mutex_unlock(&tracepoints_mutex
);
313 * Update probes, removing the faulty probes.
315 static void tracepoint_update_probes(void)
317 /* Core kernel tracepoints */
318 //ust// tracepoint_update_probe_range(__start___tracepoints,
319 //ust// __stop___tracepoints);
320 /* tracepoints in modules. */
321 lib_update_tracepoints();
322 /* Update immediate values */
324 //ust// module_imv_update();
327 static void *tracepoint_add_probe(const char *name
, void *probe
)
329 struct tracepoint_entry
*entry
;
332 entry
= get_tracepoint(name
);
334 entry
= add_tracepoint(name
);
338 old
= tracepoint_entry_add_probe(entry
, probe
);
339 if (IS_ERR(old
) && !entry
->refcount
)
340 remove_tracepoint(entry
);
345 * tracepoint_probe_register - Connect a probe to a tracepoint
346 * @name: tracepoint name
347 * @probe: probe handler
349 * Returns 0 if ok, error value on error.
350 * The probe address must at least be aligned on the architecture pointer size.
352 int tracepoint_probe_register(const char *name
, void *probe
)
356 mutex_lock(&tracepoints_mutex
);
357 old
= tracepoint_add_probe(name
, probe
);
358 mutex_unlock(&tracepoints_mutex
);
362 tracepoint_update_probes(); /* may update entry */
366 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
368 static void *tracepoint_remove_probe(const char *name
, void *probe
)
370 struct tracepoint_entry
*entry
;
373 entry
= get_tracepoint(name
);
375 return ERR_PTR(-ENOENT
);
376 old
= tracepoint_entry_remove_probe(entry
, probe
);
379 if (!entry
->refcount
)
380 remove_tracepoint(entry
);
385 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
386 * @name: tracepoint name
387 * @probe: probe function pointer
389 * We do not need to call a synchronize_sched to make sure the probes have
390 * finished running before doing a module unload, because the module unload
391 * itself uses stop_machine(), which insures that every preempt disabled section
394 int tracepoint_probe_unregister(const char *name
, void *probe
)
398 mutex_lock(&tracepoints_mutex
);
399 old
= tracepoint_remove_probe(name
, probe
);
400 mutex_unlock(&tracepoints_mutex
);
404 tracepoint_update_probes(); /* may update entry */
408 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
410 static LIST_HEAD(old_probes
);
411 static int need_update
;
413 static void tracepoint_add_old_probes(void *old
)
417 struct tp_probes
*tp_probes
= container_of(old
,
418 struct tp_probes
, probes
[0]);
419 list_add(&tp_probes
->u
.list
, &old_probes
);
424 * tracepoint_probe_register_noupdate - register a probe but not connect
425 * @name: tracepoint name
426 * @probe: probe handler
428 * caller must call tracepoint_probe_update_all()
430 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
434 mutex_lock(&tracepoints_mutex
);
435 old
= tracepoint_add_probe(name
, probe
);
437 mutex_unlock(&tracepoints_mutex
);
440 tracepoint_add_old_probes(old
);
441 mutex_unlock(&tracepoints_mutex
);
444 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
447 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
448 * @name: tracepoint name
449 * @probe: probe function pointer
451 * caller must call tracepoint_probe_update_all()
453 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
457 mutex_lock(&tracepoints_mutex
);
458 old
= tracepoint_remove_probe(name
, probe
);
460 mutex_unlock(&tracepoints_mutex
);
463 tracepoint_add_old_probes(old
);
464 mutex_unlock(&tracepoints_mutex
);
467 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
470 * tracepoint_probe_update_all - update tracepoints
472 void tracepoint_probe_update_all(void)
474 LIST_HEAD(release_probes
);
475 struct tp_probes
*pos
, *next
;
477 mutex_lock(&tracepoints_mutex
);
479 mutex_unlock(&tracepoints_mutex
);
482 if (!list_empty(&old_probes
))
483 list_replace_init(&old_probes
, &release_probes
);
485 mutex_unlock(&tracepoints_mutex
);
487 tracepoint_update_probes();
488 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
489 list_del(&pos
->u
.list
);
490 //ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
495 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
498 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
499 * @tracepoint: current tracepoints (in), next tracepoint (out)
500 * @begin: beginning of the range
501 * @end: end of the range
503 * Returns whether a next tracepoint has been found (1) or not (0).
504 * Will return the first tracepoint in the range if the input tracepoint is
507 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
508 struct tracepoint
*begin
, struct tracepoint
*end
)
510 if (!*tracepoint
&& begin
!= end
) {
514 if (*tracepoint
>= begin
&& *tracepoint
< end
)
518 //ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
520 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
524 //ust// /* Core kernel tracepoints */
525 //ust// if (!iter->module) {
526 //ust// found = tracepoint_get_iter_range(&iter->tracepoint,
527 //ust// __start___tracepoints, __stop___tracepoints);
531 /* tracepoints in libs. */
532 found
= lib_get_iter_tracepoints(iter
);
535 tracepoint_iter_reset(iter
);
538 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
540 tracepoint_get_iter(iter
);
542 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
544 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
548 * iter->tracepoint may be invalid because we blindly incremented it.
549 * Make sure it is valid by marshalling on the tracepoints, getting the
550 * tracepoints from following modules if necessary.
552 tracepoint_get_iter(iter
);
554 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
556 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
559 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
561 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
563 //ust// iter->module = NULL;
564 iter
->tracepoint
= NULL
;
566 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
568 //ust// #ifdef CONFIG_MODULES
570 //ust// int tracepoint_module_notify(struct notifier_block *self,
571 //ust// unsigned long val, void *data)
573 //ust// struct module *mod = data;
575 //ust// switch (val) {
576 //ust// case MODULE_STATE_COMING:
577 //ust// tracepoint_update_probe_range(mod->tracepoints,
578 //ust// mod->tracepoints + mod->num_tracepoints);
580 //ust// case MODULE_STATE_GOING:
581 //ust// tracepoint_update_probe_range(mod->tracepoints,
582 //ust// mod->tracepoints + mod->num_tracepoints);
588 //ust// struct notifier_block tracepoint_module_nb = {
589 //ust// .notifier_call = tracepoint_module_notify,
590 //ust// .priority = 0,
593 //ust// static int init_tracepoints(void)
595 //ust// return register_module_notifier(&tracepoint_module_nb);
597 //ust// __initcall(init_tracepoints);
599 //ust// #endif /* CONFIG_MODULES */
602 * Returns 0 if current not found.
603 * Returns 1 if current found.
605 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
607 struct tracepoint_lib
*iter_lib
;
610 //ust// mutex_lock(&module_mutex);
611 list_for_each_entry(iter_lib
, &libs
, list
) {
612 if (iter_lib
< iter
->lib
)
614 else if (iter_lib
> iter
->lib
)
615 iter
->tracepoint
= NULL
;
616 found
= marker_get_iter_range(&iter
->tracepoint
,
617 iter_lib
->tracepoints_start
,
618 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
620 iter
->lib
= iter_lib
;
624 //ust// mutex_unlock(&module_mutex);
628 void lib_update_tracepoints(void)
630 struct tracepoint_lib
*lib
;
632 //ust// mutex_lock(&module_mutex);
633 list_for_each_entry(lib
, &libs
, list
)
634 tracepoint_update_probe_range(lib
->tracepoints_start
,
635 lib
->tracepoints_start
+ lib
->tracepoints_count
);
636 //ust// mutex_unlock(&module_mutex);
639 static void (*new_tracepoint_cb
)(struct tracepoint
*) = NULL
;
641 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
643 new_tracepoint_cb
= cb
;
646 static void new_tracepoints(struct tracepoint
*start
, struct tracepoint
*end
)
648 if(new_tracepoint_cb
) {
649 struct tracepoint
*t
;
650 for(t
=start
; t
< end
; t
++) {
651 new_tracepoint_cb(t
);
656 int tracepoint_register_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
658 struct tracepoint_lib
*pl
;
660 pl
= (struct tracepoint_lib
*) malloc(sizeof(struct tracepoint_lib
));
662 pl
->tracepoints_start
= tracepoints_start
;
663 pl
->tracepoints_count
= tracepoints_count
;
665 /* FIXME: maybe protect this with its own mutex? */
666 mutex_lock(&tracepoints_mutex
);
667 list_add(&pl
->list
, &libs
);
668 mutex_unlock(&tracepoints_mutex
);
670 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
672 /* FIXME: update just the loaded lib */
673 lib_update_tracepoints();
675 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start
, tracepoints_count
);
680 int tracepoint_unregister_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
682 /*FIXME: implement; but before implementing, tracepoint_register_lib must
683 have appropriate locking. */