tracepoint: simplification for tracepoints using RCU
[linux-2.6] / kernel / tracepoint.c
1 /*
2  * Copyright (C) 2008 Mathieu Desnoyers
3  *
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.
8  *
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.
13  *
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.
17  */
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
28 extern struct tracepoint __start___tracepoints[];
29 extern struct tracepoint __stop___tracepoints[];
30
31 /* Set to 1 to enable tracepoint debug output */
32 static const int tracepoint_debug;
33
34 /*
35  * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
36  * builtin and module tracepoints and the hash table.
37  */
38 static DEFINE_MUTEX(tracepoints_mutex);
39
40 /*
41  * Tracepoint hash table, containing the active tracepoints.
42  * Protected by tracepoints_mutex.
43  */
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];
47
48 /*
49  * Note about RCU :
50  * It is used to to delay the free of multiple probes array until a quiescent
51  * state is reached.
52  * Tracepoint entries modifications are protected by the tracepoints_mutex.
53  */
54 struct tracepoint_entry {
55         struct hlist_node hlist;
56         void **funcs;
57         int refcount;   /* Number of times armed. 0 if disarmed. */
58         char name[0];
59 };
60
61 struct tp_probes {
62         struct rcu_head rcu;
63         void *probes[0];
64 };
65
66 static inline void *allocate_probes(int count)
67 {
68         struct tp_probes *p  = kmalloc(count * sizeof(void *)
69                         + sizeof(struct tp_probes), GFP_KERNEL);
70         return p == NULL ? NULL : p->probes;
71 }
72
73 static void rcu_free_old_probes(struct rcu_head *head)
74 {
75         kfree(container_of(head, struct tp_probes, rcu));
76 }
77
78 static inline void release_probes(void *old)
79 {
80         if (old) {
81                 struct tp_probes *tp_probes = container_of(old,
82                         struct tp_probes, probes[0]);
83                 call_rcu(&tp_probes->rcu, rcu_free_old_probes);
84         }
85 }
86
87 static void debug_print_probes(struct tracepoint_entry *entry)
88 {
89         int i;
90
91         if (!tracepoint_debug || !entry->funcs)
92                 return;
93
94         for (i = 0; entry->funcs[i]; i++)
95                 printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i]);
96 }
97
98 static void *
99 tracepoint_entry_add_probe(struct tracepoint_entry *entry, void *probe)
100 {
101         int nr_probes = 0;
102         void **old, **new;
103
104         WARN_ON(!probe);
105
106         debug_print_probes(entry);
107         old = entry->funcs;
108         if (old) {
109                 /* (N -> N+1), (N != 0, 1) probes */
110                 for (nr_probes = 0; old[nr_probes]; nr_probes++)
111                         if (old[nr_probes] == probe)
112                                 return ERR_PTR(-EEXIST);
113         }
114         /* + 2 : one for new probe, one for NULL func */
115         new = allocate_probes(nr_probes + 2);
116         if (new == NULL)
117                 return ERR_PTR(-ENOMEM);
118         if (old)
119                 memcpy(new, old, nr_probes * sizeof(void *));
120         new[nr_probes] = probe;
121         new[nr_probes + 1] = NULL;
122         entry->refcount = nr_probes + 1;
123         entry->funcs = new;
124         debug_print_probes(entry);
125         return old;
126 }
127
128 static void *
129 tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe)
130 {
131         int nr_probes = 0, nr_del = 0, i;
132         void **old, **new;
133
134         old = entry->funcs;
135
136         if (!old)
137                 return ERR_PTR(-ENOENT);
138
139         debug_print_probes(entry);
140         /* (N -> M), (N > 1, M >= 0) probes */
141         for (nr_probes = 0; old[nr_probes]; nr_probes++) {
142                 if ((!probe || old[nr_probes] == probe))
143                         nr_del++;
144         }
145
146         if (nr_probes - nr_del == 0) {
147                 /* N -> 0, (N > 1) */
148                 entry->funcs = NULL;
149                 entry->refcount = 0;
150                 debug_print_probes(entry);
151                 return old;
152         } else {
153                 int j = 0;
154                 /* N -> M, (N > 1, M > 0) */
155                 /* + 1 for NULL */
156                 new = allocate_probes(nr_probes - nr_del + 1);
157                 if (new == NULL)
158                         return ERR_PTR(-ENOMEM);
159                 for (i = 0; old[i]; i++)
160                         if ((probe && old[i] != probe))
161                                 new[j++] = old[i];
162                 new[nr_probes - nr_del] = NULL;
163                 entry->refcount = nr_probes - nr_del;
164                 entry->funcs = new;
165         }
166         debug_print_probes(entry);
167         return old;
168 }
169
170 /*
171  * Get tracepoint if the tracepoint is present in the tracepoint hash table.
172  * Must be called with tracepoints_mutex held.
173  * Returns NULL if not present.
174  */
175 static struct tracepoint_entry *get_tracepoint(const char *name)
176 {
177         struct hlist_head *head;
178         struct hlist_node *node;
179         struct tracepoint_entry *e;
180         u32 hash = jhash(name, strlen(name), 0);
181
182         head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
183         hlist_for_each_entry(e, node, head, hlist) {
184                 if (!strcmp(name, e->name))
185                         return e;
186         }
187         return NULL;
188 }
189
190 /*
191  * Add the tracepoint to the tracepoint hash table. Must be called with
192  * tracepoints_mutex held.
193  */
194 static struct tracepoint_entry *add_tracepoint(const char *name)
195 {
196         struct hlist_head *head;
197         struct hlist_node *node;
198         struct tracepoint_entry *e;
199         size_t name_len = strlen(name) + 1;
200         u32 hash = jhash(name, name_len-1, 0);
201
202         head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
203         hlist_for_each_entry(e, node, head, hlist) {
204                 if (!strcmp(name, e->name)) {
205                         printk(KERN_NOTICE
206                                 "tracepoint %s busy\n", name);
207                         return ERR_PTR(-EEXIST);        /* Already there */
208                 }
209         }
210         /*
211          * Using kmalloc here to allocate a variable length element. Could
212          * cause some memory fragmentation if overused.
213          */
214         e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
215         if (!e)
216                 return ERR_PTR(-ENOMEM);
217         memcpy(&e->name[0], name, name_len);
218         e->funcs = NULL;
219         e->refcount = 0;
220         hlist_add_head(&e->hlist, head);
221         return e;
222 }
223
224 /*
225  * Remove the tracepoint from the tracepoint hash table. Must be called with
226  * mutex_lock held.
227  */
228 static inline void remove_tracepoint(struct tracepoint_entry *e)
229 {
230         hlist_del(&e->hlist);
231         kfree(e);
232 }
233
234 /*
235  * Sets the probe callback corresponding to one tracepoint.
236  */
237 static void set_tracepoint(struct tracepoint_entry **entry,
238         struct tracepoint *elem, int active)
239 {
240         WARN_ON(strcmp((*entry)->name, elem->name) != 0);
241
242         /*
243          * rcu_assign_pointer has a smp_wmb() which makes sure that the new
244          * probe callbacks array is consistent before setting a pointer to it.
245          * This array is referenced by __DO_TRACE from
246          * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
247          * is used.
248          */
249         rcu_assign_pointer(elem->funcs, (*entry)->funcs);
250         elem->state = active;
251 }
252
253 /*
254  * Disable a tracepoint and its probe callback.
255  * Note: only waiting an RCU period after setting elem->call to the empty
256  * function insures that the original callback is not used anymore. This insured
257  * by preempt_disable around the call site.
258  */
259 static void disable_tracepoint(struct tracepoint *elem)
260 {
261         elem->state = 0;
262 }
263
264 /**
265  * tracepoint_update_probe_range - Update a probe range
266  * @begin: beginning of the range
267  * @end: end of the range
268  *
269  * Updates the probe callback corresponding to a range of tracepoints.
270  */
271 void tracepoint_update_probe_range(struct tracepoint *begin,
272         struct tracepoint *end)
273 {
274         struct tracepoint *iter;
275         struct tracepoint_entry *mark_entry;
276
277         mutex_lock(&tracepoints_mutex);
278         for (iter = begin; iter < end; iter++) {
279                 mark_entry = get_tracepoint(iter->name);
280                 if (mark_entry) {
281                         set_tracepoint(&mark_entry, iter,
282                                         !!mark_entry->refcount);
283                 } else {
284                         disable_tracepoint(iter);
285                 }
286         }
287         mutex_unlock(&tracepoints_mutex);
288 }
289
290 /*
291  * Update probes, removing the faulty probes.
292  */
293 static void tracepoint_update_probes(void)
294 {
295         /* Core kernel tracepoints */
296         tracepoint_update_probe_range(__start___tracepoints,
297                 __stop___tracepoints);
298         /* tracepoints in modules. */
299         module_update_tracepoints();
300 }
301
302 /**
303  * tracepoint_probe_register -  Connect a probe to a tracepoint
304  * @name: tracepoint name
305  * @probe: probe handler
306  *
307  * Returns 0 if ok, error value on error.
308  * The probe address must at least be aligned on the architecture pointer size.
309  */
310 int tracepoint_probe_register(const char *name, void *probe)
311 {
312         struct tracepoint_entry *entry;
313         int ret = 0;
314         void *old;
315
316         mutex_lock(&tracepoints_mutex);
317         entry = get_tracepoint(name);
318         if (!entry) {
319                 entry = add_tracepoint(name);
320                 if (IS_ERR(entry)) {
321                         ret = PTR_ERR(entry);
322                         goto end;
323                 }
324         }
325         old = tracepoint_entry_add_probe(entry, probe);
326         if (IS_ERR(old)) {
327                 if (!entry->refcount)
328                         remove_tracepoint(entry);
329                 ret = PTR_ERR(old);
330                 goto end;
331         }
332         mutex_unlock(&tracepoints_mutex);
333         tracepoint_update_probes();             /* may update entry */
334         release_probes(old);
335         return 0;
336 end:
337         mutex_unlock(&tracepoints_mutex);
338         return ret;
339 }
340 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
341
342 /**
343  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
344  * @name: tracepoint name
345  * @probe: probe function pointer
346  *
347  * We do not need to call a synchronize_sched to make sure the probes have
348  * finished running before doing a module unload, because the module unload
349  * itself uses stop_machine(), which insures that every preempt disabled section
350  * have finished.
351  */
352 int tracepoint_probe_unregister(const char *name, void *probe)
353 {
354         struct tracepoint_entry *entry;
355         void *old;
356         int ret = -ENOENT;
357
358         mutex_lock(&tracepoints_mutex);
359         entry = get_tracepoint(name);
360         if (!entry)
361                 goto end;
362         old = tracepoint_entry_remove_probe(entry, probe);
363         if (IS_ERR(old)) {
364                 ret = PTR_ERR(old);
365                 goto end;
366         }
367         if (!entry->refcount)
368                 remove_tracepoint(entry);
369         mutex_unlock(&tracepoints_mutex);
370         tracepoint_update_probes();             /* may update entry */
371         release_probes(old);
372         return 0;
373 end:
374         mutex_unlock(&tracepoints_mutex);
375         return ret;
376 }
377 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
378
379 /**
380  * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
381  * @tracepoint: current tracepoints (in), next tracepoint (out)
382  * @begin: beginning of the range
383  * @end: end of the range
384  *
385  * Returns whether a next tracepoint has been found (1) or not (0).
386  * Will return the first tracepoint in the range if the input tracepoint is
387  * NULL.
388  */
389 int tracepoint_get_iter_range(struct tracepoint **tracepoint,
390         struct tracepoint *begin, struct tracepoint *end)
391 {
392         if (!*tracepoint && begin != end) {
393                 *tracepoint = begin;
394                 return 1;
395         }
396         if (*tracepoint >= begin && *tracepoint < end)
397                 return 1;
398         return 0;
399 }
400 EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
401
402 static void tracepoint_get_iter(struct tracepoint_iter *iter)
403 {
404         int found = 0;
405
406         /* Core kernel tracepoints */
407         if (!iter->module) {
408                 found = tracepoint_get_iter_range(&iter->tracepoint,
409                                 __start___tracepoints, __stop___tracepoints);
410                 if (found)
411                         goto end;
412         }
413         /* tracepoints in modules. */
414         found = module_get_iter_tracepoints(iter);
415 end:
416         if (!found)
417                 tracepoint_iter_reset(iter);
418 }
419
420 void tracepoint_iter_start(struct tracepoint_iter *iter)
421 {
422         tracepoint_get_iter(iter);
423 }
424 EXPORT_SYMBOL_GPL(tracepoint_iter_start);
425
426 void tracepoint_iter_next(struct tracepoint_iter *iter)
427 {
428         iter->tracepoint++;
429         /*
430          * iter->tracepoint may be invalid because we blindly incremented it.
431          * Make sure it is valid by marshalling on the tracepoints, getting the
432          * tracepoints from following modules if necessary.
433          */
434         tracepoint_get_iter(iter);
435 }
436 EXPORT_SYMBOL_GPL(tracepoint_iter_next);
437
438 void tracepoint_iter_stop(struct tracepoint_iter *iter)
439 {
440 }
441 EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
442
443 void tracepoint_iter_reset(struct tracepoint_iter *iter)
444 {
445         iter->module = NULL;
446         iter->tracepoint = NULL;
447 }
448 EXPORT_SYMBOL_GPL(tracepoint_iter_reset);