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 //ust// #include <linux/module.h>
19 //ust// #include <linux/mutex.h>
20 //ust// #include <linux/types.h>
21 //ust// #include <linux/jhash.h>
22 //ust// #include <linux/list.h>
23 //ust// #include <linux/rcupdate.h>
24 //ust// #include <linux/tracepoint.h>
25 //ust// #include <linux/err.h>
26 //ust// #include <linux/slab.h>
27 //ust// #include <linux/immediate.h>
31 #include "kernelcompat.h"
32 #include "tracepoint.h"
36 //extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
37 //extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
39 /* Set to 1 to enable tracepoint debug output */
40 static const int tracepoint_debug
;
42 /* libraries that contain tracepoints (struct tracepoint_lib) */
43 static LIST_HEAD(libs
);
46 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
47 * builtin and module tracepoints and the hash table.
49 static DEFINE_MUTEX(tracepoints_mutex
);
52 * Tracepoint hash table, containing the active tracepoints.
53 * Protected by tracepoints_mutex.
55 #define TRACEPOINT_HASH_BITS 6
56 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
57 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
61 * It is used to to delay the free of multiple probes array until a quiescent
63 * Tracepoint entries modifications are protected by the tracepoints_mutex.
65 struct tracepoint_entry
{
66 struct hlist_node hlist
;
68 int refcount
; /* Number of times armed. 0 if disarmed. */
74 //ust// struct rcu_head rcu;
75 struct list_head list
;
80 static inline void *allocate_probes(int count
)
82 struct tp_probes
*p
= kmalloc(count
* sizeof(void *)
83 + sizeof(struct tp_probes
), GFP_KERNEL
);
84 return p
== NULL
? NULL
: p
->probes
;
87 //ust// static void rcu_free_old_probes(struct rcu_head *head)
89 //ust// kfree(container_of(head, struct tp_probes, u.rcu));
92 static inline void release_probes(void *old
)
95 struct tp_probes
*tp_probes
= container_of(old
,
96 struct tp_probes
, probes
[0]);
97 //ust// call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
103 static void debug_print_probes(struct tracepoint_entry
*entry
)
107 if (!tracepoint_debug
|| !entry
->funcs
)
110 for (i
= 0; entry
->funcs
[i
]; i
++)
111 printk(KERN_DEBUG
"Probe %d : %p\n", i
, entry
->funcs
[i
]);
115 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
122 debug_print_probes(entry
);
125 /* (N -> N+1), (N != 0, 1) probes */
126 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++)
127 if (old
[nr_probes
] == probe
)
128 return ERR_PTR(-EEXIST
);
130 /* + 2 : one for new probe, one for NULL func */
131 new = allocate_probes(nr_probes
+ 2);
133 return ERR_PTR(-ENOMEM
);
135 memcpy(new, old
, nr_probes
* sizeof(void *));
136 new[nr_probes
] = probe
;
137 new[nr_probes
+ 1] = NULL
;
138 entry
->refcount
= nr_probes
+ 1;
140 debug_print_probes(entry
);
145 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
147 int nr_probes
= 0, nr_del
= 0, i
;
153 return ERR_PTR(-ENOENT
);
155 debug_print_probes(entry
);
156 /* (N -> M), (N > 1, M >= 0) probes */
157 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++) {
158 if ((!probe
|| old
[nr_probes
] == probe
))
162 if (nr_probes
- nr_del
== 0) {
163 /* N -> 0, (N > 1) */
166 debug_print_probes(entry
);
170 /* N -> M, (N > 1, M > 0) */
172 new = allocate_probes(nr_probes
- nr_del
+ 1);
174 return ERR_PTR(-ENOMEM
);
175 for (i
= 0; old
[i
]; i
++)
176 if ((probe
&& old
[i
] != probe
))
178 new[nr_probes
- nr_del
] = NULL
;
179 entry
->refcount
= nr_probes
- nr_del
;
182 debug_print_probes(entry
);
187 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
188 * Must be called with tracepoints_mutex held.
189 * Returns NULL if not present.
191 static struct tracepoint_entry
*get_tracepoint(const char *name
)
193 struct hlist_head
*head
;
194 struct hlist_node
*node
;
195 struct tracepoint_entry
*e
;
196 u32 hash
= jhash(name
, strlen(name
), 0);
198 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
199 hlist_for_each_entry(e
, node
, head
, hlist
) {
200 if (!strcmp(name
, e
->name
))
207 * Add the tracepoint to the tracepoint hash table. Must be called with
208 * tracepoints_mutex held.
210 static struct tracepoint_entry
*add_tracepoint(const char *name
)
212 struct hlist_head
*head
;
213 struct hlist_node
*node
;
214 struct tracepoint_entry
*e
;
215 size_t name_len
= strlen(name
) + 1;
216 u32 hash
= jhash(name
, name_len
-1, 0);
218 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
219 hlist_for_each_entry(e
, node
, head
, hlist
) {
220 if (!strcmp(name
, e
->name
)) {
222 "tracepoint %s busy\n", name
);
223 return ERR_PTR(-EEXIST
); /* Already there */
227 * Using kmalloc here to allocate a variable length element. Could
228 * cause some memory fragmentation if overused.
230 e
= kmalloc(sizeof(struct tracepoint_entry
) + name_len
, GFP_KERNEL
);
232 return ERR_PTR(-ENOMEM
);
233 memcpy(&e
->name
[0], name
, name_len
);
236 hlist_add_head(&e
->hlist
, head
);
241 * Remove the tracepoint from the tracepoint hash table. Must be called with
244 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
246 hlist_del(&e
->hlist
);
251 * Sets the probe callback corresponding to one tracepoint.
253 static void set_tracepoint(struct tracepoint_entry
**entry
,
254 struct tracepoint
*elem
, int active
)
256 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
259 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
260 * probe callbacks array is consistent before setting a pointer to it.
261 * This array is referenced by __DO_TRACE from
262 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
265 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
266 elem
->state__imv
= active
;
270 * Disable a tracepoint and its probe callback.
271 * Note: only waiting an RCU period after setting elem->call to the empty
272 * function insures that the original callback is not used anymore. This insured
273 * by preempt_disable around the call site.
275 static void disable_tracepoint(struct tracepoint
*elem
)
277 elem
->state__imv
= 0;
278 rcu_assign_pointer(elem
->funcs
, NULL
);
282 * tracepoint_update_probe_range - Update a probe range
283 * @begin: beginning of the range
284 * @end: end of the range
286 * Updates the probe callback corresponding to a range of tracepoints.
288 void tracepoint_update_probe_range(struct tracepoint
*begin
,
289 struct tracepoint
*end
)
291 struct tracepoint
*iter
;
292 struct tracepoint_entry
*mark_entry
;
294 mutex_lock(&tracepoints_mutex
);
295 for (iter
= begin
; iter
< end
; iter
++) {
296 mark_entry
= get_tracepoint(iter
->name
);
298 set_tracepoint(&mark_entry
, iter
,
299 !!mark_entry
->refcount
);
301 disable_tracepoint(iter
);
304 mutex_unlock(&tracepoints_mutex
);
308 * Update probes, removing the faulty probes.
310 static void tracepoint_update_probes(void)
312 /* Core kernel tracepoints */
313 //ust// tracepoint_update_probe_range(__start___tracepoints,
314 //ust// __stop___tracepoints);
315 /* tracepoints in modules. */
316 lib_update_tracepoints();
317 /* Update immediate values */
319 //ust// module_imv_update();
322 static void *tracepoint_add_probe(const char *name
, void *probe
)
324 struct tracepoint_entry
*entry
;
327 entry
= get_tracepoint(name
);
329 entry
= add_tracepoint(name
);
333 old
= tracepoint_entry_add_probe(entry
, probe
);
334 if (IS_ERR(old
) && !entry
->refcount
)
335 remove_tracepoint(entry
);
340 * tracepoint_probe_register - Connect a probe to a tracepoint
341 * @name: tracepoint name
342 * @probe: probe handler
344 * Returns 0 if ok, error value on error.
345 * The probe address must at least be aligned on the architecture pointer size.
347 int tracepoint_probe_register(const char *name
, void *probe
)
351 mutex_lock(&tracepoints_mutex
);
352 old
= tracepoint_add_probe(name
, probe
);
353 mutex_unlock(&tracepoints_mutex
);
357 tracepoint_update_probes(); /* may update entry */
361 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
363 static void *tracepoint_remove_probe(const char *name
, void *probe
)
365 struct tracepoint_entry
*entry
;
368 entry
= get_tracepoint(name
);
370 return ERR_PTR(-ENOENT
);
371 old
= tracepoint_entry_remove_probe(entry
, probe
);
374 if (!entry
->refcount
)
375 remove_tracepoint(entry
);
380 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
381 * @name: tracepoint name
382 * @probe: probe function pointer
384 * We do not need to call a synchronize_sched to make sure the probes have
385 * finished running before doing a module unload, because the module unload
386 * itself uses stop_machine(), which insures that every preempt disabled section
389 int tracepoint_probe_unregister(const char *name
, void *probe
)
393 mutex_lock(&tracepoints_mutex
);
394 old
= tracepoint_remove_probe(name
, probe
);
395 mutex_unlock(&tracepoints_mutex
);
399 tracepoint_update_probes(); /* may update entry */
403 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
405 static LIST_HEAD(old_probes
);
406 static int need_update
;
408 static void tracepoint_add_old_probes(void *old
)
412 struct tp_probes
*tp_probes
= container_of(old
,
413 struct tp_probes
, probes
[0]);
414 list_add(&tp_probes
->u
.list
, &old_probes
);
419 * tracepoint_probe_register_noupdate - register a probe but not connect
420 * @name: tracepoint name
421 * @probe: probe handler
423 * caller must call tracepoint_probe_update_all()
425 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
429 mutex_lock(&tracepoints_mutex
);
430 old
= tracepoint_add_probe(name
, probe
);
432 mutex_unlock(&tracepoints_mutex
);
435 tracepoint_add_old_probes(old
);
436 mutex_unlock(&tracepoints_mutex
);
439 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
442 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
443 * @name: tracepoint name
444 * @probe: probe function pointer
446 * caller must call tracepoint_probe_update_all()
448 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
452 mutex_lock(&tracepoints_mutex
);
453 old
= tracepoint_remove_probe(name
, probe
);
455 mutex_unlock(&tracepoints_mutex
);
458 tracepoint_add_old_probes(old
);
459 mutex_unlock(&tracepoints_mutex
);
462 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
465 * tracepoint_probe_update_all - update tracepoints
467 void tracepoint_probe_update_all(void)
469 LIST_HEAD(release_probes
);
470 struct tp_probes
*pos
, *next
;
472 mutex_lock(&tracepoints_mutex
);
474 mutex_unlock(&tracepoints_mutex
);
477 if (!list_empty(&old_probes
))
478 list_replace_init(&old_probes
, &release_probes
);
480 mutex_unlock(&tracepoints_mutex
);
482 tracepoint_update_probes();
483 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
484 list_del(&pos
->u
.list
);
485 //ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
490 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
493 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
494 * @tracepoint: current tracepoints (in), next tracepoint (out)
495 * @begin: beginning of the range
496 * @end: end of the range
498 * Returns whether a next tracepoint has been found (1) or not (0).
499 * Will return the first tracepoint in the range if the input tracepoint is
502 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
503 struct tracepoint
*begin
, struct tracepoint
*end
)
505 if (!*tracepoint
&& begin
!= end
) {
509 if (*tracepoint
>= begin
&& *tracepoint
< end
)
513 //ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
515 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
519 //ust// /* Core kernel tracepoints */
520 //ust// if (!iter->module) {
521 //ust// found = tracepoint_get_iter_range(&iter->tracepoint,
522 //ust// __start___tracepoints, __stop___tracepoints);
526 /* tracepoints in libs. */
527 found
= lib_get_iter_tracepoints(iter
);
530 tracepoint_iter_reset(iter
);
533 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
535 tracepoint_get_iter(iter
);
537 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
539 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
543 * iter->tracepoint may be invalid because we blindly incremented it.
544 * Make sure it is valid by marshalling on the tracepoints, getting the
545 * tracepoints from following modules if necessary.
547 tracepoint_get_iter(iter
);
549 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
551 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
554 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
556 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
558 //ust// iter->module = NULL;
559 iter
->tracepoint
= NULL
;
561 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
563 //ust// #ifdef CONFIG_MODULES
565 //ust// int tracepoint_module_notify(struct notifier_block *self,
566 //ust// unsigned long val, void *data)
568 //ust// struct module *mod = data;
570 //ust// switch (val) {
571 //ust// case MODULE_STATE_COMING:
572 //ust// tracepoint_update_probe_range(mod->tracepoints,
573 //ust// mod->tracepoints + mod->num_tracepoints);
575 //ust// case MODULE_STATE_GOING:
576 //ust// tracepoint_update_probe_range(mod->tracepoints,
577 //ust// mod->tracepoints + mod->num_tracepoints);
583 //ust// struct notifier_block tracepoint_module_nb = {
584 //ust// .notifier_call = tracepoint_module_notify,
585 //ust// .priority = 0,
588 //ust// static int init_tracepoints(void)
590 //ust// return register_module_notifier(&tracepoint_module_nb);
592 //ust// __initcall(init_tracepoints);
594 //ust// #endif /* CONFIG_MODULES */
597 * Returns 0 if current not found.
598 * Returns 1 if current found.
600 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
602 struct tracepoint_lib
*iter_lib
;
605 //ust// mutex_lock(&module_mutex);
606 list_for_each_entry(iter_lib
, &libs
, list
) {
607 if (iter_lib
< iter
->lib
)
609 else if (iter_lib
> iter
->lib
)
610 iter
->tracepoint
= NULL
;
611 found
= marker_get_iter_range(&iter
->tracepoint
,
612 iter_lib
->tracepoints_start
,
613 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
615 iter
->lib
= iter_lib
;
619 //ust// mutex_unlock(&module_mutex);
623 void lib_update_tracepoints(void)
625 struct tracepoint_lib
*lib
;
627 //ust// mutex_lock(&module_mutex);
628 list_for_each_entry(lib
, &libs
, list
)
629 tracepoint_update_probe_range(lib
->tracepoints_start
,
630 lib
->tracepoints_start
+ lib
->tracepoints_count
);
631 //ust// mutex_unlock(&module_mutex);
634 static void (*new_tracepoint_cb
)(struct tracepoint
*) = NULL
;
636 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
638 new_tracepoint_cb
= cb
;
641 static void new_tracepoints(struct tracepoint
*start
, struct tracepoint
*end
)
643 if(new_tracepoint_cb
) {
644 struct tracepoint
*t
;
645 for(t
=start
; t
< end
; t
++) {
646 new_tracepoint_cb(t
);
651 int tracepoint_register_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
653 struct tracepoint_lib
*pl
;
655 pl
= (struct tracepoint_lib
*) malloc(sizeof(struct tracepoint_lib
));
657 pl
->tracepoints_start
= tracepoints_start
;
658 pl
->tracepoints_count
= tracepoints_count
;
660 /* FIXME: maybe protect this with its own mutex? */
661 mutex_lock(&tracepoints_mutex
);
662 list_add(&pl
->list
, &libs
);
663 mutex_unlock(&tracepoints_mutex
);
665 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
667 /* FIXME: update just the loaded lib */
668 lib_update_tracepoints();
670 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start
, tracepoints_count
);
675 int tracepoint_unregister_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
677 /*FIXME: implement; but before implementing, tracepoint_register_lib must
678 have appropriate locking. */