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
23 #include <urcu/rculist.h>
24 #include <urcu/hlist.h>
27 #include <ust/marker.h>
31 #include "tracercore.h"
34 __thread
long ust_reg_stack
[500];
35 volatile __thread
long *ust_reg_stack_ptr
= (long *) 0;
37 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
38 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
40 /* Set to 1 to enable marker debug output */
41 static const int marker_debug
;
44 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
45 * and module markers and the hash table.
47 static DEFINE_MUTEX(markers_mutex
);
49 static CDS_LIST_HEAD(libs
);
52 void lock_markers(void)
54 pthread_mutex_lock(&markers_mutex
);
57 void unlock_markers(void)
59 pthread_mutex_unlock(&markers_mutex
);
63 * Marker hash table, containing the active markers.
64 * Protected by module_mutex.
66 #define MARKER_HASH_BITS 6
67 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
68 static struct cds_hlist_head marker_table
[MARKER_TABLE_SIZE
];
72 * It is used to make sure every handler has finished using its private data
73 * between two consecutive operation (add or remove) on a given marker. It is
74 * also used to delay the free of multiple probes array until a quiescent state
76 * marker entries modifications are protected by the markers_mutex.
79 struct cds_hlist_node hlist
;
83 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
84 struct marker_probe_closure single
;
85 struct marker_probe_closure
*multi
;
86 int refcount
; /* Number of times armed. 0 if disarmed. */
92 unsigned char ptype
:1;
93 unsigned char format_allocated
:1;
94 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
97 #ifdef CONFIG_MARKERS_USERSPACE
98 static void marker_update_processes(void);
100 static void marker_update_processes(void)
106 * __mark_empty_function - Empty probe callback
107 * @mdata: marker data
108 * @probe_private: probe private data
109 * @call_private: call site private data
110 * @fmt: format string
111 * @...: variable argument list
113 * Empty callback provided as a probe to the markers. By providing this to a
114 * disabled marker, we make sure the execution flow is always valid even
115 * though the function pointer change and the marker enabling are two distinct
116 * operations that modifies the execution flow of preemptible code.
118 notrace
void __mark_empty_function(const struct marker
*mdata
,
119 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
122 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
125 * marker_probe_cb Callback that prepares the variable argument list for probes.
126 * @mdata: pointer of type struct marker
127 * @call_private: caller site private data
128 * @...: Variable argument list.
130 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
131 * need to put a full cmm_smp_rmb() in this branch. This is why we do not use
132 * rcu_dereference() for the pointer read.
134 notrace
void marker_probe_cb(const struct marker
*mdata
,
135 void *call_private
, struct registers
*regs
, ...)
141 * rcu_read_lock_sched does two things : disabling preemption to make
142 * sure the teardown of the callbacks can be done correctly when they
143 * are in modules and they insure RCU read coherency.
145 //ust// rcu_read_lock_sched_notrace();
146 ptype
= mdata
->ptype
;
147 if (likely(!ptype
)) {
148 marker_probe_func
*func
;
149 /* Must read the ptype before ptr. They are not data dependant,
150 * so we put an explicit cmm_smp_rmb() here. */
152 func
= mdata
->single
.func
;
153 /* Must read the ptr before private data. They are not data
154 * dependant, so we put an explicit cmm_smp_rmb() here. */
156 va_start(args
, regs
);
157 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
158 mdata
->format
, &args
);
161 struct marker_probe_closure
*multi
;
164 * Read mdata->ptype before mdata->multi.
167 multi
= mdata
->multi
;
169 * multi points to an array, therefore accessing the array
170 * depends on reading multi. However, even in this case,
171 * we must insure that the pointer is read _before_ the array
172 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
173 * in the fast path, so put the explicit cmm_barrier here.
175 cmm_smp_read_barrier_depends();
176 for (i
= 0; multi
[i
].func
; i
++) {
177 va_start(args
, regs
);
178 multi
[i
].func(mdata
, multi
[i
].probe_private
,
179 regs
, call_private
, mdata
->format
, &args
);
183 //ust// rcu_read_unlock_sched_notrace();
185 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
188 * marker_probe_cb Callback that does not prepare the variable argument list.
189 * @mdata: pointer of type struct marker
190 * @call_private: caller site private data
191 * @...: Variable argument list.
193 * Should be connected to markers "MARK_NOARGS".
195 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
196 void *call_private
, struct registers
*regs
, ...)
198 va_list args
; /* not initialized */
201 //ust// rcu_read_lock_sched_notrace();
202 ptype
= mdata
->ptype
;
203 if (likely(!ptype
)) {
204 marker_probe_func
*func
;
205 /* Must read the ptype before ptr. They are not data dependant,
206 * so we put an explicit cmm_smp_rmb() here. */
208 func
= mdata
->single
.func
;
209 /* Must read the ptr before private data. They are not data
210 * dependant, so we put an explicit cmm_smp_rmb() here. */
212 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
213 mdata
->format
, &args
);
215 struct marker_probe_closure
*multi
;
218 * Read mdata->ptype before mdata->multi.
221 multi
= mdata
->multi
;
223 * multi points to an array, therefore accessing the array
224 * depends on reading multi. However, even in this case,
225 * we must insure that the pointer is read _before_ the array
226 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
227 * in the fast path, so put the explicit cmm_barrier here.
229 cmm_smp_read_barrier_depends();
230 for (i
= 0; multi
[i
].func
; i
++)
231 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
232 call_private
, mdata
->format
, &args
);
234 //ust// rcu_read_unlock_sched_notrace();
237 static void free_old_closure(struct rcu_head
*head
)
239 struct marker_entry
*entry
= _ust_container_of(head
,
240 struct marker_entry
, rcu
);
242 /* Make sure we free the data before setting the pending flag to 0 */
244 entry
->rcu_pending
= 0;
247 static void debug_print_probes(struct marker_entry
*entry
)
255 DBG("Single probe : %p %p",
257 entry
->single
.probe_private
);
259 for (i
= 0; entry
->multi
[i
].func
; i
++)
260 DBG("Multi probe %d : %p %p", i
,
261 entry
->multi
[i
].func
,
262 entry
->multi
[i
].probe_private
);
266 static struct marker_probe_closure
*
267 marker_entry_add_probe(struct marker_entry
*entry
,
268 marker_probe_func
*probe
, void *probe_private
)
271 struct marker_probe_closure
*old
, *new;
275 debug_print_probes(entry
);
278 if (entry
->single
.func
== probe
&&
279 entry
->single
.probe_private
== probe_private
)
280 return ERR_PTR(-EBUSY
);
281 if (entry
->single
.func
== __mark_empty_function
) {
283 entry
->single
.func
= probe
;
284 entry
->single
.probe_private
= probe_private
;
287 debug_print_probes(entry
);
295 /* (N -> N+1), (N != 0, 1) probes */
296 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
297 if (old
[nr_probes
].func
== probe
298 && old
[nr_probes
].probe_private
300 return ERR_PTR(-EBUSY
);
302 /* + 2 : one for new probe, one for NULL func */
303 new = zmalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
));
305 return ERR_PTR(-ENOMEM
);
307 new[0] = entry
->single
;
310 nr_probes
* sizeof(struct marker_probe_closure
));
311 new[nr_probes
].func
= probe
;
312 new[nr_probes
].probe_private
= probe_private
;
313 entry
->refcount
= nr_probes
+ 1;
316 debug_print_probes(entry
);
320 static struct marker_probe_closure
*
321 marker_entry_remove_probe(struct marker_entry
*entry
,
322 marker_probe_func
*probe
, void *probe_private
)
324 int nr_probes
= 0, nr_del
= 0, i
;
325 struct marker_probe_closure
*old
, *new;
329 debug_print_probes(entry
);
331 /* 0 -> N is an error */
332 WARN_ON(entry
->single
.func
== __mark_empty_function
);
334 WARN_ON(probe
&& entry
->single
.func
!= probe
);
335 WARN_ON(entry
->single
.probe_private
!= probe_private
);
336 entry
->single
.func
= __mark_empty_function
;
339 debug_print_probes(entry
);
342 /* (N -> M), (N > 1, M >= 0) probes */
343 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
344 if ((!probe
|| old
[nr_probes
].func
== probe
)
345 && old
[nr_probes
].probe_private
351 if (nr_probes
- nr_del
== 0) {
352 /* N -> 0, (N > 1) */
353 entry
->single
.func
= __mark_empty_function
;
356 } else if (nr_probes
- nr_del
== 1) {
357 /* N -> 1, (N > 1) */
358 for (i
= 0; old
[i
].func
; i
++)
359 if ((probe
&& old
[i
].func
!= probe
) ||
360 old
[i
].probe_private
!= probe_private
)
361 entry
->single
= old
[i
];
366 /* N -> M, (N > 1, M > 1) */
368 new = zmalloc((nr_probes
- nr_del
+ 1) * sizeof(struct marker_probe_closure
));
370 return ERR_PTR(-ENOMEM
);
371 for (i
= 0; old
[i
].func
; i
++)
372 if ((probe
&& old
[i
].func
!= probe
) ||
373 old
[i
].probe_private
!= probe_private
)
375 entry
->refcount
= nr_probes
- nr_del
;
379 debug_print_probes(entry
);
384 * Get marker if the marker is present in the marker hash table.
385 * Must be called with markers_mutex held.
386 * Returns NULL if not present.
388 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
390 struct cds_hlist_head
*head
;
391 struct cds_hlist_node
*node
;
392 struct marker_entry
*e
;
393 size_t channel_len
= strlen(channel
) + 1;
394 size_t name_len
= strlen(name
) + 1;
397 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
398 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
399 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
400 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
407 * Add the marker to the marker hash table. Must be called with markers_mutex
410 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
413 struct cds_hlist_head
*head
;
414 struct cds_hlist_node
*node
;
415 struct marker_entry
*e
;
416 size_t channel_len
= strlen(channel
) + 1;
417 size_t name_len
= strlen(name
) + 1;
418 size_t format_len
= 0;
421 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
423 format_len
= strlen(format
) + 1;
424 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
425 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
426 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
427 DBG("Marker %s.%s busy", channel
, name
);
428 return ERR_PTR(-EBUSY
); /* Already there */
432 * Using zmalloc here to allocate a variable length element. Could
433 * cause some memory fragmentation if overused.
435 e
= zmalloc(sizeof(struct marker_entry
)
436 + channel_len
+ name_len
+ format_len
);
438 return ERR_PTR(-ENOMEM
);
439 memcpy(e
->channel
, channel
, channel_len
);
440 e
->name
= &e
->channel
[channel_len
];
441 memcpy(e
->name
, name
, name_len
);
443 e
->format
= &e
->name
[name_len
];
444 memcpy(e
->format
, format
, format_len
);
445 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
446 e
->call
= marker_probe_cb_noarg
;
448 e
->call
= marker_probe_cb
;
449 trace_mark(metadata
, core_marker_format
,
450 "channel %s name %s format %s",
451 e
->channel
, e
->name
, e
->format
);
454 e
->call
= marker_probe_cb
;
456 e
->single
.func
= __mark_empty_function
;
457 e
->single
.probe_private
= NULL
;
460 e
->format_allocated
= 0;
463 cds_hlist_add_head(&e
->hlist
, head
);
468 * Remove the marker from the marker hash table. Must be called with mutex_lock
471 static int remove_marker(const char *channel
, const char *name
)
473 struct cds_hlist_head
*head
;
474 struct cds_hlist_node
*node
;
475 struct marker_entry
*e
;
477 size_t channel_len
= strlen(channel
) + 1;
478 size_t name_len
= strlen(name
) + 1;
482 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
483 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
484 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
485 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
492 if (e
->single
.func
!= __mark_empty_function
)
494 cds_hlist_del(&e
->hlist
);
495 if (e
->format_allocated
)
497 ret
= ltt_channels_unregister(e
->channel
);
499 /* Make sure the call_rcu has been executed */
500 //ust// if (e->rcu_pending)
501 //ust// rcu_cmm_barrier_sched();
507 * Set the mark_entry format to the format found in the element.
509 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
511 entry
->format
= strdup(format
);
514 entry
->format_allocated
= 1;
516 trace_mark(metadata
, core_marker_format
,
517 "channel %s name %s format %s",
518 entry
->channel
, entry
->name
, entry
->format
);
523 * Sets the probe callback corresponding to one marker.
525 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
529 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
532 if (strcmp(entry
->format
, elem
->format
) != 0) {
533 ERR("Format mismatch for probe %s (%s), marker (%s)",
540 ret
= marker_set_format(entry
, elem
->format
);
546 * probe_cb setup (statically known) is done here. It is
547 * asynchronous with the rest of execution, therefore we only
548 * pass from a "safe" callback (with argument) to an "unsafe"
549 * callback (does not set arguments).
551 elem
->call
= entry
->call
;
552 elem
->channel_id
= entry
->channel_id
;
553 elem
->event_id
= entry
->event_id
;
556 * We only update the single probe private data when the ptr is
557 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
559 WARN_ON(elem
->single
.func
!= __mark_empty_function
560 && elem
->single
.probe_private
!= entry
->single
.probe_private
562 elem
->single
.probe_private
= entry
->single
.probe_private
;
564 * Make sure the private data is valid when we update the
568 elem
->single
.func
= entry
->single
.func
;
570 * We also make sure that the new probe callbacks array is consistent
571 * before setting a pointer to it.
573 rcu_assign_pointer(elem
->multi
, entry
->multi
);
575 * Update the function or multi probe array pointer before setting the
579 elem
->ptype
= entry
->ptype
;
581 if (elem
->tp_name
&& (active
^ _imv_read(elem
->state
))) {
582 WARN_ON(!elem
->tp_cb
);
584 * It is ok to directly call the probe registration because type
585 * checking has been done in the __trace_mark_tp() macro.
590 * try_module_get should always succeed because we hold
591 * markers_mutex to get the tp_cb address.
593 //ust// ret = try_module_get(__module_text_address(
594 //ust// (unsigned long)elem->tp_cb));
595 //ust// BUG_ON(!ret);
596 ret
= tracepoint_probe_register_noupdate(
600 ret
= tracepoint_probe_unregister_noupdate(
604 * tracepoint_probe_update_all() must be called
605 * before the module containing tp_cb is unloaded.
607 //ust// module_put(__module_text_address(
608 //ust// (unsigned long)elem->tp_cb));
611 elem
->state__imv
= active
;
617 * Disable a marker and its probe callback.
618 * Note: only waiting an RCU period after setting elem->call to the empty
619 * function insures that the original callback is not used anymore. This insured
620 * by rcu_read_lock_sched around the call site.
622 static void disable_marker(struct marker
*elem
)
626 /* leave "call" as is. It is known statically. */
627 if (elem
->tp_name
&& _imv_read(elem
->state
)) {
628 WARN_ON(!elem
->tp_cb
);
630 * It is ok to directly call the probe registration because type
631 * checking has been done in the __trace_mark_tp() macro.
633 ret
= tracepoint_probe_unregister_noupdate(elem
->tp_name
,
637 * tracepoint_probe_update_all() must be called
638 * before the module containing tp_cb is unloaded.
640 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
642 elem
->state__imv
= 0;
643 elem
->single
.func
= __mark_empty_function
;
644 /* Update the function before setting the ptype */
646 elem
->ptype
= 0; /* single probe */
648 * Leave the private data and channel_id/event_id there, because removal
649 * is racy and should be done only after an RCU period. These are never
650 * used until the next initialization anyway.
655 * is_marker_enabled - Check if a marker is enabled
656 * @channel: channel name
659 * Returns 1 if the marker is enabled, 0 if disabled.
661 int is_marker_enabled(const char *channel
, const char *name
)
663 struct marker_entry
*entry
;
665 pthread_mutex_lock(&markers_mutex
);
666 entry
= get_marker(channel
, name
);
667 pthread_mutex_unlock(&markers_mutex
);
669 return entry
&& !!entry
->refcount
;
673 * marker_update_probe_range - Update a probe range
674 * @begin: beginning of the range
675 * @end: end of the range
677 * Updates the probe callback corresponding to a range of markers.
679 void marker_update_probe_range(struct marker
*begin
,
683 struct marker_entry
*mark_entry
;
685 pthread_mutex_lock(&markers_mutex
);
686 for (iter
= begin
; iter
< end
; iter
++) {
687 mark_entry
= get_marker(iter
->channel
, iter
->name
);
689 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
691 * ignore error, continue
694 /* This is added for UST. We emit a core_marker_id event
695 * for markers that are already registered to a probe
696 * upon library load. Otherwise, no core_marker_id will
697 * be generated for these markers. Is this the right thing
700 trace_mark(metadata
, core_marker_id
,
701 "channel %s name %s event_id %hu "
702 "int #1u%zu long #1u%zu pointer #1u%zu "
703 "size_t #1u%zu alignment #1u%u",
704 iter
->channel
, iter
->name
, mark_entry
->event_id
,
705 sizeof(int), sizeof(long), sizeof(void *),
706 sizeof(size_t), ltt_get_alignment());
708 disable_marker(iter
);
711 pthread_mutex_unlock(&markers_mutex
);
714 static void lib_update_markers(void)
718 /* FIXME: we should probably take a mutex here on libs */
719 //ust// pthread_mutex_lock(&module_mutex);
720 cds_list_for_each_entry(lib
, &libs
, list
)
721 marker_update_probe_range(lib
->markers_start
,
722 lib
->markers_start
+ lib
->markers_count
);
723 //ust// pthread_mutex_unlock(&module_mutex);
727 * Update probes, removing the faulty probes.
729 * Internal callback only changed before the first probe is connected to it.
730 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
731 * transitions. All other transitions will leave the old private data valid.
732 * This makes the non-atomicity of the callback/private data updates valid.
734 * "special case" updates :
739 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
740 * Site effect : marker_set_format may delete the marker entry (creating a
743 static void marker_update_probes(void)
745 /* Core kernel markers */
746 //ust// marker_update_probe_range(__start___markers, __stop___markers);
747 /* Markers in modules. */
748 //ust// module_update_markers();
749 lib_update_markers();
750 tracepoint_probe_update_all();
751 /* Update immediate values */
753 //ust// module_imv_update(); /* FIXME: need to port for libs? */
754 marker_update_processes();
758 * marker_probe_register - Connect a probe to a marker
759 * @channel: marker channel
761 * @format: format string
762 * @probe: probe handler
763 * @probe_private: probe private data
765 * private data must be a valid allocated memory address, or NULL.
766 * Returns 0 if ok, error value on error.
767 * The probe address must at least be aligned on the architecture pointer size.
769 int marker_probe_register(const char *channel
, const char *name
,
770 const char *format
, marker_probe_func
*probe
,
773 struct marker_entry
*entry
;
774 int ret
= 0, ret_err
;
775 struct marker_probe_closure
*old
;
778 pthread_mutex_lock(&markers_mutex
);
779 entry
= get_marker(channel
, name
);
782 entry
= add_marker(channel
, name
, format
);
784 ret
= PTR_ERR(entry
);
787 ret
= ltt_channels_register(channel
);
789 goto error_remove_marker
;
790 ret
= ltt_channels_get_index_from_name(channel
);
792 goto error_unregister_channel
;
793 entry
->channel_id
= ret
;
794 ret
= ltt_channels_get_event_id(channel
, name
);
796 goto error_unregister_channel
;
797 entry
->event_id
= ret
;
799 trace_mark(metadata
, core_marker_id
,
800 "channel %s name %s event_id %hu "
801 "int #1u%zu long #1u%zu pointer #1u%zu "
802 "size_t #1u%zu alignment #1u%u",
803 channel
, name
, entry
->event_id
,
804 sizeof(int), sizeof(long), sizeof(void *),
805 sizeof(size_t), ltt_get_alignment());
808 ret
= marker_set_format(entry
, format
);
809 else if (strcmp(entry
->format
, format
))
816 * If we detect that a call_rcu is pending for this marker,
817 * make sure it's executed now.
819 //ust// if (entry->rcu_pending)
820 //ust// rcu_cmm_barrier_sched();
821 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
825 goto error_unregister_channel
;
829 pthread_mutex_unlock(&markers_mutex
);
831 /* Activate marker if necessary */
832 marker_update_probes();
834 pthread_mutex_lock(&markers_mutex
);
835 entry
= get_marker(channel
, name
);
838 //ust// if (entry->rcu_pending)
839 //ust// rcu_cmm_barrier_sched();
841 entry
->rcu_pending
= 1;
842 /* write rcu_pending before calling the RCU callback */
844 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
845 synchronize_rcu(); free_old_closure(&entry
->rcu
);
848 error_unregister_channel
:
849 ret_err
= ltt_channels_unregister(channel
);
852 ret_err
= remove_marker(channel
, name
);
855 pthread_mutex_unlock(&markers_mutex
);
858 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
861 * marker_probe_unregister - Disconnect a probe from a marker
862 * @channel: marker channel
864 * @probe: probe function pointer
865 * @probe_private: probe private data
867 * Returns the private data given to marker_probe_register, or an ERR_PTR().
868 * We do not need to call a synchronize_sched to make sure the probes have
869 * finished running before doing a module unload, because the module unload
870 * itself uses stop_machine(), which insures that every preempt disabled section
873 int marker_probe_unregister(const char *channel
, const char *name
,
874 marker_probe_func
*probe
, void *probe_private
)
876 struct marker_entry
*entry
;
877 struct marker_probe_closure
*old
;
880 pthread_mutex_lock(&markers_mutex
);
881 entry
= get_marker(channel
, name
);
884 //ust// if (entry->rcu_pending)
885 //ust// rcu_cmm_barrier_sched();
886 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
887 pthread_mutex_unlock(&markers_mutex
);
889 marker_update_probes();
891 pthread_mutex_lock(&markers_mutex
);
892 entry
= get_marker(channel
, name
);
895 //ust// if (entry->rcu_pending)
896 //ust// rcu_cmm_barrier_sched();
898 entry
->rcu_pending
= 1;
899 /* write rcu_pending before calling the RCU callback */
901 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
902 synchronize_rcu(); free_old_closure(&entry
->rcu
);
903 remove_marker(channel
, name
); /* Ignore busy error message */
906 pthread_mutex_unlock(&markers_mutex
);
909 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
911 static struct marker_entry
*
912 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
914 struct marker_entry
*entry
;
916 struct cds_hlist_head
*head
;
917 struct cds_hlist_node
*node
;
919 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
920 head
= &marker_table
[i
];
921 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
923 if (entry
->single
.func
== probe
924 && entry
->single
.probe_private
928 struct marker_probe_closure
*closure
;
929 closure
= entry
->multi
;
930 for (i
= 0; closure
[i
].func
; i
++) {
931 if (closure
[i
].func
== probe
&&
932 closure
[i
].probe_private
943 * marker_probe_unregister_private_data - Disconnect a probe from a marker
944 * @probe: probe function
945 * @probe_private: probe private data
947 * Unregister a probe by providing the registered private data.
948 * Only removes the first marker found in hash table.
949 * Return 0 on success or error value.
950 * We do not need to call a synchronize_sched to make sure the probes have
951 * finished running before doing a module unload, because the module unload
952 * itself uses stop_machine(), which insures that every preempt disabled section
955 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
958 struct marker_entry
*entry
;
960 struct marker_probe_closure
*old
;
961 char *channel
= NULL
, *name
= NULL
;
963 pthread_mutex_lock(&markers_mutex
);
964 entry
= get_marker_from_private_data(probe
, probe_private
);
969 //ust// if (entry->rcu_pending)
970 //ust// rcu_cmm_barrier_sched();
971 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
972 channel
= strdup(entry
->channel
);
973 name
= strdup(entry
->name
);
974 pthread_mutex_unlock(&markers_mutex
);
976 marker_update_probes();
978 pthread_mutex_lock(&markers_mutex
);
979 entry
= get_marker(channel
, name
);
982 //ust// if (entry->rcu_pending)
983 //ust// rcu_cmm_barrier_sched();
985 entry
->rcu_pending
= 1;
986 /* write rcu_pending before calling the RCU callback */
988 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
989 synchronize_rcu(); free_old_closure(&entry
->rcu
);
990 /* Ignore busy error message */
991 remove_marker(channel
, name
);
993 pthread_mutex_unlock(&markers_mutex
);
998 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1001 * marker_get_private_data - Get a marker's probe private data
1002 * @channel: marker channel
1003 * @name: marker name
1004 * @probe: probe to match
1005 * @num: get the nth matching probe's private data
1007 * Returns the nth private data pointer (starting from 0) matching, or an
1009 * Returns the private data pointer, or an ERR_PTR.
1010 * The private data pointer should _only_ be dereferenced if the caller is the
1011 * owner of the data, or its content could vanish. This is mostly used to
1012 * confirm that a caller is the owner of a registered probe.
1014 void *marker_get_private_data(const char *channel
, const char *name
,
1015 marker_probe_func
*probe
, int num
)
1017 struct cds_hlist_head
*head
;
1018 struct cds_hlist_node
*node
;
1019 struct marker_entry
*e
;
1020 size_t channel_len
= strlen(channel
) + 1;
1021 size_t name_len
= strlen(name
) + 1;
1025 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1026 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1027 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
1028 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1030 if (num
== 0 && e
->single
.func
== probe
)
1031 return e
->single
.probe_private
;
1033 struct marker_probe_closure
*closure
;
1036 for (i
= 0; closure
[i
].func
; i
++) {
1037 if (closure
[i
].func
!= probe
)
1040 return closure
[i
].probe_private
;
1046 return ERR_PTR(-ENOENT
);
1048 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1051 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1053 * Called when no channel users are active by the channel infrastructure.
1054 * Called with lock_markers() and channel mutex held.
1056 //ust// void markers_compact_event_ids(void)
1058 //ust// struct marker_entry *entry;
1059 //ust// unsigned int i;
1060 //ust// struct hlist_head *head;
1061 //ust// struct hlist_node *node;
1064 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1065 //ust// head = &marker_table[i];
1066 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1067 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1068 //ust// WARN_ON(ret < 0);
1069 //ust// entry->channel_id = ret;
1070 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1071 //ust// entry->name);
1072 //ust// WARN_ON(ret < 0);
1073 //ust// entry->event_id = ret;
1078 //ust//#ifdef CONFIG_MODULES
1081 * Returns 0 if current not found.
1082 * Returns 1 if current found.
1084 int lib_get_iter_markers(struct marker_iter
*iter
)
1086 struct lib
*iter_lib
;
1089 //ust// pthread_mutex_lock(&module_mutex);
1090 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
1091 if (iter_lib
< iter
->lib
)
1093 else if (iter_lib
> iter
->lib
)
1094 iter
->marker
= NULL
;
1095 found
= marker_get_iter_range(&iter
->marker
,
1096 iter_lib
->markers_start
,
1097 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1099 iter
->lib
= iter_lib
;
1103 //ust// pthread_mutex_unlock(&module_mutex);
1108 * marker_get_iter_range - Get a next marker iterator given a range.
1109 * @marker: current markers (in), next marker (out)
1110 * @begin: beginning of the range
1111 * @end: end of the range
1113 * Returns whether a next marker has been found (1) or not (0).
1114 * Will return the first marker in the range if the input marker is NULL.
1116 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1119 if (!*marker
&& begin
!= end
) {
1123 if (*marker
>= begin
&& *marker
< end
)
1127 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1129 static void marker_get_iter(struct marker_iter
*iter
)
1133 /* Core kernel markers */
1135 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1136 found
= marker_get_iter_range(&iter
->marker
,
1137 __start___markers
, __stop___markers
);
1141 /* Markers in modules. */
1142 found
= lib_get_iter_markers(iter
);
1145 marker_iter_reset(iter
);
1148 void marker_iter_start(struct marker_iter
*iter
)
1150 marker_get_iter(iter
);
1152 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1154 void marker_iter_next(struct marker_iter
*iter
)
1158 * iter->marker may be invalid because we blindly incremented it.
1159 * Make sure it is valid by marshalling on the markers, getting the
1160 * markers from following modules if necessary.
1162 marker_get_iter(iter
);
1164 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1166 void marker_iter_stop(struct marker_iter
*iter
)
1169 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1171 void marker_iter_reset(struct marker_iter
*iter
)
1174 iter
->marker
= NULL
;
1176 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1178 #ifdef CONFIG_MARKERS_USERSPACE
1180 * must be called with current->user_markers_mutex held
1182 static void free_user_marker(char __user
*state
, struct cds_hlist_head
*head
)
1184 struct user_marker
*umark
;
1185 struct cds_hlist_node
*pos
, *n
;
1187 cds_hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1188 if (umark
->state
== state
) {
1189 cds_hlist_del(&umark
->hlist
);
1196 * Update current process.
1197 * Note that we have to wait a whole scheduler period before we are sure that
1198 * every running userspace threads have their markers updated.
1199 * (synchronize_sched() can be used to insure this).
1201 //ust// void marker_update_process(void)
1203 //ust// struct user_marker *umark;
1204 //ust// struct hlist_node *pos;
1205 //ust// struct marker_entry *entry;
1207 //ust// pthread_mutex_lock(&markers_mutex);
1208 //ust// pthread_mutex_lock(¤t->group_leader->user_markers_mutex);
1209 //ust// if (strcmp(current->comm, "testprog") == 0)
1210 //ust// DBG("do update pending for testprog");
1211 //ust// hlist_for_each_entry(umark, pos,
1212 //ust// ¤t->group_leader->user_markers, hlist) {
1213 //ust// DBG("Updating marker %s in %s", umark->name, current->comm);
1214 //ust// entry = get_marker("userspace", umark->name);
1215 //ust// if (entry) {
1216 //ust// if (entry->format &&
1217 //ust// strcmp(entry->format, umark->format) != 0) {
1218 //ust// WARN("error, wrong format in process %s",
1219 //ust// current->comm);
1222 //ust// if (put_user(!!entry->refcount, umark->state)) {
1223 //ust// WARN("Marker in %s caused a fault",
1224 //ust// current->comm);
1228 //ust// if (put_user(0, umark->state)) {
1229 //ust// WARN("Marker in %s caused a fault", current->comm);
1234 //ust// clear_thread_flag(TIF_MARKER_PENDING);
1235 //ust// pthread_mutex_unlock(¤t->group_leader->user_markers_mutex);
1236 //ust// pthread_mutex_unlock(&markers_mutex);
1240 * Called at process exit and upon do_execve().
1241 * We assume that when the leader exits, no more references can be done to the
1242 * leader structure by the other threads.
1244 void exit_user_markers(struct task_struct
*p
)
1246 struct user_marker
*umark
;
1247 struct cds_hlist_node
*pos
, *n
;
1249 if (thread_group_leader(p
)) {
1250 pthread_mutex_lock(&markers_mutex
);
1251 pthread_mutex_lock(&p
->user_markers_mutex
);
1252 cds_hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1255 INIT_HLIST_HEAD(&p
->user_markers
);
1256 p
->user_markers_sequence
++;
1257 pthread_mutex_unlock(&p
->user_markers_mutex
);
1258 pthread_mutex_unlock(&markers_mutex
);
1262 int is_marker_enabled(const char *channel
, const char *name
)
1264 struct marker_entry
*entry
;
1266 pthread_mutex_lock(&markers_mutex
);
1267 entry
= get_marker(channel
, name
);
1268 pthread_mutex_unlock(&markers_mutex
);
1270 return entry
&& !!entry
->refcount
;
1274 int marker_module_notify(struct notifier_block
*self
,
1275 unsigned long val
, void *data
)
1277 struct module
*mod
= data
;
1280 case MODULE_STATE_COMING
:
1281 marker_update_probe_range(mod
->markers
,
1282 mod
->markers
+ mod
->num_markers
);
1284 case MODULE_STATE_GOING
:
1285 marker_update_probe_range(mod
->markers
,
1286 mod
->markers
+ mod
->num_markers
);
1292 struct notifier_block marker_module_nb
= {
1293 .notifier_call
= marker_module_notify
,
1297 //ust// static int init_markers(void)
1299 //ust// return register_module_notifier(&marker_module_nb);
1301 //ust// __initcall(init_markers);
1302 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1304 #endif /* CONFIG_MODULES */
1306 void ltt_dump_marker_state(struct ust_trace
*trace
)
1308 struct marker_entry
*entry
;
1309 struct ltt_probe_private_data call_data
;
1310 struct cds_hlist_head
*head
;
1311 struct cds_hlist_node
*node
;
1314 pthread_mutex_lock(&markers_mutex
);
1315 call_data
.trace
= trace
;
1316 call_data
.serializer
= NULL
;
1318 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1319 head
= &marker_table
[i
];
1320 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
1321 __trace_mark(0, metadata
, core_marker_id
,
1323 "channel %s name %s event_id %hu "
1324 "int #1u%zu long #1u%zu pointer #1u%zu "
1325 "size_t #1u%zu alignment #1u%u",
1329 sizeof(int), sizeof(long),
1330 sizeof(void *), sizeof(size_t),
1331 ltt_get_alignment());
1333 __trace_mark(0, metadata
,
1336 "channel %s name %s format %s",
1342 pthread_mutex_unlock(&markers_mutex
);
1344 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1346 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1348 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1353 static void new_markers(struct marker
*start
, struct marker
*end
)
1357 for(m
=start
; m
< end
; m
++) {
1363 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1367 pl
= (struct lib
*) zmalloc(sizeof(struct lib
));
1369 pl
->markers_start
= markers_start
;
1370 pl
->markers_count
= markers_count
;
1372 /* FIXME: maybe protect this with its own mutex? */
1374 cds_list_add(&pl
->list
, &libs
);
1377 new_markers(markers_start
, markers_start
+ markers_count
);
1379 /* FIXME: update just the loaded lib */
1380 lib_update_markers();
1382 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1387 int marker_unregister_lib(struct marker
*markers_start
)
1391 /*FIXME: implement; but before implementing, marker_register_lib must
1392 have appropriate locking. */
1396 /* FIXME: we should probably take a mutex here on libs */
1397 //ust// pthread_mutex_lock(&module_mutex);
1398 cds_list_for_each_entry(lib
, &libs
, list
) {
1399 if(lib
->markers_start
== markers_start
) {
1400 struct lib
*lib2free
= lib
;
1401 cds_list_del(&lib
->list
);
1412 static int initialized
= 0;
1414 void __attribute__((constructor
)) init_markers(void)
1417 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1422 void __attribute__((constructor
)) destroy_markers(void)
1424 marker_unregister_lib(__start___markers
);