markers: add comment
[ust.git] / libust / marker.c
1 /*
2 * Copyright (C) 2007 Mathieu Desnoyers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 //ust// #include <linux/module.h>
19 //ust// #include <linux/mutex.h>
20 //ust// #include <linux/types.h>
21 //#include "jhash.h"
22 //#include "list.h"
23 //#include "rcupdate.h"
24 //ust// #include <linux/marker.h>
25 #include <errno.h>
26 //ust// #include <linux/slab.h>
27 //ust// #include <linux/immediate.h>
28 //ust// #include <linux/sched.h>
29 //ust// #include <linux/uaccess.h>
30 //ust// #include <linux/user_marker.h>
31 //ust// #include <linux/ltt-tracer.h>
32
33 #define _LGPL_SOURCE
34 #include <urcu-bp.h>
35
36 #include "kernelcompat.h"
37
38 #include "marker.h"
39 #include "usterr.h"
40 #include "channels.h"
41 #include "tracercore.h"
42 #include "tracer.h"
43
44 extern struct marker __start___markers[] __attribute__((visibility("hidden")));
45 extern struct marker __stop___markers[] __attribute__((visibility("hidden")));
46
47 /* Set to 1 to enable marker debug output */
48 static const int marker_debug;
49
50 /*
51 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
52 * and module markers and the hash table.
53 */
54 static DEFINE_MUTEX(markers_mutex);
55
56 static LIST_HEAD(libs);
57
58
59 void lock_markers(void)
60 {
61 mutex_lock(&markers_mutex);
62 }
63
64 void unlock_markers(void)
65 {
66 mutex_unlock(&markers_mutex);
67 }
68
69 /*
70 * Marker hash table, containing the active markers.
71 * Protected by module_mutex.
72 */
73 #define MARKER_HASH_BITS 6
74 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
75 static struct hlist_head marker_table[MARKER_TABLE_SIZE];
76
77 /*
78 * Note about RCU :
79 * It is used to make sure every handler has finished using its private data
80 * between two consecutive operation (add or remove) on a given marker. It is
81 * also used to delay the free of multiple probes array until a quiescent state
82 * is reached.
83 * marker entries modifications are protected by the markers_mutex.
84 */
85 struct marker_entry {
86 struct hlist_node hlist;
87 char *format;
88 char *name;
89 /* Probe wrapper */
90 void (*call)(const struct marker *mdata, void *call_private, ...);
91 struct marker_probe_closure single;
92 struct marker_probe_closure *multi;
93 int refcount; /* Number of times armed. 0 if disarmed. */
94 struct rcu_head rcu;
95 void *oldptr;
96 int rcu_pending;
97 u16 channel_id;
98 u16 event_id;
99 unsigned char ptype:1;
100 unsigned char format_allocated:1;
101 char channel[0]; /* Contains channel'\0'name'\0'format'\0' */
102 };
103
104 #ifdef CONFIG_MARKERS_USERSPACE
105 static void marker_update_processes(void);
106 #else
107 static void marker_update_processes(void)
108 {
109 }
110 #endif
111
112 /**
113 * __mark_empty_function - Empty probe callback
114 * @mdata: marker data
115 * @probe_private: probe private data
116 * @call_private: call site private data
117 * @fmt: format string
118 * @...: variable argument list
119 *
120 * Empty callback provided as a probe to the markers. By providing this to a
121 * disabled marker, we make sure the execution flow is always valid even
122 * though the function pointer change and the marker enabling are two distinct
123 * operations that modifies the execution flow of preemptible code.
124 */
125 notrace void __mark_empty_function(const struct marker *mdata,
126 void *probe_private, void *call_private, const char *fmt, va_list *args)
127 {
128 }
129 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
130
131 /*
132 * marker_probe_cb Callback that prepares the variable argument list for probes.
133 * @mdata: pointer of type struct marker
134 * @call_private: caller site private data
135 * @...: Variable argument list.
136 *
137 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
138 * need to put a full smp_rmb() in this branch. This is why we do not use
139 * rcu_dereference() for the pointer read.
140 */
141 notrace void marker_probe_cb(const struct marker *mdata,
142 void *call_private, ...)
143 {
144 va_list args;
145 char ptype;
146
147 /*
148 * rcu_read_lock_sched does two things : disabling preemption to make
149 * sure the teardown of the callbacks can be done correctly when they
150 * are in modules and they insure RCU read coherency.
151 */
152 //ust// rcu_read_lock_sched_notrace();
153 ptype = mdata->ptype;
154 if (likely(!ptype)) {
155 marker_probe_func *func;
156 /* Must read the ptype before ptr. They are not data dependant,
157 * so we put an explicit smp_rmb() here. */
158 smp_rmb();
159 func = mdata->single.func;
160 /* Must read the ptr before private data. They are not data
161 * dependant, so we put an explicit smp_rmb() here. */
162 smp_rmb();
163 va_start(args, call_private);
164 func(mdata, mdata->single.probe_private, call_private,
165 mdata->format, &args);
166 va_end(args);
167 } else {
168 struct marker_probe_closure *multi;
169 int i;
170 /*
171 * Read mdata->ptype before mdata->multi.
172 */
173 smp_rmb();
174 multi = mdata->multi;
175 /*
176 * multi points to an array, therefore accessing the array
177 * depends on reading multi. However, even in this case,
178 * we must insure that the pointer is read _before_ the array
179 * data. Same as rcu_dereference, but we need a full smp_rmb()
180 * in the fast path, so put the explicit barrier here.
181 */
182 smp_read_barrier_depends();
183 for (i = 0; multi[i].func; i++) {
184 va_start(args, call_private);
185 multi[i].func(mdata, multi[i].probe_private,
186 call_private, mdata->format, &args);
187 va_end(args);
188 }
189 }
190 //ust// rcu_read_unlock_sched_notrace();
191 }
192 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
193
194 /*
195 * marker_probe_cb Callback that does not prepare the variable argument list.
196 * @mdata: pointer of type struct marker
197 * @call_private: caller site private data
198 * @...: Variable argument list.
199 *
200 * Should be connected to markers "MARK_NOARGS".
201 */
202 static notrace void marker_probe_cb_noarg(const struct marker *mdata,
203 void *call_private, ...)
204 {
205 va_list args; /* not initialized */
206 char ptype;
207
208 //ust// rcu_read_lock_sched_notrace();
209 ptype = mdata->ptype;
210 if (likely(!ptype)) {
211 marker_probe_func *func;
212 /* Must read the ptype before ptr. They are not data dependant,
213 * so we put an explicit smp_rmb() here. */
214 smp_rmb();
215 func = mdata->single.func;
216 /* Must read the ptr before private data. They are not data
217 * dependant, so we put an explicit smp_rmb() here. */
218 smp_rmb();
219 func(mdata, mdata->single.probe_private, call_private,
220 mdata->format, &args);
221 } else {
222 struct marker_probe_closure *multi;
223 int i;
224 /*
225 * Read mdata->ptype before mdata->multi.
226 */
227 smp_rmb();
228 multi = mdata->multi;
229 /*
230 * multi points to an array, therefore accessing the array
231 * depends on reading multi. However, even in this case,
232 * we must insure that the pointer is read _before_ the array
233 * data. Same as rcu_dereference, but we need a full smp_rmb()
234 * in the fast path, so put the explicit barrier here.
235 */
236 smp_read_barrier_depends();
237 for (i = 0; multi[i].func; i++)
238 multi[i].func(mdata, multi[i].probe_private,
239 call_private, mdata->format, &args);
240 }
241 //ust// rcu_read_unlock_sched_notrace();
242 }
243
244 static void free_old_closure(struct rcu_head *head)
245 {
246 struct marker_entry *entry = container_of(head,
247 struct marker_entry, rcu);
248 kfree(entry->oldptr);
249 /* Make sure we free the data before setting the pending flag to 0 */
250 smp_wmb();
251 entry->rcu_pending = 0;
252 }
253
254 static void debug_print_probes(struct marker_entry *entry)
255 {
256 int i;
257
258 if (!marker_debug)
259 return;
260
261 if (!entry->ptype) {
262 printk(KERN_DEBUG "Single probe : %p %p\n",
263 entry->single.func,
264 entry->single.probe_private);
265 } else {
266 for (i = 0; entry->multi[i].func; i++)
267 printk(KERN_DEBUG "Multi probe %d : %p %p\n", i,
268 entry->multi[i].func,
269 entry->multi[i].probe_private);
270 }
271 }
272
273 static struct marker_probe_closure *
274 marker_entry_add_probe(struct marker_entry *entry,
275 marker_probe_func *probe, void *probe_private)
276 {
277 int nr_probes = 0;
278 struct marker_probe_closure *old, *new;
279
280 WARN_ON(!probe);
281
282 debug_print_probes(entry);
283 old = entry->multi;
284 if (!entry->ptype) {
285 if (entry->single.func == probe &&
286 entry->single.probe_private == probe_private)
287 return ERR_PTR(-EBUSY);
288 if (entry->single.func == __mark_empty_function) {
289 /* 0 -> 1 probes */
290 entry->single.func = probe;
291 entry->single.probe_private = probe_private;
292 entry->refcount = 1;
293 entry->ptype = 0;
294 debug_print_probes(entry);
295 return NULL;
296 } else {
297 /* 1 -> 2 probes */
298 nr_probes = 1;
299 old = NULL;
300 }
301 } else {
302 /* (N -> N+1), (N != 0, 1) probes */
303 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
304 if (old[nr_probes].func == probe
305 && old[nr_probes].probe_private
306 == probe_private)
307 return ERR_PTR(-EBUSY);
308 }
309 /* + 2 : one for new probe, one for NULL func */
310 new = kzalloc((nr_probes + 2) * sizeof(struct marker_probe_closure),
311 GFP_KERNEL);
312 if (new == NULL)
313 return ERR_PTR(-ENOMEM);
314 if (!old)
315 new[0] = entry->single;
316 else
317 memcpy(new, old,
318 nr_probes * sizeof(struct marker_probe_closure));
319 new[nr_probes].func = probe;
320 new[nr_probes].probe_private = probe_private;
321 entry->refcount = nr_probes + 1;
322 entry->multi = new;
323 entry->ptype = 1;
324 debug_print_probes(entry);
325 return old;
326 }
327
328 static struct marker_probe_closure *
329 marker_entry_remove_probe(struct marker_entry *entry,
330 marker_probe_func *probe, void *probe_private)
331 {
332 int nr_probes = 0, nr_del = 0, i;
333 struct marker_probe_closure *old, *new;
334
335 old = entry->multi;
336
337 debug_print_probes(entry);
338 if (!entry->ptype) {
339 /* 0 -> N is an error */
340 WARN_ON(entry->single.func == __mark_empty_function);
341 /* 1 -> 0 probes */
342 WARN_ON(probe && entry->single.func != probe);
343 WARN_ON(entry->single.probe_private != probe_private);
344 entry->single.func = __mark_empty_function;
345 entry->refcount = 0;
346 entry->ptype = 0;
347 debug_print_probes(entry);
348 return NULL;
349 } else {
350 /* (N -> M), (N > 1, M >= 0) probes */
351 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
352 if ((!probe || old[nr_probes].func == probe)
353 && old[nr_probes].probe_private
354 == probe_private)
355 nr_del++;
356 }
357 }
358
359 if (nr_probes - nr_del == 0) {
360 /* N -> 0, (N > 1) */
361 entry->single.func = __mark_empty_function;
362 entry->refcount = 0;
363 entry->ptype = 0;
364 } else if (nr_probes - nr_del == 1) {
365 /* N -> 1, (N > 1) */
366 for (i = 0; old[i].func; i++)
367 if ((probe && old[i].func != probe) ||
368 old[i].probe_private != probe_private)
369 entry->single = old[i];
370 entry->refcount = 1;
371 entry->ptype = 0;
372 } else {
373 int j = 0;
374 /* N -> M, (N > 1, M > 1) */
375 /* + 1 for NULL */
376 new = kzalloc((nr_probes - nr_del + 1)
377 * sizeof(struct marker_probe_closure), GFP_KERNEL);
378 if (new == NULL)
379 return ERR_PTR(-ENOMEM);
380 for (i = 0; old[i].func; i++)
381 if ((probe && old[i].func != probe) ||
382 old[i].probe_private != probe_private)
383 new[j++] = old[i];
384 entry->refcount = nr_probes - nr_del;
385 entry->ptype = 1;
386 entry->multi = new;
387 }
388 debug_print_probes(entry);
389 return old;
390 }
391
392 /*
393 * Get marker if the marker is present in the marker hash table.
394 * Must be called with markers_mutex held.
395 * Returns NULL if not present.
396 */
397 static struct marker_entry *get_marker(const char *channel, const char *name)
398 {
399 struct hlist_head *head;
400 struct hlist_node *node;
401 struct marker_entry *e;
402 size_t channel_len = strlen(channel) + 1;
403 size_t name_len = strlen(name) + 1;
404 u32 hash;
405
406 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
407 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
408 hlist_for_each_entry(e, node, head, hlist) {
409 if (!strcmp(channel, e->channel) && !strcmp(name, e->name))
410 return e;
411 }
412 return NULL;
413 }
414
415 /*
416 * Add the marker to the marker hash table. Must be called with markers_mutex
417 * held.
418 */
419 static struct marker_entry *add_marker(const char *channel, const char *name,
420 const char *format)
421 {
422 struct hlist_head *head;
423 struct hlist_node *node;
424 struct marker_entry *e;
425 size_t channel_len = strlen(channel) + 1;
426 size_t name_len = strlen(name) + 1;
427 size_t format_len = 0;
428 u32 hash;
429
430 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
431 if (format)
432 format_len = strlen(format) + 1;
433 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
434 hlist_for_each_entry(e, node, head, hlist) {
435 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
436 printk(KERN_NOTICE
437 "Marker %s.%s busy\n", channel, name);
438 return ERR_PTR(-EBUSY); /* Already there */
439 }
440 }
441 /*
442 * Using kmalloc here to allocate a variable length element. Could
443 * cause some memory fragmentation if overused.
444 */
445 e = kmalloc(sizeof(struct marker_entry)
446 + channel_len + name_len + format_len,
447 GFP_KERNEL);
448 if (!e)
449 return ERR_PTR(-ENOMEM);
450 memcpy(e->channel, channel, channel_len);
451 e->name = &e->channel[channel_len];
452 memcpy(e->name, name, name_len);
453 if (format) {
454 e->format = &e->name[channel_len + name_len];
455 memcpy(e->format, format, format_len);
456 if (strcmp(e->format, MARK_NOARGS) == 0)
457 e->call = marker_probe_cb_noarg;
458 else
459 e->call = marker_probe_cb;
460 trace_mark(metadata, core_marker_format,
461 "channel %s name %s format %s",
462 e->channel, e->name, e->format);
463 } else {
464 e->format = NULL;
465 e->call = marker_probe_cb;
466 }
467 e->single.func = __mark_empty_function;
468 e->single.probe_private = NULL;
469 e->multi = NULL;
470 e->ptype = 0;
471 e->format_allocated = 0;
472 e->refcount = 0;
473 e->rcu_pending = 0;
474 hlist_add_head(&e->hlist, head);
475 return e;
476 }
477
478 /*
479 * Remove the marker from the marker hash table. Must be called with mutex_lock
480 * held.
481 */
482 static int remove_marker(const char *channel, const char *name)
483 {
484 struct hlist_head *head;
485 struct hlist_node *node;
486 struct marker_entry *e;
487 int found = 0;
488 size_t channel_len = strlen(channel) + 1;
489 size_t name_len = strlen(name) + 1;
490 u32 hash;
491 int ret;
492
493 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
494 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
495 hlist_for_each_entry(e, node, head, hlist) {
496 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
497 found = 1;
498 break;
499 }
500 }
501 if (!found)
502 return -ENOENT;
503 if (e->single.func != __mark_empty_function)
504 return -EBUSY;
505 hlist_del(&e->hlist);
506 if (e->format_allocated)
507 kfree(e->format);
508 ret = ltt_channels_unregister(e->channel);
509 WARN_ON(ret);
510 /* Make sure the call_rcu has been executed */
511 //ust// if (e->rcu_pending)
512 //ust// rcu_barrier_sched();
513 kfree(e);
514 return 0;
515 }
516
517 /*
518 * Set the mark_entry format to the format found in the element.
519 */
520 static int marker_set_format(struct marker_entry *entry, const char *format)
521 {
522 entry->format = kstrdup(format, GFP_KERNEL);
523 if (!entry->format)
524 return -ENOMEM;
525 entry->format_allocated = 1;
526
527 trace_mark(metadata, core_marker_format,
528 "channel %s name %s format %s",
529 entry->channel, entry->name, entry->format);
530 return 0;
531 }
532
533 /*
534 * Sets the probe callback corresponding to one marker.
535 */
536 static int set_marker(struct marker_entry *entry, struct marker *elem,
537 int active)
538 {
539 int ret = 0;
540 WARN_ON(strcmp(entry->name, elem->name) != 0);
541
542 if (entry->format) {
543 if (strcmp(entry->format, elem->format) != 0) {
544 printk(KERN_NOTICE
545 "Format mismatch for probe %s "
546 "(%s), marker (%s)\n",
547 entry->name,
548 entry->format,
549 elem->format);
550 return -EPERM;
551 }
552 } else {
553 ret = marker_set_format(entry, elem->format);
554 if (ret)
555 return ret;
556 }
557
558 /*
559 * probe_cb setup (statically known) is done here. It is
560 * asynchronous with the rest of execution, therefore we only
561 * pass from a "safe" callback (with argument) to an "unsafe"
562 * callback (does not set arguments).
563 */
564 elem->call = entry->call;
565 elem->channel_id = entry->channel_id;
566 elem->event_id = entry->event_id;
567 /*
568 * Sanity check :
569 * We only update the single probe private data when the ptr is
570 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
571 */
572 WARN_ON(elem->single.func != __mark_empty_function
573 && elem->single.probe_private != entry->single.probe_private
574 && !elem->ptype);
575 elem->single.probe_private = entry->single.probe_private;
576 /*
577 * Make sure the private data is valid when we update the
578 * single probe ptr.
579 */
580 smp_wmb();
581 elem->single.func = entry->single.func;
582 /*
583 * We also make sure that the new probe callbacks array is consistent
584 * before setting a pointer to it.
585 */
586 rcu_assign_pointer(elem->multi, entry->multi);
587 /*
588 * Update the function or multi probe array pointer before setting the
589 * ptype.
590 */
591 smp_wmb();
592 elem->ptype = entry->ptype;
593
594 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
595 //ust// WARN_ON(!elem->tp_cb);
596 //ust// /*
597 //ust// * It is ok to directly call the probe registration because type
598 //ust// * checking has been done in the __trace_mark_tp() macro.
599 //ust// */
600 //ust//
601 //ust// if (active) {
602 //ust// /*
603 //ust// * try_module_get should always succeed because we hold
604 //ust// * markers_mutex to get the tp_cb address.
605 //ust// */
606 //ust// ret = try_module_get(__module_text_address(
607 //ust// (unsigned long)elem->tp_cb));
608 //ust// BUG_ON(!ret);
609 //ust// ret = tracepoint_probe_register_noupdate(
610 //ust// elem->tp_name,
611 //ust// elem->tp_cb);
612 //ust// } else {
613 //ust// ret = tracepoint_probe_unregister_noupdate(
614 //ust// elem->tp_name,
615 //ust// elem->tp_cb);
616 //ust// /*
617 //ust// * tracepoint_probe_update_all() must be called
618 //ust// * before the module containing tp_cb is unloaded.
619 //ust// */
620 //ust// module_put(__module_text_address(
621 //ust// (unsigned long)elem->tp_cb));
622 //ust// }
623 //ust// }
624 elem->state__imv = active;
625
626 return ret;
627 }
628
629 /*
630 * Disable a marker and its probe callback.
631 * Note: only waiting an RCU period after setting elem->call to the empty
632 * function insures that the original callback is not used anymore. This insured
633 * by rcu_read_lock_sched around the call site.
634 */
635 static void disable_marker(struct marker *elem)
636 {
637 //ust// int ret;
638 //ust//
639 //ust// /* leave "call" as is. It is known statically. */
640 //ust// if (elem->tp_name && _imv_read(elem->state)) {
641 //ust// WARN_ON(!elem->tp_cb);
642 //ust// /*
643 //ust// * It is ok to directly call the probe registration because type
644 //ust// * checking has been done in the __trace_mark_tp() macro.
645 //ust// */
646 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
647 //ust// elem->tp_cb);
648 //ust// WARN_ON(ret);
649 //ust// /*
650 //ust// * tracepoint_probe_update_all() must be called
651 //ust// * before the module containing tp_cb is unloaded.
652 //ust// */
653 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
654 //ust// }
655 elem->state__imv = 0;
656 elem->single.func = __mark_empty_function;
657 /* Update the function before setting the ptype */
658 smp_wmb();
659 elem->ptype = 0; /* single probe */
660 /*
661 * Leave the private data and channel_id/event_id there, because removal
662 * is racy and should be done only after an RCU period. These are never
663 * used until the next initialization anyway.
664 */
665 }
666
667 /**
668 * marker_update_probe_range - Update a probe range
669 * @begin: beginning of the range
670 * @end: end of the range
671 *
672 * Updates the probe callback corresponding to a range of markers.
673 */
674 void marker_update_probe_range(struct marker *begin,
675 struct marker *end)
676 {
677 struct marker *iter;
678 struct marker_entry *mark_entry;
679
680 mutex_lock(&markers_mutex);
681 for (iter = begin; iter < end; iter++) {
682 mark_entry = get_marker(iter->channel, iter->name);
683 if (mark_entry) {
684 set_marker(mark_entry, iter, !!mark_entry->refcount);
685 /*
686 * ignore error, continue
687 */
688
689 /* This is added for UST. We emit a core_marker_id event
690 * for markers that are already registered to a probe
691 * upon library load. Otherwise, no core_marker_id will
692 * be generated for these markers. Is this the right thing
693 * to do?
694 */
695 trace_mark(metadata, core_marker_id,
696 "channel %s name %s event_id %hu "
697 "int #1u%zu long #1u%zu pointer #1u%zu "
698 "size_t #1u%zu alignment #1u%u",
699 iter->channel, iter->name, mark_entry->event_id,
700 sizeof(int), sizeof(long), sizeof(void *),
701 sizeof(size_t), ltt_get_alignment());
702 } else {
703 disable_marker(iter);
704 }
705 }
706 mutex_unlock(&markers_mutex);
707 }
708
709 static void lib_update_markers(void)
710 {
711 struct lib *lib;
712
713 /* FIXME: we should probably take a mutex here on libs */
714 //ust// mutex_lock(&module_mutex);
715 list_for_each_entry(lib, &libs, list)
716 marker_update_probe_range(lib->markers_start,
717 lib->markers_start + lib->markers_count);
718 //ust// mutex_unlock(&module_mutex);
719 }
720
721 /*
722 * Update probes, removing the faulty probes.
723 *
724 * Internal callback only changed before the first probe is connected to it.
725 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
726 * transitions. All other transitions will leave the old private data valid.
727 * This makes the non-atomicity of the callback/private data updates valid.
728 *
729 * "special case" updates :
730 * 0 -> 1 callback
731 * 1 -> 0 callback
732 * 1 -> 2 callbacks
733 * 2 -> 1 callbacks
734 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
735 * Site effect : marker_set_format may delete the marker entry (creating a
736 * replacement).
737 */
738 static void marker_update_probes(void)
739 {
740 /* Core kernel markers */
741 //ust// marker_update_probe_range(__start___markers, __stop___markers);
742 /* Markers in modules. */
743 //ust// module_update_markers();
744 lib_update_markers();
745 //ust// tracepoint_probe_update_all();
746 /* Update immediate values */
747 core_imv_update();
748 //ust// module_imv_update(); /* FIXME: need to port for libs? */
749 marker_update_processes();
750 }
751
752 /**
753 * marker_probe_register - Connect a probe to a marker
754 * @channel: marker channel
755 * @name: marker name
756 * @format: format string
757 * @probe: probe handler
758 * @probe_private: probe private data
759 *
760 * private data must be a valid allocated memory address, or NULL.
761 * Returns 0 if ok, error value on error.
762 * The probe address must at least be aligned on the architecture pointer size.
763 */
764 int marker_probe_register(const char *channel, const char *name,
765 const char *format, marker_probe_func *probe,
766 void *probe_private)
767 {
768 struct marker_entry *entry;
769 int ret = 0, ret_err;
770 struct marker_probe_closure *old;
771 int first_probe = 0;
772
773 mutex_lock(&markers_mutex);
774 entry = get_marker(channel, name);
775 if (!entry) {
776 first_probe = 1;
777 entry = add_marker(channel, name, format);
778 if (IS_ERR(entry))
779 ret = PTR_ERR(entry);
780 if (ret)
781 goto end;
782 ret = ltt_channels_register(channel);
783 if (ret)
784 goto error_remove_marker;
785 ret = ltt_channels_get_index_from_name(channel);
786 if (ret < 0)
787 goto error_unregister_channel;
788 entry->channel_id = ret;
789 ret = ltt_channels_get_event_id(channel, name);
790 if (ret < 0)
791 goto error_unregister_channel;
792 entry->event_id = ret;
793 ret = 0;
794 trace_mark(metadata, core_marker_id,
795 "channel %s name %s event_id %hu "
796 "int #1u%zu long #1u%zu pointer #1u%zu "
797 "size_t #1u%zu alignment #1u%u",
798 channel, name, entry->event_id,
799 sizeof(int), sizeof(long), sizeof(void *),
800 sizeof(size_t), ltt_get_alignment());
801 } else if (format) {
802 if (!entry->format)
803 ret = marker_set_format(entry, format);
804 else if (strcmp(entry->format, format))
805 ret = -EPERM;
806 if (ret)
807 goto end;
808 }
809
810 /*
811 * If we detect that a call_rcu is pending for this marker,
812 * make sure it's executed now.
813 */
814 //ust// if (entry->rcu_pending)
815 //ust// rcu_barrier_sched();
816 old = marker_entry_add_probe(entry, probe, probe_private);
817 if (IS_ERR(old)) {
818 ret = PTR_ERR(old);
819 if (first_probe)
820 goto error_unregister_channel;
821 else
822 goto end;
823 }
824 mutex_unlock(&markers_mutex);
825
826 /* Activate marker if necessary */
827 marker_update_probes();
828
829 mutex_lock(&markers_mutex);
830 entry = get_marker(channel, name);
831 if (!entry)
832 goto end;
833 //ust// if (entry->rcu_pending)
834 //ust// rcu_barrier_sched();
835 entry->oldptr = old;
836 entry->rcu_pending = 1;
837 /* write rcu_pending before calling the RCU callback */
838 smp_wmb();
839 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
840 synchronize_rcu(); free_old_closure(&entry->rcu);
841 goto end;
842
843 error_unregister_channel:
844 ret_err = ltt_channels_unregister(channel);
845 WARN_ON(ret_err);
846 error_remove_marker:
847 ret_err = remove_marker(channel, name);
848 WARN_ON(ret_err);
849 end:
850 mutex_unlock(&markers_mutex);
851 return ret;
852 }
853 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
854
855 /**
856 * marker_probe_unregister - Disconnect a probe from a marker
857 * @channel: marker channel
858 * @name: marker name
859 * @probe: probe function pointer
860 * @probe_private: probe private data
861 *
862 * Returns the private data given to marker_probe_register, or an ERR_PTR().
863 * We do not need to call a synchronize_sched to make sure the probes have
864 * finished running before doing a module unload, because the module unload
865 * itself uses stop_machine(), which insures that every preempt disabled section
866 * have finished.
867 */
868 int marker_probe_unregister(const char *channel, const char *name,
869 marker_probe_func *probe, void *probe_private)
870 {
871 struct marker_entry *entry;
872 struct marker_probe_closure *old;
873 int ret = -ENOENT;
874
875 mutex_lock(&markers_mutex);
876 entry = get_marker(channel, name);
877 if (!entry)
878 goto end;
879 //ust// if (entry->rcu_pending)
880 //ust// rcu_barrier_sched();
881 old = marker_entry_remove_probe(entry, probe, probe_private);
882 mutex_unlock(&markers_mutex);
883
884 marker_update_probes();
885
886 mutex_lock(&markers_mutex);
887 entry = get_marker(channel, name);
888 if (!entry)
889 goto end;
890 //ust// if (entry->rcu_pending)
891 //ust// rcu_barrier_sched();
892 entry->oldptr = old;
893 entry->rcu_pending = 1;
894 /* write rcu_pending before calling the RCU callback */
895 smp_wmb();
896 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
897 synchronize_rcu(); free_old_closure(&entry->rcu);
898 remove_marker(channel, name); /* Ignore busy error message */
899 ret = 0;
900 end:
901 mutex_unlock(&markers_mutex);
902 return ret;
903 }
904 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
905
906 static struct marker_entry *
907 get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
908 {
909 struct marker_entry *entry;
910 unsigned int i;
911 struct hlist_head *head;
912 struct hlist_node *node;
913
914 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
915 head = &marker_table[i];
916 hlist_for_each_entry(entry, node, head, hlist) {
917 if (!entry->ptype) {
918 if (entry->single.func == probe
919 && entry->single.probe_private
920 == probe_private)
921 return entry;
922 } else {
923 struct marker_probe_closure *closure;
924 closure = entry->multi;
925 for (i = 0; closure[i].func; i++) {
926 if (closure[i].func == probe &&
927 closure[i].probe_private
928 == probe_private)
929 return entry;
930 }
931 }
932 }
933 }
934 return NULL;
935 }
936
937 /**
938 * marker_probe_unregister_private_data - Disconnect a probe from a marker
939 * @probe: probe function
940 * @probe_private: probe private data
941 *
942 * Unregister a probe by providing the registered private data.
943 * Only removes the first marker found in hash table.
944 * Return 0 on success or error value.
945 * We do not need to call a synchronize_sched to make sure the probes have
946 * finished running before doing a module unload, because the module unload
947 * itself uses stop_machine(), which insures that every preempt disabled section
948 * have finished.
949 */
950 int marker_probe_unregister_private_data(marker_probe_func *probe,
951 void *probe_private)
952 {
953 struct marker_entry *entry;
954 int ret = 0;
955 struct marker_probe_closure *old;
956 const char *channel = NULL, *name = NULL;
957
958 mutex_lock(&markers_mutex);
959 entry = get_marker_from_private_data(probe, probe_private);
960 if (!entry) {
961 ret = -ENOENT;
962 goto end;
963 }
964 //ust// if (entry->rcu_pending)
965 //ust// rcu_barrier_sched();
966 old = marker_entry_remove_probe(entry, NULL, probe_private);
967 channel = kstrdup(entry->channel, GFP_KERNEL);
968 name = kstrdup(entry->name, GFP_KERNEL);
969 mutex_unlock(&markers_mutex);
970
971 marker_update_probes();
972
973 mutex_lock(&markers_mutex);
974 entry = get_marker(channel, name);
975 if (!entry)
976 goto end;
977 //ust// if (entry->rcu_pending)
978 //ust// rcu_barrier_sched();
979 entry->oldptr = old;
980 entry->rcu_pending = 1;
981 /* write rcu_pending before calling the RCU callback */
982 smp_wmb();
983 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
984 synchronize_rcu(); free_old_closure(&entry->rcu);
985 /* Ignore busy error message */
986 remove_marker(channel, name);
987 end:
988 mutex_unlock(&markers_mutex);
989 kfree(channel);
990 kfree(name);
991 return ret;
992 }
993 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
994
995 /**
996 * marker_get_private_data - Get a marker's probe private data
997 * @channel: marker channel
998 * @name: marker name
999 * @probe: probe to match
1000 * @num: get the nth matching probe's private data
1001 *
1002 * Returns the nth private data pointer (starting from 0) matching, or an
1003 * ERR_PTR.
1004 * Returns the private data pointer, or an ERR_PTR.
1005 * The private data pointer should _only_ be dereferenced if the caller is the
1006 * owner of the data, or its content could vanish. This is mostly used to
1007 * confirm that a caller is the owner of a registered probe.
1008 */
1009 void *marker_get_private_data(const char *channel, const char *name,
1010 marker_probe_func *probe, int num)
1011 {
1012 struct hlist_head *head;
1013 struct hlist_node *node;
1014 struct marker_entry *e;
1015 size_t channel_len = strlen(channel) + 1;
1016 size_t name_len = strlen(name) + 1;
1017 int i;
1018 u32 hash;
1019
1020 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
1021 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
1022 hlist_for_each_entry(e, node, head, hlist) {
1023 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
1024 if (!e->ptype) {
1025 if (num == 0 && e->single.func == probe)
1026 return e->single.probe_private;
1027 } else {
1028 struct marker_probe_closure *closure;
1029 int match = 0;
1030 closure = e->multi;
1031 for (i = 0; closure[i].func; i++) {
1032 if (closure[i].func != probe)
1033 continue;
1034 if (match++ == num)
1035 return closure[i].probe_private;
1036 }
1037 }
1038 break;
1039 }
1040 }
1041 return ERR_PTR(-ENOENT);
1042 }
1043 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1044
1045 /**
1046 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1047 *
1048 * Called when no channel users are active by the channel infrastructure.
1049 * Called with lock_markers() and channel mutex held.
1050 */
1051 //ust// void markers_compact_event_ids(void)
1052 //ust// {
1053 //ust// struct marker_entry *entry;
1054 //ust// unsigned int i;
1055 //ust// struct hlist_head *head;
1056 //ust// struct hlist_node *node;
1057 //ust// int ret;
1058 //ust//
1059 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1060 //ust// head = &marker_table[i];
1061 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1062 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1063 //ust// WARN_ON(ret < 0);
1064 //ust// entry->channel_id = ret;
1065 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1066 //ust// entry->name);
1067 //ust// WARN_ON(ret < 0);
1068 //ust// entry->event_id = ret;
1069 //ust// }
1070 //ust// }
1071 //ust// }
1072
1073 //ust//#ifdef CONFIG_MODULES
1074
1075 /*
1076 * Returns 0 if current not found.
1077 * Returns 1 if current found.
1078 */
1079 int lib_get_iter_markers(struct marker_iter *iter)
1080 {
1081 struct lib *iter_lib;
1082 int found = 0;
1083
1084 //ust// mutex_lock(&module_mutex);
1085 list_for_each_entry(iter_lib, &libs, list) {
1086 if (iter_lib < iter->lib)
1087 continue;
1088 else if (iter_lib > iter->lib)
1089 iter->marker = NULL;
1090 found = marker_get_iter_range(&iter->marker,
1091 iter_lib->markers_start,
1092 iter_lib->markers_start + iter_lib->markers_count);
1093 if (found) {
1094 iter->lib = iter_lib;
1095 break;
1096 }
1097 }
1098 //ust// mutex_unlock(&module_mutex);
1099 return found;
1100 }
1101
1102 /**
1103 * marker_get_iter_range - Get a next marker iterator given a range.
1104 * @marker: current markers (in), next marker (out)
1105 * @begin: beginning of the range
1106 * @end: end of the range
1107 *
1108 * Returns whether a next marker has been found (1) or not (0).
1109 * Will return the first marker in the range if the input marker is NULL.
1110 */
1111 int marker_get_iter_range(struct marker **marker, struct marker *begin,
1112 struct marker *end)
1113 {
1114 if (!*marker && begin != end) {
1115 *marker = begin;
1116 return 1;
1117 }
1118 if (*marker >= begin && *marker < end)
1119 return 1;
1120 return 0;
1121 }
1122 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1123
1124 static void marker_get_iter(struct marker_iter *iter)
1125 {
1126 int found = 0;
1127
1128 /* Core kernel markers */
1129 if (!iter->lib) {
1130 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1131 found = marker_get_iter_range(&iter->marker,
1132 __start___markers, __stop___markers);
1133 if (found)
1134 goto end;
1135 }
1136 /* Markers in modules. */
1137 found = lib_get_iter_markers(iter);
1138 end:
1139 if (!found)
1140 marker_iter_reset(iter);
1141 }
1142
1143 void marker_iter_start(struct marker_iter *iter)
1144 {
1145 marker_get_iter(iter);
1146 }
1147 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1148
1149 void marker_iter_next(struct marker_iter *iter)
1150 {
1151 iter->marker++;
1152 /*
1153 * iter->marker may be invalid because we blindly incremented it.
1154 * Make sure it is valid by marshalling on the markers, getting the
1155 * markers from following modules if necessary.
1156 */
1157 marker_get_iter(iter);
1158 }
1159 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1160
1161 void marker_iter_stop(struct marker_iter *iter)
1162 {
1163 }
1164 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1165
1166 void marker_iter_reset(struct marker_iter *iter)
1167 {
1168 iter->lib = NULL;
1169 iter->marker = NULL;
1170 }
1171 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1172
1173 #ifdef CONFIG_MARKERS_USERSPACE
1174 /*
1175 * must be called with current->user_markers_mutex held
1176 */
1177 static void free_user_marker(char __user *state, struct hlist_head *head)
1178 {
1179 struct user_marker *umark;
1180 struct hlist_node *pos, *n;
1181
1182 hlist_for_each_entry_safe(umark, pos, n, head, hlist) {
1183 if (umark->state == state) {
1184 hlist_del(&umark->hlist);
1185 kfree(umark);
1186 }
1187 }
1188 }
1189
1190 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1191 //ust// char __user *state, int reg)
1192 //ust// {
1193 //ust// struct user_marker *umark;
1194 //ust// long len;
1195 //ust// struct marker_entry *entry;
1196 //ust// int ret = 0;
1197 //ust//
1198 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1199 //ust// current->comm, reg ? "registers" : "unregisters",
1200 //ust// name, state);
1201 //ust// if (reg) {
1202 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1203 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1204 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1205 //ust// umark->state = state;
1206 //ust// len = strncpy_from_user(umark->name, name,
1207 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1208 //ust// if (len < 0) {
1209 //ust// ret = -EFAULT;
1210 //ust// goto error;
1211 //ust// }
1212 //ust// len = strncpy_from_user(umark->format, format,
1213 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1214 //ust// if (len < 0) {
1215 //ust// ret = -EFAULT;
1216 //ust// goto error;
1217 //ust// }
1218 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1219 //ust// umark->format);
1220 //ust// mutex_lock(&markers_mutex);
1221 //ust// entry = get_marker("userspace", umark->name);
1222 //ust// if (entry) {
1223 //ust// if (entry->format &&
1224 //ust// strcmp(entry->format, umark->format) != 0) {
1225 //ust// printk(" error, wrong format in process %s",
1226 //ust// current->comm);
1227 //ust// ret = -EPERM;
1228 //ust// goto error_unlock;
1229 //ust// }
1230 //ust// printk(" %s", !!entry->refcount
1231 //ust// ? "enabled" : "disabled");
1232 //ust// if (put_user(!!entry->refcount, state)) {
1233 //ust// ret = -EFAULT;
1234 //ust// goto error_unlock;
1235 //ust// }
1236 //ust// printk("\n");
1237 //ust// } else {
1238 //ust// printk(" disabled\n");
1239 //ust// if (put_user(0, umark->state)) {
1240 //ust// printk(KERN_WARNING
1241 //ust// "Marker in %s caused a fault\n",
1242 //ust// current->comm);
1243 //ust// goto error_unlock;
1244 //ust// }
1245 //ust// }
1246 //ust// mutex_lock(&current->group_leader->user_markers_mutex);
1247 //ust// hlist_add_head(&umark->hlist,
1248 //ust// &current->group_leader->user_markers);
1249 //ust// current->group_leader->user_markers_sequence++;
1250 //ust// mutex_unlock(&current->group_leader->user_markers_mutex);
1251 //ust// mutex_unlock(&markers_mutex);
1252 //ust// } else {
1253 //ust// mutex_lock(&current->group_leader->user_markers_mutex);
1254 //ust// free_user_marker(state,
1255 //ust// &current->group_leader->user_markers);
1256 //ust// current->group_leader->user_markers_sequence++;
1257 //ust// mutex_unlock(&current->group_leader->user_markers_mutex);
1258 //ust// }
1259 //ust// goto end;
1260 //ust// error_unlock:
1261 //ust// mutex_unlock(&markers_mutex);
1262 //ust// error:
1263 //ust// kfree(umark);
1264 //ust// end:
1265 //ust// return ret;
1266 //ust// }
1267 //ust//
1268 //ust// /*
1269 //ust// * Types :
1270 //ust// * string : 0
1271 //ust// */
1272 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1273 //ust// char __user *ubuf)
1274 //ust// {
1275 //ust// long ret = -EPERM;
1276 //ust// char *page;
1277 //ust// int len;
1278 //ust//
1279 //ust// switch (type) {
1280 //ust// case 0: /* String */
1281 //ust// ret = -ENOMEM;
1282 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1283 //ust// if (!page)
1284 //ust// goto string_out;
1285 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1286 //ust// if (len < 0) {
1287 //ust// ret = -EFAULT;
1288 //ust// goto string_err;
1289 //ust// }
1290 //ust// trace_mark(userspace, string, "string %s", page);
1291 //ust// string_err:
1292 //ust// free_page((unsigned long) page);
1293 //ust// string_out:
1294 //ust// break;
1295 //ust// default:
1296 //ust// break;
1297 //ust// }
1298 //ust// return ret;
1299 //ust// }
1300
1301 //ust// static void marker_update_processes(void)
1302 //ust// {
1303 //ust// struct task_struct *g, *t;
1304 //ust//
1305 //ust// /*
1306 //ust// * markers_mutex is taken to protect the p->user_markers read.
1307 //ust// */
1308 //ust// mutex_lock(&markers_mutex);
1309 //ust// read_lock(&tasklist_lock);
1310 //ust// for_each_process(g) {
1311 //ust// WARN_ON(!thread_group_leader(g));
1312 //ust// if (hlist_empty(&g->user_markers))
1313 //ust// continue;
1314 //ust// if (strcmp(g->comm, "testprog") == 0)
1315 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1316 //ust// t = g;
1317 //ust// do {
1318 //ust// /* TODO : implement this thread flag in each arch. */
1319 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1320 //ust// } while ((t = next_thread(t)) != g);
1321 //ust// }
1322 //ust// read_unlock(&tasklist_lock);
1323 //ust// mutex_unlock(&markers_mutex);
1324 //ust// }
1325
1326 /*
1327 * Update current process.
1328 * Note that we have to wait a whole scheduler period before we are sure that
1329 * every running userspace threads have their markers updated.
1330 * (synchronize_sched() can be used to insure this).
1331 */
1332 void marker_update_process(void)
1333 {
1334 struct user_marker *umark;
1335 struct hlist_node *pos;
1336 struct marker_entry *entry;
1337
1338 mutex_lock(&markers_mutex);
1339 mutex_lock(&current->group_leader->user_markers_mutex);
1340 if (strcmp(current->comm, "testprog") == 0)
1341 printk(KERN_DEBUG "do update pending for testprog\n");
1342 hlist_for_each_entry(umark, pos,
1343 &current->group_leader->user_markers, hlist) {
1344 printk(KERN_DEBUG "Updating marker %s in %s\n",
1345 umark->name, current->comm);
1346 entry = get_marker("userspace", umark->name);
1347 if (entry) {
1348 if (entry->format &&
1349 strcmp(entry->format, umark->format) != 0) {
1350 printk(KERN_WARNING
1351 " error, wrong format in process %s\n",
1352 current->comm);
1353 break;
1354 }
1355 if (put_user(!!entry->refcount, umark->state)) {
1356 printk(KERN_WARNING
1357 "Marker in %s caused a fault\n",
1358 current->comm);
1359 break;
1360 }
1361 } else {
1362 if (put_user(0, umark->state)) {
1363 printk(KERN_WARNING
1364 "Marker in %s caused a fault\n",
1365 current->comm);
1366 break;
1367 }
1368 }
1369 }
1370 clear_thread_flag(TIF_MARKER_PENDING);
1371 mutex_unlock(&current->group_leader->user_markers_mutex);
1372 mutex_unlock(&markers_mutex);
1373 }
1374
1375 /*
1376 * Called at process exit and upon do_execve().
1377 * We assume that when the leader exits, no more references can be done to the
1378 * leader structure by the other threads.
1379 */
1380 void exit_user_markers(struct task_struct *p)
1381 {
1382 struct user_marker *umark;
1383 struct hlist_node *pos, *n;
1384
1385 if (thread_group_leader(p)) {
1386 mutex_lock(&markers_mutex);
1387 mutex_lock(&p->user_markers_mutex);
1388 hlist_for_each_entry_safe(umark, pos, n, &p->user_markers,
1389 hlist)
1390 kfree(umark);
1391 INIT_HLIST_HEAD(&p->user_markers);
1392 p->user_markers_sequence++;
1393 mutex_unlock(&p->user_markers_mutex);
1394 mutex_unlock(&markers_mutex);
1395 }
1396 }
1397
1398 int is_marker_enabled(const char *channel, const char *name)
1399 {
1400 struct marker_entry *entry;
1401
1402 mutex_lock(&markers_mutex);
1403 entry = get_marker(channel, name);
1404 mutex_unlock(&markers_mutex);
1405
1406 return entry && !!entry->refcount;
1407 }
1408 //ust// #endif
1409
1410 int marker_module_notify(struct notifier_block *self,
1411 unsigned long val, void *data)
1412 {
1413 struct module *mod = data;
1414
1415 switch (val) {
1416 case MODULE_STATE_COMING:
1417 marker_update_probe_range(mod->markers,
1418 mod->markers + mod->num_markers);
1419 break;
1420 case MODULE_STATE_GOING:
1421 marker_update_probe_range(mod->markers,
1422 mod->markers + mod->num_markers);
1423 break;
1424 }
1425 return 0;
1426 }
1427
1428 struct notifier_block marker_module_nb = {
1429 .notifier_call = marker_module_notify,
1430 .priority = 0,
1431 };
1432
1433 //ust// static int init_markers(void)
1434 //ust// {
1435 //ust// return register_module_notifier(&marker_module_nb);
1436 //ust// }
1437 //ust// __initcall(init_markers);
1438 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1439
1440 #endif /* CONFIG_MODULES */
1441
1442 void ltt_dump_marker_state(struct ltt_trace_struct *trace)
1443 {
1444 struct marker_entry *entry;
1445 struct ltt_probe_private_data call_data;
1446 struct hlist_head *head;
1447 struct hlist_node *node;
1448 unsigned int i;
1449
1450 mutex_lock(&markers_mutex);
1451 call_data.trace = trace;
1452 call_data.serializer = NULL;
1453
1454 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1455 head = &marker_table[i];
1456 hlist_for_each_entry(entry, node, head, hlist) {
1457 __trace_mark(0, metadata, core_marker_id,
1458 &call_data,
1459 "channel %s name %s event_id %hu "
1460 "int #1u%zu long #1u%zu pointer #1u%zu "
1461 "size_t #1u%zu alignment #1u%u",
1462 entry->channel,
1463 entry->name,
1464 entry->event_id,
1465 sizeof(int), sizeof(long),
1466 sizeof(void *), sizeof(size_t),
1467 ltt_get_alignment());
1468 if (entry->format)
1469 __trace_mark(0, metadata,
1470 core_marker_format,
1471 &call_data,
1472 "channel %s name %s format %s",
1473 entry->channel,
1474 entry->name,
1475 entry->format);
1476 }
1477 }
1478 mutex_unlock(&markers_mutex);
1479 }
1480 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1481
1482 static void (*new_marker_cb)(struct marker *) = NULL;
1483
1484 void marker_set_new_marker_cb(void (*cb)(struct marker *))
1485 {
1486 new_marker_cb = cb;
1487 }
1488
1489 static void new_markers(struct marker *start, struct marker *end)
1490 {
1491 if(new_marker_cb) {
1492 struct marker *m;
1493 for(m=start; m < end; m++) {
1494 new_marker_cb(m);
1495 }
1496 }
1497 }
1498
1499 int marker_register_lib(struct marker *markers_start, int markers_count)
1500 {
1501 struct lib *pl;
1502
1503 pl = (struct lib *) malloc(sizeof(struct lib));
1504
1505 pl->markers_start = markers_start;
1506 pl->markers_count = markers_count;
1507
1508 /* FIXME: maybe protect this with its own mutex? */
1509 lock_markers();
1510 list_add(&pl->list, &libs);
1511 unlock_markers();
1512
1513 new_markers(markers_start, markers_start + markers_count);
1514
1515 /* FIXME: update just the loaded lib */
1516 lib_update_markers();
1517
1518 DBG("just registered a markers section from %p and having %d markers", markers_start, markers_count);
1519
1520 return 0;
1521 }
1522
1523 int marker_unregister_lib(struct marker *markers_start, int markers_count)
1524 {
1525 /*FIXME: implement; but before implementing, marker_register_lib must
1526 have appropriate locking. */
1527
1528 return 0;
1529 }
1530
1531 static int initialized = 0;
1532
1533 void __attribute__((constructor)) init_markers(void)
1534 {
1535 if(!initialized) {
1536 marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker));
1537 DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);
1538 initialized = 1;
1539 }
1540 }
This page took 0.079402 seconds and 5 git commands to generate.