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>
26 #include <ust/marker.h>
30 #include "tracercore.h"
33 __thread
long ust_reg_stack
[500];
34 volatile __thread
long *ust_reg_stack_ptr
= (long *) 0;
36 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
37 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
39 /* Set to 1 to enable marker debug output */
40 static const int marker_debug
;
43 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
44 * and module markers and the hash table.
46 static DEFINE_MUTEX(markers_mutex
);
48 static LIST_HEAD(libs
);
51 void lock_markers(void)
53 mutex_lock(&markers_mutex
);
56 void unlock_markers(void)
58 mutex_unlock(&markers_mutex
);
62 * Marker hash table, containing the active markers.
63 * Protected by module_mutex.
65 #define MARKER_HASH_BITS 6
66 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
67 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
71 * It is used to make sure every handler has finished using its private data
72 * between two consecutive operation (add or remove) on a given marker. It is
73 * also used to delay the free of multiple probes array until a quiescent state
75 * marker entries modifications are protected by the markers_mutex.
78 struct hlist_node hlist
;
82 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
83 struct marker_probe_closure single
;
84 struct marker_probe_closure
*multi
;
85 int refcount
; /* Number of times armed. 0 if disarmed. */
91 unsigned char ptype
:1;
92 unsigned char format_allocated
:1;
93 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
96 #ifdef CONFIG_MARKERS_USERSPACE
97 static void marker_update_processes(void);
99 static void marker_update_processes(void)
105 * __mark_empty_function - Empty probe callback
106 * @mdata: marker data
107 * @probe_private: probe private data
108 * @call_private: call site private data
109 * @fmt: format string
110 * @...: variable argument list
112 * Empty callback provided as a probe to the markers. By providing this to a
113 * disabled marker, we make sure the execution flow is always valid even
114 * though the function pointer change and the marker enabling are two distinct
115 * operations that modifies the execution flow of preemptible code.
117 notrace
void __mark_empty_function(const struct marker
*mdata
,
118 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
121 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
124 * marker_probe_cb Callback that prepares the variable argument list for probes.
125 * @mdata: pointer of type struct marker
126 * @call_private: caller site private data
127 * @...: Variable argument list.
129 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
130 * need to put a full smp_rmb() in this branch. This is why we do not use
131 * rcu_dereference() for the pointer read.
133 notrace
void marker_probe_cb(const struct marker
*mdata
,
134 void *call_private
, struct registers
*regs
, ...)
140 * rcu_read_lock_sched does two things : disabling preemption to make
141 * sure the teardown of the callbacks can be done correctly when they
142 * are in modules and they insure RCU read coherency.
144 //ust// rcu_read_lock_sched_notrace();
145 ptype
= mdata
->ptype
;
146 if (likely(!ptype
)) {
147 marker_probe_func
*func
;
148 /* Must read the ptype before ptr. They are not data dependant,
149 * so we put an explicit smp_rmb() here. */
151 func
= mdata
->single
.func
;
152 /* Must read the ptr before private data. They are not data
153 * dependant, so we put an explicit smp_rmb() here. */
155 va_start(args
, regs
);
156 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
157 mdata
->format
, &args
);
160 struct marker_probe_closure
*multi
;
163 * Read mdata->ptype before mdata->multi.
166 multi
= mdata
->multi
;
168 * multi points to an array, therefore accessing the array
169 * depends on reading multi. However, even in this case,
170 * we must insure that the pointer is read _before_ the array
171 * data. Same as rcu_dereference, but we need a full smp_rmb()
172 * in the fast path, so put the explicit barrier here.
174 smp_read_barrier_depends();
175 for (i
= 0; multi
[i
].func
; i
++) {
176 va_start(args
, regs
);
177 multi
[i
].func(mdata
, multi
[i
].probe_private
,
178 regs
, call_private
, mdata
->format
, &args
);
182 //ust// rcu_read_unlock_sched_notrace();
184 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
187 * marker_probe_cb Callback that does not prepare the variable argument list.
188 * @mdata: pointer of type struct marker
189 * @call_private: caller site private data
190 * @...: Variable argument list.
192 * Should be connected to markers "MARK_NOARGS".
194 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
195 void *call_private
, struct registers
*regs
, ...)
197 va_list args
; /* not initialized */
200 //ust// rcu_read_lock_sched_notrace();
201 ptype
= mdata
->ptype
;
202 if (likely(!ptype
)) {
203 marker_probe_func
*func
;
204 /* Must read the ptype before ptr. They are not data dependant,
205 * so we put an explicit smp_rmb() here. */
207 func
= mdata
->single
.func
;
208 /* Must read the ptr before private data. They are not data
209 * dependant, so we put an explicit smp_rmb() here. */
211 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
212 mdata
->format
, &args
);
214 struct marker_probe_closure
*multi
;
217 * Read mdata->ptype before mdata->multi.
220 multi
= mdata
->multi
;
222 * multi points to an array, therefore accessing the array
223 * depends on reading multi. However, even in this case,
224 * we must insure that the pointer is read _before_ the array
225 * data. Same as rcu_dereference, but we need a full smp_rmb()
226 * in the fast path, so put the explicit barrier here.
228 smp_read_barrier_depends();
229 for (i
= 0; multi
[i
].func
; i
++)
230 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
231 call_private
, mdata
->format
, &args
);
233 //ust// rcu_read_unlock_sched_notrace();
236 static void free_old_closure(struct rcu_head
*head
)
238 struct marker_entry
*entry
= container_of(head
,
239 struct marker_entry
, rcu
);
241 /* Make sure we free the data before setting the pending flag to 0 */
243 entry
->rcu_pending
= 0;
246 static void debug_print_probes(struct marker_entry
*entry
)
254 DBG("Single probe : %p %p",
256 entry
->single
.probe_private
);
258 for (i
= 0; entry
->multi
[i
].func
; i
++)
259 DBG("Multi probe %d : %p %p", i
,
260 entry
->multi
[i
].func
,
261 entry
->multi
[i
].probe_private
);
265 static struct marker_probe_closure
*
266 marker_entry_add_probe(struct marker_entry
*entry
,
267 marker_probe_func
*probe
, void *probe_private
)
270 struct marker_probe_closure
*old
, *new;
274 debug_print_probes(entry
);
277 if (entry
->single
.func
== probe
&&
278 entry
->single
.probe_private
== probe_private
)
279 return ERR_PTR(-EBUSY
);
280 if (entry
->single
.func
== __mark_empty_function
) {
282 entry
->single
.func
= probe
;
283 entry
->single
.probe_private
= probe_private
;
286 debug_print_probes(entry
);
294 /* (N -> N+1), (N != 0, 1) probes */
295 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
296 if (old
[nr_probes
].func
== probe
297 && old
[nr_probes
].probe_private
299 return ERR_PTR(-EBUSY
);
301 /* + 2 : one for new probe, one for NULL func */
302 new = zmalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
));
304 return ERR_PTR(-ENOMEM
);
306 new[0] = entry
->single
;
309 nr_probes
* sizeof(struct marker_probe_closure
));
310 new[nr_probes
].func
= probe
;
311 new[nr_probes
].probe_private
= probe_private
;
312 entry
->refcount
= nr_probes
+ 1;
315 debug_print_probes(entry
);
319 static struct marker_probe_closure
*
320 marker_entry_remove_probe(struct marker_entry
*entry
,
321 marker_probe_func
*probe
, void *probe_private
)
323 int nr_probes
= 0, nr_del
= 0, i
;
324 struct marker_probe_closure
*old
, *new;
328 debug_print_probes(entry
);
330 /* 0 -> N is an error */
331 WARN_ON(entry
->single
.func
== __mark_empty_function
);
333 WARN_ON(probe
&& entry
->single
.func
!= probe
);
334 WARN_ON(entry
->single
.probe_private
!= probe_private
);
335 entry
->single
.func
= __mark_empty_function
;
338 debug_print_probes(entry
);
341 /* (N -> M), (N > 1, M >= 0) probes */
342 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
343 if ((!probe
|| old
[nr_probes
].func
== probe
)
344 && old
[nr_probes
].probe_private
350 if (nr_probes
- nr_del
== 0) {
351 /* N -> 0, (N > 1) */
352 entry
->single
.func
= __mark_empty_function
;
355 } else if (nr_probes
- nr_del
== 1) {
356 /* N -> 1, (N > 1) */
357 for (i
= 0; old
[i
].func
; i
++)
358 if ((probe
&& old
[i
].func
!= probe
) ||
359 old
[i
].probe_private
!= probe_private
)
360 entry
->single
= old
[i
];
365 /* N -> M, (N > 1, M > 1) */
367 new = zmalloc((nr_probes
- nr_del
+ 1) * sizeof(struct marker_probe_closure
));
369 return ERR_PTR(-ENOMEM
);
370 for (i
= 0; old
[i
].func
; i
++)
371 if ((probe
&& old
[i
].func
!= probe
) ||
372 old
[i
].probe_private
!= probe_private
)
374 entry
->refcount
= nr_probes
- nr_del
;
378 debug_print_probes(entry
);
383 * Get marker if the marker is present in the marker hash table.
384 * Must be called with markers_mutex held.
385 * Returns NULL if not present.
387 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
389 struct hlist_head
*head
;
390 struct hlist_node
*node
;
391 struct marker_entry
*e
;
392 size_t channel_len
= strlen(channel
) + 1;
393 size_t name_len
= strlen(name
) + 1;
396 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
397 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
398 hlist_for_each_entry(e
, node
, head
, hlist
) {
399 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
406 * Add the marker to the marker hash table. Must be called with markers_mutex
409 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
412 struct hlist_head
*head
;
413 struct hlist_node
*node
;
414 struct marker_entry
*e
;
415 size_t channel_len
= strlen(channel
) + 1;
416 size_t name_len
= strlen(name
) + 1;
417 size_t format_len
= 0;
420 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
422 format_len
= strlen(format
) + 1;
423 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
424 hlist_for_each_entry(e
, node
, head
, hlist
) {
425 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
426 DBG("Marker %s.%s busy", channel
, name
);
427 return ERR_PTR(-EBUSY
); /* Already there */
431 * Using malloc here to allocate a variable length element. Could
432 * cause some memory fragmentation if overused.
434 e
= malloc(sizeof(struct marker_entry
)
435 + channel_len
+ name_len
+ format_len
);
437 return ERR_PTR(-ENOMEM
);
438 memcpy(e
->channel
, channel
, channel_len
);
439 e
->name
= &e
->channel
[channel_len
];
440 memcpy(e
->name
, name
, name_len
);
442 e
->format
= &e
->name
[channel_len
+ name_len
];
443 memcpy(e
->format
, format
, format_len
);
444 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
445 e
->call
= marker_probe_cb_noarg
;
447 e
->call
= marker_probe_cb
;
448 trace_mark(metadata
, core_marker_format
,
449 "channel %s name %s format %s",
450 e
->channel
, e
->name
, e
->format
);
453 e
->call
= marker_probe_cb
;
455 e
->single
.func
= __mark_empty_function
;
456 e
->single
.probe_private
= NULL
;
459 e
->format_allocated
= 0;
462 hlist_add_head(&e
->hlist
, head
);
467 * Remove the marker from the marker hash table. Must be called with mutex_lock
470 static int remove_marker(const char *channel
, const char *name
)
472 struct hlist_head
*head
;
473 struct hlist_node
*node
;
474 struct marker_entry
*e
;
476 size_t channel_len
= strlen(channel
) + 1;
477 size_t name_len
= strlen(name
) + 1;
481 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
482 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
483 hlist_for_each_entry(e
, node
, head
, hlist
) {
484 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
491 if (e
->single
.func
!= __mark_empty_function
)
493 hlist_del(&e
->hlist
);
494 if (e
->format_allocated
)
496 ret
= ltt_channels_unregister(e
->channel
);
498 /* Make sure the call_rcu has been executed */
499 //ust// if (e->rcu_pending)
500 //ust// rcu_barrier_sched();
506 * Set the mark_entry format to the format found in the element.
508 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
510 entry
->format
= strdup(format
);
513 entry
->format_allocated
= 1;
515 trace_mark(metadata
, core_marker_format
,
516 "channel %s name %s format %s",
517 entry
->channel
, entry
->name
, entry
->format
);
522 * Sets the probe callback corresponding to one marker.
524 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
528 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
531 if (strcmp(entry
->format
, elem
->format
) != 0) {
532 DBG("Format mismatch for probe %s (%s), marker (%s)",
539 ret
= marker_set_format(entry
, elem
->format
);
545 * probe_cb setup (statically known) is done here. It is
546 * asynchronous with the rest of execution, therefore we only
547 * pass from a "safe" callback (with argument) to an "unsafe"
548 * callback (does not set arguments).
550 elem
->call
= entry
->call
;
551 elem
->channel_id
= entry
->channel_id
;
552 elem
->event_id
= entry
->event_id
;
555 * We only update the single probe private data when the ptr is
556 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
558 WARN_ON(elem
->single
.func
!= __mark_empty_function
559 && elem
->single
.probe_private
!= entry
->single
.probe_private
561 elem
->single
.probe_private
= entry
->single
.probe_private
;
563 * Make sure the private data is valid when we update the
567 elem
->single
.func
= entry
->single
.func
;
569 * We also make sure that the new probe callbacks array is consistent
570 * before setting a pointer to it.
572 rcu_assign_pointer(elem
->multi
, entry
->multi
);
574 * Update the function or multi probe array pointer before setting the
578 elem
->ptype
= entry
->ptype
;
580 if (elem
->tp_name
&& (active
^ _imv_read(elem
->state
))) {
581 WARN_ON(!elem
->tp_cb
);
583 * It is ok to directly call the probe registration because type
584 * checking has been done in the __trace_mark_tp() macro.
589 * try_module_get should always succeed because we hold
590 * markers_mutex to get the tp_cb address.
592 //ust// ret = try_module_get(__module_text_address(
593 //ust// (unsigned long)elem->tp_cb));
594 //ust// BUG_ON(!ret);
595 ret
= tracepoint_probe_register_noupdate(
599 ret
= tracepoint_probe_unregister_noupdate(
603 * tracepoint_probe_update_all() must be called
604 * before the module containing tp_cb is unloaded.
606 //ust// module_put(__module_text_address(
607 //ust// (unsigned long)elem->tp_cb));
610 elem
->state__imv
= active
;
616 * Disable a marker and its probe callback.
617 * Note: only waiting an RCU period after setting elem->call to the empty
618 * function insures that the original callback is not used anymore. This insured
619 * by rcu_read_lock_sched around the call site.
621 static void disable_marker(struct marker
*elem
)
625 /* leave "call" as is. It is known statically. */
626 if (elem
->tp_name
&& _imv_read(elem
->state
)) {
627 WARN_ON(!elem
->tp_cb
);
629 * It is ok to directly call the probe registration because type
630 * checking has been done in the __trace_mark_tp() macro.
632 ret
= tracepoint_probe_unregister_noupdate(elem
->tp_name
,
636 * tracepoint_probe_update_all() must be called
637 * before the module containing tp_cb is unloaded.
639 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
641 elem
->state__imv
= 0;
642 elem
->single
.func
= __mark_empty_function
;
643 /* Update the function before setting the ptype */
645 elem
->ptype
= 0; /* single probe */
647 * Leave the private data and channel_id/event_id there, because removal
648 * is racy and should be done only after an RCU period. These are never
649 * used until the next initialization anyway.
654 * is_marker_enabled - Check if a marker is enabled
655 * @channel: channel name
658 * Returns 1 if the marker is enabled, 0 if disabled.
660 int is_marker_enabled(const char *channel
, const char *name
)
662 struct marker_entry
*entry
;
664 mutex_lock(&markers_mutex
);
665 entry
= get_marker(channel
, name
);
666 mutex_unlock(&markers_mutex
);
668 return entry
&& !!entry
->refcount
;
672 * marker_update_probe_range - Update a probe range
673 * @begin: beginning of the range
674 * @end: end of the range
676 * Updates the probe callback corresponding to a range of markers.
678 void marker_update_probe_range(struct marker
*begin
,
682 struct marker_entry
*mark_entry
;
684 mutex_lock(&markers_mutex
);
685 for (iter
= begin
; iter
< end
; iter
++) {
686 mark_entry
= get_marker(iter
->channel
, iter
->name
);
688 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
690 * ignore error, continue
693 /* This is added for UST. We emit a core_marker_id event
694 * for markers that are already registered to a probe
695 * upon library load. Otherwise, no core_marker_id will
696 * be generated for these markers. Is this the right thing
699 trace_mark(metadata
, core_marker_id
,
700 "channel %s name %s event_id %hu "
701 "int #1u%zu long #1u%zu pointer #1u%zu "
702 "size_t #1u%zu alignment #1u%u",
703 iter
->channel
, iter
->name
, mark_entry
->event_id
,
704 sizeof(int), sizeof(long), sizeof(void *),
705 sizeof(size_t), ltt_get_alignment());
707 disable_marker(iter
);
710 mutex_unlock(&markers_mutex
);
713 static void lib_update_markers(void)
717 /* FIXME: we should probably take a mutex here on libs */
718 //ust// mutex_lock(&module_mutex);
719 list_for_each_entry(lib
, &libs
, list
)
720 marker_update_probe_range(lib
->markers_start
,
721 lib
->markers_start
+ lib
->markers_count
);
722 //ust// mutex_unlock(&module_mutex);
726 * Update probes, removing the faulty probes.
728 * Internal callback only changed before the first probe is connected to it.
729 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
730 * transitions. All other transitions will leave the old private data valid.
731 * This makes the non-atomicity of the callback/private data updates valid.
733 * "special case" updates :
738 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
739 * Site effect : marker_set_format may delete the marker entry (creating a
742 static void marker_update_probes(void)
744 /* Core kernel markers */
745 //ust// marker_update_probe_range(__start___markers, __stop___markers);
746 /* Markers in modules. */
747 //ust// module_update_markers();
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 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_barrier_sched();
820 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
824 goto error_unregister_channel
;
828 mutex_unlock(&markers_mutex
);
830 /* Activate marker if necessary */
831 marker_update_probes();
833 mutex_lock(&markers_mutex
);
834 entry
= get_marker(channel
, name
);
837 //ust// if (entry->rcu_pending)
838 //ust// rcu_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 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 mutex_lock(&markers_mutex
);
880 entry
= get_marker(channel
, name
);
883 //ust// if (entry->rcu_pending)
884 //ust// rcu_barrier_sched();
885 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
886 mutex_unlock(&markers_mutex
);
888 marker_update_probes();
890 mutex_lock(&markers_mutex
);
891 entry
= get_marker(channel
, name
);
894 //ust// if (entry->rcu_pending)
895 //ust// rcu_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 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 hlist_head
*head
;
916 struct hlist_node
*node
;
918 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
919 head
= &marker_table
[i
];
920 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 mutex_lock(&markers_mutex
);
963 entry
= get_marker_from_private_data(probe
, probe_private
);
968 //ust// if (entry->rcu_pending)
969 //ust// rcu_barrier_sched();
970 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
971 channel
= strdup(entry
->channel
);
972 name
= strdup(entry
->name
);
973 mutex_unlock(&markers_mutex
);
975 marker_update_probes();
977 mutex_lock(&markers_mutex
);
978 entry
= get_marker(channel
, name
);
981 //ust// if (entry->rcu_pending)
982 //ust// rcu_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 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 hlist_head
*head
;
1017 struct 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 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// mutex_lock(&module_mutex);
1089 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// 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
**marker
, struct marker
*begin
,
1118 if (!*marker
&& begin
!= end
) {
1122 if (*marker
>= begin
&& *marker
< end
)
1126 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1128 static void marker_get_iter(struct marker_iter
*iter
)
1132 /* Core kernel markers */
1134 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1135 found
= marker_get_iter_range(&iter
->marker
,
1136 __start___markers
, __stop___markers
);
1140 /* Markers in modules. */
1141 found
= lib_get_iter_markers(iter
);
1144 marker_iter_reset(iter
);
1147 void marker_iter_start(struct marker_iter
*iter
)
1149 marker_get_iter(iter
);
1151 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1153 void marker_iter_next(struct marker_iter
*iter
)
1157 * iter->marker may be invalid because we blindly incremented it.
1158 * Make sure it is valid by marshalling on the markers, getting the
1159 * markers from following modules if necessary.
1161 marker_get_iter(iter
);
1163 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1165 void marker_iter_stop(struct marker_iter
*iter
)
1168 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1170 void marker_iter_reset(struct marker_iter
*iter
)
1173 iter
->marker
= NULL
;
1175 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1177 #ifdef CONFIG_MARKERS_USERSPACE
1179 * must be called with current->user_markers_mutex held
1181 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1183 struct user_marker
*umark
;
1184 struct hlist_node
*pos
, *n
;
1186 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1187 if (umark
->state
== state
) {
1188 hlist_del(&umark
->hlist
);
1195 * Update current process.
1196 * Note that we have to wait a whole scheduler period before we are sure that
1197 * every running userspace threads have their markers updated.
1198 * (synchronize_sched() can be used to insure this).
1200 //ust// void marker_update_process(void)
1202 //ust// struct user_marker *umark;
1203 //ust// struct hlist_node *pos;
1204 //ust// struct marker_entry *entry;
1206 //ust// mutex_lock(&markers_mutex);
1207 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1208 //ust// if (strcmp(current->comm, "testprog") == 0)
1209 //ust// DBG("do update pending for testprog");
1210 //ust// hlist_for_each_entry(umark, pos,
1211 //ust// ¤t->group_leader->user_markers, hlist) {
1212 //ust// DBG("Updating marker %s in %s", umark->name, current->comm);
1213 //ust// entry = get_marker("userspace", umark->name);
1214 //ust// if (entry) {
1215 //ust// if (entry->format &&
1216 //ust// strcmp(entry->format, umark->format) != 0) {
1217 //ust// WARN("error, wrong format in process %s",
1218 //ust// current->comm);
1221 //ust// if (put_user(!!entry->refcount, umark->state)) {
1222 //ust// WARN("Marker in %s caused a fault",
1223 //ust// current->comm);
1227 //ust// if (put_user(0, umark->state)) {
1228 //ust// WARN("Marker in %s caused a fault", current->comm);
1233 //ust// clear_thread_flag(TIF_MARKER_PENDING);
1234 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1235 //ust// mutex_unlock(&markers_mutex);
1239 * Called at process exit and upon do_execve().
1240 * We assume that when the leader exits, no more references can be done to the
1241 * leader structure by the other threads.
1243 void exit_user_markers(struct task_struct
*p
)
1245 struct user_marker
*umark
;
1246 struct hlist_node
*pos
, *n
;
1248 if (thread_group_leader(p
)) {
1249 mutex_lock(&markers_mutex
);
1250 mutex_lock(&p
->user_markers_mutex
);
1251 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1254 INIT_HLIST_HEAD(&p
->user_markers
);
1255 p
->user_markers_sequence
++;
1256 mutex_unlock(&p
->user_markers_mutex
);
1257 mutex_unlock(&markers_mutex
);
1261 int is_marker_enabled(const char *channel
, const char *name
)
1263 struct marker_entry
*entry
;
1265 mutex_lock(&markers_mutex
);
1266 entry
= get_marker(channel
, name
);
1267 mutex_unlock(&markers_mutex
);
1269 return entry
&& !!entry
->refcount
;
1273 int marker_module_notify(struct notifier_block
*self
,
1274 unsigned long val
, void *data
)
1276 struct module
*mod
= data
;
1279 case MODULE_STATE_COMING
:
1280 marker_update_probe_range(mod
->markers
,
1281 mod
->markers
+ mod
->num_markers
);
1283 case MODULE_STATE_GOING
:
1284 marker_update_probe_range(mod
->markers
,
1285 mod
->markers
+ mod
->num_markers
);
1291 struct notifier_block marker_module_nb
= {
1292 .notifier_call
= marker_module_notify
,
1296 //ust// static int init_markers(void)
1298 //ust// return register_module_notifier(&marker_module_nb);
1300 //ust// __initcall(init_markers);
1301 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1303 #endif /* CONFIG_MODULES */
1305 void ltt_dump_marker_state(struct ust_trace
*trace
)
1307 struct marker_entry
*entry
;
1308 struct ltt_probe_private_data call_data
;
1309 struct hlist_head
*head
;
1310 struct hlist_node
*node
;
1313 mutex_lock(&markers_mutex
);
1314 call_data
.trace
= trace
;
1315 call_data
.serializer
= NULL
;
1317 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1318 head
= &marker_table
[i
];
1319 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1320 __trace_mark(0, metadata
, core_marker_id
,
1322 "channel %s name %s event_id %hu "
1323 "int #1u%zu long #1u%zu pointer #1u%zu "
1324 "size_t #1u%zu alignment #1u%u",
1328 sizeof(int), sizeof(long),
1329 sizeof(void *), sizeof(size_t),
1330 ltt_get_alignment());
1332 __trace_mark(0, metadata
,
1335 "channel %s name %s format %s",
1341 mutex_unlock(&markers_mutex
);
1343 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1345 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1347 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1352 static void new_markers(struct marker
*start
, struct marker
*end
)
1356 for(m
=start
; m
< end
; m
++) {
1362 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1366 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1368 pl
->markers_start
= markers_start
;
1369 pl
->markers_count
= markers_count
;
1371 /* FIXME: maybe protect this with its own mutex? */
1373 list_add(&pl
->list
, &libs
);
1376 new_markers(markers_start
, markers_start
+ markers_count
);
1378 /* FIXME: update just the loaded lib */
1379 lib_update_markers();
1381 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1386 int marker_unregister_lib(struct marker
*markers_start
)
1390 /*FIXME: implement; but before implementing, marker_register_lib must
1391 have appropriate locking. */
1395 /* FIXME: we should probably take a mutex here on libs */
1396 //ust// mutex_lock(&module_mutex);
1397 list_for_each_entry(lib
, &libs
, list
) {
1398 if(lib
->markers_start
== markers_start
) {
1399 struct lib
*lib2free
= lib
;
1400 list_del(&lib
->list
);
1411 static int initialized
= 0;
1413 void __attribute__((constructor
)) init_markers(void)
1416 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1421 void __attribute__((constructor
)) destroy_markers(void)
1423 marker_unregister_lib(__start___markers
);