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 #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>
28 extern struct tracepoint __start___tracepoints
[];
29 extern struct tracepoint __stop___tracepoints
[];
31 /* Set to 1 to enable tracepoint debug output */
32 static const int tracepoint_debug
;
35 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
36 * builtin and module tracepoints and the hash table.
38 static DEFINE_MUTEX(tracepoints_mutex
);
41 * Tracepoint hash table, containing the active tracepoints.
42 * Protected by tracepoints_mutex.
44 #define TRACEPOINT_HASH_BITS 6
45 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
46 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
50 * It is used to to delay the free of multiple probes array until a quiescent
52 * Tracepoint entries modifications are protected by the tracepoints_mutex.
54 struct tracepoint_entry
{
55 struct hlist_node hlist
;
57 int refcount
; /* Number of times armed. 0 if disarmed. */
64 struct list_head list
;
69 static inline void *allocate_probes(int count
)
71 struct tp_probes
*p
= kmalloc(count
* sizeof(void *)
72 + sizeof(struct tp_probes
), GFP_KERNEL
);
73 return p
== NULL
? NULL
: p
->probes
;
76 static void rcu_free_old_probes(struct rcu_head
*head
)
78 kfree(container_of(head
, struct tp_probes
, u
.rcu
));
81 static inline void release_probes(void *old
)
84 struct tp_probes
*tp_probes
= container_of(old
,
85 struct tp_probes
, probes
[0]);
86 call_rcu_sched(&tp_probes
->u
.rcu
, rcu_free_old_probes
);
90 static void debug_print_probes(struct tracepoint_entry
*entry
)
94 if (!tracepoint_debug
|| !entry
->funcs
)
97 for (i
= 0; entry
->funcs
[i
]; i
++)
98 printk(KERN_DEBUG
"Probe %d : %p\n", i
, entry
->funcs
[i
]);
102 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
109 debug_print_probes(entry
);
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
);
117 /* + 2 : one for new probe, one for NULL func */
118 new = allocate_probes(nr_probes
+ 2);
120 return ERR_PTR(-ENOMEM
);
122 memcpy(new, old
, nr_probes
* sizeof(void *));
123 new[nr_probes
] = probe
;
124 new[nr_probes
+ 1] = NULL
;
125 entry
->refcount
= nr_probes
+ 1;
127 debug_print_probes(entry
);
132 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
134 int nr_probes
= 0, nr_del
= 0, i
;
140 return ERR_PTR(-ENOENT
);
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
))
149 if (nr_probes
- nr_del
== 0) {
150 /* N -> 0, (N > 1) */
153 debug_print_probes(entry
);
157 /* N -> M, (N > 1, M > 0) */
159 new = allocate_probes(nr_probes
- nr_del
+ 1);
161 return ERR_PTR(-ENOMEM
);
162 for (i
= 0; old
[i
]; i
++)
163 if ((probe
&& old
[i
] != probe
))
165 new[nr_probes
- nr_del
] = NULL
;
166 entry
->refcount
= nr_probes
- nr_del
;
169 debug_print_probes(entry
);
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.
178 static struct tracepoint_entry
*get_tracepoint(const char *name
)
180 struct hlist_head
*head
;
181 struct hlist_node
*node
;
182 struct tracepoint_entry
*e
;
183 u32 hash
= jhash(name
, strlen(name
), 0);
185 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
186 hlist_for_each_entry(e
, node
, head
, hlist
) {
187 if (!strcmp(name
, e
->name
))
194 * Add the tracepoint to the tracepoint hash table. Must be called with
195 * tracepoints_mutex held.
197 static struct tracepoint_entry
*add_tracepoint(const char *name
)
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);
205 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
206 hlist_for_each_entry(e
, node
, head
, hlist
) {
207 if (!strcmp(name
, e
->name
)) {
209 "tracepoint %s busy\n", name
);
210 return ERR_PTR(-EEXIST
); /* Already there */
214 * Using kmalloc here to allocate a variable length element. Could
215 * cause some memory fragmentation if overused.
217 e
= kmalloc(sizeof(struct tracepoint_entry
) + name_len
, GFP_KERNEL
);
219 return ERR_PTR(-ENOMEM
);
220 memcpy(&e
->name
[0], name
, name_len
);
223 hlist_add_head(&e
->hlist
, head
);
228 * Remove the tracepoint from the tracepoint hash table. Must be called with
231 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
233 hlist_del(&e
->hlist
);
238 * Sets the probe callback corresponding to one tracepoint.
240 static void set_tracepoint(struct tracepoint_entry
**entry
,
241 struct tracepoint
*elem
, int active
)
243 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
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()
252 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
253 elem
->state
= active
;
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.
262 static void disable_tracepoint(struct tracepoint
*elem
)
265 rcu_assign_pointer(elem
->funcs
, NULL
);
269 * tracepoint_update_probe_range - Update a probe range
270 * @begin: beginning of the range
271 * @end: end of the range
273 * Updates the probe callback corresponding to a range of tracepoints.
276 tracepoint_update_probe_range(struct tracepoint
*begin
, struct tracepoint
*end
)
278 struct tracepoint
*iter
;
279 struct tracepoint_entry
*mark_entry
;
284 mutex_lock(&tracepoints_mutex
);
285 for (iter
= begin
; iter
< end
; iter
++) {
286 mark_entry
= get_tracepoint(iter
->name
);
288 set_tracepoint(&mark_entry
, iter
,
289 !!mark_entry
->refcount
);
291 disable_tracepoint(iter
);
294 mutex_unlock(&tracepoints_mutex
);
298 * Update probes, removing the faulty probes.
300 static void tracepoint_update_probes(void)
302 /* Core kernel tracepoints */
303 tracepoint_update_probe_range(__start___tracepoints
,
304 __stop___tracepoints
);
305 /* tracepoints in modules. */
306 module_update_tracepoints();
309 static void *tracepoint_add_probe(const char *name
, void *probe
)
311 struct tracepoint_entry
*entry
;
314 entry
= get_tracepoint(name
);
316 entry
= add_tracepoint(name
);
320 old
= tracepoint_entry_add_probe(entry
, probe
);
321 if (IS_ERR(old
) && !entry
->refcount
)
322 remove_tracepoint(entry
);
327 * tracepoint_probe_register - Connect a probe to a tracepoint
328 * @name: tracepoint name
329 * @probe: probe handler
331 * Returns 0 if ok, error value on error.
332 * The probe address must at least be aligned on the architecture pointer size.
334 int tracepoint_probe_register(const char *name
, void *probe
)
338 mutex_lock(&tracepoints_mutex
);
339 old
= tracepoint_add_probe(name
, probe
);
340 mutex_unlock(&tracepoints_mutex
);
344 tracepoint_update_probes(); /* may update entry */
348 EXPORT_SYMBOL_GPL(tracepoint_probe_register
);
350 static void *tracepoint_remove_probe(const char *name
, void *probe
)
352 struct tracepoint_entry
*entry
;
355 entry
= get_tracepoint(name
);
357 return ERR_PTR(-ENOENT
);
358 old
= tracepoint_entry_remove_probe(entry
, probe
);
361 if (!entry
->refcount
)
362 remove_tracepoint(entry
);
367 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
368 * @name: tracepoint name
369 * @probe: probe function pointer
371 * We do not need to call a synchronize_sched to make sure the probes have
372 * finished running before doing a module unload, because the module unload
373 * itself uses stop_machine(), which insures that every preempt disabled section
376 int tracepoint_probe_unregister(const char *name
, void *probe
)
380 mutex_lock(&tracepoints_mutex
);
381 old
= tracepoint_remove_probe(name
, probe
);
382 mutex_unlock(&tracepoints_mutex
);
386 tracepoint_update_probes(); /* may update entry */
390 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister
);
392 static LIST_HEAD(old_probes
);
393 static int need_update
;
395 static void tracepoint_add_old_probes(void *old
)
399 struct tp_probes
*tp_probes
= container_of(old
,
400 struct tp_probes
, probes
[0]);
401 list_add(&tp_probes
->u
.list
, &old_probes
);
406 * tracepoint_probe_register_noupdate - register a probe but not connect
407 * @name: tracepoint name
408 * @probe: probe handler
410 * caller must call tracepoint_probe_update_all()
412 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
416 mutex_lock(&tracepoints_mutex
);
417 old
= tracepoint_add_probe(name
, probe
);
419 mutex_unlock(&tracepoints_mutex
);
422 tracepoint_add_old_probes(old
);
423 mutex_unlock(&tracepoints_mutex
);
426 EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate
);
429 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
430 * @name: tracepoint name
431 * @probe: probe function pointer
433 * caller must call tracepoint_probe_update_all()
435 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
439 mutex_lock(&tracepoints_mutex
);
440 old
= tracepoint_remove_probe(name
, probe
);
442 mutex_unlock(&tracepoints_mutex
);
445 tracepoint_add_old_probes(old
);
446 mutex_unlock(&tracepoints_mutex
);
449 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate
);
452 * tracepoint_probe_update_all - update tracepoints
454 void tracepoint_probe_update_all(void)
456 LIST_HEAD(release_probes
);
457 struct tp_probes
*pos
, *next
;
459 mutex_lock(&tracepoints_mutex
);
461 mutex_unlock(&tracepoints_mutex
);
464 if (!list_empty(&old_probes
))
465 list_replace_init(&old_probes
, &release_probes
);
467 mutex_unlock(&tracepoints_mutex
);
469 tracepoint_update_probes();
470 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
471 list_del(&pos
->u
.list
);
472 call_rcu_sched(&pos
->u
.rcu
, rcu_free_old_probes
);
475 EXPORT_SYMBOL_GPL(tracepoint_probe_update_all
);
478 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
479 * @tracepoint: current tracepoints (in), next tracepoint (out)
480 * @begin: beginning of the range
481 * @end: end of the range
483 * Returns whether a next tracepoint has been found (1) or not (0).
484 * Will return the first tracepoint in the range if the input tracepoint is
487 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
488 struct tracepoint
*begin
, struct tracepoint
*end
)
490 if (!*tracepoint
&& begin
!= end
) {
494 if (*tracepoint
>= begin
&& *tracepoint
< end
)
498 EXPORT_SYMBOL_GPL(tracepoint_get_iter_range
);
500 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
504 /* Core kernel tracepoints */
506 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
507 __start___tracepoints
, __stop___tracepoints
);
511 /* tracepoints in modules. */
512 found
= module_get_iter_tracepoints(iter
);
515 tracepoint_iter_reset(iter
);
518 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
520 tracepoint_get_iter(iter
);
522 EXPORT_SYMBOL_GPL(tracepoint_iter_start
);
524 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
528 * iter->tracepoint may be invalid because we blindly incremented it.
529 * Make sure it is valid by marshalling on the tracepoints, getting the
530 * tracepoints from following modules if necessary.
532 tracepoint_get_iter(iter
);
534 EXPORT_SYMBOL_GPL(tracepoint_iter_next
);
536 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
539 EXPORT_SYMBOL_GPL(tracepoint_iter_stop
);
541 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
544 iter
->tracepoint
= NULL
;
546 EXPORT_SYMBOL_GPL(tracepoint_iter_reset
);
548 #ifdef CONFIG_MODULES
550 int tracepoint_module_notify(struct notifier_block
*self
,
551 unsigned long val
, void *data
)
553 struct module
*mod
= data
;
556 case MODULE_STATE_COMING
:
557 tracepoint_update_probe_range(mod
->tracepoints
,
558 mod
->tracepoints
+ mod
->num_tracepoints
);
560 case MODULE_STATE_GOING
:
561 tracepoint_update_probe_range(mod
->tracepoints
,
562 mod
->tracepoints
+ mod
->num_tracepoints
);
568 struct notifier_block tracepoint_module_nb
= {
569 .notifier_call
= tracepoint_module_notify
,
573 static int init_tracepoints(void)
575 return register_module_notifier(&tracepoint_module_nb
);
577 __initcall(init_tracepoints
);
579 #endif /* CONFIG_MODULES */