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>
28 #include <ust/tracepoint.h>
32 #include "tracercore.h"
35 __thread
long ust_reg_stack
[500];
36 volatile __thread
long *ust_reg_stack_ptr
= (long *) 0;
38 extern struct marker
* const __start___markers_ptrs
[] __attribute__((visibility("hidden")));
39 extern struct marker
* const __stop___markers_ptrs
[] __attribute__((visibility("hidden")));
41 /* Set to 1 to enable marker debug output */
42 static const int marker_debug
;
45 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
46 * and module markers and the hash table.
48 static DEFINE_MUTEX(markers_mutex
);
50 static CDS_LIST_HEAD(libs
);
53 void lock_markers(void)
55 pthread_mutex_lock(&markers_mutex
);
58 void unlock_markers(void)
60 pthread_mutex_unlock(&markers_mutex
);
64 * Marker hash table, containing the active markers.
65 * Protected by module_mutex.
67 #define MARKER_HASH_BITS 6
68 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
69 static struct cds_hlist_head marker_table
[MARKER_TABLE_SIZE
];
73 * It is used to make sure every handler has finished using its private data
74 * between two consecutive operation (add or remove) on a given marker. It is
75 * also used to delay the free of multiple probes array until a quiescent state
77 * marker entries modifications are protected by the markers_mutex.
80 struct cds_hlist_node hlist
;
84 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
85 struct marker_probe_closure single
;
86 struct marker_probe_closure
*multi
;
87 int refcount
; /* Number of times armed. 0 if disarmed. */
93 unsigned char ptype
:1;
94 unsigned char format_allocated
:1;
95 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
98 #ifdef CONFIG_MARKERS_USERSPACE
99 static void marker_update_processes(void);
101 static void marker_update_processes(void)
107 * __mark_empty_function - Empty probe callback
108 * @mdata: marker data
109 * @probe_private: probe private data
110 * @call_private: call site private data
111 * @fmt: format string
112 * @...: variable argument list
114 * Empty callback provided as a probe to the markers. By providing this to a
115 * disabled marker, we make sure the execution flow is always valid even
116 * though the function pointer change and the marker enabling are two distinct
117 * operations that modifies the execution flow of preemptible code.
119 notrace
void __mark_empty_function(const struct marker
*mdata
,
120 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
123 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
126 * marker_probe_cb Callback that prepares the variable argument list for probes.
127 * @mdata: pointer of type struct marker
128 * @call_private: caller site private data
129 * @...: Variable argument list.
131 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
132 * need to put a full cmm_smp_rmb() in this branch. This is why we do not use
133 * rcu_dereference() for the pointer read.
135 notrace
void marker_probe_cb(const struct marker
*mdata
,
136 void *call_private
, struct registers
*regs
, ...)
142 * rcu_read_lock_sched does two things : disabling preemption to make
143 * sure the teardown of the callbacks can be done correctly when they
144 * are in modules and they insure RCU read coherency.
146 //ust// rcu_read_lock_sched_notrace();
147 ptype
= mdata
->ptype
;
148 if (likely(!ptype
)) {
149 marker_probe_func
*func
;
150 /* Must read the ptype before ptr. They are not data dependant,
151 * so we put an explicit cmm_smp_rmb() here. */
153 func
= mdata
->single
.func
;
154 /* Must read the ptr before private data. They are not data
155 * dependant, so we put an explicit cmm_smp_rmb() here. */
157 va_start(args
, regs
);
158 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
159 mdata
->format
, &args
);
162 struct marker_probe_closure
*multi
;
165 * Read mdata->ptype before mdata->multi.
168 multi
= mdata
->multi
;
170 * multi points to an array, therefore accessing the array
171 * depends on reading multi. However, even in this case,
172 * we must insure that the pointer is read _before_ the array
173 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
174 * in the fast path, so put the explicit cmm_barrier here.
176 cmm_smp_read_barrier_depends();
177 for (i
= 0; multi
[i
].func
; i
++) {
178 va_start(args
, regs
);
179 multi
[i
].func(mdata
, multi
[i
].probe_private
,
180 regs
, call_private
, mdata
->format
, &args
);
184 //ust// rcu_read_unlock_sched_notrace();
186 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
189 * marker_probe_cb Callback that does not prepare the variable argument list.
190 * @mdata: pointer of type struct marker
191 * @call_private: caller site private data
192 * @...: Variable argument list.
194 * Should be connected to markers "MARK_NOARGS".
196 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
197 void *call_private
, struct registers
*regs
, ...)
199 va_list args
; /* not initialized */
202 //ust// rcu_read_lock_sched_notrace();
203 ptype
= mdata
->ptype
;
204 if (likely(!ptype
)) {
205 marker_probe_func
*func
;
206 /* Must read the ptype before ptr. They are not data dependant,
207 * so we put an explicit cmm_smp_rmb() here. */
209 func
= mdata
->single
.func
;
210 /* Must read the ptr before private data. They are not data
211 * dependant, so we put an explicit cmm_smp_rmb() here. */
213 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
214 mdata
->format
, &args
);
216 struct marker_probe_closure
*multi
;
219 * Read mdata->ptype before mdata->multi.
222 multi
= mdata
->multi
;
224 * multi points to an array, therefore accessing the array
225 * depends on reading multi. However, even in this case,
226 * we must insure that the pointer is read _before_ the array
227 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
228 * in the fast path, so put the explicit cmm_barrier here.
230 cmm_smp_read_barrier_depends();
231 for (i
= 0; multi
[i
].func
; i
++)
232 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
233 call_private
, mdata
->format
, &args
);
235 //ust// rcu_read_unlock_sched_notrace();
238 static void free_old_closure(struct rcu_head
*head
)
240 struct marker_entry
*entry
= _ust_container_of(head
,
241 struct marker_entry
, rcu
);
243 /* Make sure we free the data before setting the pending flag to 0 */
245 entry
->rcu_pending
= 0;
248 static void debug_print_probes(struct marker_entry
*entry
)
256 DBG("Single probe : %p %p",
258 entry
->single
.probe_private
);
260 for (i
= 0; entry
->multi
[i
].func
; i
++)
261 DBG("Multi probe %d : %p %p", i
,
262 entry
->multi
[i
].func
,
263 entry
->multi
[i
].probe_private
);
267 static struct marker_probe_closure
*
268 marker_entry_add_probe(struct marker_entry
*entry
,
269 marker_probe_func
*probe
, void *probe_private
)
272 struct marker_probe_closure
*old
, *new;
276 debug_print_probes(entry
);
279 if (entry
->single
.func
== probe
&&
280 entry
->single
.probe_private
== probe_private
)
281 return ERR_PTR(-EBUSY
);
282 if (entry
->single
.func
== __mark_empty_function
) {
284 entry
->single
.func
= probe
;
285 entry
->single
.probe_private
= probe_private
;
288 debug_print_probes(entry
);
296 /* (N -> N+1), (N != 0, 1) probes */
297 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
298 if (old
[nr_probes
].func
== probe
299 && old
[nr_probes
].probe_private
301 return ERR_PTR(-EBUSY
);
303 /* + 2 : one for new probe, one for NULL func */
304 new = zmalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
));
306 return ERR_PTR(-ENOMEM
);
308 new[0] = entry
->single
;
311 nr_probes
* sizeof(struct marker_probe_closure
));
312 new[nr_probes
].func
= probe
;
313 new[nr_probes
].probe_private
= probe_private
;
314 entry
->refcount
= nr_probes
+ 1;
317 debug_print_probes(entry
);
321 static struct marker_probe_closure
*
322 marker_entry_remove_probe(struct marker_entry
*entry
,
323 marker_probe_func
*probe
, void *probe_private
)
325 int nr_probes
= 0, nr_del
= 0, i
;
326 struct marker_probe_closure
*old
, *new;
330 debug_print_probes(entry
);
332 /* 0 -> N is an error */
333 WARN_ON(entry
->single
.func
== __mark_empty_function
);
335 WARN_ON(probe
&& entry
->single
.func
!= probe
);
336 WARN_ON(entry
->single
.probe_private
!= probe_private
);
337 entry
->single
.func
= __mark_empty_function
;
340 debug_print_probes(entry
);
343 /* (N -> M), (N > 1, M >= 0) probes */
344 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
345 if ((!probe
|| old
[nr_probes
].func
== probe
)
346 && old
[nr_probes
].probe_private
352 if (nr_probes
- nr_del
== 0) {
353 /* N -> 0, (N > 1) */
354 entry
->single
.func
= __mark_empty_function
;
357 } else if (nr_probes
- nr_del
== 1) {
358 /* N -> 1, (N > 1) */
359 for (i
= 0; old
[i
].func
; i
++)
360 if ((probe
&& old
[i
].func
!= probe
) ||
361 old
[i
].probe_private
!= probe_private
)
362 entry
->single
= old
[i
];
367 /* N -> M, (N > 1, M > 1) */
369 new = zmalloc((nr_probes
- nr_del
+ 1) * sizeof(struct marker_probe_closure
));
371 return ERR_PTR(-ENOMEM
);
372 for (i
= 0; old
[i
].func
; i
++)
373 if ((probe
&& old
[i
].func
!= probe
) ||
374 old
[i
].probe_private
!= probe_private
)
376 entry
->refcount
= nr_probes
- nr_del
;
380 debug_print_probes(entry
);
385 * Get marker if the marker is present in the marker hash table.
386 * Must be called with markers_mutex held.
387 * Returns NULL if not present.
389 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
391 struct cds_hlist_head
*head
;
392 struct cds_hlist_node
*node
;
393 struct marker_entry
*e
;
394 size_t channel_len
= strlen(channel
) + 1;
395 size_t name_len
= strlen(name
) + 1;
398 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
399 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
400 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
401 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
408 * Add the marker to the marker hash table. Must be called with markers_mutex
411 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
414 struct cds_hlist_head
*head
;
415 struct cds_hlist_node
*node
;
416 struct marker_entry
*e
;
417 size_t channel_len
= strlen(channel
) + 1;
418 size_t name_len
= strlen(name
) + 1;
419 size_t format_len
= 0;
422 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
424 format_len
= strlen(format
) + 1;
425 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
426 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
427 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
428 DBG("Marker %s.%s busy", channel
, name
);
429 return ERR_PTR(-EBUSY
); /* Already there */
433 * Using zmalloc here to allocate a variable length element. Could
434 * cause some memory fragmentation if overused.
436 e
= zmalloc(sizeof(struct marker_entry
)
437 + channel_len
+ name_len
+ format_len
);
439 return ERR_PTR(-ENOMEM
);
440 memcpy(e
->channel
, channel
, channel_len
);
441 e
->name
= &e
->channel
[channel_len
];
442 memcpy(e
->name
, name
, name_len
);
444 e
->format
= &e
->name
[name_len
];
445 memcpy(e
->format
, format
, format_len
);
446 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
447 e
->call
= marker_probe_cb_noarg
;
449 e
->call
= marker_probe_cb
;
450 trace_mark(metadata
, core_marker_format
,
451 "channel %s name %s format %s",
452 e
->channel
, e
->name
, e
->format
);
455 e
->call
= marker_probe_cb
;
457 e
->single
.func
= __mark_empty_function
;
458 e
->single
.probe_private
= NULL
;
461 e
->format_allocated
= 0;
464 cds_hlist_add_head(&e
->hlist
, head
);
469 * Remove the marker from the marker hash table. Must be called with mutex_lock
472 static int remove_marker(const char *channel
, const char *name
)
474 struct cds_hlist_head
*head
;
475 struct cds_hlist_node
*node
;
476 struct marker_entry
*e
;
478 size_t channel_len
= strlen(channel
) + 1;
479 size_t name_len
= strlen(name
) + 1;
483 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
484 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
485 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
486 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
493 if (e
->single
.func
!= __mark_empty_function
)
495 cds_hlist_del(&e
->hlist
);
496 if (e
->format_allocated
)
498 ret
= ltt_channels_unregister(e
->channel
);
500 /* Make sure the call_rcu has been executed */
501 //ust// if (e->rcu_pending)
502 //ust// rcu_cmm_barrier_sched();
508 * Set the mark_entry format to the format found in the element.
510 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
512 entry
->format
= strdup(format
);
515 entry
->format_allocated
= 1;
517 trace_mark(metadata
, core_marker_format
,
518 "channel %s name %s format %s",
519 entry
->channel
, entry
->name
, entry
->format
);
524 * Sets the probe callback corresponding to one marker.
526 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
530 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
533 if (strcmp(entry
->format
, elem
->format
) != 0) {
534 ERR("Format mismatch for probe %s (%s), marker (%s)",
541 ret
= marker_set_format(entry
, elem
->format
);
547 * probe_cb setup (statically known) is done here. It is
548 * asynchronous with the rest of execution, therefore we only
549 * pass from a "safe" callback (with argument) to an "unsafe"
550 * callback (does not set arguments).
552 elem
->call
= entry
->call
;
553 elem
->channel_id
= entry
->channel_id
;
554 elem
->event_id
= entry
->event_id
;
557 * We only update the single probe private data when the ptr is
558 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
560 WARN_ON(elem
->single
.func
!= __mark_empty_function
561 && elem
->single
.probe_private
!= entry
->single
.probe_private
563 elem
->single
.probe_private
= entry
->single
.probe_private
;
565 * Make sure the private data is valid when we update the
569 elem
->single
.func
= entry
->single
.func
;
571 * We also make sure that the new probe callbacks array is consistent
572 * before setting a pointer to it.
574 rcu_assign_pointer(elem
->multi
, entry
->multi
);
576 * Update the function or multi probe array pointer before setting the
580 elem
->ptype
= entry
->ptype
;
582 if (elem
->tp_name
&& (active
^ _imv_read(elem
->state
))) {
583 WARN_ON(!elem
->tp_cb
);
585 * It is ok to directly call the probe registration because type
586 * checking has been done in the __trace_mark_tp() macro.
591 * try_module_get should always succeed because we hold
592 * markers_mutex to get the tp_cb address.
594 //ust// ret = try_module_get(__module_text_address(
595 //ust// (unsigned long)elem->tp_cb));
596 //ust// BUG_ON(!ret);
597 ret
= tracepoint_probe_register_noupdate(
601 ret
= tracepoint_probe_unregister_noupdate(
605 * tracepoint_probe_update_all() must be called
606 * before the module containing tp_cb is unloaded.
608 //ust// module_put(__module_text_address(
609 //ust// (unsigned long)elem->tp_cb));
612 elem
->state__imv
= active
;
618 * Disable a marker and its probe callback.
619 * Note: only waiting an RCU period after setting elem->call to the empty
620 * function insures that the original callback is not used anymore. This insured
621 * by rcu_read_lock_sched around the call site.
623 static void disable_marker(struct marker
*elem
)
627 /* leave "call" as is. It is known statically. */
628 if (elem
->tp_name
&& _imv_read(elem
->state
)) {
629 WARN_ON(!elem
->tp_cb
);
631 * It is ok to directly call the probe registration because type
632 * checking has been done in the __trace_mark_tp() macro.
634 ret
= tracepoint_probe_unregister_noupdate(elem
->tp_name
,
638 * tracepoint_probe_update_all() must be called
639 * before the module containing tp_cb is unloaded.
641 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
643 elem
->state__imv
= 0;
644 elem
->single
.func
= __mark_empty_function
;
645 /* Update the function before setting the ptype */
647 elem
->ptype
= 0; /* single probe */
649 * Leave the private data and channel_id/event_id there, because removal
650 * is racy and should be done only after an RCU period. These are never
651 * used until the next initialization anyway.
656 * is_marker_enabled - Check if a marker is enabled
657 * @channel: channel name
660 * Returns 1 if the marker is enabled, 0 if disabled.
662 int is_marker_enabled(const char *channel
, const char *name
)
664 struct marker_entry
*entry
;
666 pthread_mutex_lock(&markers_mutex
);
667 entry
= get_marker(channel
, name
);
668 pthread_mutex_unlock(&markers_mutex
);
670 return entry
&& !!entry
->refcount
;
674 * marker_update_probe_range - Update a probe range
675 * @begin: beginning of the range
676 * @end: end of the range
678 * Updates the probe callback corresponding to a range of markers.
680 void marker_update_probe_range(struct marker
* const *begin
,
681 struct marker
* const *end
)
683 struct marker
* const *iter
;
684 struct marker_entry
*mark_entry
;
686 pthread_mutex_lock(&markers_mutex
);
687 for (iter
= begin
; iter
< end
; iter
++) {
689 continue; /* skip dummy */
690 mark_entry
= get_marker((*iter
)->channel
, (*iter
)->name
);
692 set_marker(mark_entry
, *iter
, !!mark_entry
->refcount
);
694 * ignore error, continue
697 /* This is added for UST. We emit a core_marker_id event
698 * for markers that are already registered to a probe
699 * upon library load. Otherwise, no core_marker_id will
700 * be generated for these markers. Is this the right thing
703 trace_mark(metadata
, core_marker_id
,
704 "channel %s name %s event_id %hu "
705 "int #1u%zu long #1u%zu pointer #1u%zu "
706 "size_t #1u%zu alignment #1u%u",
707 (*iter
)->channel
, (*iter
)->name
, mark_entry
->event_id
,
708 sizeof(int), sizeof(long), sizeof(void *),
709 sizeof(size_t), ltt_get_alignment());
711 disable_marker(*iter
);
714 pthread_mutex_unlock(&markers_mutex
);
717 static void lib_update_markers(void)
721 /* FIXME: we should probably take a mutex here on libs */
722 //ust// pthread_mutex_lock(&module_mutex);
723 cds_list_for_each_entry(lib
, &libs
, list
)
724 marker_update_probe_range(lib
->markers_start
,
725 lib
->markers_start
+ lib
->markers_count
);
726 //ust// pthread_mutex_unlock(&module_mutex);
730 * Update probes, removing the faulty probes.
732 * Internal callback only changed before the first probe is connected to it.
733 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
734 * transitions. All other transitions will leave the old private data valid.
735 * This makes the non-atomicity of the callback/private data updates valid.
737 * "special case" updates :
742 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
743 * Site effect : marker_set_format may delete the marker entry (creating a
746 static void marker_update_probes(void)
748 lib_update_markers();
749 tracepoint_probe_update_all();
750 /* Update immediate values */
752 //ust// module_imv_update(); /* FIXME: need to port for libs? */
753 marker_update_processes();
757 * marker_probe_register - Connect a probe to a marker
758 * @channel: marker channel
760 * @format: format string
761 * @probe: probe handler
762 * @probe_private: probe private data
764 * private data must be a valid allocated memory address, or NULL.
765 * Returns 0 if ok, error value on error.
766 * The probe address must at least be aligned on the architecture pointer size.
768 int marker_probe_register(const char *channel
, const char *name
,
769 const char *format
, marker_probe_func
*probe
,
772 struct marker_entry
*entry
;
773 int ret
= 0, ret_err
;
774 struct marker_probe_closure
*old
;
777 pthread_mutex_lock(&markers_mutex
);
778 entry
= get_marker(channel
, name
);
781 entry
= add_marker(channel
, name
, format
);
783 ret
= PTR_ERR(entry
);
786 ret
= ltt_channels_register(channel
);
788 goto error_remove_marker
;
789 ret
= ltt_channels_get_index_from_name(channel
);
791 goto error_unregister_channel
;
792 entry
->channel_id
= ret
;
793 ret
= ltt_channels_get_event_id(channel
, name
);
795 goto error_unregister_channel
;
796 entry
->event_id
= ret
;
798 trace_mark(metadata
, core_marker_id
,
799 "channel %s name %s event_id %hu "
800 "int #1u%zu long #1u%zu pointer #1u%zu "
801 "size_t #1u%zu alignment #1u%u",
802 channel
, name
, entry
->event_id
,
803 sizeof(int), sizeof(long), sizeof(void *),
804 sizeof(size_t), ltt_get_alignment());
807 ret
= marker_set_format(entry
, format
);
808 else if (strcmp(entry
->format
, format
))
815 * If we detect that a call_rcu is pending for this marker,
816 * make sure it's executed now.
818 //ust// if (entry->rcu_pending)
819 //ust// rcu_cmm_barrier_sched();
820 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
824 goto error_unregister_channel
;
828 pthread_mutex_unlock(&markers_mutex
);
830 /* Activate marker if necessary */
831 marker_update_probes();
833 pthread_mutex_lock(&markers_mutex
);
834 entry
= get_marker(channel
, name
);
837 //ust// if (entry->rcu_pending)
838 //ust// rcu_cmm_barrier_sched();
840 entry
->rcu_pending
= 1;
841 /* write rcu_pending before calling the RCU callback */
843 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
844 synchronize_rcu(); free_old_closure(&entry
->rcu
);
847 error_unregister_channel
:
848 ret_err
= ltt_channels_unregister(channel
);
851 ret_err
= remove_marker(channel
, name
);
854 pthread_mutex_unlock(&markers_mutex
);
857 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
860 * marker_probe_unregister - Disconnect a probe from a marker
861 * @channel: marker channel
863 * @probe: probe function pointer
864 * @probe_private: probe private data
866 * Returns the private data given to marker_probe_register, or an ERR_PTR().
867 * We do not need to call a synchronize_sched to make sure the probes have
868 * finished running before doing a module unload, because the module unload
869 * itself uses stop_machine(), which insures that every preempt disabled section
872 int marker_probe_unregister(const char *channel
, const char *name
,
873 marker_probe_func
*probe
, void *probe_private
)
875 struct marker_entry
*entry
;
876 struct marker_probe_closure
*old
;
879 pthread_mutex_lock(&markers_mutex
);
880 entry
= get_marker(channel
, name
);
883 //ust// if (entry->rcu_pending)
884 //ust// rcu_cmm_barrier_sched();
885 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
886 pthread_mutex_unlock(&markers_mutex
);
888 marker_update_probes();
890 pthread_mutex_lock(&markers_mutex
);
891 entry
= get_marker(channel
, name
);
894 //ust// if (entry->rcu_pending)
895 //ust// rcu_cmm_barrier_sched();
897 entry
->rcu_pending
= 1;
898 /* write rcu_pending before calling the RCU callback */
900 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
901 synchronize_rcu(); free_old_closure(&entry
->rcu
);
902 remove_marker(channel
, name
); /* Ignore busy error message */
905 pthread_mutex_unlock(&markers_mutex
);
908 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
910 static struct marker_entry
*
911 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
913 struct marker_entry
*entry
;
915 struct cds_hlist_head
*head
;
916 struct cds_hlist_node
*node
;
918 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
919 head
= &marker_table
[i
];
920 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
922 if (entry
->single
.func
== probe
923 && entry
->single
.probe_private
927 struct marker_probe_closure
*closure
;
928 closure
= entry
->multi
;
929 for (i
= 0; closure
[i
].func
; i
++) {
930 if (closure
[i
].func
== probe
&&
931 closure
[i
].probe_private
942 * marker_probe_unregister_private_data - Disconnect a probe from a marker
943 * @probe: probe function
944 * @probe_private: probe private data
946 * Unregister a probe by providing the registered private data.
947 * Only removes the first marker found in hash table.
948 * Return 0 on success or error value.
949 * We do not need to call a synchronize_sched to make sure the probes have
950 * finished running before doing a module unload, because the module unload
951 * itself uses stop_machine(), which insures that every preempt disabled section
954 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
957 struct marker_entry
*entry
;
959 struct marker_probe_closure
*old
;
960 char *channel
= NULL
, *name
= NULL
;
962 pthread_mutex_lock(&markers_mutex
);
963 entry
= get_marker_from_private_data(probe
, probe_private
);
968 //ust// if (entry->rcu_pending)
969 //ust// rcu_cmm_barrier_sched();
970 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
971 channel
= strdup(entry
->channel
);
972 name
= strdup(entry
->name
);
973 pthread_mutex_unlock(&markers_mutex
);
975 marker_update_probes();
977 pthread_mutex_lock(&markers_mutex
);
978 entry
= get_marker(channel
, name
);
981 //ust// if (entry->rcu_pending)
982 //ust// rcu_cmm_barrier_sched();
984 entry
->rcu_pending
= 1;
985 /* write rcu_pending before calling the RCU callback */
987 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
988 synchronize_rcu(); free_old_closure(&entry
->rcu
);
989 /* Ignore busy error message */
990 remove_marker(channel
, name
);
992 pthread_mutex_unlock(&markers_mutex
);
997 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1000 * marker_get_private_data - Get a marker's probe private data
1001 * @channel: marker channel
1002 * @name: marker name
1003 * @probe: probe to match
1004 * @num: get the nth matching probe's private data
1006 * Returns the nth private data pointer (starting from 0) matching, or an
1008 * Returns the private data pointer, or an ERR_PTR.
1009 * The private data pointer should _only_ be dereferenced if the caller is the
1010 * owner of the data, or its content could vanish. This is mostly used to
1011 * confirm that a caller is the owner of a registered probe.
1013 void *marker_get_private_data(const char *channel
, const char *name
,
1014 marker_probe_func
*probe
, int num
)
1016 struct cds_hlist_head
*head
;
1017 struct cds_hlist_node
*node
;
1018 struct marker_entry
*e
;
1019 size_t channel_len
= strlen(channel
) + 1;
1020 size_t name_len
= strlen(name
) + 1;
1024 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1025 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1026 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
1027 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1029 if (num
== 0 && e
->single
.func
== probe
)
1030 return e
->single
.probe_private
;
1032 struct marker_probe_closure
*closure
;
1035 for (i
= 0; closure
[i
].func
; i
++) {
1036 if (closure
[i
].func
!= probe
)
1039 return closure
[i
].probe_private
;
1045 return ERR_PTR(-ENOENT
);
1047 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1050 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1052 * Called when no channel users are active by the channel infrastructure.
1053 * Called with lock_markers() and channel mutex held.
1055 //ust// void markers_compact_event_ids(void)
1057 //ust// struct marker_entry *entry;
1058 //ust// unsigned int i;
1059 //ust// struct hlist_head *head;
1060 //ust// struct hlist_node *node;
1063 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1064 //ust// head = &marker_table[i];
1065 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1066 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1067 //ust// WARN_ON(ret < 0);
1068 //ust// entry->channel_id = ret;
1069 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1070 //ust// entry->name);
1071 //ust// WARN_ON(ret < 0);
1072 //ust// entry->event_id = ret;
1077 //ust//#ifdef CONFIG_MODULES
1080 * Returns 0 if current not found.
1081 * Returns 1 if current found.
1083 int lib_get_iter_markers(struct marker_iter
*iter
)
1085 struct lib
*iter_lib
;
1088 //ust// pthread_mutex_lock(&module_mutex);
1089 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
1090 if (iter_lib
< iter
->lib
)
1092 else if (iter_lib
> iter
->lib
)
1093 iter
->marker
= NULL
;
1094 found
= marker_get_iter_range(&iter
->marker
,
1095 iter_lib
->markers_start
,
1096 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1098 iter
->lib
= iter_lib
;
1102 //ust// pthread_mutex_unlock(&module_mutex);
1107 * marker_get_iter_range - Get a next marker iterator given a range.
1108 * @marker: current markers (in), next marker (out)
1109 * @begin: beginning of the range
1110 * @end: end of the range
1112 * Returns whether a next marker has been found (1) or not (0).
1113 * Will return the first marker in the range if the input marker is NULL.
1115 int marker_get_iter_range(struct marker
* const **marker
,
1116 struct marker
* const *begin
,
1117 struct marker
* const *end
)
1119 if (!*marker
&& begin
!= end
)
1121 while (*marker
>= begin
&& *marker
< end
) {
1123 (*marker
)++; /* skip dummy */
1129 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1131 static void marker_get_iter(struct marker_iter
*iter
)
1135 found
= lib_get_iter_markers(iter
);
1137 marker_iter_reset(iter
);
1140 void marker_iter_start(struct marker_iter
*iter
)
1142 marker_get_iter(iter
);
1144 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1146 void marker_iter_next(struct marker_iter
*iter
)
1150 * iter->marker may be invalid because we blindly incremented it.
1151 * Make sure it is valid by marshalling on the markers, getting the
1152 * markers from following modules if necessary.
1154 marker_get_iter(iter
);
1156 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1158 void marker_iter_stop(struct marker_iter
*iter
)
1161 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1163 void marker_iter_reset(struct marker_iter
*iter
)
1166 iter
->marker
= NULL
;
1168 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1170 #ifdef CONFIG_MARKERS_USERSPACE
1172 * must be called with current->user_markers_mutex held
1174 static void free_user_marker(char __user
*state
, struct cds_hlist_head
*head
)
1176 struct user_marker
*umark
;
1177 struct cds_hlist_node
*pos
, *n
;
1179 cds_hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1180 if (umark
->state
== state
) {
1181 cds_hlist_del(&umark
->hlist
);
1188 * Update current process.
1189 * Note that we have to wait a whole scheduler period before we are sure that
1190 * every running userspace threads have their markers updated.
1191 * (synchronize_sched() can be used to insure this).
1193 //ust// void marker_update_process(void)
1195 //ust// struct user_marker *umark;
1196 //ust// struct hlist_node *pos;
1197 //ust// struct marker_entry *entry;
1199 //ust// pthread_mutex_lock(&markers_mutex);
1200 //ust// pthread_mutex_lock(¤t->group_leader->user_markers_mutex);
1201 //ust// if (strcmp(current->comm, "testprog") == 0)
1202 //ust// DBG("do update pending for testprog");
1203 //ust// hlist_for_each_entry(umark, pos,
1204 //ust// ¤t->group_leader->user_markers, hlist) {
1205 //ust// DBG("Updating marker %s in %s", umark->name, current->comm);
1206 //ust// entry = get_marker("userspace", umark->name);
1207 //ust// if (entry) {
1208 //ust// if (entry->format &&
1209 //ust// strcmp(entry->format, umark->format) != 0) {
1210 //ust// WARN("error, wrong format in process %s",
1211 //ust// current->comm);
1214 //ust// if (put_user(!!entry->refcount, umark->state)) {
1215 //ust// WARN("Marker in %s caused a fault",
1216 //ust// current->comm);
1220 //ust// if (put_user(0, umark->state)) {
1221 //ust// WARN("Marker in %s caused a fault", current->comm);
1226 //ust// clear_thread_flag(TIF_MARKER_PENDING);
1227 //ust// pthread_mutex_unlock(¤t->group_leader->user_markers_mutex);
1228 //ust// pthread_mutex_unlock(&markers_mutex);
1232 * Called at process exit and upon do_execve().
1233 * We assume that when the leader exits, no more references can be done to the
1234 * leader structure by the other threads.
1236 void exit_user_markers(struct task_struct
*p
)
1238 struct user_marker
*umark
;
1239 struct cds_hlist_node
*pos
, *n
;
1241 if (thread_group_leader(p
)) {
1242 pthread_mutex_lock(&markers_mutex
);
1243 pthread_mutex_lock(&p
->user_markers_mutex
);
1244 cds_hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1247 INIT_HLIST_HEAD(&p
->user_markers
);
1248 p
->user_markers_sequence
++;
1249 pthread_mutex_unlock(&p
->user_markers_mutex
);
1250 pthread_mutex_unlock(&markers_mutex
);
1254 int is_marker_enabled(const char *channel
, const char *name
)
1256 struct marker_entry
*entry
;
1258 pthread_mutex_lock(&markers_mutex
);
1259 entry
= get_marker(channel
, name
);
1260 pthread_mutex_unlock(&markers_mutex
);
1262 return entry
&& !!entry
->refcount
;
1266 int marker_module_notify(struct notifier_block
*self
,
1267 unsigned long val
, void *data
)
1269 struct module
*mod
= data
;
1272 case MODULE_STATE_COMING
:
1273 marker_update_probe_range(mod
->markers
,
1274 mod
->markers
+ mod
->num_markers
);
1276 case MODULE_STATE_GOING
:
1277 marker_update_probe_range(mod
->markers
,
1278 mod
->markers
+ mod
->num_markers
);
1284 struct notifier_block marker_module_nb
= {
1285 .notifier_call
= marker_module_notify
,
1289 //ust// static int init_markers(void)
1291 //ust// return register_module_notifier(&marker_module_nb);
1293 //ust// __initcall(init_markers);
1294 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1296 #endif /* CONFIG_MODULES */
1298 void ltt_dump_marker_state(struct ust_trace
*trace
)
1300 struct marker_entry
*entry
;
1301 struct ltt_probe_private_data call_data
;
1302 struct cds_hlist_head
*head
;
1303 struct cds_hlist_node
*node
;
1306 pthread_mutex_lock(&markers_mutex
);
1307 call_data
.trace
= trace
;
1308 call_data
.serializer
= NULL
;
1310 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1311 head
= &marker_table
[i
];
1312 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
1313 __trace_mark(0, metadata
, core_marker_id
,
1315 "channel %s name %s event_id %hu "
1316 "int #1u%zu long #1u%zu pointer #1u%zu "
1317 "size_t #1u%zu alignment #1u%u",
1321 sizeof(int), sizeof(long),
1322 sizeof(void *), sizeof(size_t),
1323 ltt_get_alignment());
1325 __trace_mark(0, metadata
,
1328 "channel %s name %s format %s",
1334 pthread_mutex_unlock(&markers_mutex
);
1336 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1338 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1340 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1345 static void new_markers(struct marker
* const *start
, struct marker
* const *end
)
1347 if (new_marker_cb
) {
1348 struct marker
* const *m
;
1350 for(m
= start
; m
< end
; m
++) {
1357 int marker_register_lib(struct marker
* const *markers_start
, int markers_count
)
1359 struct lib
*pl
, *iter
;
1361 pl
= (struct lib
*) zmalloc(sizeof(struct lib
));
1363 pl
->markers_start
= markers_start
;
1364 pl
->markers_count
= markers_count
;
1366 /* FIXME: maybe protect this with its own mutex? */
1370 * We sort the libs by struct lib pointer address.
1372 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
1373 BUG_ON(iter
== pl
); /* Should never be in the list twice */
1375 /* We belong to the location right after iter. */
1376 cds_list_add(&pl
->list
, &iter
->list
);
1380 /* We should be added at the head of the list */
1381 cds_list_add(&pl
->list
, &libs
);
1385 new_markers(markers_start
, markers_start
+ markers_count
);
1387 /* FIXME: update just the loaded lib */
1388 lib_update_markers();
1390 /* markers_count - 1: skip dummy */
1391 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
- 1);
1396 int marker_unregister_lib(struct marker
* const *markers_start
)
1400 /*FIXME: implement; but before implementing, marker_register_lib must
1401 have appropriate locking. */
1405 /* FIXME: we should probably take a mutex here on libs */
1406 //ust// pthread_mutex_lock(&module_mutex);
1407 cds_list_for_each_entry(lib
, &libs
, list
) {
1408 if(lib
->markers_start
== markers_start
) {
1409 struct lib
*lib2free
= lib
;
1410 cds_list_del(&lib
->list
);
1421 static int initialized
= 0;
1423 void __attribute__((constructor
)) init_markers(void)
1426 marker_register_lib(__start___markers_ptrs
,
1427 __stop___markers_ptrs
1428 - __start___markers_ptrs
);
1433 void __attribute__((destructor
)) destroy_markers(void)
1435 marker_unregister_lib(__start___markers_ptrs
);