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
24 #include <ust/kernelcompat.h>
25 #include <ust/marker.h>
29 #include "tracercore.h"
32 __thread
long ust_reg_stack
[500];
33 volatile __thread
long *ust_reg_stack_ptr
= (long *) 0;
35 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
36 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
38 /* Set to 1 to enable marker debug output */
39 static const int marker_debug
;
42 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
43 * and module markers and the hash table.
45 static DEFINE_MUTEX(markers_mutex
);
47 static LIST_HEAD(libs
);
50 void lock_markers(void)
52 mutex_lock(&markers_mutex
);
55 void unlock_markers(void)
57 mutex_unlock(&markers_mutex
);
61 * Marker hash table, containing the active markers.
62 * Protected by module_mutex.
64 #define MARKER_HASH_BITS 6
65 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
66 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
70 * It is used to make sure every handler has finished using its private data
71 * between two consecutive operation (add or remove) on a given marker. It is
72 * also used to delay the free of multiple probes array until a quiescent state
74 * marker entries modifications are protected by the markers_mutex.
77 struct hlist_node hlist
;
81 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
82 struct marker_probe_closure single
;
83 struct marker_probe_closure
*multi
;
84 int refcount
; /* Number of times armed. 0 if disarmed. */
90 unsigned char ptype
:1;
91 unsigned char format_allocated
:1;
92 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
95 #ifdef CONFIG_MARKERS_USERSPACE
96 static void marker_update_processes(void);
98 static void marker_update_processes(void)
104 * __mark_empty_function - Empty probe callback
105 * @mdata: marker data
106 * @probe_private: probe private data
107 * @call_private: call site private data
108 * @fmt: format string
109 * @...: variable argument list
111 * Empty callback provided as a probe to the markers. By providing this to a
112 * disabled marker, we make sure the execution flow is always valid even
113 * though the function pointer change and the marker enabling are two distinct
114 * operations that modifies the execution flow of preemptible code.
116 notrace
void __mark_empty_function(const struct marker
*mdata
,
117 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
120 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
123 * marker_probe_cb Callback that prepares the variable argument list for probes.
124 * @mdata: pointer of type struct marker
125 * @call_private: caller site private data
126 * @...: Variable argument list.
128 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
129 * need to put a full smp_rmb() in this branch. This is why we do not use
130 * rcu_dereference() for the pointer read.
132 notrace
void marker_probe_cb(const struct marker
*mdata
,
133 void *call_private
, struct registers
*regs
, ...)
139 * rcu_read_lock_sched does two things : disabling preemption to make
140 * sure the teardown of the callbacks can be done correctly when they
141 * are in modules and they insure RCU read coherency.
143 //ust// rcu_read_lock_sched_notrace();
144 ptype
= mdata
->ptype
;
145 if (likely(!ptype
)) {
146 marker_probe_func
*func
;
147 /* Must read the ptype before ptr. They are not data dependant,
148 * so we put an explicit smp_rmb() here. */
150 func
= mdata
->single
.func
;
151 /* Must read the ptr before private data. They are not data
152 * dependant, so we put an explicit smp_rmb() here. */
154 va_start(args
, regs
);
155 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
156 mdata
->format
, &args
);
159 struct marker_probe_closure
*multi
;
162 * Read mdata->ptype before mdata->multi.
165 multi
= mdata
->multi
;
167 * multi points to an array, therefore accessing the array
168 * depends on reading multi. However, even in this case,
169 * we must insure that the pointer is read _before_ the array
170 * data. Same as rcu_dereference, but we need a full smp_rmb()
171 * in the fast path, so put the explicit barrier here.
173 smp_read_barrier_depends();
174 for (i
= 0; multi
[i
].func
; i
++) {
175 va_start(args
, regs
);
176 multi
[i
].func(mdata
, multi
[i
].probe_private
,
177 regs
, call_private
, mdata
->format
, &args
);
181 //ust// rcu_read_unlock_sched_notrace();
183 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
186 * marker_probe_cb Callback that does not prepare the variable argument list.
187 * @mdata: pointer of type struct marker
188 * @call_private: caller site private data
189 * @...: Variable argument list.
191 * Should be connected to markers "MARK_NOARGS".
193 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
194 void *call_private
, struct registers
*regs
, ...)
196 va_list args
; /* not initialized */
199 //ust// rcu_read_lock_sched_notrace();
200 ptype
= mdata
->ptype
;
201 if (likely(!ptype
)) {
202 marker_probe_func
*func
;
203 /* Must read the ptype before ptr. They are not data dependant,
204 * so we put an explicit smp_rmb() here. */
206 func
= mdata
->single
.func
;
207 /* Must read the ptr before private data. They are not data
208 * dependant, so we put an explicit smp_rmb() here. */
210 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
211 mdata
->format
, &args
);
213 struct marker_probe_closure
*multi
;
216 * Read mdata->ptype before mdata->multi.
219 multi
= mdata
->multi
;
221 * multi points to an array, therefore accessing the array
222 * depends on reading multi. However, even in this case,
223 * we must insure that the pointer is read _before_ the array
224 * data. Same as rcu_dereference, but we need a full smp_rmb()
225 * in the fast path, so put the explicit barrier here.
227 smp_read_barrier_depends();
228 for (i
= 0; multi
[i
].func
; i
++)
229 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
230 call_private
, mdata
->format
, &args
);
232 //ust// rcu_read_unlock_sched_notrace();
235 static void free_old_closure(struct rcu_head
*head
)
237 struct marker_entry
*entry
= container_of(head
,
238 struct marker_entry
, rcu
);
240 /* Make sure we free the data before setting the pending flag to 0 */
242 entry
->rcu_pending
= 0;
245 static void debug_print_probes(struct marker_entry
*entry
)
253 DBG("Single probe : %p %p",
255 entry
->single
.probe_private
);
257 for (i
= 0; entry
->multi
[i
].func
; i
++)
258 DBG("Multi probe %d : %p %p", i
,
259 entry
->multi
[i
].func
,
260 entry
->multi
[i
].probe_private
);
264 static struct marker_probe_closure
*
265 marker_entry_add_probe(struct marker_entry
*entry
,
266 marker_probe_func
*probe
, void *probe_private
)
269 struct marker_probe_closure
*old
, *new;
273 debug_print_probes(entry
);
276 if (entry
->single
.func
== probe
&&
277 entry
->single
.probe_private
== probe_private
)
278 return ERR_PTR(-EBUSY
);
279 if (entry
->single
.func
== __mark_empty_function
) {
281 entry
->single
.func
= probe
;
282 entry
->single
.probe_private
= probe_private
;
285 debug_print_probes(entry
);
293 /* (N -> N+1), (N != 0, 1) probes */
294 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
295 if (old
[nr_probes
].func
== probe
296 && old
[nr_probes
].probe_private
298 return ERR_PTR(-EBUSY
);
300 /* + 2 : one for new probe, one for NULL func */
301 new = zmalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
));
303 return ERR_PTR(-ENOMEM
);
305 new[0] = entry
->single
;
308 nr_probes
* sizeof(struct marker_probe_closure
));
309 new[nr_probes
].func
= probe
;
310 new[nr_probes
].probe_private
= probe_private
;
311 entry
->refcount
= nr_probes
+ 1;
314 debug_print_probes(entry
);
318 static struct marker_probe_closure
*
319 marker_entry_remove_probe(struct marker_entry
*entry
,
320 marker_probe_func
*probe
, void *probe_private
)
322 int nr_probes
= 0, nr_del
= 0, i
;
323 struct marker_probe_closure
*old
, *new;
327 debug_print_probes(entry
);
329 /* 0 -> N is an error */
330 WARN_ON(entry
->single
.func
== __mark_empty_function
);
332 WARN_ON(probe
&& entry
->single
.func
!= probe
);
333 WARN_ON(entry
->single
.probe_private
!= probe_private
);
334 entry
->single
.func
= __mark_empty_function
;
337 debug_print_probes(entry
);
340 /* (N -> M), (N > 1, M >= 0) probes */
341 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
342 if ((!probe
|| old
[nr_probes
].func
== probe
)
343 && old
[nr_probes
].probe_private
349 if (nr_probes
- nr_del
== 0) {
350 /* N -> 0, (N > 1) */
351 entry
->single
.func
= __mark_empty_function
;
354 } else if (nr_probes
- nr_del
== 1) {
355 /* N -> 1, (N > 1) */
356 for (i
= 0; old
[i
].func
; i
++)
357 if ((probe
&& old
[i
].func
!= probe
) ||
358 old
[i
].probe_private
!= probe_private
)
359 entry
->single
= old
[i
];
364 /* N -> M, (N > 1, M > 1) */
366 new = zmalloc((nr_probes
- nr_del
+ 1) * sizeof(struct marker_probe_closure
));
368 return ERR_PTR(-ENOMEM
);
369 for (i
= 0; old
[i
].func
; i
++)
370 if ((probe
&& old
[i
].func
!= probe
) ||
371 old
[i
].probe_private
!= probe_private
)
373 entry
->refcount
= nr_probes
- nr_del
;
377 debug_print_probes(entry
);
382 * Get marker if the marker is present in the marker hash table.
383 * Must be called with markers_mutex held.
384 * Returns NULL if not present.
386 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
388 struct hlist_head
*head
;
389 struct hlist_node
*node
;
390 struct marker_entry
*e
;
391 size_t channel_len
= strlen(channel
) + 1;
392 size_t name_len
= strlen(name
) + 1;
395 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
396 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
397 hlist_for_each_entry(e
, node
, head
, hlist
) {
398 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
405 * Add the marker to the marker hash table. Must be called with markers_mutex
408 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
411 struct hlist_head
*head
;
412 struct hlist_node
*node
;
413 struct marker_entry
*e
;
414 size_t channel_len
= strlen(channel
) + 1;
415 size_t name_len
= strlen(name
) + 1;
416 size_t format_len
= 0;
419 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
421 format_len
= strlen(format
) + 1;
422 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
423 hlist_for_each_entry(e
, node
, head
, hlist
) {
424 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
425 DBG("Marker %s.%s busy", channel
, name
);
426 return ERR_PTR(-EBUSY
); /* Already there */
430 * Using malloc here to allocate a variable length element. Could
431 * cause some memory fragmentation if overused.
433 e
= malloc(sizeof(struct marker_entry
)
434 + channel_len
+ name_len
+ format_len
);
436 return ERR_PTR(-ENOMEM
);
437 memcpy(e
->channel
, channel
, channel_len
);
438 e
->name
= &e
->channel
[channel_len
];
439 memcpy(e
->name
, name
, name_len
);
441 e
->format
= &e
->name
[channel_len
+ name_len
];
442 memcpy(e
->format
, format
, format_len
);
443 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
444 e
->call
= marker_probe_cb_noarg
;
446 e
->call
= marker_probe_cb
;
447 trace_mark(metadata
, core_marker_format
,
448 "channel %s name %s format %s",
449 e
->channel
, e
->name
, e
->format
);
452 e
->call
= marker_probe_cb
;
454 e
->single
.func
= __mark_empty_function
;
455 e
->single
.probe_private
= NULL
;
458 e
->format_allocated
= 0;
461 hlist_add_head(&e
->hlist
, head
);
466 * Remove the marker from the marker hash table. Must be called with mutex_lock
469 static int remove_marker(const char *channel
, const char *name
)
471 struct hlist_head
*head
;
472 struct hlist_node
*node
;
473 struct marker_entry
*e
;
475 size_t channel_len
= strlen(channel
) + 1;
476 size_t name_len
= strlen(name
) + 1;
480 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
481 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
482 hlist_for_each_entry(e
, node
, head
, hlist
) {
483 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
490 if (e
->single
.func
!= __mark_empty_function
)
492 hlist_del(&e
->hlist
);
493 if (e
->format_allocated
)
495 ret
= ltt_channels_unregister(e
->channel
);
497 /* Make sure the call_rcu has been executed */
498 //ust// if (e->rcu_pending)
499 //ust// rcu_barrier_sched();
505 * Set the mark_entry format to the format found in the element.
507 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
509 entry
->format
= strdup(format
);
512 entry
->format_allocated
= 1;
514 trace_mark(metadata
, core_marker_format
,
515 "channel %s name %s format %s",
516 entry
->channel
, entry
->name
, entry
->format
);
521 * Sets the probe callback corresponding to one marker.
523 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
527 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
530 if (strcmp(entry
->format
, elem
->format
) != 0) {
531 DBG("Format mismatch for probe %s (%s), marker (%s)",
538 ret
= marker_set_format(entry
, elem
->format
);
544 * probe_cb setup (statically known) is done here. It is
545 * asynchronous with the rest of execution, therefore we only
546 * pass from a "safe" callback (with argument) to an "unsafe"
547 * callback (does not set arguments).
549 elem
->call
= entry
->call
;
550 elem
->channel_id
= entry
->channel_id
;
551 elem
->event_id
= entry
->event_id
;
554 * We only update the single probe private data when the ptr is
555 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
557 WARN_ON(elem
->single
.func
!= __mark_empty_function
558 && elem
->single
.probe_private
!= entry
->single
.probe_private
560 elem
->single
.probe_private
= entry
->single
.probe_private
;
562 * Make sure the private data is valid when we update the
566 elem
->single
.func
= entry
->single
.func
;
568 * We also make sure that the new probe callbacks array is consistent
569 * before setting a pointer to it.
571 rcu_assign_pointer(elem
->multi
, entry
->multi
);
573 * Update the function or multi probe array pointer before setting the
577 elem
->ptype
= entry
->ptype
;
579 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
580 //ust// WARN_ON(!elem->tp_cb);
582 //ust// * It is ok to directly call the probe registration because type
583 //ust// * checking has been done in the __trace_mark_tp() macro.
586 //ust// if (active) {
588 //ust// * try_module_get should always succeed because we hold
589 //ust// * markers_mutex to get the tp_cb address.
591 //ust// ret = try_module_get(__module_text_address(
592 //ust// (unsigned long)elem->tp_cb));
593 //ust// BUG_ON(!ret);
594 //ust// ret = tracepoint_probe_register_noupdate(
595 //ust// elem->tp_name,
596 //ust// elem->tp_cb);
598 //ust// ret = tracepoint_probe_unregister_noupdate(
599 //ust// elem->tp_name,
600 //ust// elem->tp_cb);
602 //ust// * tracepoint_probe_update_all() must be called
603 //ust// * before the module containing tp_cb is unloaded.
605 //ust// module_put(__module_text_address(
606 //ust// (unsigned long)elem->tp_cb));
609 elem
->state__imv
= active
;
615 * Disable a marker and its probe callback.
616 * Note: only waiting an RCU period after setting elem->call to the empty
617 * function insures that the original callback is not used anymore. This insured
618 * by rcu_read_lock_sched around the call site.
620 static void disable_marker(struct marker
*elem
)
624 //ust// /* leave "call" as is. It is known statically. */
625 //ust// if (elem->tp_name && _imv_read(elem->state)) {
626 //ust// WARN_ON(!elem->tp_cb);
628 //ust// * It is ok to directly call the probe registration because type
629 //ust// * checking has been done in the __trace_mark_tp() macro.
631 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
632 //ust// elem->tp_cb);
633 //ust// WARN_ON(ret);
635 //ust// * tracepoint_probe_update_all() must be called
636 //ust// * before the module containing tp_cb is unloaded.
638 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
640 elem
->state__imv
= 0;
641 elem
->single
.func
= __mark_empty_function
;
642 /* Update the function before setting the ptype */
644 elem
->ptype
= 0; /* single probe */
646 * Leave the private data and channel_id/event_id there, because removal
647 * is racy and should be done only after an RCU period. These are never
648 * used until the next initialization anyway.
653 * is_marker_enabled - Check if a marker is enabled
654 * @channel: channel name
657 * Returns 1 if the marker is enabled, 0 if disabled.
659 int is_marker_enabled(const char *channel
, const char *name
)
661 struct marker_entry
*entry
;
663 mutex_lock(&markers_mutex
);
664 entry
= get_marker(channel
, name
);
665 mutex_unlock(&markers_mutex
);
667 return entry
&& !!entry
->refcount
;
671 * marker_update_probe_range - Update a probe range
672 * @begin: beginning of the range
673 * @end: end of the range
675 * Updates the probe callback corresponding to a range of markers.
677 void marker_update_probe_range(struct marker
*begin
,
681 struct marker_entry
*mark_entry
;
683 mutex_lock(&markers_mutex
);
684 for (iter
= begin
; iter
< end
; iter
++) {
685 mark_entry
= get_marker(iter
->channel
, iter
->name
);
687 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
689 * ignore error, continue
692 /* This is added for UST. We emit a core_marker_id event
693 * for markers that are already registered to a probe
694 * upon library load. Otherwise, no core_marker_id will
695 * be generated for these markers. Is this the right thing
698 trace_mark(metadata
, core_marker_id
,
699 "channel %s name %s event_id %hu "
700 "int #1u%zu long #1u%zu pointer #1u%zu "
701 "size_t #1u%zu alignment #1u%u",
702 iter
->channel
, iter
->name
, mark_entry
->event_id
,
703 sizeof(int), sizeof(long), sizeof(void *),
704 sizeof(size_t), ltt_get_alignment());
706 disable_marker(iter
);
709 mutex_unlock(&markers_mutex
);
712 static void lib_update_markers(void)
716 /* FIXME: we should probably take a mutex here on libs */
717 //ust// mutex_lock(&module_mutex);
718 list_for_each_entry(lib
, &libs
, list
)
719 marker_update_probe_range(lib
->markers_start
,
720 lib
->markers_start
+ lib
->markers_count
);
721 //ust// mutex_unlock(&module_mutex);
725 * Update probes, removing the faulty probes.
727 * Internal callback only changed before the first probe is connected to it.
728 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
729 * transitions. All other transitions will leave the old private data valid.
730 * This makes the non-atomicity of the callback/private data updates valid.
732 * "special case" updates :
737 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
738 * Site effect : marker_set_format may delete the marker entry (creating a
741 static void marker_update_probes(void)
743 /* Core kernel markers */
744 //ust// marker_update_probe_range(__start___markers, __stop___markers);
745 /* Markers in modules. */
746 //ust// module_update_markers();
747 lib_update_markers();
748 //ust// tracepoint_probe_update_all();
749 /* Update immediate values */
751 //ust// module_imv_update(); /* FIXME: need to port for libs? */
752 marker_update_processes();
756 * marker_probe_register - Connect a probe to a marker
757 * @channel: marker channel
759 * @format: format string
760 * @probe: probe handler
761 * @probe_private: probe private data
763 * private data must be a valid allocated memory address, or NULL.
764 * Returns 0 if ok, error value on error.
765 * The probe address must at least be aligned on the architecture pointer size.
767 int marker_probe_register(const char *channel
, const char *name
,
768 const char *format
, marker_probe_func
*probe
,
771 struct marker_entry
*entry
;
772 int ret
= 0, ret_err
;
773 struct marker_probe_closure
*old
;
776 mutex_lock(&markers_mutex
);
777 entry
= get_marker(channel
, name
);
780 entry
= add_marker(channel
, name
, format
);
782 ret
= PTR_ERR(entry
);
785 ret
= ltt_channels_register(channel
);
787 goto error_remove_marker
;
788 ret
= ltt_channels_get_index_from_name(channel
);
790 goto error_unregister_channel
;
791 entry
->channel_id
= ret
;
792 ret
= ltt_channels_get_event_id(channel
, name
);
794 goto error_unregister_channel
;
795 entry
->event_id
= ret
;
797 trace_mark(metadata
, core_marker_id
,
798 "channel %s name %s event_id %hu "
799 "int #1u%zu long #1u%zu pointer #1u%zu "
800 "size_t #1u%zu alignment #1u%u",
801 channel
, name
, entry
->event_id
,
802 sizeof(int), sizeof(long), sizeof(void *),
803 sizeof(size_t), ltt_get_alignment());
806 ret
= marker_set_format(entry
, format
);
807 else if (strcmp(entry
->format
, format
))
814 * If we detect that a call_rcu is pending for this marker,
815 * make sure it's executed now.
817 //ust// if (entry->rcu_pending)
818 //ust// rcu_barrier_sched();
819 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
823 goto error_unregister_channel
;
827 mutex_unlock(&markers_mutex
);
829 /* Activate marker if necessary */
830 marker_update_probes();
832 mutex_lock(&markers_mutex
);
833 entry
= get_marker(channel
, name
);
836 //ust// if (entry->rcu_pending)
837 //ust// rcu_barrier_sched();
839 entry
->rcu_pending
= 1;
840 /* write rcu_pending before calling the RCU callback */
842 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
843 synchronize_rcu(); free_old_closure(&entry
->rcu
);
846 error_unregister_channel
:
847 ret_err
= ltt_channels_unregister(channel
);
850 ret_err
= remove_marker(channel
, name
);
853 mutex_unlock(&markers_mutex
);
856 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
859 * marker_probe_unregister - Disconnect a probe from a marker
860 * @channel: marker channel
862 * @probe: probe function pointer
863 * @probe_private: probe private data
865 * Returns the private data given to marker_probe_register, or an ERR_PTR().
866 * We do not need to call a synchronize_sched to make sure the probes have
867 * finished running before doing a module unload, because the module unload
868 * itself uses stop_machine(), which insures that every preempt disabled section
871 int marker_probe_unregister(const char *channel
, const char *name
,
872 marker_probe_func
*probe
, void *probe_private
)
874 struct marker_entry
*entry
;
875 struct marker_probe_closure
*old
;
878 mutex_lock(&markers_mutex
);
879 entry
= get_marker(channel
, name
);
882 //ust// if (entry->rcu_pending)
883 //ust// rcu_barrier_sched();
884 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
885 mutex_unlock(&markers_mutex
);
887 marker_update_probes();
889 mutex_lock(&markers_mutex
);
890 entry
= get_marker(channel
, name
);
893 //ust// if (entry->rcu_pending)
894 //ust// rcu_barrier_sched();
896 entry
->rcu_pending
= 1;
897 /* write rcu_pending before calling the RCU callback */
899 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
900 synchronize_rcu(); free_old_closure(&entry
->rcu
);
901 remove_marker(channel
, name
); /* Ignore busy error message */
904 mutex_unlock(&markers_mutex
);
907 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
909 static struct marker_entry
*
910 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
912 struct marker_entry
*entry
;
914 struct hlist_head
*head
;
915 struct hlist_node
*node
;
917 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
918 head
= &marker_table
[i
];
919 hlist_for_each_entry(entry
, node
, head
, hlist
) {
921 if (entry
->single
.func
== probe
922 && entry
->single
.probe_private
926 struct marker_probe_closure
*closure
;
927 closure
= entry
->multi
;
928 for (i
= 0; closure
[i
].func
; i
++) {
929 if (closure
[i
].func
== probe
&&
930 closure
[i
].probe_private
941 * marker_probe_unregister_private_data - Disconnect a probe from a marker
942 * @probe: probe function
943 * @probe_private: probe private data
945 * Unregister a probe by providing the registered private data.
946 * Only removes the first marker found in hash table.
947 * Return 0 on success or error value.
948 * We do not need to call a synchronize_sched to make sure the probes have
949 * finished running before doing a module unload, because the module unload
950 * itself uses stop_machine(), which insures that every preempt disabled section
953 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
956 struct marker_entry
*entry
;
958 struct marker_probe_closure
*old
;
959 char *channel
= NULL
, *name
= NULL
;
961 mutex_lock(&markers_mutex
);
962 entry
= get_marker_from_private_data(probe
, probe_private
);
967 //ust// if (entry->rcu_pending)
968 //ust// rcu_barrier_sched();
969 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
970 channel
= strdup(entry
->channel
);
971 name
= strdup(entry
->name
);
972 mutex_unlock(&markers_mutex
);
974 marker_update_probes();
976 mutex_lock(&markers_mutex
);
977 entry
= get_marker(channel
, name
);
980 //ust// if (entry->rcu_pending)
981 //ust// rcu_barrier_sched();
983 entry
->rcu_pending
= 1;
984 /* write rcu_pending before calling the RCU callback */
986 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
987 synchronize_rcu(); free_old_closure(&entry
->rcu
);
988 /* Ignore busy error message */
989 remove_marker(channel
, name
);
991 mutex_unlock(&markers_mutex
);
996 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
999 * marker_get_private_data - Get a marker's probe private data
1000 * @channel: marker channel
1001 * @name: marker name
1002 * @probe: probe to match
1003 * @num: get the nth matching probe's private data
1005 * Returns the nth private data pointer (starting from 0) matching, or an
1007 * Returns the private data pointer, or an ERR_PTR.
1008 * The private data pointer should _only_ be dereferenced if the caller is the
1009 * owner of the data, or its content could vanish. This is mostly used to
1010 * confirm that a caller is the owner of a registered probe.
1012 void *marker_get_private_data(const char *channel
, const char *name
,
1013 marker_probe_func
*probe
, int num
)
1015 struct hlist_head
*head
;
1016 struct hlist_node
*node
;
1017 struct marker_entry
*e
;
1018 size_t channel_len
= strlen(channel
) + 1;
1019 size_t name_len
= strlen(name
) + 1;
1023 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1024 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1025 hlist_for_each_entry(e
, node
, head
, hlist
) {
1026 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1028 if (num
== 0 && e
->single
.func
== probe
)
1029 return e
->single
.probe_private
;
1031 struct marker_probe_closure
*closure
;
1034 for (i
= 0; closure
[i
].func
; i
++) {
1035 if (closure
[i
].func
!= probe
)
1038 return closure
[i
].probe_private
;
1044 return ERR_PTR(-ENOENT
);
1046 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1049 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1051 * Called when no channel users are active by the channel infrastructure.
1052 * Called with lock_markers() and channel mutex held.
1054 //ust// void markers_compact_event_ids(void)
1056 //ust// struct marker_entry *entry;
1057 //ust// unsigned int i;
1058 //ust// struct hlist_head *head;
1059 //ust// struct hlist_node *node;
1062 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1063 //ust// head = &marker_table[i];
1064 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1065 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1066 //ust// WARN_ON(ret < 0);
1067 //ust// entry->channel_id = ret;
1068 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1069 //ust// entry->name);
1070 //ust// WARN_ON(ret < 0);
1071 //ust// entry->event_id = ret;
1076 //ust//#ifdef CONFIG_MODULES
1079 * Returns 0 if current not found.
1080 * Returns 1 if current found.
1082 int lib_get_iter_markers(struct marker_iter
*iter
)
1084 struct lib
*iter_lib
;
1087 //ust// mutex_lock(&module_mutex);
1088 list_for_each_entry(iter_lib
, &libs
, list
) {
1089 if (iter_lib
< iter
->lib
)
1091 else if (iter_lib
> iter
->lib
)
1092 iter
->marker
= NULL
;
1093 found
= marker_get_iter_range(&iter
->marker
,
1094 iter_lib
->markers_start
,
1095 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1097 iter
->lib
= iter_lib
;
1101 //ust// mutex_unlock(&module_mutex);
1106 * marker_get_iter_range - Get a next marker iterator given a range.
1107 * @marker: current markers (in), next marker (out)
1108 * @begin: beginning of the range
1109 * @end: end of the range
1111 * Returns whether a next marker has been found (1) or not (0).
1112 * Will return the first marker in the range if the input marker is NULL.
1114 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1117 if (!*marker
&& begin
!= end
) {
1121 if (*marker
>= begin
&& *marker
< end
)
1125 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1127 static void marker_get_iter(struct marker_iter
*iter
)
1131 /* Core kernel markers */
1133 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1134 found
= marker_get_iter_range(&iter
->marker
,
1135 __start___markers
, __stop___markers
);
1139 /* Markers in modules. */
1140 found
= lib_get_iter_markers(iter
);
1143 marker_iter_reset(iter
);
1146 void marker_iter_start(struct marker_iter
*iter
)
1148 marker_get_iter(iter
);
1150 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1152 void marker_iter_next(struct marker_iter
*iter
)
1156 * iter->marker may be invalid because we blindly incremented it.
1157 * Make sure it is valid by marshalling on the markers, getting the
1158 * markers from following modules if necessary.
1160 marker_get_iter(iter
);
1162 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1164 void marker_iter_stop(struct marker_iter
*iter
)
1167 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1169 void marker_iter_reset(struct marker_iter
*iter
)
1172 iter
->marker
= NULL
;
1174 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1176 #ifdef CONFIG_MARKERS_USERSPACE
1178 * must be called with current->user_markers_mutex held
1180 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1182 struct user_marker
*umark
;
1183 struct hlist_node
*pos
, *n
;
1185 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1186 if (umark
->state
== state
) {
1187 hlist_del(&umark
->hlist
);
1194 * Update current process.
1195 * Note that we have to wait a whole scheduler period before we are sure that
1196 * every running userspace threads have their markers updated.
1197 * (synchronize_sched() can be used to insure this).
1199 //ust// void marker_update_process(void)
1201 //ust// struct user_marker *umark;
1202 //ust// struct hlist_node *pos;
1203 //ust// struct marker_entry *entry;
1205 //ust// mutex_lock(&markers_mutex);
1206 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1207 //ust// if (strcmp(current->comm, "testprog") == 0)
1208 //ust// DBG("do update pending for testprog");
1209 //ust// hlist_for_each_entry(umark, pos,
1210 //ust// ¤t->group_leader->user_markers, hlist) {
1211 //ust// DBG("Updating marker %s in %s", umark->name, current->comm);
1212 //ust// entry = get_marker("userspace", umark->name);
1213 //ust// if (entry) {
1214 //ust// if (entry->format &&
1215 //ust// strcmp(entry->format, umark->format) != 0) {
1216 //ust// WARN("error, wrong format in process %s",
1217 //ust// current->comm);
1220 //ust// if (put_user(!!entry->refcount, umark->state)) {
1221 //ust// WARN("Marker in %s caused a fault",
1222 //ust// current->comm);
1226 //ust// if (put_user(0, umark->state)) {
1227 //ust// WARN("Marker in %s caused a fault", current->comm);
1232 //ust// clear_thread_flag(TIF_MARKER_PENDING);
1233 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1234 //ust// mutex_unlock(&markers_mutex);
1238 * Called at process exit and upon do_execve().
1239 * We assume that when the leader exits, no more references can be done to the
1240 * leader structure by the other threads.
1242 void exit_user_markers(struct task_struct
*p
)
1244 struct user_marker
*umark
;
1245 struct hlist_node
*pos
, *n
;
1247 if (thread_group_leader(p
)) {
1248 mutex_lock(&markers_mutex
);
1249 mutex_lock(&p
->user_markers_mutex
);
1250 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1253 INIT_HLIST_HEAD(&p
->user_markers
);
1254 p
->user_markers_sequence
++;
1255 mutex_unlock(&p
->user_markers_mutex
);
1256 mutex_unlock(&markers_mutex
);
1260 int is_marker_enabled(const char *channel
, const char *name
)
1262 struct marker_entry
*entry
;
1264 mutex_lock(&markers_mutex
);
1265 entry
= get_marker(channel
, name
);
1266 mutex_unlock(&markers_mutex
);
1268 return entry
&& !!entry
->refcount
;
1272 int marker_module_notify(struct notifier_block
*self
,
1273 unsigned long val
, void *data
)
1275 struct module
*mod
= data
;
1278 case MODULE_STATE_COMING
:
1279 marker_update_probe_range(mod
->markers
,
1280 mod
->markers
+ mod
->num_markers
);
1282 case MODULE_STATE_GOING
:
1283 marker_update_probe_range(mod
->markers
,
1284 mod
->markers
+ mod
->num_markers
);
1290 struct notifier_block marker_module_nb
= {
1291 .notifier_call
= marker_module_notify
,
1295 //ust// static int init_markers(void)
1297 //ust// return register_module_notifier(&marker_module_nb);
1299 //ust// __initcall(init_markers);
1300 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1302 #endif /* CONFIG_MODULES */
1304 void ltt_dump_marker_state(struct ust_trace
*trace
)
1306 struct marker_entry
*entry
;
1307 struct ltt_probe_private_data call_data
;
1308 struct hlist_head
*head
;
1309 struct hlist_node
*node
;
1312 mutex_lock(&markers_mutex
);
1313 call_data
.trace
= trace
;
1314 call_data
.serializer
= NULL
;
1316 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1317 head
= &marker_table
[i
];
1318 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1319 __trace_mark(0, metadata
, core_marker_id
,
1321 "channel %s name %s event_id %hu "
1322 "int #1u%zu long #1u%zu pointer #1u%zu "
1323 "size_t #1u%zu alignment #1u%u",
1327 sizeof(int), sizeof(long),
1328 sizeof(void *), sizeof(size_t),
1329 ltt_get_alignment());
1331 __trace_mark(0, metadata
,
1334 "channel %s name %s format %s",
1340 mutex_unlock(&markers_mutex
);
1342 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1344 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1346 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1351 static void new_markers(struct marker
*start
, struct marker
*end
)
1355 for(m
=start
; m
< end
; m
++) {
1361 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1365 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1367 pl
->markers_start
= markers_start
;
1368 pl
->markers_count
= markers_count
;
1370 /* FIXME: maybe protect this with its own mutex? */
1372 list_add(&pl
->list
, &libs
);
1375 new_markers(markers_start
, markers_start
+ markers_count
);
1377 /* FIXME: update just the loaded lib */
1378 lib_update_markers();
1380 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1385 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1387 /*FIXME: implement; but before implementing, marker_register_lib must
1388 have appropriate locking. */
1393 static int initialized
= 0;
1395 void __attribute__((constructor
)) init_markers(void)
1398 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1399 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);