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 __thread
long ust_reg_stack
[500];
45 volatile __thread
long *ust_reg_stack_ptr
= (long *) 0;
47 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
48 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
50 /* Set to 1 to enable marker debug output */
51 static const int marker_debug
;
54 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
55 * and module markers and the hash table.
57 static DEFINE_MUTEX(markers_mutex
);
59 static LIST_HEAD(libs
);
62 void lock_markers(void)
64 mutex_lock(&markers_mutex
);
67 void unlock_markers(void)
69 mutex_unlock(&markers_mutex
);
73 * Marker hash table, containing the active markers.
74 * Protected by module_mutex.
76 #define MARKER_HASH_BITS 6
77 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
78 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
82 * It is used to make sure every handler has finished using its private data
83 * between two consecutive operation (add or remove) on a given marker. It is
84 * also used to delay the free of multiple probes array until a quiescent state
86 * marker entries modifications are protected by the markers_mutex.
89 struct hlist_node hlist
;
93 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
94 struct marker_probe_closure single
;
95 struct marker_probe_closure
*multi
;
96 int refcount
; /* Number of times armed. 0 if disarmed. */
102 unsigned char ptype
:1;
103 unsigned char format_allocated
:1;
104 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
107 #ifdef CONFIG_MARKERS_USERSPACE
108 static void marker_update_processes(void);
110 static void marker_update_processes(void)
116 * __mark_empty_function - Empty probe callback
117 * @mdata: marker data
118 * @probe_private: probe private data
119 * @call_private: call site private data
120 * @fmt: format string
121 * @...: variable argument list
123 * Empty callback provided as a probe to the markers. By providing this to a
124 * disabled marker, we make sure the execution flow is always valid even
125 * though the function pointer change and the marker enabling are two distinct
126 * operations that modifies the execution flow of preemptible code.
128 notrace
void __mark_empty_function(const struct marker
*mdata
,
129 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
132 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
135 * marker_probe_cb Callback that prepares the variable argument list for probes.
136 * @mdata: pointer of type struct marker
137 * @call_private: caller site private data
138 * @...: Variable argument list.
140 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
141 * need to put a full smp_rmb() in this branch. This is why we do not use
142 * rcu_dereference() for the pointer read.
144 notrace
void marker_probe_cb(const struct marker
*mdata
,
145 void *call_private
, struct registers
*regs
, ...)
151 * rcu_read_lock_sched does two things : disabling preemption to make
152 * sure the teardown of the callbacks can be done correctly when they
153 * are in modules and they insure RCU read coherency.
155 //ust// rcu_read_lock_sched_notrace();
156 ptype
= mdata
->ptype
;
157 if (likely(!ptype
)) {
158 marker_probe_func
*func
;
159 /* Must read the ptype before ptr. They are not data dependant,
160 * so we put an explicit smp_rmb() here. */
162 func
= mdata
->single
.func
;
163 /* Must read the ptr before private data. They are not data
164 * dependant, so we put an explicit smp_rmb() here. */
166 va_start(args
, regs
);
167 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
168 mdata
->format
, &args
);
171 struct marker_probe_closure
*multi
;
174 * Read mdata->ptype before mdata->multi.
177 multi
= mdata
->multi
;
179 * multi points to an array, therefore accessing the array
180 * depends on reading multi. However, even in this case,
181 * we must insure that the pointer is read _before_ the array
182 * data. Same as rcu_dereference, but we need a full smp_rmb()
183 * in the fast path, so put the explicit barrier here.
185 smp_read_barrier_depends();
186 for (i
= 0; multi
[i
].func
; i
++) {
187 va_start(args
, regs
);
188 multi
[i
].func(mdata
, multi
[i
].probe_private
,
189 regs
, call_private
, mdata
->format
, &args
);
193 //ust// rcu_read_unlock_sched_notrace();
195 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
198 * marker_probe_cb Callback that does not prepare the variable argument list.
199 * @mdata: pointer of type struct marker
200 * @call_private: caller site private data
201 * @...: Variable argument list.
203 * Should be connected to markers "MARK_NOARGS".
205 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
206 void *call_private
, struct registers
*regs
, ...)
208 va_list args
; /* not initialized */
211 //ust// rcu_read_lock_sched_notrace();
212 ptype
= mdata
->ptype
;
213 if (likely(!ptype
)) {
214 marker_probe_func
*func
;
215 /* Must read the ptype before ptr. They are not data dependant,
216 * so we put an explicit smp_rmb() here. */
218 func
= mdata
->single
.func
;
219 /* Must read the ptr before private data. They are not data
220 * dependant, so we put an explicit smp_rmb() here. */
222 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
223 mdata
->format
, &args
);
225 struct marker_probe_closure
*multi
;
228 * Read mdata->ptype before mdata->multi.
231 multi
= mdata
->multi
;
233 * multi points to an array, therefore accessing the array
234 * depends on reading multi. However, even in this case,
235 * we must insure that the pointer is read _before_ the array
236 * data. Same as rcu_dereference, but we need a full smp_rmb()
237 * in the fast path, so put the explicit barrier here.
239 smp_read_barrier_depends();
240 for (i
= 0; multi
[i
].func
; i
++)
241 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
242 call_private
, mdata
->format
, &args
);
244 //ust// rcu_read_unlock_sched_notrace();
247 static void free_old_closure(struct rcu_head
*head
)
249 struct marker_entry
*entry
= container_of(head
,
250 struct marker_entry
, rcu
);
251 kfree(entry
->oldptr
);
252 /* Make sure we free the data before setting the pending flag to 0 */
254 entry
->rcu_pending
= 0;
257 static void debug_print_probes(struct marker_entry
*entry
)
265 DBG("Single probe : %p %p",
267 entry
->single
.probe_private
);
269 for (i
= 0; entry
->multi
[i
].func
; i
++)
270 DBG("Multi probe %d : %p %p", i
,
271 entry
->multi
[i
].func
,
272 entry
->multi
[i
].probe_private
);
276 static struct marker_probe_closure
*
277 marker_entry_add_probe(struct marker_entry
*entry
,
278 marker_probe_func
*probe
, void *probe_private
)
281 struct marker_probe_closure
*old
, *new;
285 debug_print_probes(entry
);
288 if (entry
->single
.func
== probe
&&
289 entry
->single
.probe_private
== probe_private
)
290 return ERR_PTR(-EBUSY
);
291 if (entry
->single
.func
== __mark_empty_function
) {
293 entry
->single
.func
= probe
;
294 entry
->single
.probe_private
= probe_private
;
297 debug_print_probes(entry
);
305 /* (N -> N+1), (N != 0, 1) probes */
306 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
307 if (old
[nr_probes
].func
== probe
308 && old
[nr_probes
].probe_private
310 return ERR_PTR(-EBUSY
);
312 /* + 2 : one for new probe, one for NULL func */
313 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
316 return ERR_PTR(-ENOMEM
);
318 new[0] = entry
->single
;
321 nr_probes
* sizeof(struct marker_probe_closure
));
322 new[nr_probes
].func
= probe
;
323 new[nr_probes
].probe_private
= probe_private
;
324 entry
->refcount
= nr_probes
+ 1;
327 debug_print_probes(entry
);
331 static struct marker_probe_closure
*
332 marker_entry_remove_probe(struct marker_entry
*entry
,
333 marker_probe_func
*probe
, void *probe_private
)
335 int nr_probes
= 0, nr_del
= 0, i
;
336 struct marker_probe_closure
*old
, *new;
340 debug_print_probes(entry
);
342 /* 0 -> N is an error */
343 WARN_ON(entry
->single
.func
== __mark_empty_function
);
345 WARN_ON(probe
&& entry
->single
.func
!= probe
);
346 WARN_ON(entry
->single
.probe_private
!= probe_private
);
347 entry
->single
.func
= __mark_empty_function
;
350 debug_print_probes(entry
);
353 /* (N -> M), (N > 1, M >= 0) probes */
354 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
355 if ((!probe
|| old
[nr_probes
].func
== probe
)
356 && old
[nr_probes
].probe_private
362 if (nr_probes
- nr_del
== 0) {
363 /* N -> 0, (N > 1) */
364 entry
->single
.func
= __mark_empty_function
;
367 } else if (nr_probes
- nr_del
== 1) {
368 /* N -> 1, (N > 1) */
369 for (i
= 0; old
[i
].func
; i
++)
370 if ((probe
&& old
[i
].func
!= probe
) ||
371 old
[i
].probe_private
!= probe_private
)
372 entry
->single
= old
[i
];
377 /* N -> M, (N > 1, M > 1) */
379 new = kzalloc((nr_probes
- nr_del
+ 1)
380 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
382 return ERR_PTR(-ENOMEM
);
383 for (i
= 0; old
[i
].func
; i
++)
384 if ((probe
&& old
[i
].func
!= probe
) ||
385 old
[i
].probe_private
!= probe_private
)
387 entry
->refcount
= nr_probes
- nr_del
;
391 debug_print_probes(entry
);
396 * Get marker if the marker is present in the marker hash table.
397 * Must be called with markers_mutex held.
398 * Returns NULL if not present.
400 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
402 struct hlist_head
*head
;
403 struct hlist_node
*node
;
404 struct marker_entry
*e
;
405 size_t channel_len
= strlen(channel
) + 1;
406 size_t name_len
= strlen(name
) + 1;
409 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
410 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
411 hlist_for_each_entry(e
, node
, head
, hlist
) {
412 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
419 * Add the marker to the marker hash table. Must be called with markers_mutex
422 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
425 struct hlist_head
*head
;
426 struct hlist_node
*node
;
427 struct marker_entry
*e
;
428 size_t channel_len
= strlen(channel
) + 1;
429 size_t name_len
= strlen(name
) + 1;
430 size_t format_len
= 0;
433 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
435 format_len
= strlen(format
) + 1;
436 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
437 hlist_for_each_entry(e
, node
, head
, hlist
) {
438 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
439 DBG("Marker %s.%s busy", 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) {
546 DBG("Format mismatch for probe %s (%s), marker (%s)",
553 ret
= marker_set_format(entry
, elem
->format
);
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).
564 elem
->call
= entry
->call
;
565 elem
->channel_id
= entry
->channel_id
;
566 elem
->event_id
= entry
->event_id
;
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)
572 WARN_ON(elem
->single
.func
!= __mark_empty_function
573 && elem
->single
.probe_private
!= entry
->single
.probe_private
575 elem
->single
.probe_private
= entry
->single
.probe_private
;
577 * Make sure the private data is valid when we update the
581 elem
->single
.func
= entry
->single
.func
;
583 * We also make sure that the new probe callbacks array is consistent
584 * before setting a pointer to it.
586 rcu_assign_pointer(elem
->multi
, entry
->multi
);
588 * Update the function or multi probe array pointer before setting the
592 elem
->ptype
= entry
->ptype
;
594 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
595 //ust// WARN_ON(!elem->tp_cb);
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.
601 //ust// if (active) {
603 //ust// * try_module_get should always succeed because we hold
604 //ust// * markers_mutex to get the tp_cb address.
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);
613 //ust// ret = tracepoint_probe_unregister_noupdate(
614 //ust// elem->tp_name,
615 //ust// elem->tp_cb);
617 //ust// * tracepoint_probe_update_all() must be called
618 //ust// * before the module containing tp_cb is unloaded.
620 //ust// module_put(__module_text_address(
621 //ust// (unsigned long)elem->tp_cb));
624 elem
->state__imv
= active
;
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.
635 static void disable_marker(struct marker
*elem
)
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);
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.
646 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
647 //ust// elem->tp_cb);
648 //ust// WARN_ON(ret);
650 //ust// * tracepoint_probe_update_all() must be called
651 //ust// * before the module containing tp_cb is unloaded.
653 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
655 elem
->state__imv
= 0;
656 elem
->single
.func
= __mark_empty_function
;
657 /* Update the function before setting the ptype */
659 elem
->ptype
= 0; /* single probe */
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.
668 * is_marker_enabled - Check if a marker is enabled
669 * @channel: channel name
672 * Returns 1 if the marker is enabled, 0 if disabled.
674 int is_marker_enabled(const char *channel
, const char *name
)
676 struct marker_entry
*entry
;
678 mutex_lock(&markers_mutex
);
679 entry
= get_marker(channel
, name
);
680 mutex_unlock(&markers_mutex
);
682 return entry
&& !!entry
->refcount
;
686 * marker_update_probe_range - Update a probe range
687 * @begin: beginning of the range
688 * @end: end of the range
690 * Updates the probe callback corresponding to a range of markers.
692 void marker_update_probe_range(struct marker
*begin
,
696 struct marker_entry
*mark_entry
;
698 mutex_lock(&markers_mutex
);
699 for (iter
= begin
; iter
< end
; iter
++) {
700 mark_entry
= get_marker(iter
->channel
, iter
->name
);
702 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
704 * ignore error, continue
707 /* This is added for UST. We emit a core_marker_id event
708 * for markers that are already registered to a probe
709 * upon library load. Otherwise, no core_marker_id will
710 * be generated for these markers. Is this the right thing
713 trace_mark(metadata
, core_marker_id
,
714 "channel %s name %s event_id %hu "
715 "int #1u%zu long #1u%zu pointer #1u%zu "
716 "size_t #1u%zu alignment #1u%u",
717 iter
->channel
, iter
->name
, mark_entry
->event_id
,
718 sizeof(int), sizeof(long), sizeof(void *),
719 sizeof(size_t), ltt_get_alignment());
721 disable_marker(iter
);
724 mutex_unlock(&markers_mutex
);
727 static void lib_update_markers(void)
731 /* FIXME: we should probably take a mutex here on libs */
732 //ust// mutex_lock(&module_mutex);
733 list_for_each_entry(lib
, &libs
, list
)
734 marker_update_probe_range(lib
->markers_start
,
735 lib
->markers_start
+ lib
->markers_count
);
736 //ust// mutex_unlock(&module_mutex);
740 * Update probes, removing the faulty probes.
742 * Internal callback only changed before the first probe is connected to it.
743 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
744 * transitions. All other transitions will leave the old private data valid.
745 * This makes the non-atomicity of the callback/private data updates valid.
747 * "special case" updates :
752 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
753 * Site effect : marker_set_format may delete the marker entry (creating a
756 static void marker_update_probes(void)
758 /* Core kernel markers */
759 //ust// marker_update_probe_range(__start___markers, __stop___markers);
760 /* Markers in modules. */
761 //ust// module_update_markers();
762 lib_update_markers();
763 //ust// tracepoint_probe_update_all();
764 /* Update immediate values */
766 //ust// module_imv_update(); /* FIXME: need to port for libs? */
767 marker_update_processes();
771 * marker_probe_register - Connect a probe to a marker
772 * @channel: marker channel
774 * @format: format string
775 * @probe: probe handler
776 * @probe_private: probe private data
778 * private data must be a valid allocated memory address, or NULL.
779 * Returns 0 if ok, error value on error.
780 * The probe address must at least be aligned on the architecture pointer size.
782 int marker_probe_register(const char *channel
, const char *name
,
783 const char *format
, marker_probe_func
*probe
,
786 struct marker_entry
*entry
;
787 int ret
= 0, ret_err
;
788 struct marker_probe_closure
*old
;
791 mutex_lock(&markers_mutex
);
792 entry
= get_marker(channel
, name
);
795 entry
= add_marker(channel
, name
, format
);
797 ret
= PTR_ERR(entry
);
800 ret
= ltt_channels_register(channel
);
802 goto error_remove_marker
;
803 ret
= ltt_channels_get_index_from_name(channel
);
805 goto error_unregister_channel
;
806 entry
->channel_id
= ret
;
807 ret
= ltt_channels_get_event_id(channel
, name
);
809 goto error_unregister_channel
;
810 entry
->event_id
= ret
;
812 trace_mark(metadata
, core_marker_id
,
813 "channel %s name %s event_id %hu "
814 "int #1u%zu long #1u%zu pointer #1u%zu "
815 "size_t #1u%zu alignment #1u%u",
816 channel
, name
, entry
->event_id
,
817 sizeof(int), sizeof(long), sizeof(void *),
818 sizeof(size_t), ltt_get_alignment());
821 ret
= marker_set_format(entry
, format
);
822 else if (strcmp(entry
->format
, format
))
829 * If we detect that a call_rcu is pending for this marker,
830 * make sure it's executed now.
832 //ust// if (entry->rcu_pending)
833 //ust// rcu_barrier_sched();
834 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
838 goto error_unregister_channel
;
842 mutex_unlock(&markers_mutex
);
844 /* Activate marker if necessary */
845 marker_update_probes();
847 mutex_lock(&markers_mutex
);
848 entry
= get_marker(channel
, name
);
851 //ust// if (entry->rcu_pending)
852 //ust// rcu_barrier_sched();
854 entry
->rcu_pending
= 1;
855 /* write rcu_pending before calling the RCU callback */
857 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
858 synchronize_rcu(); free_old_closure(&entry
->rcu
);
861 error_unregister_channel
:
862 ret_err
= ltt_channels_unregister(channel
);
865 ret_err
= remove_marker(channel
, name
);
868 mutex_unlock(&markers_mutex
);
871 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
874 * marker_probe_unregister - Disconnect a probe from a marker
875 * @channel: marker channel
877 * @probe: probe function pointer
878 * @probe_private: probe private data
880 * Returns the private data given to marker_probe_register, or an ERR_PTR().
881 * We do not need to call a synchronize_sched to make sure the probes have
882 * finished running before doing a module unload, because the module unload
883 * itself uses stop_machine(), which insures that every preempt disabled section
886 int marker_probe_unregister(const char *channel
, const char *name
,
887 marker_probe_func
*probe
, void *probe_private
)
889 struct marker_entry
*entry
;
890 struct marker_probe_closure
*old
;
893 mutex_lock(&markers_mutex
);
894 entry
= get_marker(channel
, name
);
897 //ust// if (entry->rcu_pending)
898 //ust// rcu_barrier_sched();
899 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
900 mutex_unlock(&markers_mutex
);
902 marker_update_probes();
904 mutex_lock(&markers_mutex
);
905 entry
= get_marker(channel
, name
);
908 //ust// if (entry->rcu_pending)
909 //ust// rcu_barrier_sched();
911 entry
->rcu_pending
= 1;
912 /* write rcu_pending before calling the RCU callback */
914 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
915 synchronize_rcu(); free_old_closure(&entry
->rcu
);
916 remove_marker(channel
, name
); /* Ignore busy error message */
919 mutex_unlock(&markers_mutex
);
922 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
924 static struct marker_entry
*
925 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
927 struct marker_entry
*entry
;
929 struct hlist_head
*head
;
930 struct hlist_node
*node
;
932 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
933 head
= &marker_table
[i
];
934 hlist_for_each_entry(entry
, node
, head
, hlist
) {
936 if (entry
->single
.func
== probe
937 && entry
->single
.probe_private
941 struct marker_probe_closure
*closure
;
942 closure
= entry
->multi
;
943 for (i
= 0; closure
[i
].func
; i
++) {
944 if (closure
[i
].func
== probe
&&
945 closure
[i
].probe_private
956 * marker_probe_unregister_private_data - Disconnect a probe from a marker
957 * @probe: probe function
958 * @probe_private: probe private data
960 * Unregister a probe by providing the registered private data.
961 * Only removes the first marker found in hash table.
962 * Return 0 on success or error value.
963 * We do not need to call a synchronize_sched to make sure the probes have
964 * finished running before doing a module unload, because the module unload
965 * itself uses stop_machine(), which insures that every preempt disabled section
968 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
971 struct marker_entry
*entry
;
973 struct marker_probe_closure
*old
;
974 const char *channel
= NULL
, *name
= NULL
;
976 mutex_lock(&markers_mutex
);
977 entry
= get_marker_from_private_data(probe
, probe_private
);
982 //ust// if (entry->rcu_pending)
983 //ust// rcu_barrier_sched();
984 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
985 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
986 name
= kstrdup(entry
->name
, GFP_KERNEL
);
987 mutex_unlock(&markers_mutex
);
989 marker_update_probes();
991 mutex_lock(&markers_mutex
);
992 entry
= get_marker(channel
, name
);
995 //ust// if (entry->rcu_pending)
996 //ust// rcu_barrier_sched();
998 entry
->rcu_pending
= 1;
999 /* write rcu_pending before calling the RCU callback */
1001 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
1002 synchronize_rcu(); free_old_closure(&entry
->rcu
);
1003 /* Ignore busy error message */
1004 remove_marker(channel
, name
);
1006 mutex_unlock(&markers_mutex
);
1011 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1014 * marker_get_private_data - Get a marker's probe private data
1015 * @channel: marker channel
1016 * @name: marker name
1017 * @probe: probe to match
1018 * @num: get the nth matching probe's private data
1020 * Returns the nth private data pointer (starting from 0) matching, or an
1022 * Returns the private data pointer, or an ERR_PTR.
1023 * The private data pointer should _only_ be dereferenced if the caller is the
1024 * owner of the data, or its content could vanish. This is mostly used to
1025 * confirm that a caller is the owner of a registered probe.
1027 void *marker_get_private_data(const char *channel
, const char *name
,
1028 marker_probe_func
*probe
, int num
)
1030 struct hlist_head
*head
;
1031 struct hlist_node
*node
;
1032 struct marker_entry
*e
;
1033 size_t channel_len
= strlen(channel
) + 1;
1034 size_t name_len
= strlen(name
) + 1;
1038 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1039 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1040 hlist_for_each_entry(e
, node
, head
, hlist
) {
1041 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1043 if (num
== 0 && e
->single
.func
== probe
)
1044 return e
->single
.probe_private
;
1046 struct marker_probe_closure
*closure
;
1049 for (i
= 0; closure
[i
].func
; i
++) {
1050 if (closure
[i
].func
!= probe
)
1053 return closure
[i
].probe_private
;
1059 return ERR_PTR(-ENOENT
);
1061 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1064 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1066 * Called when no channel users are active by the channel infrastructure.
1067 * Called with lock_markers() and channel mutex held.
1069 //ust// void markers_compact_event_ids(void)
1071 //ust// struct marker_entry *entry;
1072 //ust// unsigned int i;
1073 //ust// struct hlist_head *head;
1074 //ust// struct hlist_node *node;
1077 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1078 //ust// head = &marker_table[i];
1079 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1080 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1081 //ust// WARN_ON(ret < 0);
1082 //ust// entry->channel_id = ret;
1083 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1084 //ust// entry->name);
1085 //ust// WARN_ON(ret < 0);
1086 //ust// entry->event_id = ret;
1091 //ust//#ifdef CONFIG_MODULES
1094 * Returns 0 if current not found.
1095 * Returns 1 if current found.
1097 int lib_get_iter_markers(struct marker_iter
*iter
)
1099 struct lib
*iter_lib
;
1102 //ust// mutex_lock(&module_mutex);
1103 list_for_each_entry(iter_lib
, &libs
, list
) {
1104 if (iter_lib
< iter
->lib
)
1106 else if (iter_lib
> iter
->lib
)
1107 iter
->marker
= NULL
;
1108 found
= marker_get_iter_range(&iter
->marker
,
1109 iter_lib
->markers_start
,
1110 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1112 iter
->lib
= iter_lib
;
1116 //ust// mutex_unlock(&module_mutex);
1121 * marker_get_iter_range - Get a next marker iterator given a range.
1122 * @marker: current markers (in), next marker (out)
1123 * @begin: beginning of the range
1124 * @end: end of the range
1126 * Returns whether a next marker has been found (1) or not (0).
1127 * Will return the first marker in the range if the input marker is NULL.
1129 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1132 if (!*marker
&& begin
!= end
) {
1136 if (*marker
>= begin
&& *marker
< end
)
1140 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1142 static void marker_get_iter(struct marker_iter
*iter
)
1146 /* Core kernel markers */
1148 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1149 found
= marker_get_iter_range(&iter
->marker
,
1150 __start___markers
, __stop___markers
);
1154 /* Markers in modules. */
1155 found
= lib_get_iter_markers(iter
);
1158 marker_iter_reset(iter
);
1161 void marker_iter_start(struct marker_iter
*iter
)
1163 marker_get_iter(iter
);
1165 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1167 void marker_iter_next(struct marker_iter
*iter
)
1171 * iter->marker may be invalid because we blindly incremented it.
1172 * Make sure it is valid by marshalling on the markers, getting the
1173 * markers from following modules if necessary.
1175 marker_get_iter(iter
);
1177 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1179 void marker_iter_stop(struct marker_iter
*iter
)
1182 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1184 void marker_iter_reset(struct marker_iter
*iter
)
1187 iter
->marker
= NULL
;
1189 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1191 #ifdef CONFIG_MARKERS_USERSPACE
1193 * must be called with current->user_markers_mutex held
1195 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1197 struct user_marker
*umark
;
1198 struct hlist_node
*pos
, *n
;
1200 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1201 if (umark
->state
== state
) {
1202 hlist_del(&umark
->hlist
);
1209 * Update current process.
1210 * Note that we have to wait a whole scheduler period before we are sure that
1211 * every running userspace threads have their markers updated.
1212 * (synchronize_sched() can be used to insure this).
1214 //ust// void marker_update_process(void)
1216 //ust// struct user_marker *umark;
1217 //ust// struct hlist_node *pos;
1218 //ust// struct marker_entry *entry;
1220 //ust// mutex_lock(&markers_mutex);
1221 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1222 //ust// if (strcmp(current->comm, "testprog") == 0)
1223 //ust// DBG("do update pending for testprog");
1224 //ust// hlist_for_each_entry(umark, pos,
1225 //ust// ¤t->group_leader->user_markers, hlist) {
1226 //ust// DBG("Updating marker %s in %s", umark->name, current->comm);
1227 //ust// entry = get_marker("userspace", umark->name);
1228 //ust// if (entry) {
1229 //ust// if (entry->format &&
1230 //ust// strcmp(entry->format, umark->format) != 0) {
1231 //ust// WARN("error, wrong format in process %s",
1232 //ust// current->comm);
1235 //ust// if (put_user(!!entry->refcount, umark->state)) {
1236 //ust// WARN("Marker in %s caused a fault",
1237 //ust// current->comm);
1241 //ust// if (put_user(0, umark->state)) {
1242 //ust// WARN("Marker in %s caused a fault", current->comm);
1247 //ust// clear_thread_flag(TIF_MARKER_PENDING);
1248 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1249 //ust// mutex_unlock(&markers_mutex);
1253 * Called at process exit and upon do_execve().
1254 * We assume that when the leader exits, no more references can be done to the
1255 * leader structure by the other threads.
1257 void exit_user_markers(struct task_struct
*p
)
1259 struct user_marker
*umark
;
1260 struct hlist_node
*pos
, *n
;
1262 if (thread_group_leader(p
)) {
1263 mutex_lock(&markers_mutex
);
1264 mutex_lock(&p
->user_markers_mutex
);
1265 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1268 INIT_HLIST_HEAD(&p
->user_markers
);
1269 p
->user_markers_sequence
++;
1270 mutex_unlock(&p
->user_markers_mutex
);
1271 mutex_unlock(&markers_mutex
);
1275 int is_marker_enabled(const char *channel
, const char *name
)
1277 struct marker_entry
*entry
;
1279 mutex_lock(&markers_mutex
);
1280 entry
= get_marker(channel
, name
);
1281 mutex_unlock(&markers_mutex
);
1283 return entry
&& !!entry
->refcount
;
1287 int marker_module_notify(struct notifier_block
*self
,
1288 unsigned long val
, void *data
)
1290 struct module
*mod
= data
;
1293 case MODULE_STATE_COMING
:
1294 marker_update_probe_range(mod
->markers
,
1295 mod
->markers
+ mod
->num_markers
);
1297 case MODULE_STATE_GOING
:
1298 marker_update_probe_range(mod
->markers
,
1299 mod
->markers
+ mod
->num_markers
);
1305 struct notifier_block marker_module_nb
= {
1306 .notifier_call
= marker_module_notify
,
1310 //ust// static int init_markers(void)
1312 //ust// return register_module_notifier(&marker_module_nb);
1314 //ust// __initcall(init_markers);
1315 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1317 #endif /* CONFIG_MODULES */
1319 void ltt_dump_marker_state(struct ust_trace
*trace
)
1321 struct marker_entry
*entry
;
1322 struct ltt_probe_private_data call_data
;
1323 struct hlist_head
*head
;
1324 struct hlist_node
*node
;
1327 mutex_lock(&markers_mutex
);
1328 call_data
.trace
= trace
;
1329 call_data
.serializer
= NULL
;
1331 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1332 head
= &marker_table
[i
];
1333 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1334 __trace_mark(0, metadata
, core_marker_id
,
1336 "channel %s name %s event_id %hu "
1337 "int #1u%zu long #1u%zu pointer #1u%zu "
1338 "size_t #1u%zu alignment #1u%u",
1342 sizeof(int), sizeof(long),
1343 sizeof(void *), sizeof(size_t),
1344 ltt_get_alignment());
1346 __trace_mark(0, metadata
,
1349 "channel %s name %s format %s",
1355 mutex_unlock(&markers_mutex
);
1357 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1359 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1361 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1366 static void new_markers(struct marker
*start
, struct marker
*end
)
1370 for(m
=start
; m
< end
; m
++) {
1376 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1379 struct marker_addr
*addr
;
1381 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1383 pl
->markers_start
= markers_start
;
1384 pl
->markers_count
= markers_count
;
1386 /* FIXME: maybe protect this with its own mutex? */
1388 list_add(&pl
->list
, &libs
);
1391 new_markers(markers_start
, markers_start
+ markers_count
);
1393 /* FIXME: update just the loaded lib */
1394 lib_update_markers();
1396 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1401 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1403 /*FIXME: implement; but before implementing, marker_register_lib must
1404 have appropriate locking. */
1409 static int initialized
= 0;
1411 void __attribute__((constructor
)) init_markers(void)
1414 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1415 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);