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
18 //ust// #include <linux/module.h>
19 //ust// #include <linux/mutex.h>
20 //ust// #include <linux/types.h>
23 //#include "rcupdate.h"
24 //ust// #include <linux/marker.h>
26 //ust// #include <linux/slab.h>
27 //ust// #include <linux/immediate.h>
28 //ust// #include <linux/sched.h>
29 //ust// #include <linux/uaccess.h>
30 //ust// #include <linux/user_marker.h>
31 //ust// #include <linux/ltt-tracer.h>
36 #include <ust/kernelcompat.h>
38 #include <ust/marker.h>
41 #include "tracercore.h"
44 __thread
long ust_reg_stack
[500];
45 volatile __thread
long *ust_reg_stack_ptr
= (long *) 0;
47 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
48 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
50 /* Set to 1 to enable marker debug output */
51 static const int marker_debug
;
54 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
55 * and module markers and the hash table.
57 static DEFINE_MUTEX(markers_mutex
);
59 static LIST_HEAD(libs
);
62 void lock_markers(void)
64 mutex_lock(&markers_mutex
);
67 void unlock_markers(void)
69 mutex_unlock(&markers_mutex
);
73 * Marker hash table, containing the active markers.
74 * Protected by module_mutex.
76 #define MARKER_HASH_BITS 6
77 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
78 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
82 * It is used to make sure every handler has finished using its private data
83 * between two consecutive operation (add or remove) on a given marker. It is
84 * also used to delay the free of multiple probes array until a quiescent state
86 * marker entries modifications are protected by the markers_mutex.
89 struct hlist_node hlist
;
93 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
94 struct marker_probe_closure single
;
95 struct marker_probe_closure
*multi
;
96 int refcount
; /* Number of times armed. 0 if disarmed. */
102 unsigned char ptype
:1;
103 unsigned char format_allocated
:1;
104 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
107 #ifdef CONFIG_MARKERS_USERSPACE
108 static void marker_update_processes(void);
110 static void marker_update_processes(void)
116 * __mark_empty_function - Empty probe callback
117 * @mdata: marker data
118 * @probe_private: probe private data
119 * @call_private: call site private data
120 * @fmt: format string
121 * @...: variable argument list
123 * Empty callback provided as a probe to the markers. By providing this to a
124 * disabled marker, we make sure the execution flow is always valid even
125 * though the function pointer change and the marker enabling are two distinct
126 * operations that modifies the execution flow of preemptible code.
128 notrace
void __mark_empty_function(const struct marker
*mdata
,
129 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
132 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
135 * marker_probe_cb Callback that prepares the variable argument list for probes.
136 * @mdata: pointer of type struct marker
137 * @call_private: caller site private data
138 * @...: Variable argument list.
140 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
141 * need to put a full smp_rmb() in this branch. This is why we do not use
142 * rcu_dereference() for the pointer read.
144 notrace
void marker_probe_cb(const struct marker
*mdata
,
145 void *call_private
, struct registers
*regs
, ...)
151 * rcu_read_lock_sched does two things : disabling preemption to make
152 * sure the teardown of the callbacks can be done correctly when they
153 * are in modules and they insure RCU read coherency.
155 //ust// rcu_read_lock_sched_notrace();
156 ptype
= mdata
->ptype
;
157 if (likely(!ptype
)) {
158 marker_probe_func
*func
;
159 /* Must read the ptype before ptr. They are not data dependant,
160 * so we put an explicit smp_rmb() here. */
162 func
= mdata
->single
.func
;
163 /* Must read the ptr before private data. They are not data
164 * dependant, so we put an explicit smp_rmb() here. */
166 va_start(args
, regs
);
167 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
168 mdata
->format
, &args
);
171 struct marker_probe_closure
*multi
;
174 * Read mdata->ptype before mdata->multi.
177 multi
= mdata
->multi
;
179 * multi points to an array, therefore accessing the array
180 * depends on reading multi. However, even in this case,
181 * we must insure that the pointer is read _before_ the array
182 * data. Same as rcu_dereference, but we need a full smp_rmb()
183 * in the fast path, so put the explicit barrier here.
185 smp_read_barrier_depends();
186 for (i
= 0; multi
[i
].func
; i
++) {
187 va_start(args
, regs
);
188 multi
[i
].func(mdata
, multi
[i
].probe_private
,
189 regs
, call_private
, mdata
->format
, &args
);
193 //ust// rcu_read_unlock_sched_notrace();
195 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
198 * marker_probe_cb Callback that does not prepare the variable argument list.
199 * @mdata: pointer of type struct marker
200 * @call_private: caller site private data
201 * @...: Variable argument list.
203 * Should be connected to markers "MARK_NOARGS".
205 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
206 void *call_private
, struct registers
*regs
, ...)
208 va_list args
; /* not initialized */
211 //ust// rcu_read_lock_sched_notrace();
212 ptype
= mdata
->ptype
;
213 if (likely(!ptype
)) {
214 marker_probe_func
*func
;
215 /* Must read the ptype before ptr. They are not data dependant,
216 * so we put an explicit smp_rmb() here. */
218 func
= mdata
->single
.func
;
219 /* Must read the ptr before private data. They are not data
220 * dependant, so we put an explicit smp_rmb() here. */
222 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
223 mdata
->format
, &args
);
225 struct marker_probe_closure
*multi
;
228 * Read mdata->ptype before mdata->multi.
231 multi
= mdata
->multi
;
233 * multi points to an array, therefore accessing the array
234 * depends on reading multi. However, even in this case,
235 * we must insure that the pointer is read _before_ the array
236 * data. Same as rcu_dereference, but we need a full smp_rmb()
237 * in the fast path, so put the explicit barrier here.
239 smp_read_barrier_depends();
240 for (i
= 0; multi
[i
].func
; i
++)
241 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
242 call_private
, mdata
->format
, &args
);
244 //ust// rcu_read_unlock_sched_notrace();
247 static void free_old_closure(struct rcu_head
*head
)
249 struct marker_entry
*entry
= container_of(head
,
250 struct marker_entry
, rcu
);
251 kfree(entry
->oldptr
);
252 /* Make sure we free the data before setting the pending flag to 0 */
254 entry
->rcu_pending
= 0;
257 static void debug_print_probes(struct marker_entry
*entry
)
265 printk(KERN_DEBUG
"Single probe : %p %p\n",
267 entry
->single
.probe_private
);
269 for (i
= 0; entry
->multi
[i
].func
; i
++)
270 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
271 entry
->multi
[i
].func
,
272 entry
->multi
[i
].probe_private
);
276 static struct marker_probe_closure
*
277 marker_entry_add_probe(struct marker_entry
*entry
,
278 marker_probe_func
*probe
, void *probe_private
)
281 struct marker_probe_closure
*old
, *new;
285 debug_print_probes(entry
);
288 if (entry
->single
.func
== probe
&&
289 entry
->single
.probe_private
== probe_private
)
290 return ERR_PTR(-EBUSY
);
291 if (entry
->single
.func
== __mark_empty_function
) {
293 entry
->single
.func
= probe
;
294 entry
->single
.probe_private
= probe_private
;
297 debug_print_probes(entry
);
305 /* (N -> N+1), (N != 0, 1) probes */
306 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
307 if (old
[nr_probes
].func
== probe
308 && old
[nr_probes
].probe_private
310 return ERR_PTR(-EBUSY
);
312 /* + 2 : one for new probe, one for NULL func */
313 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
316 return ERR_PTR(-ENOMEM
);
318 new[0] = entry
->single
;
321 nr_probes
* sizeof(struct marker_probe_closure
));
322 new[nr_probes
].func
= probe
;
323 new[nr_probes
].probe_private
= probe_private
;
324 entry
->refcount
= nr_probes
+ 1;
327 debug_print_probes(entry
);
331 static struct marker_probe_closure
*
332 marker_entry_remove_probe(struct marker_entry
*entry
,
333 marker_probe_func
*probe
, void *probe_private
)
335 int nr_probes
= 0, nr_del
= 0, i
;
336 struct marker_probe_closure
*old
, *new;
340 debug_print_probes(entry
);
342 /* 0 -> N is an error */
343 WARN_ON(entry
->single
.func
== __mark_empty_function
);
345 WARN_ON(probe
&& entry
->single
.func
!= probe
);
346 WARN_ON(entry
->single
.probe_private
!= probe_private
);
347 entry
->single
.func
= __mark_empty_function
;
350 debug_print_probes(entry
);
353 /* (N -> M), (N > 1, M >= 0) probes */
354 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
355 if ((!probe
|| old
[nr_probes
].func
== probe
)
356 && old
[nr_probes
].probe_private
362 if (nr_probes
- nr_del
== 0) {
363 /* N -> 0, (N > 1) */
364 entry
->single
.func
= __mark_empty_function
;
367 } else if (nr_probes
- nr_del
== 1) {
368 /* N -> 1, (N > 1) */
369 for (i
= 0; old
[i
].func
; i
++)
370 if ((probe
&& old
[i
].func
!= probe
) ||
371 old
[i
].probe_private
!= probe_private
)
372 entry
->single
= old
[i
];
377 /* N -> M, (N > 1, M > 1) */
379 new = kzalloc((nr_probes
- nr_del
+ 1)
380 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
382 return ERR_PTR(-ENOMEM
);
383 for (i
= 0; old
[i
].func
; i
++)
384 if ((probe
&& old
[i
].func
!= probe
) ||
385 old
[i
].probe_private
!= probe_private
)
387 entry
->refcount
= nr_probes
- nr_del
;
391 debug_print_probes(entry
);
396 * Get marker if the marker is present in the marker hash table.
397 * Must be called with markers_mutex held.
398 * Returns NULL if not present.
400 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
402 struct hlist_head
*head
;
403 struct hlist_node
*node
;
404 struct marker_entry
*e
;
405 size_t channel_len
= strlen(channel
) + 1;
406 size_t name_len
= strlen(name
) + 1;
409 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
410 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
411 hlist_for_each_entry(e
, node
, head
, hlist
) {
412 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
419 * Add the marker to the marker hash table. Must be called with markers_mutex
422 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
425 struct hlist_head
*head
;
426 struct hlist_node
*node
;
427 struct marker_entry
*e
;
428 size_t channel_len
= strlen(channel
) + 1;
429 size_t name_len
= strlen(name
) + 1;
430 size_t format_len
= 0;
433 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
435 format_len
= strlen(format
) + 1;
436 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
437 hlist_for_each_entry(e
, node
, head
, hlist
) {
438 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
440 "Marker %s.%s busy\n", channel
, name
);
441 return ERR_PTR(-EBUSY
); /* Already there */
445 * Using kmalloc here to allocate a variable length element. Could
446 * cause some memory fragmentation if overused.
448 e
= kmalloc(sizeof(struct marker_entry
)
449 + channel_len
+ name_len
+ format_len
,
452 return ERR_PTR(-ENOMEM
);
453 memcpy(e
->channel
, channel
, channel_len
);
454 e
->name
= &e
->channel
[channel_len
];
455 memcpy(e
->name
, name
, name_len
);
457 e
->format
= &e
->name
[channel_len
+ name_len
];
458 memcpy(e
->format
, format
, format_len
);
459 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
460 e
->call
= marker_probe_cb_noarg
;
462 e
->call
= marker_probe_cb
;
463 trace_mark(metadata
, core_marker_format
,
464 "channel %s name %s format %s",
465 e
->channel
, e
->name
, e
->format
);
468 e
->call
= marker_probe_cb
;
470 e
->single
.func
= __mark_empty_function
;
471 e
->single
.probe_private
= NULL
;
474 e
->format_allocated
= 0;
477 hlist_add_head(&e
->hlist
, head
);
482 * Remove the marker from the marker hash table. Must be called with mutex_lock
485 static int remove_marker(const char *channel
, const char *name
)
487 struct hlist_head
*head
;
488 struct hlist_node
*node
;
489 struct marker_entry
*e
;
491 size_t channel_len
= strlen(channel
) + 1;
492 size_t name_len
= strlen(name
) + 1;
496 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
497 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
498 hlist_for_each_entry(e
, node
, head
, hlist
) {
499 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
506 if (e
->single
.func
!= __mark_empty_function
)
508 hlist_del(&e
->hlist
);
509 if (e
->format_allocated
)
511 ret
= ltt_channels_unregister(e
->channel
);
513 /* Make sure the call_rcu has been executed */
514 //ust// if (e->rcu_pending)
515 //ust// rcu_barrier_sched();
521 * Set the mark_entry format to the format found in the element.
523 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
525 entry
->format
= kstrdup(format
, GFP_KERNEL
);
528 entry
->format_allocated
= 1;
530 trace_mark(metadata
, core_marker_format
,
531 "channel %s name %s format %s",
532 entry
->channel
, entry
->name
, entry
->format
);
537 * Sets the probe callback corresponding to one marker.
539 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
543 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
546 if (strcmp(entry
->format
, elem
->format
) != 0) {
548 "Format mismatch for probe %s "
549 "(%s), marker (%s)\n",
556 ret
= marker_set_format(entry
, elem
->format
);
562 * probe_cb setup (statically known) is done here. It is
563 * asynchronous with the rest of execution, therefore we only
564 * pass from a "safe" callback (with argument) to an "unsafe"
565 * callback (does not set arguments).
567 elem
->call
= entry
->call
;
568 elem
->channel_id
= entry
->channel_id
;
569 elem
->event_id
= entry
->event_id
;
572 * We only update the single probe private data when the ptr is
573 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
575 WARN_ON(elem
->single
.func
!= __mark_empty_function
576 && elem
->single
.probe_private
!= entry
->single
.probe_private
578 elem
->single
.probe_private
= entry
->single
.probe_private
;
580 * Make sure the private data is valid when we update the
584 elem
->single
.func
= entry
->single
.func
;
586 * We also make sure that the new probe callbacks array is consistent
587 * before setting a pointer to it.
589 rcu_assign_pointer(elem
->multi
, entry
->multi
);
591 * Update the function or multi probe array pointer before setting the
595 elem
->ptype
= entry
->ptype
;
597 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
598 //ust// WARN_ON(!elem->tp_cb);
600 //ust// * It is ok to directly call the probe registration because type
601 //ust// * checking has been done in the __trace_mark_tp() macro.
604 //ust// if (active) {
606 //ust// * try_module_get should always succeed because we hold
607 //ust// * markers_mutex to get the tp_cb address.
609 //ust// ret = try_module_get(__module_text_address(
610 //ust// (unsigned long)elem->tp_cb));
611 //ust// BUG_ON(!ret);
612 //ust// ret = tracepoint_probe_register_noupdate(
613 //ust// elem->tp_name,
614 //ust// elem->tp_cb);
616 //ust// ret = tracepoint_probe_unregister_noupdate(
617 //ust// elem->tp_name,
618 //ust// elem->tp_cb);
620 //ust// * tracepoint_probe_update_all() must be called
621 //ust// * before the module containing tp_cb is unloaded.
623 //ust// module_put(__module_text_address(
624 //ust// (unsigned long)elem->tp_cb));
627 elem
->state__imv
= active
;
633 * Disable a marker and its probe callback.
634 * Note: only waiting an RCU period after setting elem->call to the empty
635 * function insures that the original callback is not used anymore. This insured
636 * by rcu_read_lock_sched around the call site.
638 static void disable_marker(struct marker
*elem
)
642 //ust// /* leave "call" as is. It is known statically. */
643 //ust// if (elem->tp_name && _imv_read(elem->state)) {
644 //ust// WARN_ON(!elem->tp_cb);
646 //ust// * It is ok to directly call the probe registration because type
647 //ust// * checking has been done in the __trace_mark_tp() macro.
649 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
650 //ust// elem->tp_cb);
651 //ust// WARN_ON(ret);
653 //ust// * tracepoint_probe_update_all() must be called
654 //ust// * before the module containing tp_cb is unloaded.
656 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
658 elem
->state__imv
= 0;
659 elem
->single
.func
= __mark_empty_function
;
660 /* Update the function before setting the ptype */
662 elem
->ptype
= 0; /* single probe */
664 * Leave the private data and channel_id/event_id there, because removal
665 * is racy and should be done only after an RCU period. These are never
666 * used until the next initialization anyway.
671 * is_marker_enabled - Check if a marker is enabled
672 * @channel: channel name
675 * Returns 1 if the marker is enabled, 0 if disabled.
677 int is_marker_enabled(const char *channel
, const char *name
)
679 struct marker_entry
*entry
;
681 mutex_lock(&markers_mutex
);
682 entry
= get_marker(channel
, name
);
683 mutex_unlock(&markers_mutex
);
685 return entry
&& !!entry
->refcount
;
689 * marker_update_probe_range - Update a probe range
690 * @begin: beginning of the range
691 * @end: end of the range
693 * Updates the probe callback corresponding to a range of markers.
695 void marker_update_probe_range(struct marker
*begin
,
699 struct marker_entry
*mark_entry
;
701 mutex_lock(&markers_mutex
);
702 for (iter
= begin
; iter
< end
; iter
++) {
703 mark_entry
= get_marker(iter
->channel
, iter
->name
);
705 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
707 * ignore error, continue
710 /* This is added for UST. We emit a core_marker_id event
711 * for markers that are already registered to a probe
712 * upon library load. Otherwise, no core_marker_id will
713 * be generated for these markers. Is this the right thing
716 trace_mark(metadata
, core_marker_id
,
717 "channel %s name %s event_id %hu "
718 "int #1u%zu long #1u%zu pointer #1u%zu "
719 "size_t #1u%zu alignment #1u%u",
720 iter
->channel
, iter
->name
, mark_entry
->event_id
,
721 sizeof(int), sizeof(long), sizeof(void *),
722 sizeof(size_t), ltt_get_alignment());
724 disable_marker(iter
);
727 mutex_unlock(&markers_mutex
);
730 static void lib_update_markers(void)
734 /* FIXME: we should probably take a mutex here on libs */
735 //ust// mutex_lock(&module_mutex);
736 list_for_each_entry(lib
, &libs
, list
)
737 marker_update_probe_range(lib
->markers_start
,
738 lib
->markers_start
+ lib
->markers_count
);
739 //ust// mutex_unlock(&module_mutex);
743 * Update probes, removing the faulty probes.
745 * Internal callback only changed before the first probe is connected to it.
746 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
747 * transitions. All other transitions will leave the old private data valid.
748 * This makes the non-atomicity of the callback/private data updates valid.
750 * "special case" updates :
755 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
756 * Site effect : marker_set_format may delete the marker entry (creating a
759 static void marker_update_probes(void)
761 /* Core kernel markers */
762 //ust// marker_update_probe_range(__start___markers, __stop___markers);
763 /* Markers in modules. */
764 //ust// module_update_markers();
765 lib_update_markers();
766 //ust// tracepoint_probe_update_all();
767 /* Update immediate values */
769 //ust// module_imv_update(); /* FIXME: need to port for libs? */
770 marker_update_processes();
774 * marker_probe_register - Connect a probe to a marker
775 * @channel: marker channel
777 * @format: format string
778 * @probe: probe handler
779 * @probe_private: probe private data
781 * private data must be a valid allocated memory address, or NULL.
782 * Returns 0 if ok, error value on error.
783 * The probe address must at least be aligned on the architecture pointer size.
785 int marker_probe_register(const char *channel
, const char *name
,
786 const char *format
, marker_probe_func
*probe
,
789 struct marker_entry
*entry
;
790 int ret
= 0, ret_err
;
791 struct marker_probe_closure
*old
;
794 mutex_lock(&markers_mutex
);
795 entry
= get_marker(channel
, name
);
798 entry
= add_marker(channel
, name
, format
);
800 ret
= PTR_ERR(entry
);
803 ret
= ltt_channels_register(channel
);
805 goto error_remove_marker
;
806 ret
= ltt_channels_get_index_from_name(channel
);
808 goto error_unregister_channel
;
809 entry
->channel_id
= ret
;
810 ret
= ltt_channels_get_event_id(channel
, name
);
812 goto error_unregister_channel
;
813 entry
->event_id
= ret
;
815 trace_mark(metadata
, core_marker_id
,
816 "channel %s name %s event_id %hu "
817 "int #1u%zu long #1u%zu pointer #1u%zu "
818 "size_t #1u%zu alignment #1u%u",
819 channel
, name
, entry
->event_id
,
820 sizeof(int), sizeof(long), sizeof(void *),
821 sizeof(size_t), ltt_get_alignment());
824 ret
= marker_set_format(entry
, format
);
825 else if (strcmp(entry
->format
, format
))
832 * If we detect that a call_rcu is pending for this marker,
833 * make sure it's executed now.
835 //ust// if (entry->rcu_pending)
836 //ust// rcu_barrier_sched();
837 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
841 goto error_unregister_channel
;
845 mutex_unlock(&markers_mutex
);
847 /* Activate marker if necessary */
848 marker_update_probes();
850 mutex_lock(&markers_mutex
);
851 entry
= get_marker(channel
, name
);
854 //ust// if (entry->rcu_pending)
855 //ust// rcu_barrier_sched();
857 entry
->rcu_pending
= 1;
858 /* write rcu_pending before calling the RCU callback */
860 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
861 synchronize_rcu(); free_old_closure(&entry
->rcu
);
864 error_unregister_channel
:
865 ret_err
= ltt_channels_unregister(channel
);
868 ret_err
= remove_marker(channel
, name
);
871 mutex_unlock(&markers_mutex
);
874 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
877 * marker_probe_unregister - Disconnect a probe from a marker
878 * @channel: marker channel
880 * @probe: probe function pointer
881 * @probe_private: probe private data
883 * Returns the private data given to marker_probe_register, or an ERR_PTR().
884 * We do not need to call a synchronize_sched to make sure the probes have
885 * finished running before doing a module unload, because the module unload
886 * itself uses stop_machine(), which insures that every preempt disabled section
889 int marker_probe_unregister(const char *channel
, const char *name
,
890 marker_probe_func
*probe
, void *probe_private
)
892 struct marker_entry
*entry
;
893 struct marker_probe_closure
*old
;
896 mutex_lock(&markers_mutex
);
897 entry
= get_marker(channel
, name
);
900 //ust// if (entry->rcu_pending)
901 //ust// rcu_barrier_sched();
902 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
903 mutex_unlock(&markers_mutex
);
905 marker_update_probes();
907 mutex_lock(&markers_mutex
);
908 entry
= get_marker(channel
, name
);
911 //ust// if (entry->rcu_pending)
912 //ust// rcu_barrier_sched();
914 entry
->rcu_pending
= 1;
915 /* write rcu_pending before calling the RCU callback */
917 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
918 synchronize_rcu(); free_old_closure(&entry
->rcu
);
919 remove_marker(channel
, name
); /* Ignore busy error message */
922 mutex_unlock(&markers_mutex
);
925 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
927 static struct marker_entry
*
928 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
930 struct marker_entry
*entry
;
932 struct hlist_head
*head
;
933 struct hlist_node
*node
;
935 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
936 head
= &marker_table
[i
];
937 hlist_for_each_entry(entry
, node
, head
, hlist
) {
939 if (entry
->single
.func
== probe
940 && entry
->single
.probe_private
944 struct marker_probe_closure
*closure
;
945 closure
= entry
->multi
;
946 for (i
= 0; closure
[i
].func
; i
++) {
947 if (closure
[i
].func
== probe
&&
948 closure
[i
].probe_private
959 * marker_probe_unregister_private_data - Disconnect a probe from a marker
960 * @probe: probe function
961 * @probe_private: probe private data
963 * Unregister a probe by providing the registered private data.
964 * Only removes the first marker found in hash table.
965 * Return 0 on success or error value.
966 * We do not need to call a synchronize_sched to make sure the probes have
967 * finished running before doing a module unload, because the module unload
968 * itself uses stop_machine(), which insures that every preempt disabled section
971 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
974 struct marker_entry
*entry
;
976 struct marker_probe_closure
*old
;
977 const char *channel
= NULL
, *name
= NULL
;
979 mutex_lock(&markers_mutex
);
980 entry
= get_marker_from_private_data(probe
, probe_private
);
985 //ust// if (entry->rcu_pending)
986 //ust// rcu_barrier_sched();
987 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
988 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
989 name
= kstrdup(entry
->name
, GFP_KERNEL
);
990 mutex_unlock(&markers_mutex
);
992 marker_update_probes();
994 mutex_lock(&markers_mutex
);
995 entry
= get_marker(channel
, name
);
998 //ust// if (entry->rcu_pending)
999 //ust// rcu_barrier_sched();
1000 entry
->oldptr
= old
;
1001 entry
->rcu_pending
= 1;
1002 /* write rcu_pending before calling the RCU callback */
1004 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
1005 synchronize_rcu(); free_old_closure(&entry
->rcu
);
1006 /* Ignore busy error message */
1007 remove_marker(channel
, name
);
1009 mutex_unlock(&markers_mutex
);
1014 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1017 * marker_get_private_data - Get a marker's probe private data
1018 * @channel: marker channel
1019 * @name: marker name
1020 * @probe: probe to match
1021 * @num: get the nth matching probe's private data
1023 * Returns the nth private data pointer (starting from 0) matching, or an
1025 * Returns the private data pointer, or an ERR_PTR.
1026 * The private data pointer should _only_ be dereferenced if the caller is the
1027 * owner of the data, or its content could vanish. This is mostly used to
1028 * confirm that a caller is the owner of a registered probe.
1030 void *marker_get_private_data(const char *channel
, const char *name
,
1031 marker_probe_func
*probe
, int num
)
1033 struct hlist_head
*head
;
1034 struct hlist_node
*node
;
1035 struct marker_entry
*e
;
1036 size_t channel_len
= strlen(channel
) + 1;
1037 size_t name_len
= strlen(name
) + 1;
1041 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1042 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1043 hlist_for_each_entry(e
, node
, head
, hlist
) {
1044 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1046 if (num
== 0 && e
->single
.func
== probe
)
1047 return e
->single
.probe_private
;
1049 struct marker_probe_closure
*closure
;
1052 for (i
= 0; closure
[i
].func
; i
++) {
1053 if (closure
[i
].func
!= probe
)
1056 return closure
[i
].probe_private
;
1062 return ERR_PTR(-ENOENT
);
1064 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1067 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1069 * Called when no channel users are active by the channel infrastructure.
1070 * Called with lock_markers() and channel mutex held.
1072 //ust// void markers_compact_event_ids(void)
1074 //ust// struct marker_entry *entry;
1075 //ust// unsigned int i;
1076 //ust// struct hlist_head *head;
1077 //ust// struct hlist_node *node;
1080 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1081 //ust// head = &marker_table[i];
1082 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1083 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1084 //ust// WARN_ON(ret < 0);
1085 //ust// entry->channel_id = ret;
1086 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1087 //ust// entry->name);
1088 //ust// WARN_ON(ret < 0);
1089 //ust// entry->event_id = ret;
1094 //ust//#ifdef CONFIG_MODULES
1097 * Returns 0 if current not found.
1098 * Returns 1 if current found.
1100 int lib_get_iter_markers(struct marker_iter
*iter
)
1102 struct lib
*iter_lib
;
1105 //ust// mutex_lock(&module_mutex);
1106 list_for_each_entry(iter_lib
, &libs
, list
) {
1107 if (iter_lib
< iter
->lib
)
1109 else if (iter_lib
> iter
->lib
)
1110 iter
->marker
= NULL
;
1111 found
= marker_get_iter_range(&iter
->marker
,
1112 iter_lib
->markers_start
,
1113 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1115 iter
->lib
= iter_lib
;
1119 //ust// mutex_unlock(&module_mutex);
1124 * marker_get_iter_range - Get a next marker iterator given a range.
1125 * @marker: current markers (in), next marker (out)
1126 * @begin: beginning of the range
1127 * @end: end of the range
1129 * Returns whether a next marker has been found (1) or not (0).
1130 * Will return the first marker in the range if the input marker is NULL.
1132 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1135 if (!*marker
&& begin
!= end
) {
1139 if (*marker
>= begin
&& *marker
< end
)
1143 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1145 static void marker_get_iter(struct marker_iter
*iter
)
1149 /* Core kernel markers */
1151 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1152 found
= marker_get_iter_range(&iter
->marker
,
1153 __start___markers
, __stop___markers
);
1157 /* Markers in modules. */
1158 found
= lib_get_iter_markers(iter
);
1161 marker_iter_reset(iter
);
1164 void marker_iter_start(struct marker_iter
*iter
)
1166 marker_get_iter(iter
);
1168 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1170 void marker_iter_next(struct marker_iter
*iter
)
1174 * iter->marker may be invalid because we blindly incremented it.
1175 * Make sure it is valid by marshalling on the markers, getting the
1176 * markers from following modules if necessary.
1178 marker_get_iter(iter
);
1180 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1182 void marker_iter_stop(struct marker_iter
*iter
)
1185 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1187 void marker_iter_reset(struct marker_iter
*iter
)
1190 iter
->marker
= NULL
;
1192 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1194 #ifdef CONFIG_MARKERS_USERSPACE
1196 * must be called with current->user_markers_mutex held
1198 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1200 struct user_marker
*umark
;
1201 struct hlist_node
*pos
, *n
;
1203 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1204 if (umark
->state
== state
) {
1205 hlist_del(&umark
->hlist
);
1211 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1212 //ust// char __user *state, int reg)
1214 //ust// struct user_marker *umark;
1216 //ust// struct marker_entry *entry;
1217 //ust// int ret = 0;
1219 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1220 //ust// current->comm, reg ? "registers" : "unregisters",
1221 //ust// name, state);
1223 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1224 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1225 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1226 //ust// umark->state = state;
1227 //ust// len = strncpy_from_user(umark->name, name,
1228 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1229 //ust// if (len < 0) {
1230 //ust// ret = -EFAULT;
1233 //ust// len = strncpy_from_user(umark->format, format,
1234 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1235 //ust// if (len < 0) {
1236 //ust// ret = -EFAULT;
1239 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1240 //ust// umark->format);
1241 //ust// mutex_lock(&markers_mutex);
1242 //ust// entry = get_marker("userspace", umark->name);
1243 //ust// if (entry) {
1244 //ust// if (entry->format &&
1245 //ust// strcmp(entry->format, umark->format) != 0) {
1246 //ust// printk(" error, wrong format in process %s",
1247 //ust// current->comm);
1248 //ust// ret = -EPERM;
1249 //ust// goto error_unlock;
1251 //ust// printk(" %s", !!entry->refcount
1252 //ust// ? "enabled" : "disabled");
1253 //ust// if (put_user(!!entry->refcount, state)) {
1254 //ust// ret = -EFAULT;
1255 //ust// goto error_unlock;
1257 //ust// printk("\n");
1259 //ust// printk(" disabled\n");
1260 //ust// if (put_user(0, umark->state)) {
1261 //ust// printk(KERN_WARNING
1262 //ust// "Marker in %s caused a fault\n",
1263 //ust// current->comm);
1264 //ust// goto error_unlock;
1267 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1268 //ust// hlist_add_head(&umark->hlist,
1269 //ust// ¤t->group_leader->user_markers);
1270 //ust// current->group_leader->user_markers_sequence++;
1271 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1272 //ust// mutex_unlock(&markers_mutex);
1274 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1275 //ust// free_user_marker(state,
1276 //ust// ¤t->group_leader->user_markers);
1277 //ust// current->group_leader->user_markers_sequence++;
1278 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1281 //ust// error_unlock:
1282 //ust// mutex_unlock(&markers_mutex);
1284 //ust// kfree(umark);
1291 //ust// * string : 0
1293 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1294 //ust// char __user *ubuf)
1296 //ust// long ret = -EPERM;
1300 //ust// switch (type) {
1301 //ust// case 0: /* String */
1302 //ust// ret = -ENOMEM;
1303 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1305 //ust// goto string_out;
1306 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1307 //ust// if (len < 0) {
1308 //ust// ret = -EFAULT;
1309 //ust// goto string_err;
1311 //ust// trace_mark(userspace, string, "string %s", page);
1313 //ust// free_page((unsigned long) page);
1322 //ust// static void marker_update_processes(void)
1324 //ust// struct task_struct *g, *t;
1327 //ust// * markers_mutex is taken to protect the p->user_markers read.
1329 //ust// mutex_lock(&markers_mutex);
1330 //ust// read_lock(&tasklist_lock);
1331 //ust// for_each_process(g) {
1332 //ust// WARN_ON(!thread_group_leader(g));
1333 //ust// if (hlist_empty(&g->user_markers))
1335 //ust// if (strcmp(g->comm, "testprog") == 0)
1336 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1339 //ust// /* TODO : implement this thread flag in each arch. */
1340 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1341 //ust// } while ((t = next_thread(t)) != g);
1343 //ust// read_unlock(&tasklist_lock);
1344 //ust// mutex_unlock(&markers_mutex);
1348 * Update current process.
1349 * Note that we have to wait a whole scheduler period before we are sure that
1350 * every running userspace threads have their markers updated.
1351 * (synchronize_sched() can be used to insure this).
1353 void marker_update_process(void)
1355 struct user_marker
*umark
;
1356 struct hlist_node
*pos
;
1357 struct marker_entry
*entry
;
1359 mutex_lock(&markers_mutex
);
1360 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1361 if (strcmp(current
->comm
, "testprog") == 0)
1362 printk(KERN_DEBUG
"do update pending for testprog\n");
1363 hlist_for_each_entry(umark
, pos
,
1364 ¤t
->group_leader
->user_markers
, hlist
) {
1365 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1366 umark
->name
, current
->comm
);
1367 entry
= get_marker("userspace", umark
->name
);
1369 if (entry
->format
&&
1370 strcmp(entry
->format
, umark
->format
) != 0) {
1372 " error, wrong format in process %s\n",
1376 if (put_user(!!entry
->refcount
, umark
->state
)) {
1378 "Marker in %s caused a fault\n",
1383 if (put_user(0, umark
->state
)) {
1385 "Marker in %s caused a fault\n",
1391 clear_thread_flag(TIF_MARKER_PENDING
);
1392 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1393 mutex_unlock(&markers_mutex
);
1397 * Called at process exit and upon do_execve().
1398 * We assume that when the leader exits, no more references can be done to the
1399 * leader structure by the other threads.
1401 void exit_user_markers(struct task_struct
*p
)
1403 struct user_marker
*umark
;
1404 struct hlist_node
*pos
, *n
;
1406 if (thread_group_leader(p
)) {
1407 mutex_lock(&markers_mutex
);
1408 mutex_lock(&p
->user_markers_mutex
);
1409 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1412 INIT_HLIST_HEAD(&p
->user_markers
);
1413 p
->user_markers_sequence
++;
1414 mutex_unlock(&p
->user_markers_mutex
);
1415 mutex_unlock(&markers_mutex
);
1419 int is_marker_enabled(const char *channel
, const char *name
)
1421 struct marker_entry
*entry
;
1423 mutex_lock(&markers_mutex
);
1424 entry
= get_marker(channel
, name
);
1425 mutex_unlock(&markers_mutex
);
1427 return entry
&& !!entry
->refcount
;
1431 int marker_module_notify(struct notifier_block
*self
,
1432 unsigned long val
, void *data
)
1434 struct module
*mod
= data
;
1437 case MODULE_STATE_COMING
:
1438 marker_update_probe_range(mod
->markers
,
1439 mod
->markers
+ mod
->num_markers
);
1441 case MODULE_STATE_GOING
:
1442 marker_update_probe_range(mod
->markers
,
1443 mod
->markers
+ mod
->num_markers
);
1449 struct notifier_block marker_module_nb
= {
1450 .notifier_call
= marker_module_notify
,
1454 //ust// static int init_markers(void)
1456 //ust// return register_module_notifier(&marker_module_nb);
1458 //ust// __initcall(init_markers);
1459 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1461 #endif /* CONFIG_MODULES */
1463 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1465 struct marker_entry
*entry
;
1466 struct ltt_probe_private_data call_data
;
1467 struct hlist_head
*head
;
1468 struct hlist_node
*node
;
1471 mutex_lock(&markers_mutex
);
1472 call_data
.trace
= trace
;
1473 call_data
.serializer
= NULL
;
1475 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1476 head
= &marker_table
[i
];
1477 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1478 __trace_mark(0, metadata
, core_marker_id
,
1480 "channel %s name %s event_id %hu "
1481 "int #1u%zu long #1u%zu pointer #1u%zu "
1482 "size_t #1u%zu alignment #1u%u",
1486 sizeof(int), sizeof(long),
1487 sizeof(void *), sizeof(size_t),
1488 ltt_get_alignment());
1490 __trace_mark(0, metadata
,
1493 "channel %s name %s format %s",
1499 mutex_unlock(&markers_mutex
);
1501 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1503 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1505 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1510 static void new_markers(struct marker
*start
, struct marker
*end
)
1514 for(m
=start
; m
< end
; m
++) {
1520 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1523 struct marker_addr
*addr
;
1525 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1527 pl
->markers_start
= markers_start
;
1528 pl
->markers_count
= markers_count
;
1530 /* FIXME: maybe protect this with its own mutex? */
1532 list_add(&pl
->list
, &libs
);
1535 new_markers(markers_start
, markers_start
+ markers_count
);
1537 /* FIXME: update just the loaded lib */
1538 lib_update_markers();
1540 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1545 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1547 /*FIXME: implement; but before implementing, marker_register_lib must
1548 have appropriate locking. */
1553 static int initialized
= 0;
1555 void __attribute__((constructor
)) init_markers(void)
1558 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1559 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);