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"
40 //extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
41 //extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
43 /* Set to 1 to enable tracepoint debug output */
44 static const int tracepoint_debug
;
46 /* libraries that contain tracepoints (struct tracepoint_lib) */
47 static LIST_HEAD(libs
);
50 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
51 * builtin and module tracepoints and the hash table.
53 static DEFINE_MUTEX(tracepoints_mutex
);
56 * Tracepoint hash table, containing the active tracepoints.
57 * Protected by tracepoints_mutex.
59 #define TRACEPOINT_HASH_BITS 6
60 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
61 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
65 * It is used to to delay the free of multiple probes array until a quiescent
67 * Tracepoint entries modifications are protected by the tracepoints_mutex.
69 struct tracepoint_entry
{
70 struct hlist_node hlist
;
72 int refcount
; /* Number of times armed. 0 if disarmed. */
78 //ust// struct rcu_head rcu;
79 struct list_head list
;
84 static inline void *allocate_probes(int count
)
86 struct tp_probes
*p
= kmalloc(count
* sizeof(void *)
87 + sizeof(struct tp_probes
), GFP_KERNEL
);
88 return p
== NULL
? NULL
: p
->probes
;
91 //ust// static void rcu_free_old_probes(struct rcu_head *head)
93 //ust// kfree(container_of(head, struct tp_probes, u.rcu));
96 static inline void release_probes(void *old
)
99 struct tp_probes
*tp_probes
= container_of(old
,
100 struct tp_probes
, probes
[0]);
101 //ust// call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
107 static void debug_print_probes(struct tracepoint_entry
*entry
)
111 if (!tracepoint_debug
|| !entry
->funcs
)
114 for (i
= 0; entry
->funcs
[i
]; i
++)
115 printk(KERN_DEBUG
"Probe %d : %p\n", i
, entry
->funcs
[i
]);
119 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
126 debug_print_probes(entry
);
129 /* (N -> N+1), (N != 0, 1) probes */
130 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++)
131 if (old
[nr_probes
] == probe
)
132 return ERR_PTR(-EEXIST
);
134 /* + 2 : one for new probe, one for NULL func */
135 new = allocate_probes(nr_probes
+ 2);
137 return ERR_PTR(-ENOMEM
);
139 memcpy(new, old
, nr_probes
* sizeof(void *));
140 new[nr_probes
] = probe
;
141 new[nr_probes
+ 1] = NULL
;
142 entry
->refcount
= nr_probes
+ 1;
144 debug_print_probes(entry
);
149 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
151 int nr_probes
= 0, nr_del
= 0, i
;
157 return ERR_PTR(-ENOENT
);
159 debug_print_probes(entry
);
160 /* (N -> M), (N > 1, M >= 0) probes */
161 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++) {
162 if ((!probe
|| old
[nr_probes
] == probe
))
166 if (nr_probes
- nr_del
== 0) {
167 /* N -> 0, (N > 1) */
170 debug_print_probes(entry
);
174 /* N -> M, (N > 1, M > 0) */
176 new = allocate_probes(nr_probes
- nr_del
+ 1);
178 return ERR_PTR(-ENOMEM
);
179 for (i
= 0; old
[i
]; i
++)
180 if ((probe
&& old
[i
] != probe
))
182 new[nr_probes
- nr_del
] = NULL
;
183 entry
->refcount
= nr_probes
- nr_del
;
186 debug_print_probes(entry
);
191 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
192 * Must be called with tracepoints_mutex held.
193 * Returns NULL if not present.
195 static struct tracepoint_entry
*get_tracepoint(const char *name
)
197 struct hlist_head
*head
;
198 struct hlist_node
*node
;
199 struct tracepoint_entry
*e
;
200 u32 hash
= jhash(name
, strlen(name
), 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
))
211 * Add the tracepoint to the tracepoint hash table. Must be called with
212 * tracepoints_mutex held.
214 static struct tracepoint_entry
*add_tracepoint(const char *name
)
216 struct hlist_head
*head
;
217 struct hlist_node
*node
;
218 struct tracepoint_entry
*e
;
219 size_t name_len
= strlen(name
) + 1;
220 u32 hash
= jhash(name
, name_len
-1, 0);
222 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
223 hlist_for_each_entry(e
, node
, head
, hlist
) {
224 if (!strcmp(name
, e
->name
)) {
226 "tracepoint %s busy\n", name
);
227 return ERR_PTR(-EEXIST
); /* Already there */
231 * Using kmalloc here to allocate a variable length element. Could
232 * cause some memory fragmentation if overused.
234 e
= kmalloc(sizeof(struct tracepoint_entry
) + name_len
, GFP_KERNEL
);
236 return ERR_PTR(-ENOMEM
);
237 memcpy(&e
->name
[0], name
, name_len
);
240 hlist_add_head(&e
->hlist
, head
);
245 * Remove the tracepoint from the tracepoint hash table. Must be called with
248 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
250 hlist_del(&e
->hlist
);
255 * Sets the probe callback corresponding to one tracepoint.
257 static void set_tracepoint(struct tracepoint_entry
**entry
,
258 struct tracepoint
*elem
, int active
)
260 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
263 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
264 * probe callbacks array is consistent before setting a pointer to it.
265 * This array is referenced by __DO_TRACE from
266 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
269 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
270 elem
->state__imv
= active
;
274 * Disable a tracepoint and its probe callback.
275 * Note: only waiting an RCU period after setting elem->call to the empty
276 * function insures that the original callback is not used anymore. This insured
277 * by preempt_disable around the call site.
279 static void disable_tracepoint(struct tracepoint
*elem
)
281 elem
->state__imv
= 0;
282 rcu_assign_pointer(elem
->funcs
, NULL
);
286 * tracepoint_update_probe_range - Update a probe range
287 * @begin: beginning of the range
288 * @end: end of the range
290 * Updates the probe callback corresponding to a range of tracepoints.
292 void tracepoint_update_probe_range(struct tracepoint
*begin
,
293 struct tracepoint
*end
)
295 struct tracepoint
*iter
;
296 struct tracepoint_entry
*mark_entry
;
298 mutex_lock(&tracepoints_mutex
);
299 for (iter
= begin
; iter
< end
; iter
++) {
300 mark_entry
= get_tracepoint(iter
->name
);
302 set_tracepoint(&mark_entry
, iter
,
303 !!mark_entry
->refcount
);
305 disable_tracepoint(iter
);
308 mutex_unlock(&tracepoints_mutex
);
312 * Update probes, removing the faulty probes.
314 static void tracepoint_update_probes(void)
316 /* Core kernel tracepoints */
317 //ust// tracepoint_update_probe_range(__start___tracepoints,
318 //ust// __stop___tracepoints);
319 /* tracepoints in modules. */
320 lib_update_tracepoints();
321 /* Update immediate values */
323 //ust// module_imv_update();
326 static void *tracepoint_add_probe(const char *name
, void *probe
)
328 struct tracepoint_entry
*entry
;
331 entry
= get_tracepoint(name
);
333 entry
= add_tracepoint(name
);
337 old
= tracepoint_entry_add_probe(entry
, probe
);
338 if (IS_ERR(old
) && !entry
->refcount
)
339 remove_tracepoint(entry
);
344 * tracepoint_probe_register - Connect a probe to a tracepoint
345 * @name: tracepoint name
346 * @probe: probe handler
348 * Returns 0 if ok, error value on error.
349 * The probe address must at least be aligned on the architecture pointer size.
351 int tracepoint_probe_register(const char *name
, void *probe
)
355 mutex_lock(&tracepoints_mutex
);
356 old
= tracepoint_add_probe(name
, probe
);
357 mutex_unlock(&tracepoints_mutex
);
361 tracepoint_update_probes(); /* may update entry */
365 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
367 static void *tracepoint_remove_probe(const char *name
, void *probe
)
369 struct tracepoint_entry
*entry
;
372 entry
= get_tracepoint(name
);
374 return ERR_PTR(-ENOENT
);
375 old
= tracepoint_entry_remove_probe(entry
, probe
);
378 if (!entry
->refcount
)
379 remove_tracepoint(entry
);
384 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
385 * @name: tracepoint name
386 * @probe: probe function pointer
388 * We do not need to call a synchronize_sched to make sure the probes have
389 * finished running before doing a module unload, because the module unload
390 * itself uses stop_machine(), which insures that every preempt disabled section
393 int tracepoint_probe_unregister(const char *name
, void *probe
)
397 mutex_lock(&tracepoints_mutex
);
398 old
= tracepoint_remove_probe(name
, probe
);
399 mutex_unlock(&tracepoints_mutex
);
403 tracepoint_update_probes(); /* may update entry */
407 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
409 static LIST_HEAD(old_probes
);
410 static int need_update
;
412 static void tracepoint_add_old_probes(void *old
)
416 struct tp_probes
*tp_probes
= container_of(old
,
417 struct tp_probes
, probes
[0]);
418 list_add(&tp_probes
->u
.list
, &old_probes
);
423 * tracepoint_probe_register_noupdate - register a probe but not connect
424 * @name: tracepoint name
425 * @probe: probe handler
427 * caller must call tracepoint_probe_update_all()
429 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
433 mutex_lock(&tracepoints_mutex
);
434 old
= tracepoint_add_probe(name
, probe
);
436 mutex_unlock(&tracepoints_mutex
);
439 tracepoint_add_old_probes(old
);
440 mutex_unlock(&tracepoints_mutex
);
443 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
446 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
447 * @name: tracepoint name
448 * @probe: probe function pointer
450 * caller must call tracepoint_probe_update_all()
452 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
456 mutex_lock(&tracepoints_mutex
);
457 old
= tracepoint_remove_probe(name
, probe
);
459 mutex_unlock(&tracepoints_mutex
);
462 tracepoint_add_old_probes(old
);
463 mutex_unlock(&tracepoints_mutex
);
466 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
469 * tracepoint_probe_update_all - update tracepoints
471 void tracepoint_probe_update_all(void)
473 LIST_HEAD(release_probes
);
474 struct tp_probes
*pos
, *next
;
476 mutex_lock(&tracepoints_mutex
);
478 mutex_unlock(&tracepoints_mutex
);
481 if (!list_empty(&old_probes
))
482 list_replace_init(&old_probes
, &release_probes
);
484 mutex_unlock(&tracepoints_mutex
);
486 tracepoint_update_probes();
487 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
488 list_del(&pos
->u
.list
);
489 //ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
494 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
497 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
498 * @tracepoint: current tracepoints (in), next tracepoint (out)
499 * @begin: beginning of the range
500 * @end: end of the range
502 * Returns whether a next tracepoint has been found (1) or not (0).
503 * Will return the first tracepoint in the range if the input tracepoint is
506 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
507 struct tracepoint
*begin
, struct tracepoint
*end
)
509 if (!*tracepoint
&& begin
!= end
) {
513 if (*tracepoint
>= begin
&& *tracepoint
< end
)
517 //ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
519 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
523 //ust// /* Core kernel tracepoints */
524 //ust// if (!iter->module) {
525 //ust// found = tracepoint_get_iter_range(&iter->tracepoint,
526 //ust// __start___tracepoints, __stop___tracepoints);
530 /* tracepoints in libs. */
531 found
= lib_get_iter_tracepoints(iter
);
534 tracepoint_iter_reset(iter
);
537 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
539 tracepoint_get_iter(iter
);
541 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
543 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
547 * iter->tracepoint may be invalid because we blindly incremented it.
548 * Make sure it is valid by marshalling on the tracepoints, getting the
549 * tracepoints from following modules if necessary.
551 tracepoint_get_iter(iter
);
553 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
555 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
558 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
560 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
562 //ust// iter->module = NULL;
563 iter
->tracepoint
= NULL
;
565 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
567 //ust// #ifdef CONFIG_MODULES
569 //ust// int tracepoint_module_notify(struct notifier_block *self,
570 //ust// unsigned long val, void *data)
572 //ust// struct module *mod = data;
574 //ust// switch (val) {
575 //ust// case MODULE_STATE_COMING:
576 //ust// tracepoint_update_probe_range(mod->tracepoints,
577 //ust// mod->tracepoints + mod->num_tracepoints);
579 //ust// case MODULE_STATE_GOING:
580 //ust// tracepoint_update_probe_range(mod->tracepoints,
581 //ust// mod->tracepoints + mod->num_tracepoints);
587 //ust// struct notifier_block tracepoint_module_nb = {
588 //ust// .notifier_call = tracepoint_module_notify,
589 //ust// .priority = 0,
592 //ust// static int init_tracepoints(void)
594 //ust// return register_module_notifier(&tracepoint_module_nb);
596 //ust// __initcall(init_tracepoints);
598 //ust// #endif /* CONFIG_MODULES */
601 * Returns 0 if current not found.
602 * Returns 1 if current found.
604 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
606 struct tracepoint_lib
*iter_lib
;
609 //ust// mutex_lock(&module_mutex);
610 list_for_each_entry(iter_lib
, &libs
, list
) {
611 if (iter_lib
< iter
->lib
)
613 else if (iter_lib
> iter
->lib
)
614 iter
->tracepoint
= NULL
;
615 found
= marker_get_iter_range(&iter
->tracepoint
,
616 iter_lib
->tracepoints_start
,
617 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
619 iter
->lib
= iter_lib
;
623 //ust// mutex_unlock(&module_mutex);
627 void lib_update_tracepoints(void)
629 struct tracepoint_lib
*lib
;
631 //ust// mutex_lock(&module_mutex);
632 list_for_each_entry(lib
, &libs
, list
)
633 tracepoint_update_probe_range(lib
->tracepoints_start
,
634 lib
->tracepoints_start
+ lib
->tracepoints_count
);
635 //ust// mutex_unlock(&module_mutex);
638 static void (*new_tracepoint_cb
)(struct tracepoint
*) = NULL
;
640 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
642 new_tracepoint_cb
= cb
;
645 static void new_tracepoints(struct tracepoint
*start
, struct tracepoint
*end
)
647 if(new_tracepoint_cb
) {
648 struct tracepoint
*t
;
649 for(t
=start
; t
< end
; t
++) {
650 new_tracepoint_cb(t
);
655 int tracepoint_register_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
657 struct tracepoint_lib
*pl
;
659 pl
= (struct tracepoint_lib
*) malloc(sizeof(struct tracepoint_lib
));
661 pl
->tracepoints_start
= tracepoints_start
;
662 pl
->tracepoints_count
= tracepoints_count
;
664 /* FIXME: maybe protect this with its own mutex? */
665 mutex_lock(&tracepoints_mutex
);
666 list_add(&pl
->list
, &libs
);
667 mutex_unlock(&tracepoints_mutex
);
669 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
671 /* FIXME: update just the loaded lib */
672 lib_update_tracepoints();
674 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start
, tracepoints_count
);
679 int tracepoint_unregister_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
681 /*FIXME: implement; but before implementing, tracepoint_register_lib must
682 have appropriate locking. */