2 * Copyright (C) 2007 Mathieu Desnoyers
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.
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.
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
18 //ust// #include <linux/module.h>
19 //ust// #include <linux/mutex.h>
20 //ust// #include <linux/types.h>
23 //#include "rcupdate.h"
24 //ust// #include <linux/marker.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>
36 #include <ust/kernelcompat.h>
38 #include <ust/marker.h>
41 #include "tracercore.h"
44 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
45 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
46 extern struct marker_addr __start___marker_addr
[] __attribute__((visibility("hidden")));
47 extern struct marker_addr __stop___marker_addr
[] __attribute__((visibility("hidden")));
49 /* Set to 1 to enable marker debug output */
50 static const int marker_debug
;
53 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
54 * and module markers and the hash table.
56 static DEFINE_MUTEX(markers_mutex
);
58 static LIST_HEAD(libs
);
61 void lock_markers(void)
63 mutex_lock(&markers_mutex
);
66 void unlock_markers(void)
68 mutex_unlock(&markers_mutex
);
72 * Marker hash table, containing the active markers.
73 * Protected by module_mutex.
75 #define MARKER_HASH_BITS 6
76 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
77 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
81 * It is used to make sure every handler has finished using its private data
82 * between two consecutive operation (add or remove) on a given marker. It is
83 * also used to delay the free of multiple probes array until a quiescent state
85 * marker entries modifications are protected by the markers_mutex.
88 struct hlist_node hlist
;
92 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
93 struct marker_probe_closure single
;
94 struct marker_probe_closure
*multi
;
95 int refcount
; /* Number of times armed. 0 if disarmed. */
101 unsigned char ptype
:1;
102 unsigned char format_allocated
:1;
103 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
106 #ifdef CONFIG_MARKERS_USERSPACE
107 static void marker_update_processes(void);
109 static void marker_update_processes(void)
115 * __mark_empty_function - Empty probe callback
116 * @mdata: marker data
117 * @probe_private: probe private data
118 * @call_private: call site private data
119 * @fmt: format string
120 * @...: variable argument list
122 * Empty callback provided as a probe to the markers. By providing this to a
123 * disabled marker, we make sure the execution flow is always valid even
124 * though the function pointer change and the marker enabling are two distinct
125 * operations that modifies the execution flow of preemptible code.
127 notrace
void __mark_empty_function(const struct marker
*mdata
,
128 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
131 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
134 * marker_probe_cb Callback that prepares the variable argument list for probes.
135 * @mdata: pointer of type struct marker
136 * @call_private: caller site private data
137 * @...: Variable argument list.
139 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
140 * need to put a full smp_rmb() in this branch. This is why we do not use
141 * rcu_dereference() for the pointer read.
143 notrace
void marker_probe_cb(const struct marker
*mdata
,
144 void *call_private
, struct registers
*regs
, ...)
150 * rcu_read_lock_sched does two things : disabling preemption to make
151 * sure the teardown of the callbacks can be done correctly when they
152 * are in modules and they insure RCU read coherency.
154 //ust// rcu_read_lock_sched_notrace();
155 ptype
= mdata
->ptype
;
156 if (likely(!ptype
)) {
157 marker_probe_func
*func
;
158 /* Must read the ptype before ptr. They are not data dependant,
159 * so we put an explicit smp_rmb() here. */
161 func
= mdata
->single
.func
;
162 /* Must read the ptr before private data. They are not data
163 * dependant, so we put an explicit smp_rmb() here. */
165 va_start(args
, regs
);
166 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
167 mdata
->format
, &args
);
170 struct marker_probe_closure
*multi
;
173 * Read mdata->ptype before mdata->multi.
176 multi
= mdata
->multi
;
178 * multi points to an array, therefore accessing the array
179 * depends on reading multi. However, even in this case,
180 * we must insure that the pointer is read _before_ the array
181 * data. Same as rcu_dereference, but we need a full smp_rmb()
182 * in the fast path, so put the explicit barrier here.
184 smp_read_barrier_depends();
185 for (i
= 0; multi
[i
].func
; i
++) {
186 va_start(args
, regs
);
187 multi
[i
].func(mdata
, multi
[i
].probe_private
,
188 regs
, call_private
, mdata
->format
, &args
);
192 //ust// rcu_read_unlock_sched_notrace();
194 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
197 * marker_probe_cb Callback that does not prepare the variable argument list.
198 * @mdata: pointer of type struct marker
199 * @call_private: caller site private data
200 * @...: Variable argument list.
202 * Should be connected to markers "MARK_NOARGS".
204 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
205 void *call_private
, struct registers
*regs
, ...)
207 va_list args
; /* not initialized */
210 //ust// rcu_read_lock_sched_notrace();
211 ptype
= mdata
->ptype
;
212 if (likely(!ptype
)) {
213 marker_probe_func
*func
;
214 /* Must read the ptype before ptr. They are not data dependant,
215 * so we put an explicit smp_rmb() here. */
217 func
= mdata
->single
.func
;
218 /* Must read the ptr before private data. They are not data
219 * dependant, so we put an explicit smp_rmb() here. */
221 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
222 mdata
->format
, &args
);
224 struct marker_probe_closure
*multi
;
227 * Read mdata->ptype before mdata->multi.
230 multi
= mdata
->multi
;
232 * multi points to an array, therefore accessing the array
233 * depends on reading multi. However, even in this case,
234 * we must insure that the pointer is read _before_ the array
235 * data. Same as rcu_dereference, but we need a full smp_rmb()
236 * in the fast path, so put the explicit barrier here.
238 smp_read_barrier_depends();
239 for (i
= 0; multi
[i
].func
; i
++)
240 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
241 call_private
, mdata
->format
, &args
);
243 //ust// rcu_read_unlock_sched_notrace();
246 static void free_old_closure(struct rcu_head
*head
)
248 struct marker_entry
*entry
= container_of(head
,
249 struct marker_entry
, rcu
);
250 kfree(entry
->oldptr
);
251 /* Make sure we free the data before setting the pending flag to 0 */
253 entry
->rcu_pending
= 0;
256 static void debug_print_probes(struct marker_entry
*entry
)
264 printk(KERN_DEBUG
"Single probe : %p %p\n",
266 entry
->single
.probe_private
);
268 for (i
= 0; entry
->multi
[i
].func
; i
++)
269 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
270 entry
->multi
[i
].func
,
271 entry
->multi
[i
].probe_private
);
275 static struct marker_probe_closure
*
276 marker_entry_add_probe(struct marker_entry
*entry
,
277 marker_probe_func
*probe
, void *probe_private
)
280 struct marker_probe_closure
*old
, *new;
284 debug_print_probes(entry
);
287 if (entry
->single
.func
== probe
&&
288 entry
->single
.probe_private
== probe_private
)
289 return ERR_PTR(-EBUSY
);
290 if (entry
->single
.func
== __mark_empty_function
) {
292 entry
->single
.func
= probe
;
293 entry
->single
.probe_private
= probe_private
;
296 debug_print_probes(entry
);
304 /* (N -> N+1), (N != 0, 1) probes */
305 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
306 if (old
[nr_probes
].func
== probe
307 && old
[nr_probes
].probe_private
309 return ERR_PTR(-EBUSY
);
311 /* + 2 : one for new probe, one for NULL func */
312 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
315 return ERR_PTR(-ENOMEM
);
317 new[0] = entry
->single
;
320 nr_probes
* sizeof(struct marker_probe_closure
));
321 new[nr_probes
].func
= probe
;
322 new[nr_probes
].probe_private
= probe_private
;
323 entry
->refcount
= nr_probes
+ 1;
326 debug_print_probes(entry
);
330 static struct marker_probe_closure
*
331 marker_entry_remove_probe(struct marker_entry
*entry
,
332 marker_probe_func
*probe
, void *probe_private
)
334 int nr_probes
= 0, nr_del
= 0, i
;
335 struct marker_probe_closure
*old
, *new;
339 debug_print_probes(entry
);
341 /* 0 -> N is an error */
342 WARN_ON(entry
->single
.func
== __mark_empty_function
);
344 WARN_ON(probe
&& entry
->single
.func
!= probe
);
345 WARN_ON(entry
->single
.probe_private
!= probe_private
);
346 entry
->single
.func
= __mark_empty_function
;
349 debug_print_probes(entry
);
352 /* (N -> M), (N > 1, M >= 0) probes */
353 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
354 if ((!probe
|| old
[nr_probes
].func
== probe
)
355 && old
[nr_probes
].probe_private
361 if (nr_probes
- nr_del
== 0) {
362 /* N -> 0, (N > 1) */
363 entry
->single
.func
= __mark_empty_function
;
366 } else if (nr_probes
- nr_del
== 1) {
367 /* N -> 1, (N > 1) */
368 for (i
= 0; old
[i
].func
; i
++)
369 if ((probe
&& old
[i
].func
!= probe
) ||
370 old
[i
].probe_private
!= probe_private
)
371 entry
->single
= old
[i
];
376 /* N -> M, (N > 1, M > 1) */
378 new = kzalloc((nr_probes
- nr_del
+ 1)
379 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
381 return ERR_PTR(-ENOMEM
);
382 for (i
= 0; old
[i
].func
; i
++)
383 if ((probe
&& old
[i
].func
!= probe
) ||
384 old
[i
].probe_private
!= probe_private
)
386 entry
->refcount
= nr_probes
- nr_del
;
390 debug_print_probes(entry
);
395 * Get marker if the marker is present in the marker hash table.
396 * Must be called with markers_mutex held.
397 * Returns NULL if not present.
399 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
401 struct hlist_head
*head
;
402 struct hlist_node
*node
;
403 struct marker_entry
*e
;
404 size_t channel_len
= strlen(channel
) + 1;
405 size_t name_len
= strlen(name
) + 1;
408 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
409 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
410 hlist_for_each_entry(e
, node
, head
, hlist
) {
411 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
418 * Add the marker to the marker hash table. Must be called with markers_mutex
421 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
424 struct hlist_head
*head
;
425 struct hlist_node
*node
;
426 struct marker_entry
*e
;
427 size_t channel_len
= strlen(channel
) + 1;
428 size_t name_len
= strlen(name
) + 1;
429 size_t format_len
= 0;
432 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
434 format_len
= strlen(format
) + 1;
435 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
436 hlist_for_each_entry(e
, node
, head
, hlist
) {
437 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
439 "Marker %s.%s busy\n", channel
, name
);
440 return ERR_PTR(-EBUSY
); /* Already there */
444 * Using kmalloc here to allocate a variable length element. Could
445 * cause some memory fragmentation if overused.
447 e
= kmalloc(sizeof(struct marker_entry
)
448 + channel_len
+ name_len
+ format_len
,
451 return ERR_PTR(-ENOMEM
);
452 memcpy(e
->channel
, channel
, channel_len
);
453 e
->name
= &e
->channel
[channel_len
];
454 memcpy(e
->name
, name
, name_len
);
456 e
->format
= &e
->name
[channel_len
+ name_len
];
457 memcpy(e
->format
, format
, format_len
);
458 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
459 e
->call
= marker_probe_cb_noarg
;
461 e
->call
= marker_probe_cb
;
462 trace_mark(metadata
, core_marker_format
,
463 "channel %s name %s format %s",
464 e
->channel
, e
->name
, e
->format
);
467 e
->call
= marker_probe_cb
;
469 e
->single
.func
= __mark_empty_function
;
470 e
->single
.probe_private
= NULL
;
473 e
->format_allocated
= 0;
476 hlist_add_head(&e
->hlist
, head
);
481 * Remove the marker from the marker hash table. Must be called with mutex_lock
484 static int remove_marker(const char *channel
, const char *name
)
486 struct hlist_head
*head
;
487 struct hlist_node
*node
;
488 struct marker_entry
*e
;
490 size_t channel_len
= strlen(channel
) + 1;
491 size_t name_len
= strlen(name
) + 1;
495 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
496 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
497 hlist_for_each_entry(e
, node
, head
, hlist
) {
498 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
505 if (e
->single
.func
!= __mark_empty_function
)
507 hlist_del(&e
->hlist
);
508 if (e
->format_allocated
)
510 ret
= ltt_channels_unregister(e
->channel
);
512 /* Make sure the call_rcu has been executed */
513 //ust// if (e->rcu_pending)
514 //ust// rcu_barrier_sched();
520 * Set the mark_entry format to the format found in the element.
522 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
524 entry
->format
= kstrdup(format
, GFP_KERNEL
);
527 entry
->format_allocated
= 1;
529 trace_mark(metadata
, core_marker_format
,
530 "channel %s name %s format %s",
531 entry
->channel
, entry
->name
, entry
->format
);
536 * Sets the probe callback corresponding to one marker.
538 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
542 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
545 if (strcmp(entry
->format
, elem
->format
) != 0) {
547 "Format mismatch for probe %s "
548 "(%s), marker (%s)\n",
555 ret
= marker_set_format(entry
, elem
->format
);
561 * probe_cb setup (statically known) is done here. It is
562 * asynchronous with the rest of execution, therefore we only
563 * pass from a "safe" callback (with argument) to an "unsafe"
564 * callback (does not set arguments).
566 elem
->call
= entry
->call
;
567 elem
->channel_id
= entry
->channel_id
;
568 elem
->event_id
= entry
->event_id
;
571 * We only update the single probe private data when the ptr is
572 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
574 WARN_ON(elem
->single
.func
!= __mark_empty_function
575 && elem
->single
.probe_private
!= entry
->single
.probe_private
577 elem
->single
.probe_private
= entry
->single
.probe_private
;
579 * Make sure the private data is valid when we update the
583 elem
->single
.func
= entry
->single
.func
;
585 * We also make sure that the new probe callbacks array is consistent
586 * before setting a pointer to it.
588 rcu_assign_pointer(elem
->multi
, entry
->multi
);
590 * Update the function or multi probe array pointer before setting the
594 elem
->ptype
= entry
->ptype
;
596 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
597 //ust// WARN_ON(!elem->tp_cb);
599 //ust// * It is ok to directly call the probe registration because type
600 //ust// * checking has been done in the __trace_mark_tp() macro.
603 //ust// if (active) {
605 //ust// * try_module_get should always succeed because we hold
606 //ust// * markers_mutex to get the tp_cb address.
608 //ust// ret = try_module_get(__module_text_address(
609 //ust// (unsigned long)elem->tp_cb));
610 //ust// BUG_ON(!ret);
611 //ust// ret = tracepoint_probe_register_noupdate(
612 //ust// elem->tp_name,
613 //ust// elem->tp_cb);
615 //ust// ret = tracepoint_probe_unregister_noupdate(
616 //ust// elem->tp_name,
617 //ust// elem->tp_cb);
619 //ust// * tracepoint_probe_update_all() must be called
620 //ust// * before the module containing tp_cb is unloaded.
622 //ust// module_put(__module_text_address(
623 //ust// (unsigned long)elem->tp_cb));
626 elem
->state__imv
= active
;
632 * Disable a marker and its probe callback.
633 * Note: only waiting an RCU period after setting elem->call to the empty
634 * function insures that the original callback is not used anymore. This insured
635 * by rcu_read_lock_sched around the call site.
637 static void disable_marker(struct marker
*elem
)
641 //ust// /* leave "call" as is. It is known statically. */
642 //ust// if (elem->tp_name && _imv_read(elem->state)) {
643 //ust// WARN_ON(!elem->tp_cb);
645 //ust// * It is ok to directly call the probe registration because type
646 //ust// * checking has been done in the __trace_mark_tp() macro.
648 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
649 //ust// elem->tp_cb);
650 //ust// WARN_ON(ret);
652 //ust// * tracepoint_probe_update_all() must be called
653 //ust// * before the module containing tp_cb is unloaded.
655 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
657 elem
->state__imv
= 0;
658 elem
->single
.func
= __mark_empty_function
;
659 /* Update the function before setting the ptype */
661 elem
->ptype
= 0; /* single probe */
663 * Leave the private data and channel_id/event_id there, because removal
664 * is racy and should be done only after an RCU period. These are never
665 * used until the next initialization anyway.
670 * marker_update_probe_range - Update a probe range
671 * @begin: beginning of the range
672 * @end: end of the range
674 * Updates the probe callback corresponding to a range of markers.
676 void marker_update_probe_range(struct marker
*begin
,
680 struct marker_entry
*mark_entry
;
682 mutex_lock(&markers_mutex
);
683 for (iter
= begin
; iter
< end
; iter
++) {
684 mark_entry
= get_marker(iter
->channel
, iter
->name
);
686 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
688 * ignore error, continue
691 /* This is added for UST. We emit a core_marker_id event
692 * for markers that are already registered to a probe
693 * upon library load. Otherwise, no core_marker_id will
694 * be generated for these markers. Is this the right thing
697 trace_mark(metadata
, core_marker_id
,
698 "channel %s name %s event_id %hu "
699 "int #1u%zu long #1u%zu pointer #1u%zu "
700 "size_t #1u%zu alignment #1u%u",
701 iter
->channel
, iter
->name
, mark_entry
->event_id
,
702 sizeof(int), sizeof(long), sizeof(void *),
703 sizeof(size_t), ltt_get_alignment());
705 disable_marker(iter
);
708 mutex_unlock(&markers_mutex
);
711 static void lib_update_markers(void)
715 /* FIXME: we should probably take a mutex here on libs */
716 //ust// mutex_lock(&module_mutex);
717 list_for_each_entry(lib
, &libs
, list
)
718 marker_update_probe_range(lib
->markers_start
,
719 lib
->markers_start
+ lib
->markers_count
);
720 //ust// mutex_unlock(&module_mutex);
724 * Update probes, removing the faulty probes.
726 * Internal callback only changed before the first probe is connected to it.
727 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
728 * transitions. All other transitions will leave the old private data valid.
729 * This makes the non-atomicity of the callback/private data updates valid.
731 * "special case" updates :
736 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
737 * Site effect : marker_set_format may delete the marker entry (creating a
740 static void marker_update_probes(void)
742 /* Core kernel markers */
743 //ust// marker_update_probe_range(__start___markers, __stop___markers);
744 /* Markers in modules. */
745 //ust// module_update_markers();
746 lib_update_markers();
747 //ust// tracepoint_probe_update_all();
748 /* Update immediate values */
750 //ust// module_imv_update(); /* FIXME: need to port for libs? */
751 marker_update_processes();
755 * marker_probe_register - Connect a probe to a marker
756 * @channel: marker channel
758 * @format: format string
759 * @probe: probe handler
760 * @probe_private: probe private data
762 * private data must be a valid allocated memory address, or NULL.
763 * Returns 0 if ok, error value on error.
764 * The probe address must at least be aligned on the architecture pointer size.
766 int marker_probe_register(const char *channel
, const char *name
,
767 const char *format
, marker_probe_func
*probe
,
770 struct marker_entry
*entry
;
771 int ret
= 0, ret_err
;
772 struct marker_probe_closure
*old
;
775 mutex_lock(&markers_mutex
);
776 entry
= get_marker(channel
, name
);
779 entry
= add_marker(channel
, name
, format
);
781 ret
= PTR_ERR(entry
);
784 ret
= ltt_channels_register(channel
);
786 goto error_remove_marker
;
787 ret
= ltt_channels_get_index_from_name(channel
);
789 goto error_unregister_channel
;
790 entry
->channel_id
= ret
;
791 ret
= ltt_channels_get_event_id(channel
, name
);
793 goto error_unregister_channel
;
794 entry
->event_id
= ret
;
796 trace_mark(metadata
, core_marker_id
,
797 "channel %s name %s event_id %hu "
798 "int #1u%zu long #1u%zu pointer #1u%zu "
799 "size_t #1u%zu alignment #1u%u",
800 channel
, name
, entry
->event_id
,
801 sizeof(int), sizeof(long), sizeof(void *),
802 sizeof(size_t), ltt_get_alignment());
805 ret
= marker_set_format(entry
, format
);
806 else if (strcmp(entry
->format
, format
))
813 * If we detect that a call_rcu is pending for this marker,
814 * make sure it's executed now.
816 //ust// if (entry->rcu_pending)
817 //ust// rcu_barrier_sched();
818 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
822 goto error_unregister_channel
;
826 mutex_unlock(&markers_mutex
);
828 /* Activate marker if necessary */
829 marker_update_probes();
831 mutex_lock(&markers_mutex
);
832 entry
= get_marker(channel
, name
);
835 //ust// if (entry->rcu_pending)
836 //ust// rcu_barrier_sched();
838 entry
->rcu_pending
= 1;
839 /* write rcu_pending before calling the RCU callback */
841 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
842 synchronize_rcu(); free_old_closure(&entry
->rcu
);
845 error_unregister_channel
:
846 ret_err
= ltt_channels_unregister(channel
);
849 ret_err
= remove_marker(channel
, name
);
852 mutex_unlock(&markers_mutex
);
855 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
858 * marker_probe_unregister - Disconnect a probe from a marker
859 * @channel: marker channel
861 * @probe: probe function pointer
862 * @probe_private: probe private data
864 * Returns the private data given to marker_probe_register, or an ERR_PTR().
865 * We do not need to call a synchronize_sched to make sure the probes have
866 * finished running before doing a module unload, because the module unload
867 * itself uses stop_machine(), which insures that every preempt disabled section
870 int marker_probe_unregister(const char *channel
, const char *name
,
871 marker_probe_func
*probe
, void *probe_private
)
873 struct marker_entry
*entry
;
874 struct marker_probe_closure
*old
;
877 mutex_lock(&markers_mutex
);
878 entry
= get_marker(channel
, name
);
881 //ust// if (entry->rcu_pending)
882 //ust// rcu_barrier_sched();
883 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
884 mutex_unlock(&markers_mutex
);
886 marker_update_probes();
888 mutex_lock(&markers_mutex
);
889 entry
= get_marker(channel
, name
);
892 //ust// if (entry->rcu_pending)
893 //ust// rcu_barrier_sched();
895 entry
->rcu_pending
= 1;
896 /* write rcu_pending before calling the RCU callback */
898 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
899 synchronize_rcu(); free_old_closure(&entry
->rcu
);
900 remove_marker(channel
, name
); /* Ignore busy error message */
903 mutex_unlock(&markers_mutex
);
906 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
908 static struct marker_entry
*
909 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
911 struct marker_entry
*entry
;
913 struct hlist_head
*head
;
914 struct hlist_node
*node
;
916 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
917 head
= &marker_table
[i
];
918 hlist_for_each_entry(entry
, node
, head
, hlist
) {
920 if (entry
->single
.func
== probe
921 && entry
->single
.probe_private
925 struct marker_probe_closure
*closure
;
926 closure
= entry
->multi
;
927 for (i
= 0; closure
[i
].func
; i
++) {
928 if (closure
[i
].func
== probe
&&
929 closure
[i
].probe_private
940 * marker_probe_unregister_private_data - Disconnect a probe from a marker
941 * @probe: probe function
942 * @probe_private: probe private data
944 * Unregister a probe by providing the registered private data.
945 * Only removes the first marker found in hash table.
946 * Return 0 on success or error value.
947 * We do not need to call a synchronize_sched to make sure the probes have
948 * finished running before doing a module unload, because the module unload
949 * itself uses stop_machine(), which insures that every preempt disabled section
952 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
955 struct marker_entry
*entry
;
957 struct marker_probe_closure
*old
;
958 const char *channel
= NULL
, *name
= NULL
;
960 mutex_lock(&markers_mutex
);
961 entry
= get_marker_from_private_data(probe
, probe_private
);
966 //ust// if (entry->rcu_pending)
967 //ust// rcu_barrier_sched();
968 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
969 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
970 name
= kstrdup(entry
->name
, GFP_KERNEL
);
971 mutex_unlock(&markers_mutex
);
973 marker_update_probes();
975 mutex_lock(&markers_mutex
);
976 entry
= get_marker(channel
, name
);
979 //ust// if (entry->rcu_pending)
980 //ust// rcu_barrier_sched();
982 entry
->rcu_pending
= 1;
983 /* write rcu_pending before calling the RCU callback */
985 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
986 synchronize_rcu(); free_old_closure(&entry
->rcu
);
987 /* Ignore busy error message */
988 remove_marker(channel
, name
);
990 mutex_unlock(&markers_mutex
);
995 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
998 * marker_get_private_data - Get a marker's probe private data
999 * @channel: marker channel
1000 * @name: marker name
1001 * @probe: probe to match
1002 * @num: get the nth matching probe's private data
1004 * Returns the nth private data pointer (starting from 0) matching, or an
1006 * Returns the private data pointer, or an ERR_PTR.
1007 * The private data pointer should _only_ be dereferenced if the caller is the
1008 * owner of the data, or its content could vanish. This is mostly used to
1009 * confirm that a caller is the owner of a registered probe.
1011 void *marker_get_private_data(const char *channel
, const char *name
,
1012 marker_probe_func
*probe
, int num
)
1014 struct hlist_head
*head
;
1015 struct hlist_node
*node
;
1016 struct marker_entry
*e
;
1017 size_t channel_len
= strlen(channel
) + 1;
1018 size_t name_len
= strlen(name
) + 1;
1022 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1023 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1024 hlist_for_each_entry(e
, node
, head
, hlist
) {
1025 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1027 if (num
== 0 && e
->single
.func
== probe
)
1028 return e
->single
.probe_private
;
1030 struct marker_probe_closure
*closure
;
1033 for (i
= 0; closure
[i
].func
; i
++) {
1034 if (closure
[i
].func
!= probe
)
1037 return closure
[i
].probe_private
;
1043 return ERR_PTR(-ENOENT
);
1045 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1048 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1050 * Called when no channel users are active by the channel infrastructure.
1051 * Called with lock_markers() and channel mutex held.
1053 //ust// void markers_compact_event_ids(void)
1055 //ust// struct marker_entry *entry;
1056 //ust// unsigned int i;
1057 //ust// struct hlist_head *head;
1058 //ust// struct hlist_node *node;
1061 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1062 //ust// head = &marker_table[i];
1063 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1064 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1065 //ust// WARN_ON(ret < 0);
1066 //ust// entry->channel_id = ret;
1067 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1068 //ust// entry->name);
1069 //ust// WARN_ON(ret < 0);
1070 //ust// entry->event_id = ret;
1075 //ust//#ifdef CONFIG_MODULES
1078 * Returns 0 if current not found.
1079 * Returns 1 if current found.
1081 int lib_get_iter_markers(struct marker_iter
*iter
)
1083 struct lib
*iter_lib
;
1086 //ust// mutex_lock(&module_mutex);
1087 list_for_each_entry(iter_lib
, &libs
, list
) {
1088 if (iter_lib
< iter
->lib
)
1090 else if (iter_lib
> iter
->lib
)
1091 iter
->marker
= NULL
;
1092 found
= marker_get_iter_range(&iter
->marker
,
1093 iter_lib
->markers_start
,
1094 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1096 iter
->lib
= iter_lib
;
1100 //ust// mutex_unlock(&module_mutex);
1105 * marker_get_iter_range - Get a next marker iterator given a range.
1106 * @marker: current markers (in), next marker (out)
1107 * @begin: beginning of the range
1108 * @end: end of the range
1110 * Returns whether a next marker has been found (1) or not (0).
1111 * Will return the first marker in the range if the input marker is NULL.
1113 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1116 if (!*marker
&& begin
!= end
) {
1120 if (*marker
>= begin
&& *marker
< end
)
1124 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1126 static void marker_get_iter(struct marker_iter
*iter
)
1130 /* Core kernel markers */
1132 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1133 found
= marker_get_iter_range(&iter
->marker
,
1134 __start___markers
, __stop___markers
);
1138 /* Markers in modules. */
1139 found
= lib_get_iter_markers(iter
);
1142 marker_iter_reset(iter
);
1145 void marker_iter_start(struct marker_iter
*iter
)
1147 marker_get_iter(iter
);
1149 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1151 void marker_iter_next(struct marker_iter
*iter
)
1155 * iter->marker may be invalid because we blindly incremented it.
1156 * Make sure it is valid by marshalling on the markers, getting the
1157 * markers from following modules if necessary.
1159 marker_get_iter(iter
);
1161 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1163 void marker_iter_stop(struct marker_iter
*iter
)
1166 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1168 void marker_iter_reset(struct marker_iter
*iter
)
1171 iter
->marker
= NULL
;
1173 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1175 #ifdef CONFIG_MARKERS_USERSPACE
1177 * must be called with current->user_markers_mutex held
1179 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1181 struct user_marker
*umark
;
1182 struct hlist_node
*pos
, *n
;
1184 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1185 if (umark
->state
== state
) {
1186 hlist_del(&umark
->hlist
);
1192 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1193 //ust// char __user *state, int reg)
1195 //ust// struct user_marker *umark;
1197 //ust// struct marker_entry *entry;
1198 //ust// int ret = 0;
1200 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1201 //ust// current->comm, reg ? "registers" : "unregisters",
1202 //ust// name, state);
1204 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1205 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1206 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1207 //ust// umark->state = state;
1208 //ust// len = strncpy_from_user(umark->name, name,
1209 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1210 //ust// if (len < 0) {
1211 //ust// ret = -EFAULT;
1214 //ust// len = strncpy_from_user(umark->format, format,
1215 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1216 //ust// if (len < 0) {
1217 //ust// ret = -EFAULT;
1220 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1221 //ust// umark->format);
1222 //ust// mutex_lock(&markers_mutex);
1223 //ust// entry = get_marker("userspace", umark->name);
1224 //ust// if (entry) {
1225 //ust// if (entry->format &&
1226 //ust// strcmp(entry->format, umark->format) != 0) {
1227 //ust// printk(" error, wrong format in process %s",
1228 //ust// current->comm);
1229 //ust// ret = -EPERM;
1230 //ust// goto error_unlock;
1232 //ust// printk(" %s", !!entry->refcount
1233 //ust// ? "enabled" : "disabled");
1234 //ust// if (put_user(!!entry->refcount, state)) {
1235 //ust// ret = -EFAULT;
1236 //ust// goto error_unlock;
1238 //ust// printk("\n");
1240 //ust// printk(" disabled\n");
1241 //ust// if (put_user(0, umark->state)) {
1242 //ust// printk(KERN_WARNING
1243 //ust// "Marker in %s caused a fault\n",
1244 //ust// current->comm);
1245 //ust// goto error_unlock;
1248 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1249 //ust// hlist_add_head(&umark->hlist,
1250 //ust// ¤t->group_leader->user_markers);
1251 //ust// current->group_leader->user_markers_sequence++;
1252 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1253 //ust// mutex_unlock(&markers_mutex);
1255 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1256 //ust// free_user_marker(state,
1257 //ust// ¤t->group_leader->user_markers);
1258 //ust// current->group_leader->user_markers_sequence++;
1259 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1262 //ust// error_unlock:
1263 //ust// mutex_unlock(&markers_mutex);
1265 //ust// kfree(umark);
1272 //ust// * string : 0
1274 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1275 //ust// char __user *ubuf)
1277 //ust// long ret = -EPERM;
1281 //ust// switch (type) {
1282 //ust// case 0: /* String */
1283 //ust// ret = -ENOMEM;
1284 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1286 //ust// goto string_out;
1287 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1288 //ust// if (len < 0) {
1289 //ust// ret = -EFAULT;
1290 //ust// goto string_err;
1292 //ust// trace_mark(userspace, string, "string %s", page);
1294 //ust// free_page((unsigned long) page);
1303 //ust// static void marker_update_processes(void)
1305 //ust// struct task_struct *g, *t;
1308 //ust// * markers_mutex is taken to protect the p->user_markers read.
1310 //ust// mutex_lock(&markers_mutex);
1311 //ust// read_lock(&tasklist_lock);
1312 //ust// for_each_process(g) {
1313 //ust// WARN_ON(!thread_group_leader(g));
1314 //ust// if (hlist_empty(&g->user_markers))
1316 //ust// if (strcmp(g->comm, "testprog") == 0)
1317 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1320 //ust// /* TODO : implement this thread flag in each arch. */
1321 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1322 //ust// } while ((t = next_thread(t)) != g);
1324 //ust// read_unlock(&tasklist_lock);
1325 //ust// mutex_unlock(&markers_mutex);
1329 * Update current process.
1330 * Note that we have to wait a whole scheduler period before we are sure that
1331 * every running userspace threads have their markers updated.
1332 * (synchronize_sched() can be used to insure this).
1334 void marker_update_process(void)
1336 struct user_marker
*umark
;
1337 struct hlist_node
*pos
;
1338 struct marker_entry
*entry
;
1340 mutex_lock(&markers_mutex
);
1341 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1342 if (strcmp(current
->comm
, "testprog") == 0)
1343 printk(KERN_DEBUG
"do update pending for testprog\n");
1344 hlist_for_each_entry(umark
, pos
,
1345 ¤t
->group_leader
->user_markers
, hlist
) {
1346 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1347 umark
->name
, current
->comm
);
1348 entry
= get_marker("userspace", umark
->name
);
1350 if (entry
->format
&&
1351 strcmp(entry
->format
, umark
->format
) != 0) {
1353 " error, wrong format in process %s\n",
1357 if (put_user(!!entry
->refcount
, umark
->state
)) {
1359 "Marker in %s caused a fault\n",
1364 if (put_user(0, umark
->state
)) {
1366 "Marker in %s caused a fault\n",
1372 clear_thread_flag(TIF_MARKER_PENDING
);
1373 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1374 mutex_unlock(&markers_mutex
);
1378 * Called at process exit and upon do_execve().
1379 * We assume that when the leader exits, no more references can be done to the
1380 * leader structure by the other threads.
1382 void exit_user_markers(struct task_struct
*p
)
1384 struct user_marker
*umark
;
1385 struct hlist_node
*pos
, *n
;
1387 if (thread_group_leader(p
)) {
1388 mutex_lock(&markers_mutex
);
1389 mutex_lock(&p
->user_markers_mutex
);
1390 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1393 INIT_HLIST_HEAD(&p
->user_markers
);
1394 p
->user_markers_sequence
++;
1395 mutex_unlock(&p
->user_markers_mutex
);
1396 mutex_unlock(&markers_mutex
);
1400 int is_marker_enabled(const char *channel
, const char *name
)
1402 struct marker_entry
*entry
;
1404 mutex_lock(&markers_mutex
);
1405 entry
= get_marker(channel
, name
);
1406 mutex_unlock(&markers_mutex
);
1408 return entry
&& !!entry
->refcount
;
1412 int marker_module_notify(struct notifier_block
*self
,
1413 unsigned long val
, void *data
)
1415 struct module
*mod
= data
;
1418 case MODULE_STATE_COMING
:
1419 marker_update_probe_range(mod
->markers
,
1420 mod
->markers
+ mod
->num_markers
);
1422 case MODULE_STATE_GOING
:
1423 marker_update_probe_range(mod
->markers
,
1424 mod
->markers
+ mod
->num_markers
);
1430 struct notifier_block marker_module_nb
= {
1431 .notifier_call
= marker_module_notify
,
1435 //ust// static int init_markers(void)
1437 //ust// return register_module_notifier(&marker_module_nb);
1439 //ust// __initcall(init_markers);
1440 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1442 #endif /* CONFIG_MODULES */
1444 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1446 struct marker_entry
*entry
;
1447 struct ltt_probe_private_data call_data
;
1448 struct hlist_head
*head
;
1449 struct hlist_node
*node
;
1452 mutex_lock(&markers_mutex
);
1453 call_data
.trace
= trace
;
1454 call_data
.serializer
= NULL
;
1456 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1457 head
= &marker_table
[i
];
1458 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1459 __trace_mark(0, metadata
, core_marker_id
,
1461 "channel %s name %s event_id %hu "
1462 "int #1u%zu long #1u%zu pointer #1u%zu "
1463 "size_t #1u%zu alignment #1u%u",
1467 sizeof(int), sizeof(long),
1468 sizeof(void *), sizeof(size_t),
1469 ltt_get_alignment());
1471 __trace_mark(0, metadata
,
1474 "channel %s name %s format %s",
1480 mutex_unlock(&markers_mutex
);
1482 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1484 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1486 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1491 static void new_markers(struct marker
*start
, struct marker
*end
)
1495 for(m
=start
; m
< end
; m
++) {
1501 int marker_register_lib(struct marker
*markers_start
, struct marker_addr
*marker_addr_start
, int markers_count
)
1504 struct marker_addr
*addr
;
1506 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1508 pl
->markers_start
= markers_start
;
1509 pl
->markers_addr_start
= marker_addr_start
;
1510 pl
->markers_count
= markers_count
;
1513 for(addr
= marker_addr_start
; addr
< marker_addr_start
+ markers_count
; addr
++)
1514 addr
->marker
->location
= addr
->addr
;
1517 /* FIXME: maybe protect this with its own mutex? */
1519 list_add(&pl
->list
, &libs
);
1522 new_markers(markers_start
, markers_start
+ markers_count
);
1524 /* FIXME: update just the loaded lib */
1525 lib_update_markers();
1527 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1532 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1534 /*FIXME: implement; but before implementing, marker_register_lib must
1535 have appropriate locking. */
1540 static int initialized
= 0;
1542 void __attribute__((constructor
)) init_markers(void)
1545 marker_register_lib(__start___markers
, __start___marker_addr
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1546 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);