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;
7 * version 2.1 of the License.
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>
30 #include "usterr_signal_safe.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 ust_marker
* const __start___ust_marker_ptrs
[] __attribute__((visibility("hidden")));
39 extern struct ust_marker
* const __stop___ust_marker_ptrs
[] __attribute__((visibility("hidden")));
41 /* Set to 1 to enable ust_marker debug output */
42 static const int ust_marker_debug
;
45 * ust_marker_mutex nests inside module_mutex. ust_marker mutex protects
46 * the builtin and module ust_marker and the hash table.
48 static DEFINE_MUTEX(ust_marker_mutex
);
50 static CDS_LIST_HEAD(ust_marker_libs
);
53 void lock_ust_marker(void)
55 pthread_mutex_lock(&ust_marker_mutex
);
58 void unlock_ust_marker(void)
60 pthread_mutex_unlock(&ust_marker_mutex
);
64 * ust_marker hash table, containing the active ust_marker.
65 * Protected by module_mutex.
67 #define UST_MARKER_HASH_BITS 6
68 #define UST_MARKER_TABLE_SIZE (1 << UST_MARKER_HASH_BITS)
69 static struct cds_hlist_head ust_marker_table
[UST_MARKER_TABLE_SIZE
];
73 * It is used to make sure every handler has finished using its private
74 * data between two consecutive operation (add or remove) on a given
75 * ust_marker. It is also used to delay the free of multiple probes
76 * array until a quiescent state is reached. ust_marker entries
77 * modifications are protected by the ust_marker_mutex.
79 struct ust_marker_entry
{
80 struct cds_hlist_node hlist
;
84 void (*call
)(const struct ust_marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
85 struct ust_marker_probe_closure single
;
86 struct ust_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_UST_MARKER_USERSPACE
99 static void ust_marker_update_processes(void);
101 static void ust_marker_update_processes(void)
107 * __ust_marker_empty_function - Empty probe callback
108 * @mdata: ust_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 ust_marker. By providing
115 * this to a disabled ust_marker, we make sure the execution flow is
116 * always valid even though the function pointer change and the
117 * ust_marker enabling are two distinct operations that modifies the
118 * execution flow of preemptible code.
120 notrace
void __ust_marker_empty_function(const struct ust_marker
*mdata
,
121 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
124 //ust// EXPORT_SYMBOL_GPL(__ust_marker_empty_function);
127 * ust_marker_probe_cb Callback that prepares the variable argument list for probes.
128 * @mdata: pointer of type struct ust_marker
129 * @call_private: caller site private data
130 * @...: Variable argument list.
132 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
133 * need to put a full cmm_smp_rmb() in this branch. This is why we do not use
134 * rcu_dereference() for the pointer read.
136 notrace
void ust_marker_probe_cb(const struct ust_marker
*mdata
,
137 void *call_private
, struct registers
*regs
, ...)
143 * rcu_read_lock_sched does two things : disabling preemption to make
144 * sure the teardown of the callbacks can be done correctly when they
145 * are in modules and they insure RCU read coherency.
147 //ust// rcu_read_lock_sched_notrace();
148 ptype
= mdata
->ptype
;
149 if (likely(!ptype
)) {
150 ust_marker_probe_func
*func
;
151 /* Must read the ptype before ptr. They are not data dependant,
152 * so we put an explicit cmm_smp_rmb() here. */
154 func
= mdata
->single
.func
;
155 /* Must read the ptr before private data. They are not data
156 * dependant, so we put an explicit cmm_smp_rmb() here. */
158 va_start(args
, regs
);
159 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
160 mdata
->format
, &args
);
163 struct ust_marker_probe_closure
*multi
;
166 * Read mdata->ptype before mdata->multi.
169 multi
= mdata
->multi
;
171 * multi points to an array, therefore accessing the array
172 * depends on reading multi. However, even in this case,
173 * we must insure that the pointer is read _before_ the array
174 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
175 * in the fast path, so put the explicit cmm_barrier here.
177 cmm_smp_read_barrier_depends();
178 for (i
= 0; multi
[i
].func
; i
++) {
179 va_start(args
, regs
);
180 multi
[i
].func(mdata
, multi
[i
].probe_private
,
181 regs
, call_private
, mdata
->format
, &args
);
185 //ust// rcu_read_unlock_sched_notrace();
187 //ust// EXPORT_SYMBOL_GPL(ust_marker_probe_cb);
190 * ust_marker_probe_cb Callback that does not prepare the variable argument list.
191 * @mdata: pointer of type struct ust_marker
192 * @call_private: caller site private data
193 * @...: Variable argument list.
195 * Should be connected to ust_marker "UST_MARKER_NOARGS".
197 static notrace
void ust_marker_probe_cb_noarg(const struct ust_marker
*mdata
,
198 void *call_private
, struct registers
*regs
, ...)
200 va_list args
; /* not initialized */
203 //ust// rcu_read_lock_sched_notrace();
204 ptype
= mdata
->ptype
;
205 if (likely(!ptype
)) {
206 ust_marker_probe_func
*func
;
207 /* Must read the ptype before ptr. They are not data dependant,
208 * so we put an explicit cmm_smp_rmb() here. */
210 func
= mdata
->single
.func
;
211 /* Must read the ptr before private data. They are not data
212 * dependant, so we put an explicit cmm_smp_rmb() here. */
214 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
215 mdata
->format
, &args
);
217 struct ust_marker_probe_closure
*multi
;
220 * Read mdata->ptype before mdata->multi.
223 multi
= mdata
->multi
;
225 * multi points to an array, therefore accessing the array
226 * depends on reading multi. However, even in this case,
227 * we must insure that the pointer is read _before_ the array
228 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
229 * in the fast path, so put the explicit cmm_barrier here.
231 cmm_smp_read_barrier_depends();
232 for (i
= 0; multi
[i
].func
; i
++)
233 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
234 call_private
, mdata
->format
, &args
);
236 //ust// rcu_read_unlock_sched_notrace();
239 static void free_old_closure(struct rcu_head
*head
)
241 struct ust_marker_entry
*entry
= _ust_container_of(head
,
242 struct ust_marker_entry
, rcu
);
244 /* Make sure we free the data before setting the pending flag to 0 */
246 entry
->rcu_pending
= 0;
249 static void debug_print_probes(struct ust_marker_entry
*entry
)
253 if (!ust_marker_debug
)
257 DBG("Single probe : %p %p",
259 entry
->single
.probe_private
);
261 for (i
= 0; entry
->multi
[i
].func
; i
++)
262 DBG("Multi probe %d : %p %p", i
,
263 entry
->multi
[i
].func
,
264 entry
->multi
[i
].probe_private
);
268 static struct ust_marker_probe_closure
*
269 ust_marker_entry_add_probe(struct ust_marker_entry
*entry
,
270 ust_marker_probe_func
*probe
, void *probe_private
)
273 struct ust_marker_probe_closure
*old
, *new;
277 debug_print_probes(entry
);
280 if (entry
->single
.func
== probe
&&
281 entry
->single
.probe_private
== probe_private
)
282 return ERR_PTR(-EBUSY
);
283 if (entry
->single
.func
== __ust_marker_empty_function
) {
285 entry
->single
.func
= probe
;
286 entry
->single
.probe_private
= probe_private
;
289 debug_print_probes(entry
);
297 /* (N -> N+1), (N != 0, 1) probes */
298 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
299 if (old
[nr_probes
].func
== probe
300 && old
[nr_probes
].probe_private
302 return ERR_PTR(-EBUSY
);
304 /* + 2 : one for new probe, one for NULL func */
305 new = zmalloc((nr_probes
+ 2) * sizeof(struct ust_marker_probe_closure
));
307 return ERR_PTR(-ENOMEM
);
309 new[0] = entry
->single
;
312 nr_probes
* sizeof(struct ust_marker_probe_closure
));
313 new[nr_probes
].func
= probe
;
314 new[nr_probes
].probe_private
= probe_private
;
315 entry
->refcount
= nr_probes
+ 1;
318 debug_print_probes(entry
);
322 static struct ust_marker_probe_closure
*
323 ust_marker_entry_remove_probe(struct ust_marker_entry
*entry
,
324 ust_marker_probe_func
*probe
, void *probe_private
)
326 int nr_probes
= 0, nr_del
= 0, i
;
327 struct ust_marker_probe_closure
*old
, *new;
331 debug_print_probes(entry
);
333 /* 0 -> N is an error */
334 WARN_ON(entry
->single
.func
== __ust_marker_empty_function
);
336 WARN_ON(probe
&& entry
->single
.func
!= probe
);
337 WARN_ON(entry
->single
.probe_private
!= probe_private
);
338 entry
->single
.func
= __ust_marker_empty_function
;
341 debug_print_probes(entry
);
344 /* (N -> M), (N > 1, M >= 0) probes */
345 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
346 if ((!probe
|| old
[nr_probes
].func
== probe
)
347 && old
[nr_probes
].probe_private
353 if (nr_probes
- nr_del
== 0) {
354 /* N -> 0, (N > 1) */
355 entry
->single
.func
= __ust_marker_empty_function
;
358 } else if (nr_probes
- nr_del
== 1) {
359 /* N -> 1, (N > 1) */
360 for (i
= 0; old
[i
].func
; i
++)
361 if ((probe
&& old
[i
].func
!= probe
) ||
362 old
[i
].probe_private
!= probe_private
)
363 entry
->single
= old
[i
];
368 /* N -> M, (N > 1, M > 1) */
370 new = zmalloc((nr_probes
- nr_del
+ 1) * sizeof(struct ust_marker_probe_closure
));
372 return ERR_PTR(-ENOMEM
);
373 for (i
= 0; old
[i
].func
; i
++)
374 if ((probe
&& old
[i
].func
!= probe
) ||
375 old
[i
].probe_private
!= probe_private
)
377 entry
->refcount
= nr_probes
- nr_del
;
381 debug_print_probes(entry
);
386 * Get ust_marker if the ust_marker is present in the ust_marker hash table.
387 * Must be called with ust_marker_mutex held.
388 * Returns NULL if not present.
390 static struct ust_marker_entry
*get_ust_marker(const char *channel
, const char *name
)
392 struct cds_hlist_head
*head
;
393 struct cds_hlist_node
*node
;
394 struct ust_marker_entry
*e
;
395 size_t channel_len
= strlen(channel
) + 1;
396 size_t name_len
= strlen(name
) + 1;
399 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
400 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
401 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
402 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
409 * Add the ust_marker to the ust_marker hash table. Must be called with
410 * ust_marker_mutex held.
412 static struct ust_marker_entry
*add_ust_marker(const char *channel
, const char *name
,
415 struct cds_hlist_head
*head
;
416 struct cds_hlist_node
*node
;
417 struct ust_marker_entry
*e
;
418 size_t channel_len
= strlen(channel
) + 1;
419 size_t name_len
= strlen(name
) + 1;
420 size_t format_len
= 0;
423 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
425 format_len
= strlen(format
) + 1;
426 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
427 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
428 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
429 DBG("ust_marker %s.%s busy", channel
, name
);
430 return ERR_PTR(-EBUSY
); /* Already there */
434 * Using zmalloc here to allocate a variable length element. Could
435 * cause some memory fragmentation if overused.
437 e
= zmalloc(sizeof(struct ust_marker_entry
)
438 + channel_len
+ name_len
+ format_len
);
440 return ERR_PTR(-ENOMEM
);
441 memcpy(e
->channel
, channel
, channel_len
);
442 e
->name
= &e
->channel
[channel_len
];
443 memcpy(e
->name
, name
, name_len
);
445 e
->format
= &e
->name
[name_len
];
446 memcpy(e
->format
, format
, format_len
);
447 if (strcmp(e
->format
, UST_MARKER_NOARGS
) == 0)
448 e
->call
= ust_marker_probe_cb_noarg
;
450 e
->call
= ust_marker_probe_cb
;
451 __ust_marker(metadata
, core_marker_format
, NULL
,
452 "channel %s name %s format %s",
453 e
->channel
, e
->name
, e
->format
);
456 e
->call
= ust_marker_probe_cb
;
458 e
->single
.func
= __ust_marker_empty_function
;
459 e
->single
.probe_private
= NULL
;
462 e
->format_allocated
= 0;
465 cds_hlist_add_head(&e
->hlist
, head
);
470 * Remove the ust_marker from the ust_marker hash table. Must be called with mutex_lock
473 static int remove_ust_marker(const char *channel
, const char *name
)
475 struct cds_hlist_head
*head
;
476 struct cds_hlist_node
*node
;
477 struct ust_marker_entry
*e
;
479 size_t channel_len
= strlen(channel
) + 1;
480 size_t name_len
= strlen(name
) + 1;
484 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
485 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
486 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
487 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
494 if (e
->single
.func
!= __ust_marker_empty_function
)
496 cds_hlist_del(&e
->hlist
);
497 if (e
->format_allocated
)
499 ret
= ltt_channels_unregister(e
->channel
);
501 /* Make sure the call_rcu has been executed */
502 //ust// if (e->rcu_pending)
503 //ust// rcu_cmm_barrier_sched();
509 * Set the mark_entry format to the format found in the element.
511 static int ust_marker_set_format(struct ust_marker_entry
*entry
, const char *format
)
513 entry
->format
= strdup(format
);
516 entry
->format_allocated
= 1;
518 __ust_marker(metadata
, core_marker_format
, NULL
,
519 "channel %s name %s format %s",
520 entry
->channel
, entry
->name
, entry
->format
);
525 * Sets the probe callback corresponding to one ust_marker.
527 static int set_ust_marker(struct ust_marker_entry
*entry
, struct ust_marker
*elem
,
531 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
534 if (strcmp(entry
->format
, elem
->format
) != 0) {
535 ERR("Format mismatch for probe %s (%s), ust_marker (%s)",
542 ret
= ust_marker_set_format(entry
, elem
->format
);
548 * probe_cb setup (statically known) is done here. It is
549 * asynchronous with the rest of execution, therefore we only
550 * pass from a "safe" callback (with argument) to an "unsafe"
551 * callback (does not set arguments).
553 elem
->call
= entry
->call
;
554 elem
->channel_id
= entry
->channel_id
;
555 elem
->event_id
= entry
->event_id
;
558 * We only update the single probe private data when the ptr is
559 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
561 WARN_ON(elem
->single
.func
!= __ust_marker_empty_function
562 && elem
->single
.probe_private
!= entry
->single
.probe_private
564 elem
->single
.probe_private
= entry
->single
.probe_private
;
566 * Make sure the private data is valid when we update the
570 elem
->single
.func
= entry
->single
.func
;
572 * We also make sure that the new probe callbacks array is consistent
573 * before setting a pointer to it.
575 rcu_assign_pointer(elem
->multi
, entry
->multi
);
577 * Update the function or multi probe array pointer before setting the
581 elem
->ptype
= entry
->ptype
;
583 if (elem
->tp_name
&& (active
^ elem
->state
)) {
584 WARN_ON(!elem
->tp_cb
);
586 * It is ok to directly call the probe registration because type
587 * checking has been done in the __ust_marker_tp() macro.
592 * try_module_get should always succeed because we hold
593 * ust_marker_mutex to get the tp_cb address.
595 //ust// ret = try_module_get(__module_text_address(
596 //ust// (unsigned long)elem->tp_cb));
597 //ust// BUG_ON(!ret);
598 ret
= tracepoint_probe_register_noupdate(
602 ret
= tracepoint_probe_unregister_noupdate(
606 * tracepoint_probe_update_all() must be called
607 * before the module containing tp_cb is unloaded.
609 //ust// module_put(__module_text_address(
610 //ust// (unsigned long)elem->tp_cb));
613 elem
->state
= active
;
619 * Disable a ust_marker and its probe callback.
620 * Note: only waiting an RCU period after setting elem->call to the empty
621 * function insures that the original callback is not used anymore. This insured
622 * by rcu_read_lock_sched around the call site.
624 static void disable_ust_marker(struct ust_marker
*elem
)
628 /* leave "call" as is. It is known statically. */
629 if (elem
->tp_name
&& elem
->state
) {
630 WARN_ON(!elem
->tp_cb
);
632 * It is ok to directly call the probe registration because type
633 * checking has been done in the __ust_marker_tp() macro.
635 ret
= tracepoint_probe_unregister_noupdate(elem
->tp_name
,
639 * tracepoint_probe_update_all() must be called
640 * before the module containing tp_cb is unloaded.
642 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
645 elem
->single
.func
= __ust_marker_empty_function
;
646 /* Update the function before setting the ptype */
648 elem
->ptype
= 0; /* single probe */
650 * Leave the private data and channel_id/event_id there, because removal
651 * is racy and should be done only after an RCU period. These are never
652 * used until the next initialization anyway.
657 * is_ust_marker_enabled - Check if a ust_marker is enabled
658 * @channel: channel name
659 * @name: ust_marker name
661 * Returns 1 if the ust_marker is enabled, 0 if disabled.
663 int is_ust_marker_enabled(const char *channel
, const char *name
)
665 struct ust_marker_entry
*entry
;
667 pthread_mutex_lock(&ust_marker_mutex
);
668 entry
= get_ust_marker(channel
, name
);
669 pthread_mutex_unlock(&ust_marker_mutex
);
671 return entry
&& !!entry
->refcount
;
675 * ust_marker_update_probe_range - Update a probe range
676 * @begin: beginning of the range
677 * @end: end of the range
679 * Updates the probe callback corresponding to a range of ust_marker.
681 void ust_marker_update_probe_range(struct ust_marker
* const *begin
,
682 struct ust_marker
* const *end
)
684 struct ust_marker
* const *iter
;
685 struct ust_marker_entry
*mark_entry
;
687 pthread_mutex_lock(&ust_marker_mutex
);
688 for (iter
= begin
; iter
< end
; iter
++) {
690 continue; /* skip dummy */
691 mark_entry
= get_ust_marker((*iter
)->channel
, (*iter
)->name
);
693 set_ust_marker(mark_entry
, *iter
, !!mark_entry
->refcount
);
695 * ignore error, continue
698 disable_ust_marker(*iter
);
701 pthread_mutex_unlock(&ust_marker_mutex
);
704 static void lib_update_ust_marker(void)
706 struct ust_marker_lib
*lib
;
708 /* FIXME: we should probably take a mutex here on libs */
709 //ust// pthread_mutex_lock(&module_mutex);
710 cds_list_for_each_entry(lib
, &ust_marker_libs
, list
)
711 ust_marker_update_probe_range(lib
->ust_marker_start
,
712 lib
->ust_marker_start
+ lib
->ust_marker_count
);
713 //ust// pthread_mutex_unlock(&module_mutex);
717 * Update probes, removing the faulty probes.
719 * Internal callback only changed before the first probe is connected to it.
720 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
721 * transitions. All other transitions will leave the old private data valid.
722 * This makes the non-atomicity of the callback/private data updates valid.
724 * "special case" updates :
729 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
730 * Site effect : ust_marker_set_format may delete the ust_marker entry (creating a
733 static void ust_marker_update_probes(void)
735 lib_update_ust_marker();
736 tracepoint_probe_update_all();
737 ust_marker_update_processes();
741 * ust_marker_probe_register - Connect a probe to a ust_marker
742 * @channel: ust_marker channel
743 * @name: ust_marker name
744 * @format: format string
745 * @probe: probe handler
746 * @probe_private: probe private data
748 * private data must be a valid allocated memory address, or NULL.
749 * Returns 0 if ok, error value on error.
750 * The probe address must at least be aligned on the architecture pointer size.
752 int ust_marker_probe_register(const char *channel
, const char *name
,
753 const char *format
, ust_marker_probe_func
*probe
,
756 struct ust_marker_entry
*entry
;
757 int ret
= 0, ret_err
;
758 struct ust_marker_probe_closure
*old
;
761 pthread_mutex_lock(&ust_marker_mutex
);
762 entry
= get_ust_marker(channel
, name
);
765 entry
= add_ust_marker(channel
, name
, format
);
767 ret
= PTR_ERR(entry
);
770 ret
= ltt_channels_register(channel
);
772 goto error_remove_ust_marker
;
773 ret
= ltt_channels_get_index_from_name(channel
);
775 goto error_unregister_channel
;
776 entry
->channel_id
= ret
;
777 ret
= ltt_channels_get_event_id(channel
, name
);
779 goto error_unregister_channel
;
780 entry
->event_id
= ret
;
782 __ust_marker(metadata
, core_marker_id
, NULL
,
783 "channel %s name %s event_id %hu "
784 "int #1u%zu long #1u%zu pointer #1u%zu "
785 "size_t #1u%zu alignment #1u%u",
786 channel
, name
, entry
->event_id
,
787 sizeof(int), sizeof(long), sizeof(void *),
788 sizeof(size_t), ltt_get_alignment());
791 ret
= ust_marker_set_format(entry
, format
);
792 else if (strcmp(entry
->format
, format
))
799 * If we detect that a call_rcu is pending for this ust_marker,
800 * make sure it's executed now.
802 //ust// if (entry->rcu_pending)
803 //ust// rcu_cmm_barrier_sched();
804 old
= ust_marker_entry_add_probe(entry
, probe
, probe_private
);
808 goto error_unregister_channel
;
812 pthread_mutex_unlock(&ust_marker_mutex
);
814 /* Activate ust_marker if necessary */
815 ust_marker_update_probes();
817 pthread_mutex_lock(&ust_marker_mutex
);
818 entry
= get_ust_marker(channel
, name
);
821 //ust// if (entry->rcu_pending)
822 //ust// rcu_cmm_barrier_sched();
824 entry
->rcu_pending
= 1;
825 /* write rcu_pending before calling the RCU callback */
827 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
828 synchronize_rcu(); free_old_closure(&entry
->rcu
);
831 error_unregister_channel
:
832 ret_err
= ltt_channels_unregister(channel
);
834 error_remove_ust_marker
:
835 ret_err
= remove_ust_marker(channel
, name
);
838 pthread_mutex_unlock(&ust_marker_mutex
);
841 //ust// EXPORT_SYMBOL_GPL(ust_marker_probe_register);
844 * ust_marker_probe_unregister - Disconnect a probe from a ust_marker
845 * @channel: ust_marker channel
846 * @name: ust_marker name
847 * @probe: probe function pointer
848 * @probe_private: probe private data
850 * Returns the private data given to ust_marker_probe_register, or an ERR_PTR().
851 * We do not need to call a synchronize_sched to make sure the probes have
852 * finished running before doing a module unload, because the module unload
853 * itself uses stop_machine(), which insures that every preempt disabled section
856 int ust_marker_probe_unregister(const char *channel
, const char *name
,
857 ust_marker_probe_func
*probe
, void *probe_private
)
859 struct ust_marker_entry
*entry
;
860 struct ust_marker_probe_closure
*old
;
863 pthread_mutex_lock(&ust_marker_mutex
);
864 entry
= get_ust_marker(channel
, name
);
867 //ust// if (entry->rcu_pending)
868 //ust// rcu_cmm_barrier_sched();
869 old
= ust_marker_entry_remove_probe(entry
, probe
, probe_private
);
870 pthread_mutex_unlock(&ust_marker_mutex
);
872 ust_marker_update_probes();
874 pthread_mutex_lock(&ust_marker_mutex
);
875 entry
= get_ust_marker(channel
, name
);
878 //ust// if (entry->rcu_pending)
879 //ust// rcu_cmm_barrier_sched();
881 entry
->rcu_pending
= 1;
882 /* write rcu_pending before calling the RCU callback */
884 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
885 synchronize_rcu(); free_old_closure(&entry
->rcu
);
886 remove_ust_marker(channel
, name
); /* Ignore busy error message */
889 pthread_mutex_unlock(&ust_marker_mutex
);
892 //ust// EXPORT_SYMBOL_GPL(ust_marker_probe_unregister);
894 static struct ust_marker_entry
*
895 get_ust_marker_from_private_data(ust_marker_probe_func
*probe
, void *probe_private
)
897 struct ust_marker_entry
*entry
;
899 struct cds_hlist_head
*head
;
900 struct cds_hlist_node
*node
;
902 for (i
= 0; i
< UST_MARKER_TABLE_SIZE
; i
++) {
903 head
= &ust_marker_table
[i
];
904 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
906 if (entry
->single
.func
== probe
907 && entry
->single
.probe_private
911 struct ust_marker_probe_closure
*closure
;
912 closure
= entry
->multi
;
913 for (i
= 0; closure
[i
].func
; i
++) {
914 if (closure
[i
].func
== probe
&&
915 closure
[i
].probe_private
926 * ust_marker_probe_unregister_private_data - Disconnect a probe from a ust_marker
927 * @probe: probe function
928 * @probe_private: probe private data
930 * Unregister a probe by providing the registered private data.
931 * Only removes the first ust_marker found in hash table.
932 * Return 0 on success or error value.
933 * We do not need to call a synchronize_sched to make sure the probes have
934 * finished running before doing a module unload, because the module unload
935 * itself uses stop_machine(), which insures that every preempt disabled section
938 int ust_marker_probe_unregister_private_data(ust_marker_probe_func
*probe
,
941 struct ust_marker_entry
*entry
;
943 struct ust_marker_probe_closure
*old
;
944 char *channel
= NULL
, *name
= NULL
;
946 pthread_mutex_lock(&ust_marker_mutex
);
947 entry
= get_ust_marker_from_private_data(probe
, probe_private
);
952 //ust// if (entry->rcu_pending)
953 //ust// rcu_cmm_barrier_sched();
954 old
= ust_marker_entry_remove_probe(entry
, NULL
, probe_private
);
955 channel
= strdup(entry
->channel
);
956 name
= strdup(entry
->name
);
957 pthread_mutex_unlock(&ust_marker_mutex
);
959 ust_marker_update_probes();
961 pthread_mutex_lock(&ust_marker_mutex
);
962 entry
= get_ust_marker(channel
, name
);
965 //ust// if (entry->rcu_pending)
966 //ust// rcu_cmm_barrier_sched();
968 entry
->rcu_pending
= 1;
969 /* write rcu_pending before calling the RCU callback */
971 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
972 synchronize_rcu(); free_old_closure(&entry
->rcu
);
973 /* Ignore busy error message */
974 remove_ust_marker(channel
, name
);
976 pthread_mutex_unlock(&ust_marker_mutex
);
981 //ust// EXPORT_SYMBOL_GPL(ust_marker_probe_unregister_private_data);
984 * ust_marker_get_private_data - Get a ust_marker's probe private data
985 * @channel: ust_marker channel
986 * @name: ust_marker name
987 * @probe: probe to match
988 * @num: get the nth matching probe's private data
990 * Returns the nth private data pointer (starting from 0) matching, or an
992 * Returns the private data pointer, or an ERR_PTR.
993 * The private data pointer should _only_ be dereferenced if the caller is the
994 * owner of the data, or its content could vanish. This is mostly used to
995 * confirm that a caller is the owner of a registered probe.
997 void *ust_marker_get_private_data(const char *channel
, const char *name
,
998 ust_marker_probe_func
*probe
, int num
)
1000 struct cds_hlist_head
*head
;
1001 struct cds_hlist_node
*node
;
1002 struct ust_marker_entry
*e
;
1003 size_t channel_len
= strlen(channel
) + 1;
1004 size_t name_len
= strlen(name
) + 1;
1008 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1009 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
1010 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
1011 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1013 if (num
== 0 && e
->single
.func
== probe
)
1014 return e
->single
.probe_private
;
1016 struct ust_marker_probe_closure
*closure
;
1019 for (i
= 0; closure
[i
].func
; i
++) {
1020 if (closure
[i
].func
!= probe
)
1023 return closure
[i
].probe_private
;
1029 return ERR_PTR(-ENOENT
);
1031 //ust// EXPORT_SYMBOL_GPL(ust_marker_get_private_data);
1034 * ust_marker_compact_event_ids - Compact ust_marker event IDs and reassign channels
1036 * Called when no channel users are active by the channel infrastructure.
1037 * Called with lock_ust_marker() and channel mutex held.
1039 //ust// void ust_marker_compact_event_ids(void)
1041 //ust// struct ust_marker_entry *entry;
1042 //ust// unsigned int i;
1043 //ust// struct hlist_head *head;
1044 //ust// struct hlist_node *node;
1047 //ust// for (i = 0; i < UST_MARKER_TABLE_SIZE; i++) {
1048 //ust// head = &ust_marker_table[i];
1049 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1050 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1051 //ust// WARN_ON(ret < 0);
1052 //ust// entry->channel_id = ret;
1053 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1054 //ust// entry->name);
1055 //ust// WARN_ON(ret < 0);
1056 //ust// entry->event_id = ret;
1061 //ust//#ifdef CONFIG_MODULES
1064 * Returns 0 if current not found.
1065 * Returns 1 if current found.
1067 int lib_get_iter_ust_marker(struct ust_marker_iter
*iter
)
1069 struct ust_marker_lib
*iter_lib
;
1072 //ust// pthread_mutex_lock(&module_mutex);
1073 cds_list_for_each_entry(iter_lib
, &ust_marker_libs
, list
) {
1074 if (iter_lib
< iter
->lib
)
1076 else if (iter_lib
> iter
->lib
)
1077 iter
->ust_marker
= NULL
;
1078 found
= ust_marker_get_iter_range(&iter
->ust_marker
,
1079 iter_lib
->ust_marker_start
,
1080 iter_lib
->ust_marker_start
+ iter_lib
->ust_marker_count
);
1082 iter
->lib
= iter_lib
;
1086 //ust// pthread_mutex_unlock(&module_mutex);
1091 * ust_marker_get_iter_range - Get a next ust_marker iterator given a range.
1092 * @ust_marker: current ust_marker (in), next ust_marker (out)
1093 * @begin: beginning of the range
1094 * @end: end of the range
1096 * Returns whether a next ust_marker has been found (1) or not (0).
1097 * Will return the first ust_marker in the range if the input ust_marker is NULL.
1099 int ust_marker_get_iter_range(struct ust_marker
* const **ust_marker
,
1100 struct ust_marker
* const *begin
,
1101 struct ust_marker
* const *end
)
1103 if (!*ust_marker
&& begin
!= end
)
1104 *ust_marker
= begin
;
1105 while (*ust_marker
>= begin
&& *ust_marker
< end
) {
1107 (*ust_marker
)++; /* skip dummy */
1113 //ust// EXPORT_SYMBOL_GPL(ust_marker_get_iter_range);
1115 static void ust_marker_get_iter(struct ust_marker_iter
*iter
)
1119 found
= lib_get_iter_ust_marker(iter
);
1121 ust_marker_iter_reset(iter
);
1124 void ust_marker_iter_start(struct ust_marker_iter
*iter
)
1126 ust_marker_get_iter(iter
);
1128 //ust// EXPORT_SYMBOL_GPL(ust_marker_iter_start);
1130 void ust_marker_iter_next(struct ust_marker_iter
*iter
)
1134 * iter->ust_marker may be invalid because we blindly incremented it.
1135 * Make sure it is valid by marshalling on the ust_marker, getting the
1136 * ust_marker from following modules if necessary.
1138 ust_marker_get_iter(iter
);
1140 //ust// EXPORT_SYMBOL_GPL(ust_marker_iter_next);
1142 void ust_marker_iter_stop(struct ust_marker_iter
*iter
)
1145 //ust// EXPORT_SYMBOL_GPL(ust_marker_iter_stop);
1147 void ust_marker_iter_reset(struct ust_marker_iter
*iter
)
1150 iter
->ust_marker
= NULL
;
1152 //ust// EXPORT_SYMBOL_GPL(ust_marker_iter_reset);
1154 #ifdef CONFIG_UST_MARKER_USERSPACE
1156 * must be called with current->user_ust_marker_mutex held
1158 static void free_user_ust_marker(char __user
*state
, struct cds_hlist_head
*head
)
1160 struct user_ust_marker
*umark
;
1161 struct cds_hlist_node
*pos
, *n
;
1163 cds_hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1164 if (umark
->state
== state
) {
1165 cds_hlist_del(&umark
->hlist
);
1172 * Update current process.
1173 * Note that we have to wait a whole scheduler period before we are sure that
1174 * every running userspace threads have their ust_marker updated.
1175 * (synchronize_sched() can be used to insure this).
1177 //ust// void ust_marker_update_process(void)
1179 //ust// struct user_ust_marker *umark;
1180 //ust// struct hlist_node *pos;
1181 //ust// struct ust_marker_entry *entry;
1183 //ust// pthread_mutex_lock(&ust_marker_mutex);
1184 //ust// pthread_mutex_lock(¤t->group_leader->user_ust_marker_mutex);
1185 //ust// if (strcmp(current->comm, "testprog") == 0)
1186 //ust// DBG("do update pending for testprog");
1187 //ust// hlist_for_each_entry(umark, pos,
1188 //ust// ¤t->group_leader->user_ust_marker, hlist) {
1189 //ust// DBG("Updating ust_marker %s in %s", umark->name, current->comm);
1190 //ust// entry = get_ust_marker("userspace", umark->name);
1191 //ust// if (entry) {
1192 //ust// if (entry->format &&
1193 //ust// strcmp(entry->format, umark->format) != 0) {
1194 //ust// WARN("error, wrong format in process %s",
1195 //ust// current->comm);
1198 //ust// if (put_user(!!entry->refcount, umark->state)) {
1199 //ust// WARN("ust_marker in %s caused a fault",
1200 //ust// current->comm);
1204 //ust// if (put_user(0, umark->state)) {
1205 //ust// WARN("ust_marker in %s caused a fault", current->comm);
1210 //ust// clear_thread_flag(TIF_UST_MARKER_PENDING);
1211 //ust// pthread_mutex_unlock(¤t->group_leader->user_ust_marker_mutex);
1212 //ust// pthread_mutex_unlock(&ust_marker_mutex);
1216 * Called at process exit and upon do_execve().
1217 * We assume that when the leader exits, no more references can be done to the
1218 * leader structure by the other threads.
1220 void exit_user_ust_marker(struct task_struct
*p
)
1222 struct user_ust_marker
*umark
;
1223 struct cds_hlist_node
*pos
, *n
;
1225 if (thread_group_leader(p
)) {
1226 pthread_mutex_lock(&ust_marker_mutex
);
1227 pthread_mutex_lock(&p
->user_ust_marker_mutex
);
1228 cds_hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_ust_marker
,
1231 INIT_HLIST_HEAD(&p
->user_ust_marker
);
1232 p
->user_ust_marker_sequence
++;
1233 pthread_mutex_unlock(&p
->user_ust_marker_mutex
);
1234 pthread_mutex_unlock(&ust_marker_mutex
);
1238 int is_ust_marker_enabled(const char *channel
, const char *name
)
1240 struct ust_marker_entry
*entry
;
1242 pthread_mutex_lock(&ust_marker_mutex
);
1243 entry
= get_ust_marker(channel
, name
);
1244 pthread_mutex_unlock(&ust_marker_mutex
);
1246 return entry
&& !!entry
->refcount
;
1250 int ust_marker_module_notify(struct notifier_block
*self
,
1251 unsigned long val
, void *data
)
1253 struct module
*mod
= data
;
1256 case MODULE_STATE_COMING
:
1257 ust_marker_update_probe_range(mod
->ust_marker
,
1258 mod
->ust_marker
+ mod
->num_ust_marker
);
1260 case MODULE_STATE_GOING
:
1261 ust_marker_update_probe_range(mod
->ust_marker
,
1262 mod
->ust_marker
+ mod
->num_ust_marker
);
1268 struct notifier_block ust_marker_module_nb
= {
1269 .notifier_call
= ust_marker_module_notify
,
1273 //ust// static int init_ust_marker(void)
1275 //ust// return register_module_notifier(&ust_marker_module_nb);
1277 //ust// __initcall(init_ust_marker);
1278 /* TODO: call ust_marker_module_nb() when a library is linked at runtime (dlopen)? */
1280 #endif /* CONFIG_MODULES */
1282 void ltt_dump_ust_marker_state(struct ust_trace
*trace
)
1284 struct ust_marker_entry
*entry
;
1285 struct ltt_probe_private_data call_data
;
1286 struct cds_hlist_head
*head
;
1287 struct cds_hlist_node
*node
;
1290 pthread_mutex_lock(&ust_marker_mutex
);
1291 call_data
.trace
= trace
;
1292 call_data
.serializer
= NULL
;
1294 for (i
= 0; i
< UST_MARKER_TABLE_SIZE
; i
++) {
1295 head
= &ust_marker_table
[i
];
1296 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
1297 __ust_marker(metadata
, core_marker_id
,
1299 "channel %s name %s event_id %hu "
1300 "int #1u%zu long #1u%zu pointer #1u%zu "
1301 "size_t #1u%zu alignment #1u%u",
1305 sizeof(int), sizeof(long),
1306 sizeof(void *), sizeof(size_t),
1307 ltt_get_alignment());
1309 __ust_marker(metadata
,
1312 "channel %s name %s format %s",
1318 pthread_mutex_unlock(&ust_marker_mutex
);
1320 //ust// EXPORT_SYMBOL_GPL(ltt_dump_ust_marker_state);
1322 static void (*new_ust_marker_cb
)(struct ust_marker
*) = NULL
;
1324 void ust_marker_set_new_ust_marker_cb(void (*cb
)(struct ust_marker
*))
1326 new_ust_marker_cb
= cb
;
1329 static void new_ust_marker(struct ust_marker
* const *start
, struct ust_marker
* const *end
)
1331 if (new_ust_marker_cb
) {
1332 struct ust_marker
* const *m
;
1334 for(m
= start
; m
< end
; m
++) {
1336 new_ust_marker_cb(*m
);
1341 int ust_marker_register_lib(struct ust_marker
* const *ust_marker_start
, int ust_marker_count
)
1343 struct ust_marker_lib
*pl
, *iter
;
1345 pl
= (struct ust_marker_lib
*) zmalloc(sizeof(struct ust_marker_lib
));
1347 pl
->ust_marker_start
= ust_marker_start
;
1348 pl
->ust_marker_count
= ust_marker_count
;
1350 /* FIXME: maybe protect this with its own mutex? */
1354 * We sort the libs by struct lib pointer address.
1356 cds_list_for_each_entry_reverse(iter
, &ust_marker_libs
, list
) {
1357 BUG_ON(iter
== pl
); /* Should never be in the list twice */
1359 /* We belong to the location right after iter. */
1360 cds_list_add(&pl
->list
, &iter
->list
);
1364 /* We should be added at the head of the list */
1365 cds_list_add(&pl
->list
, &ust_marker_libs
);
1367 unlock_ust_marker();
1369 new_ust_marker(ust_marker_start
, ust_marker_start
+ ust_marker_count
);
1371 /* FIXME: update just the loaded lib */
1372 lib_update_ust_marker();
1374 DBG("just registered a ust_marker section from %p and having %d ust_marker (minus dummy ust_marker)", ust_marker_start
, ust_marker_count
);
1379 int ust_marker_unregister_lib(struct ust_marker
* const *ust_marker_start
)
1381 struct ust_marker_lib
*lib
;
1383 /*FIXME: implement; but before implementing, ust_marker_register_lib must
1384 have appropriate locking. */
1388 /* FIXME: we should probably take a mutex here on libs */
1389 //ust// pthread_mutex_lock(&module_mutex);
1390 cds_list_for_each_entry(lib
, &ust_marker_libs
, list
) {
1391 if(lib
->ust_marker_start
== ust_marker_start
) {
1392 struct ust_marker_lib
*lib2free
= lib
;
1393 cds_list_del(&lib
->list
);
1399 unlock_ust_marker();
1404 static int initialized
= 0;
1406 void __attribute__((constructor
)) init_ust_marker(void)
1409 ust_marker_register_lib(__start___ust_marker_ptrs
,
1410 __stop___ust_marker_ptrs
1411 - __start___ust_marker_ptrs
);
1416 void __attribute__((destructor
)) destroy_ust_marker(void)
1418 ust_marker_unregister_lib(__start___ust_marker_ptrs
);