1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
28extern struct tracepoint __start___tracepoints[];
29extern struct tracepoint __stop___tracepoints[];
30
31
32static const int tracepoint_debug;
33
34
35
36
37
38static DEFINE_MUTEX(tracepoints_mutex);
39
40
41
42
43
44#define TRACEPOINT_HASH_BITS 6
45#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
46static struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
47
48
49
50
51
52
53
54struct tracepoint_entry {
55 struct hlist_node hlist;
56 void **funcs;
57 int refcount;
58 char name[0];
59};
60
61struct tp_probes {
62 union {
63 struct rcu_head rcu;
64 struct list_head list;
65 } u;
66 void *probes[0];
67};
68
69static inline void *allocate_probes(int count)
70{
71 struct tp_probes *p = kmalloc(count * sizeof(void *)
72 + sizeof(struct tp_probes), GFP_KERNEL);
73 return p == NULL ? NULL : p->probes;
74}
75
76static void rcu_free_old_probes(struct rcu_head *head)
77{
78 kfree(container_of(head, struct tp_probes, u.rcu));
79}
80
81static inline void release_probes(void *old)
82{
83 if (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);
87 }
88}
89
90static void debug_print_probes(struct tracepoint_entry *entry)
91{
92 int i;
93
94 if (!tracepoint_debug || !entry->funcs)
95 return;
96
97 for (i = 0; entry->funcs[i]; i++)
98 printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i]);
99}
100
101static void *
102tracepoint_entry_add_probe(struct tracepoint_entry *entry, void *probe)
103{
104 int nr_probes = 0;
105 void **old, **new;
106
107 WARN_ON(!probe);
108
109 debug_print_probes(entry);
110 old = entry->funcs;
111 if (old) {
112
113 for (nr_probes = 0; old[nr_probes]; nr_probes++)
114 if (old[nr_probes] == probe)
115 return ERR_PTR(-EEXIST);
116 }
117
118 new = allocate_probes(nr_probes + 2);
119 if (new == NULL)
120 return ERR_PTR(-ENOMEM);
121 if (old)
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;
126 entry->funcs = new;
127 debug_print_probes(entry);
128 return old;
129}
130
131static void *
132tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe)
133{
134 int nr_probes = 0, nr_del = 0, i;
135 void **old, **new;
136
137 old = entry->funcs;
138
139 if (!old)
140 return ERR_PTR(-ENOENT);
141
142 debug_print_probes(entry);
143
144 for (nr_probes = 0; old[nr_probes]; nr_probes++) {
145 if ((!probe || old[nr_probes] == probe))
146 nr_del++;
147 }
148
149 if (nr_probes - nr_del == 0) {
150
151 entry->funcs = NULL;
152 entry->refcount = 0;
153 debug_print_probes(entry);
154 return old;
155 } else {
156 int j = 0;
157
158
159 new = allocate_probes(nr_probes - nr_del + 1);
160 if (new == NULL)
161 return ERR_PTR(-ENOMEM);
162 for (i = 0; old[i]; i++)
163 if ((probe && old[i] != probe))
164 new[j++] = old[i];
165 new[nr_probes - nr_del] = NULL;
166 entry->refcount = nr_probes - nr_del;
167 entry->funcs = new;
168 }
169 debug_print_probes(entry);
170 return old;
171}
172
173
174
175
176
177
178static struct tracepoint_entry *get_tracepoint(const char *name)
179{
180 struct hlist_head *head;
181 struct hlist_node *node;
182 struct tracepoint_entry *e;
183 u32 hash = jhash(name, strlen(name), 0);
184
185 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
186 hlist_for_each_entry(e, node, head, hlist) {
187 if (!strcmp(name, e->name))
188 return e;
189 }
190 return NULL;
191}
192
193
194
195
196
197static struct tracepoint_entry *add_tracepoint(const char *name)
198{
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);
204
205 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
206 hlist_for_each_entry(e, node, head, hlist) {
207 if (!strcmp(name, e->name)) {
208 printk(KERN_NOTICE
209 "tracepoint %s busy\n", name);
210 return ERR_PTR(-EEXIST);
211 }
212 }
213
214
215
216
217 e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
218 if (!e)
219 return ERR_PTR(-ENOMEM);
220 memcpy(&e->name[0], name, name_len);
221 e->funcs = NULL;
222 e->refcount = 0;
223 hlist_add_head(&e->hlist, head);
224 return e;
225}
226
227
228
229
230
231static inline void remove_tracepoint(struct tracepoint_entry *e)
232{
233 hlist_del(&e->hlist);
234 kfree(e);
235}
236
237
238
239
240static void set_tracepoint(struct tracepoint_entry **entry,
241 struct tracepoint *elem, int active)
242{
243 WARN_ON(strcmp((*entry)->name, elem->name) != 0);
244
245
246
247
248
249
250
251
252 rcu_assign_pointer(elem->funcs, (*entry)->funcs);
253 elem->state = active;
254}
255
256
257
258
259
260
261
262static void disable_tracepoint(struct tracepoint *elem)
263{
264 elem->state = 0;
265 rcu_assign_pointer(elem->funcs, NULL);
266}
267
268
269
270
271
272
273
274
275void
276tracepoint_update_probe_range(struct tracepoint *begin, struct tracepoint *end)
277{
278 struct tracepoint *iter;
279 struct tracepoint_entry *mark_entry;
280
281 if (!begin)
282 return;
283
284 mutex_lock(&tracepoints_mutex);
285 for (iter = begin; iter < end; iter++) {
286 mark_entry = get_tracepoint(iter->name);
287 if (mark_entry) {
288 set_tracepoint(&mark_entry, iter,
289 !!mark_entry->refcount);
290 } else {
291 disable_tracepoint(iter);
292 }
293 }
294 mutex_unlock(&tracepoints_mutex);
295}
296
297
298
299
300static void tracepoint_update_probes(void)
301{
302
303 tracepoint_update_probe_range(__start___tracepoints,
304 __stop___tracepoints);
305
306 module_update_tracepoints();
307}
308
309static void *tracepoint_add_probe(const char *name, void *probe)
310{
311 struct tracepoint_entry *entry;
312 void *old;
313
314 entry = get_tracepoint(name);
315 if (!entry) {
316 entry = add_tracepoint(name);
317 if (IS_ERR(entry))
318 return entry;
319 }
320 old = tracepoint_entry_add_probe(entry, probe);
321 if (IS_ERR(old) && !entry->refcount)
322 remove_tracepoint(entry);
323 return old;
324}
325
326
327
328
329
330
331
332
333
334int tracepoint_probe_register(const char *name, void *probe)
335{
336 void *old;
337
338 mutex_lock(&tracepoints_mutex);
339 old = tracepoint_add_probe(name, probe);
340 mutex_unlock(&tracepoints_mutex);
341 if (IS_ERR(old))
342 return PTR_ERR(old);
343
344 tracepoint_update_probes();
345 release_probes(old);
346 return 0;
347}
348EXPORT_SYMBOL_GPL(tracepoint_probe_register);
349
350static void *tracepoint_remove_probe(const char *name, void *probe)
351{
352 struct tracepoint_entry *entry;
353 void *old;
354
355 entry = get_tracepoint(name);
356 if (!entry)
357 return ERR_PTR(-ENOENT);
358 old = tracepoint_entry_remove_probe(entry, probe);
359 if (IS_ERR(old))
360 return old;
361 if (!entry->refcount)
362 remove_tracepoint(entry);
363 return old;
364}
365
366
367
368
369
370
371
372
373
374
375
376int tracepoint_probe_unregister(const char *name, void *probe)
377{
378 void *old;
379
380 mutex_lock(&tracepoints_mutex);
381 old = tracepoint_remove_probe(name, probe);
382 mutex_unlock(&tracepoints_mutex);
383 if (IS_ERR(old))
384 return PTR_ERR(old);
385
386 tracepoint_update_probes();
387 release_probes(old);
388 return 0;
389}
390EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
391
392static LIST_HEAD(old_probes);
393static int need_update;
394
395static void tracepoint_add_old_probes(void *old)
396{
397 need_update = 1;
398 if (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);
402 }
403}
404
405
406
407
408
409
410
411
412int tracepoint_probe_register_noupdate(const char *name, void *probe)
413{
414 void *old;
415
416 mutex_lock(&tracepoints_mutex);
417 old = tracepoint_add_probe(name, probe);
418 if (IS_ERR(old)) {
419 mutex_unlock(&tracepoints_mutex);
420 return PTR_ERR(old);
421 }
422 tracepoint_add_old_probes(old);
423 mutex_unlock(&tracepoints_mutex);
424 return 0;
425}
426EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
427
428
429
430
431
432
433
434
435int tracepoint_probe_unregister_noupdate(const char *name, void *probe)
436{
437 void *old;
438
439 mutex_lock(&tracepoints_mutex);
440 old = tracepoint_remove_probe(name, probe);
441 if (IS_ERR(old)) {
442 mutex_unlock(&tracepoints_mutex);
443 return PTR_ERR(old);
444 }
445 tracepoint_add_old_probes(old);
446 mutex_unlock(&tracepoints_mutex);
447 return 0;
448}
449EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
450
451
452
453
454void tracepoint_probe_update_all(void)
455{
456 LIST_HEAD(release_probes);
457 struct tp_probes *pos, *next;
458
459 mutex_lock(&tracepoints_mutex);
460 if (!need_update) {
461 mutex_unlock(&tracepoints_mutex);
462 return;
463 }
464 if (!list_empty(&old_probes))
465 list_replace_init(&old_probes, &release_probes);
466 need_update = 0;
467 mutex_unlock(&tracepoints_mutex);
468
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);
473 }
474}
475EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
476
477
478
479
480
481
482
483
484
485
486
487int tracepoint_get_iter_range(struct tracepoint **tracepoint,
488 struct tracepoint *begin, struct tracepoint *end)
489{
490 if (!*tracepoint && begin != end) {
491 *tracepoint = begin;
492 return 1;
493 }
494 if (*tracepoint >= begin && *tracepoint < end)
495 return 1;
496 return 0;
497}
498EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
499
500static void tracepoint_get_iter(struct tracepoint_iter *iter)
501{
502 int found = 0;
503
504
505 if (!iter->module) {
506 found = tracepoint_get_iter_range(&iter->tracepoint,
507 __start___tracepoints, __stop___tracepoints);
508 if (found)
509 goto end;
510 }
511
512 found = module_get_iter_tracepoints(iter);
513end:
514 if (!found)
515 tracepoint_iter_reset(iter);
516}
517
518void tracepoint_iter_start(struct tracepoint_iter *iter)
519{
520 tracepoint_get_iter(iter);
521}
522EXPORT_SYMBOL_GPL(tracepoint_iter_start);
523
524void tracepoint_iter_next(struct tracepoint_iter *iter)
525{
526 iter->tracepoint++;
527
528
529
530
531
532 tracepoint_get_iter(iter);
533}
534EXPORT_SYMBOL_GPL(tracepoint_iter_next);
535
536void tracepoint_iter_stop(struct tracepoint_iter *iter)
537{
538}
539EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
540
541void tracepoint_iter_reset(struct tracepoint_iter *iter)
542{
543 iter->module = NULL;
544 iter->tracepoint = NULL;
545}
546EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
547
548#ifdef CONFIG_MODULES
549
550int tracepoint_module_notify(struct notifier_block *self,
551 unsigned long val, void *data)
552{
553 struct module *mod = data;
554
555 switch (val) {
556 case MODULE_STATE_COMING:
557 tracepoint_update_probe_range(mod->tracepoints,
558 mod->tracepoints + mod->num_tracepoints);
559 break;
560 case MODULE_STATE_GOING:
561 tracepoint_update_probe_range(mod->tracepoints,
562 mod->tracepoints + mod->num_tracepoints);
563 break;
564 }
565 return 0;
566}
567
568struct notifier_block tracepoint_module_nb = {
569 .notifier_call = tracepoint_module_notify,
570 .priority = 0,
571};
572
573static int init_tracepoints(void)
574{
575 return register_module_notifier(&tracepoint_module_nb);
576}
577__initcall(init_tracepoints);
578
579#endif
580