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>
27 #include <linux/immediate.h>
29 extern struct tracepoint __start___tracepoints
[];
30 extern struct tracepoint __stop___tracepoints
[];
32 /* Set to 1 to enable tracepoint debug output */
33 static const int tracepoint_debug
;
36 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
37 * builtin and module tracepoints and the hash table.
39 static DEFINE_MUTEX(tracepoints_mutex
);
42 * Tracepoint hash table, containing the active tracepoints.
43 * Protected by tracepoints_mutex.
45 #define TRACEPOINT_HASH_BITS 6
46 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
47 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
51 * It is used to to delay the free of multiple probes array until a quiescent
53 * Tracepoint entries modifications are protected by the tracepoints_mutex.
55 struct tracepoint_entry
{
56 struct hlist_node hlist
;
58 int refcount
; /* Number of times armed. 0 if disarmed. */
65 struct list_head list
;
70 static inline void *allocate_probes(int count
)
72 struct tp_probes
*p
= kmalloc(count
* sizeof(void *)
73 + sizeof(struct tp_probes
), GFP_KERNEL
);
74 return p
== NULL
? NULL
: p
->probes
;
77 static void rcu_free_old_probes(struct rcu_head
*head
)
79 kfree(container_of(head
, struct tp_probes
, u
.rcu
));
82 static inline void release_probes(void *old
)
85 struct tp_probes
*tp_probes
= container_of(old
,
86 struct tp_probes
, probes
[0]);
87 call_rcu_sched(&tp_probes
->u
.rcu
, rcu_free_old_probes
);
91 static void debug_print_probes(struct tracepoint_entry
*entry
)
95 if (!tracepoint_debug
|| !entry
->funcs
)
98 for (i
= 0; entry
->funcs
[i
]; i
++)
99 printk(KERN_DEBUG
"Probe %d : %p\n", i
, entry
->funcs
[i
]);
103 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
110 debug_print_probes(entry
);
113 /* (N -> N+1), (N != 0, 1) probes */
114 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++)
115 if (old
[nr_probes
] == probe
)
116 return ERR_PTR(-EEXIST
);
118 /* + 2 : one for new probe, one for NULL func */
119 new = allocate_probes(nr_probes
+ 2);
121 return ERR_PTR(-ENOMEM
);
123 memcpy(new, old
, nr_probes
* sizeof(void *));
124 new[nr_probes
] = probe
;
125 new[nr_probes
+ 1] = NULL
;
126 entry
->refcount
= nr_probes
+ 1;
128 debug_print_probes(entry
);
133 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
135 int nr_probes
= 0, nr_del
= 0, i
;
141 return ERR_PTR(-ENOENT
);
143 debug_print_probes(entry
);
144 /* (N -> M), (N > 1, M >= 0) probes */
145 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++) {
146 if ((!probe
|| old
[nr_probes
] == probe
))
150 if (nr_probes
- nr_del
== 0) {
151 /* N -> 0, (N > 1) */
154 debug_print_probes(entry
);
158 /* N -> M, (N > 1, M > 0) */
160 new = allocate_probes(nr_probes
- nr_del
+ 1);
162 return ERR_PTR(-ENOMEM
);
163 for (i
= 0; old
[i
]; i
++)
164 if ((probe
&& old
[i
] != probe
))
166 new[nr_probes
- nr_del
] = NULL
;
167 entry
->refcount
= nr_probes
- nr_del
;
170 debug_print_probes(entry
);
175 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
176 * Must be called with tracepoints_mutex held.
177 * Returns NULL if not present.
179 static struct tracepoint_entry
*get_tracepoint(const char *name
)
181 struct hlist_head
*head
;
182 struct hlist_node
*node
;
183 struct tracepoint_entry
*e
;
184 u32 hash
= jhash(name
, strlen(name
), 0);
186 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
187 hlist_for_each_entry(e
, node
, head
, hlist
) {
188 if (!strcmp(name
, e
->name
))
195 * Add the tracepoint to the tracepoint hash table. Must be called with
196 * tracepoints_mutex held.
198 static struct tracepoint_entry
*add_tracepoint(const char *name
)
200 struct hlist_head
*head
;
201 struct hlist_node
*node
;
202 struct tracepoint_entry
*e
;
203 size_t name_len
= strlen(name
) + 1;
204 u32 hash
= jhash(name
, name_len
-1, 0);
206 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
207 hlist_for_each_entry(e
, node
, head
, hlist
) {
208 if (!strcmp(name
, e
->name
)) {
210 "tracepoint %s busy\n", name
);
211 return ERR_PTR(-EEXIST
); /* Already there */
215 * Using kmalloc here to allocate a variable length element. Could
216 * cause some memory fragmentation if overused.
218 e
= kmalloc(sizeof(struct tracepoint_entry
) + name_len
, GFP_KERNEL
);
220 return ERR_PTR(-ENOMEM
);
221 memcpy(&e
->name
[0], name
, name_len
);
224 hlist_add_head(&e
->hlist
, head
);
229 * Remove the tracepoint from the tracepoint hash table. Must be called with
232 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
234 hlist_del(&e
->hlist
);
239 * Sets the probe callback corresponding to one tracepoint.
241 static void set_tracepoint(struct tracepoint_entry
**entry
,
242 struct tracepoint
*elem
, int active
)
244 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
247 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
248 * probe callbacks array is consistent before setting a pointer to it.
249 * This array is referenced by __DO_TRACE from
250 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
253 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
254 elem
->state__imv
= active
;
258 * Disable a tracepoint and its probe callback.
259 * Note: only waiting an RCU period after setting elem->call to the empty
260 * function insures that the original callback is not used anymore. This insured
261 * by preempt_disable around the call site.
263 static void disable_tracepoint(struct tracepoint
*elem
)
265 elem
->state__imv
= 0;
266 rcu_assign_pointer(elem
->funcs
, NULL
);
270 * tracepoint_update_probe_range - Update a probe range
271 * @begin: beginning of the range
272 * @end: end of the range
274 * Updates the probe callback corresponding to a range of tracepoints.
276 void tracepoint_update_probe_range(struct tracepoint
*begin
,
277 struct tracepoint
*end
)
279 struct tracepoint
*iter
;
280 struct tracepoint_entry
*mark_entry
;
282 mutex_lock(&tracepoints_mutex
);
283 for (iter
= begin
; iter
< end
; iter
++) {
284 mark_entry
= get_tracepoint(iter
->name
);
286 set_tracepoint(&mark_entry
, iter
,
287 !!mark_entry
->refcount
);
289 disable_tracepoint(iter
);
292 mutex_unlock(&tracepoints_mutex
);
296 * Update probes, removing the faulty probes.
298 static void tracepoint_update_probes(void)
300 /* Core kernel tracepoints */
301 tracepoint_update_probe_range(__start___tracepoints
,
302 __stop___tracepoints
);
303 /* tracepoints in modules. */
304 module_update_tracepoints();
305 /* Update immediate values */
310 static void *tracepoint_add_probe(const char *name
, void *probe
)
312 struct tracepoint_entry
*entry
;
315 entry
= get_tracepoint(name
);
317 entry
= add_tracepoint(name
);
321 old
= tracepoint_entry_add_probe(entry
, probe
);
322 if (IS_ERR(old
) && !entry
->refcount
)
323 remove_tracepoint(entry
);
328 * tracepoint_probe_register - Connect a probe to a tracepoint
329 * @name: tracepoint name
330 * @probe: probe handler
332 * Returns 0 if ok, error value on error.
333 * The probe address must at least be aligned on the architecture pointer size.
335 int tracepoint_probe_register(const char *name
, void *probe
)
339 mutex_lock(&tracepoints_mutex
);
340 old
= tracepoint_add_probe(name
, probe
);
341 mutex_unlock(&tracepoints_mutex
);
345 tracepoint_update_probes(); /* may update entry */
349 EXPORT_SYMBOL_GPL(tracepoint_probe_register
);
351 static void *tracepoint_remove_probe(const char *name
, void *probe
)
353 struct tracepoint_entry
*entry
;
356 entry
= get_tracepoint(name
);
358 return ERR_PTR(-ENOENT
);
359 old
= tracepoint_entry_remove_probe(entry
, probe
);
362 if (!entry
->refcount
)
363 remove_tracepoint(entry
);
368 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
369 * @name: tracepoint name
370 * @probe: probe function pointer
372 * We do not need to call a synchronize_sched to make sure the probes have
373 * finished running before doing a module unload, because the module unload
374 * itself uses stop_machine(), which insures that every preempt disabled section
377 int tracepoint_probe_unregister(const char *name
, void *probe
)
381 mutex_lock(&tracepoints_mutex
);
382 old
= tracepoint_remove_probe(name
, probe
);
383 mutex_unlock(&tracepoints_mutex
);
387 tracepoint_update_probes(); /* may update entry */
391 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister
);
393 static LIST_HEAD(old_probes
);
394 static int need_update
;
396 static void tracepoint_add_old_probes(void *old
)
400 struct tp_probes
*tp_probes
= container_of(old
,
401 struct tp_probes
, probes
[0]);
402 list_add(&tp_probes
->u
.list
, &old_probes
);
407 * tracepoint_probe_register_noupdate - register a probe but not connect
408 * @name: tracepoint name
409 * @probe: probe handler
411 * caller must call tracepoint_probe_update_all()
413 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
417 mutex_lock(&tracepoints_mutex
);
418 old
= tracepoint_add_probe(name
, probe
);
420 mutex_unlock(&tracepoints_mutex
);
423 tracepoint_add_old_probes(old
);
424 mutex_unlock(&tracepoints_mutex
);
427 EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate
);
430 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
431 * @name: tracepoint name
432 * @probe: probe function pointer
434 * caller must call tracepoint_probe_update_all()
436 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
440 mutex_lock(&tracepoints_mutex
);
441 old
= tracepoint_remove_probe(name
, probe
);
443 mutex_unlock(&tracepoints_mutex
);
446 tracepoint_add_old_probes(old
);
447 mutex_unlock(&tracepoints_mutex
);
450 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate
);
453 * tracepoint_probe_update_all - update tracepoints
455 void tracepoint_probe_update_all(void)
457 LIST_HEAD(release_probes
);
458 struct tp_probes
*pos
, *next
;
460 mutex_lock(&tracepoints_mutex
);
462 mutex_unlock(&tracepoints_mutex
);
465 if (!list_empty(&old_probes
))
466 list_replace_init(&old_probes
, &release_probes
);
468 mutex_unlock(&tracepoints_mutex
);
470 tracepoint_update_probes();
471 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
472 list_del(&pos
->u
.list
);
473 call_rcu_sched(&pos
->u
.rcu
, rcu_free_old_probes
);
476 EXPORT_SYMBOL_GPL(tracepoint_probe_update_all
);
479 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
480 * @tracepoint: current tracepoints (in), next tracepoint (out)
481 * @begin: beginning of the range
482 * @end: end of the range
484 * Returns whether a next tracepoint has been found (1) or not (0).
485 * Will return the first tracepoint in the range if the input tracepoint is
488 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
489 struct tracepoint
*begin
, struct tracepoint
*end
)
491 if (!*tracepoint
&& begin
!= end
) {
495 if (*tracepoint
>= begin
&& *tracepoint
< end
)
499 EXPORT_SYMBOL_GPL(tracepoint_get_iter_range
);
501 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
505 /* Core kernel tracepoints */
507 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
508 __start___tracepoints
, __stop___tracepoints
);
512 /* tracepoints in modules. */
513 found
= module_get_iter_tracepoints(iter
);
516 tracepoint_iter_reset(iter
);
519 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
521 tracepoint_get_iter(iter
);
523 EXPORT_SYMBOL_GPL(tracepoint_iter_start
);
525 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
529 * iter->tracepoint may be invalid because we blindly incremented it.
530 * Make sure it is valid by marshalling on the tracepoints, getting the
531 * tracepoints from following modules if necessary.
533 tracepoint_get_iter(iter
);
535 EXPORT_SYMBOL_GPL(tracepoint_iter_next
);
537 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
540 EXPORT_SYMBOL_GPL(tracepoint_iter_stop
);
542 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
545 iter
->tracepoint
= NULL
;
547 EXPORT_SYMBOL_GPL(tracepoint_iter_reset
);
549 #ifdef CONFIG_MODULES
551 int tracepoint_module_notify(struct notifier_block
*self
,
552 unsigned long val
, void *data
)
554 struct module
*mod
= data
;
557 case MODULE_STATE_COMING
:
558 tracepoint_update_probe_range(mod
->tracepoints
,
559 mod
->tracepoints
+ mod
->num_tracepoints
);
561 case MODULE_STATE_GOING
:
562 tracepoint_update_probe_range(mod
->tracepoints
,
563 mod
->tracepoints
+ mod
->num_tracepoints
);
569 struct notifier_block tracepoint_module_nb
= {
570 .notifier_call
= tracepoint_module_notify
,
574 static int init_tracepoints(void)
576 return register_module_notifier(&tracepoint_module_nb
);
578 __initcall(init_tracepoints
);
580 #endif /* CONFIG_MODULES */