2 * Copyright (C) 2008 Mathieu Desnoyers
3 * Copyright (C) 2009 Pierre-Marc Fournier
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; 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"
43 //extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
44 //extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
46 /* Set to 1 to enable tracepoint debug output */
47 static const int tracepoint_debug
;
49 /* libraries that contain tracepoints (struct tracepoint_lib) */
50 static LIST_HEAD(libs
);
53 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
54 * builtin and module tracepoints and the hash table.
56 static DEFINE_MUTEX(tracepoints_mutex
);
59 * Tracepoint hash table, containing the active tracepoints.
60 * Protected by tracepoints_mutex.
62 #define TRACEPOINT_HASH_BITS 6
63 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
64 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
68 * It is used to to delay the free of multiple probes array until a quiescent
70 * Tracepoint entries modifications are protected by the tracepoints_mutex.
72 struct tracepoint_entry
{
73 struct hlist_node hlist
;
75 int refcount
; /* Number of times armed. 0 if disarmed. */
81 //ust// struct rcu_head rcu;
82 struct list_head list
;
87 static inline void *allocate_probes(int count
)
89 struct tp_probes
*p
= kmalloc(count
* sizeof(void *)
90 + sizeof(struct tp_probes
), GFP_KERNEL
);
91 return p
== NULL
? NULL
: p
->probes
;
94 //ust// static void rcu_free_old_probes(struct rcu_head *head)
96 //ust// kfree(container_of(head, struct tp_probes, u.rcu));
99 static inline void release_probes(void *old
)
102 struct tp_probes
*tp_probes
= container_of(old
,
103 struct tp_probes
, probes
[0]);
104 //ust// call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
110 static void debug_print_probes(struct tracepoint_entry
*entry
)
114 if (!tracepoint_debug
|| !entry
->funcs
)
117 for (i
= 0; entry
->funcs
[i
]; i
++)
118 printk(KERN_DEBUG
"Probe %d : %p\n", i
, entry
->funcs
[i
]);
122 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
129 debug_print_probes(entry
);
132 /* (N -> N+1), (N != 0, 1) probes */
133 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++)
134 if (old
[nr_probes
] == probe
)
135 return ERR_PTR(-EEXIST
);
137 /* + 2 : one for new probe, one for NULL func */
138 new = allocate_probes(nr_probes
+ 2);
140 return ERR_PTR(-ENOMEM
);
142 memcpy(new, old
, nr_probes
* sizeof(void *));
143 new[nr_probes
] = probe
;
144 new[nr_probes
+ 1] = NULL
;
145 entry
->refcount
= nr_probes
+ 1;
147 debug_print_probes(entry
);
152 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
154 int nr_probes
= 0, nr_del
= 0, i
;
160 return ERR_PTR(-ENOENT
);
162 debug_print_probes(entry
);
163 /* (N -> M), (N > 1, M >= 0) probes */
164 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++) {
165 if ((!probe
|| old
[nr_probes
] == probe
))
169 if (nr_probes
- nr_del
== 0) {
170 /* N -> 0, (N > 1) */
173 debug_print_probes(entry
);
177 /* N -> M, (N > 1, M > 0) */
179 new = allocate_probes(nr_probes
- nr_del
+ 1);
181 return ERR_PTR(-ENOMEM
);
182 for (i
= 0; old
[i
]; i
++)
183 if ((probe
&& old
[i
] != probe
))
185 new[nr_probes
- nr_del
] = NULL
;
186 entry
->refcount
= nr_probes
- nr_del
;
189 debug_print_probes(entry
);
194 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
195 * Must be called with tracepoints_mutex held.
196 * Returns NULL if not present.
198 static struct tracepoint_entry
*get_tracepoint(const char *name
)
200 struct hlist_head
*head
;
201 struct hlist_node
*node
;
202 struct tracepoint_entry
*e
;
203 u32 hash
= jhash(name
, strlen(name
), 0);
205 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
206 hlist_for_each_entry(e
, node
, head
, hlist
) {
207 if (!strcmp(name
, e
->name
))
214 * Add the tracepoint to the tracepoint hash table. Must be called with
215 * tracepoints_mutex held.
217 static struct tracepoint_entry
*add_tracepoint(const char *name
)
219 struct hlist_head
*head
;
220 struct hlist_node
*node
;
221 struct tracepoint_entry
*e
;
222 size_t name_len
= strlen(name
) + 1;
223 u32 hash
= jhash(name
, name_len
-1, 0);
225 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
226 hlist_for_each_entry(e
, node
, head
, hlist
) {
227 if (!strcmp(name
, e
->name
)) {
229 "tracepoint %s busy\n", name
);
230 return ERR_PTR(-EEXIST
); /* Already there */
234 * Using kmalloc here to allocate a variable length element. Could
235 * cause some memory fragmentation if overused.
237 e
= kmalloc(sizeof(struct tracepoint_entry
) + name_len
, GFP_KERNEL
);
239 return ERR_PTR(-ENOMEM
);
240 memcpy(&e
->name
[0], name
, name_len
);
243 hlist_add_head(&e
->hlist
, head
);
248 * Remove the tracepoint from the tracepoint hash table. Must be called with
251 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
253 hlist_del(&e
->hlist
);
258 * Sets the probe callback corresponding to one tracepoint.
260 static void set_tracepoint(struct tracepoint_entry
**entry
,
261 struct tracepoint
*elem
, int active
)
263 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
266 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
267 * probe callbacks array is consistent before setting a pointer to it.
268 * This array is referenced by __DO_TRACE from
269 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
272 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
273 elem
->state__imv
= active
;
277 * Disable a tracepoint and its probe callback.
278 * Note: only waiting an RCU period after setting elem->call to the empty
279 * function insures that the original callback is not used anymore. This insured
280 * by preempt_disable around the call site.
282 static void disable_tracepoint(struct tracepoint
*elem
)
284 elem
->state__imv
= 0;
285 rcu_assign_pointer(elem
->funcs
, NULL
);
289 * tracepoint_update_probe_range - Update a probe range
290 * @begin: beginning of the range
291 * @end: end of the range
293 * Updates the probe callback corresponding to a range of tracepoints.
295 void tracepoint_update_probe_range(struct tracepoint
*begin
,
296 struct tracepoint
*end
)
298 struct tracepoint
*iter
;
299 struct tracepoint_entry
*mark_entry
;
301 mutex_lock(&tracepoints_mutex
);
302 for (iter
= begin
; iter
< end
; iter
++) {
303 mark_entry
= get_tracepoint(iter
->name
);
305 set_tracepoint(&mark_entry
, iter
,
306 !!mark_entry
->refcount
);
308 disable_tracepoint(iter
);
311 mutex_unlock(&tracepoints_mutex
);
315 * Update probes, removing the faulty probes.
317 static void tracepoint_update_probes(void)
319 /* Core kernel tracepoints */
320 //ust// tracepoint_update_probe_range(__start___tracepoints,
321 //ust// __stop___tracepoints);
322 /* tracepoints in modules. */
323 lib_update_tracepoints();
324 /* Update immediate values */
326 //ust// module_imv_update();
329 static void *tracepoint_add_probe(const char *name
, void *probe
)
331 struct tracepoint_entry
*entry
;
334 entry
= get_tracepoint(name
);
336 entry
= add_tracepoint(name
);
340 old
= tracepoint_entry_add_probe(entry
, probe
);
341 if (IS_ERR(old
) && !entry
->refcount
)
342 remove_tracepoint(entry
);
347 * tracepoint_probe_register - Connect a probe to a tracepoint
348 * @name: tracepoint name
349 * @probe: probe handler
351 * Returns 0 if ok, error value on error.
352 * The probe address must at least be aligned on the architecture pointer size.
354 int tracepoint_probe_register(const char *name
, void *probe
)
358 mutex_lock(&tracepoints_mutex
);
359 old
= tracepoint_add_probe(name
, probe
);
360 mutex_unlock(&tracepoints_mutex
);
364 tracepoint_update_probes(); /* may update entry */
368 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
370 static void *tracepoint_remove_probe(const char *name
, void *probe
)
372 struct tracepoint_entry
*entry
;
375 entry
= get_tracepoint(name
);
377 return ERR_PTR(-ENOENT
);
378 old
= tracepoint_entry_remove_probe(entry
, probe
);
381 if (!entry
->refcount
)
382 remove_tracepoint(entry
);
387 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
388 * @name: tracepoint name
389 * @probe: probe function pointer
391 * We do not need to call a synchronize_sched to make sure the probes have
392 * finished running before doing a module unload, because the module unload
393 * itself uses stop_machine(), which insures that every preempt disabled section
396 int tracepoint_probe_unregister(const char *name
, void *probe
)
400 mutex_lock(&tracepoints_mutex
);
401 old
= tracepoint_remove_probe(name
, probe
);
402 mutex_unlock(&tracepoints_mutex
);
406 tracepoint_update_probes(); /* may update entry */
410 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
412 static LIST_HEAD(old_probes
);
413 static int need_update
;
415 static void tracepoint_add_old_probes(void *old
)
419 struct tp_probes
*tp_probes
= container_of(old
,
420 struct tp_probes
, probes
[0]);
421 list_add(&tp_probes
->u
.list
, &old_probes
);
426 * tracepoint_probe_register_noupdate - register a probe but not connect
427 * @name: tracepoint name
428 * @probe: probe handler
430 * caller must call tracepoint_probe_update_all()
432 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
436 mutex_lock(&tracepoints_mutex
);
437 old
= tracepoint_add_probe(name
, probe
);
439 mutex_unlock(&tracepoints_mutex
);
442 tracepoint_add_old_probes(old
);
443 mutex_unlock(&tracepoints_mutex
);
446 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
449 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
450 * @name: tracepoint name
451 * @probe: probe function pointer
453 * caller must call tracepoint_probe_update_all()
455 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
459 mutex_lock(&tracepoints_mutex
);
460 old
= tracepoint_remove_probe(name
, probe
);
462 mutex_unlock(&tracepoints_mutex
);
465 tracepoint_add_old_probes(old
);
466 mutex_unlock(&tracepoints_mutex
);
469 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
472 * tracepoint_probe_update_all - update tracepoints
474 void tracepoint_probe_update_all(void)
476 LIST_HEAD(release_probes
);
477 struct tp_probes
*pos
, *next
;
479 mutex_lock(&tracepoints_mutex
);
481 mutex_unlock(&tracepoints_mutex
);
484 if (!list_empty(&old_probes
))
485 list_replace_init(&old_probes
, &release_probes
);
487 mutex_unlock(&tracepoints_mutex
);
489 tracepoint_update_probes();
490 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
491 list_del(&pos
->u
.list
);
492 //ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
497 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
500 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
501 * @tracepoint: current tracepoints (in), next tracepoint (out)
502 * @begin: beginning of the range
503 * @end: end of the range
505 * Returns whether a next tracepoint has been found (1) or not (0).
506 * Will return the first tracepoint in the range if the input tracepoint is
509 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
510 struct tracepoint
*begin
, struct tracepoint
*end
)
512 if (!*tracepoint
&& begin
!= end
) {
516 if (*tracepoint
>= begin
&& *tracepoint
< end
)
520 //ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
522 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
526 //ust// /* Core kernel tracepoints */
527 //ust// if (!iter->module) {
528 //ust// found = tracepoint_get_iter_range(&iter->tracepoint,
529 //ust// __start___tracepoints, __stop___tracepoints);
533 /* tracepoints in libs. */
534 found
= lib_get_iter_tracepoints(iter
);
537 tracepoint_iter_reset(iter
);
540 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
542 tracepoint_get_iter(iter
);
544 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
546 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
550 * iter->tracepoint may be invalid because we blindly incremented it.
551 * Make sure it is valid by marshalling on the tracepoints, getting the
552 * tracepoints from following modules if necessary.
554 tracepoint_get_iter(iter
);
556 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
558 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
561 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
563 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
565 //ust// iter->module = NULL;
566 iter
->tracepoint
= NULL
;
568 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
570 //ust// #ifdef CONFIG_MODULES
572 //ust// int tracepoint_module_notify(struct notifier_block *self,
573 //ust// unsigned long val, void *data)
575 //ust// struct module *mod = data;
577 //ust// switch (val) {
578 //ust// case MODULE_STATE_COMING:
579 //ust// tracepoint_update_probe_range(mod->tracepoints,
580 //ust// mod->tracepoints + mod->num_tracepoints);
582 //ust// case MODULE_STATE_GOING:
583 //ust// tracepoint_update_probe_range(mod->tracepoints,
584 //ust// mod->tracepoints + mod->num_tracepoints);
590 //ust// struct notifier_block tracepoint_module_nb = {
591 //ust// .notifier_call = tracepoint_module_notify,
592 //ust// .priority = 0,
595 //ust// static int init_tracepoints(void)
597 //ust// return register_module_notifier(&tracepoint_module_nb);
599 //ust// __initcall(init_tracepoints);
601 //ust// #endif /* CONFIG_MODULES */
604 * Returns 0 if current not found.
605 * Returns 1 if current found.
607 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
609 struct tracepoint_lib
*iter_lib
;
612 //ust// mutex_lock(&module_mutex);
613 list_for_each_entry(iter_lib
, &libs
, list
) {
614 if (iter_lib
< iter
->lib
)
616 else if (iter_lib
> iter
->lib
)
617 iter
->tracepoint
= NULL
;
618 found
= marker_get_iter_range(&iter
->tracepoint
,
619 iter_lib
->tracepoints_start
,
620 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
622 iter
->lib
= iter_lib
;
626 //ust// mutex_unlock(&module_mutex);
630 void lib_update_tracepoints(void)
632 struct tracepoint_lib
*lib
;
634 //ust// mutex_lock(&module_mutex);
635 list_for_each_entry(lib
, &libs
, list
)
636 tracepoint_update_probe_range(lib
->tracepoints_start
,
637 lib
->tracepoints_start
+ lib
->tracepoints_count
);
638 //ust// mutex_unlock(&module_mutex);
641 static void (*new_tracepoint_cb
)(struct tracepoint
*) = NULL
;
643 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
645 new_tracepoint_cb
= cb
;
648 static void new_tracepoints(struct tracepoint
*start
, struct tracepoint
*end
)
650 if(new_tracepoint_cb
) {
651 struct tracepoint
*t
;
652 for(t
=start
; t
< end
; t
++) {
653 new_tracepoint_cb(t
);
658 int tracepoint_register_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
660 struct tracepoint_lib
*pl
;
662 pl
= (struct tracepoint_lib
*) malloc(sizeof(struct tracepoint_lib
));
664 pl
->tracepoints_start
= tracepoints_start
;
665 pl
->tracepoints_count
= tracepoints_count
;
667 /* FIXME: maybe protect this with its own mutex? */
668 mutex_lock(&tracepoints_mutex
);
669 list_add(&pl
->list
, &libs
);
670 mutex_unlock(&tracepoints_mutex
);
672 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
674 /* FIXME: update just the loaded lib */
675 lib_update_tracepoints();
677 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start
, tracepoints_count
);
682 int tracepoint_unregister_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
684 /*FIXME: implement; but before implementing, tracepoint_register_lib must
685 have appropriate locking. */