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")));
49 extern struct marker_addr __start___marker_addr
[] __attribute__((visibility("hidden")));
50 extern struct marker_addr __stop___marker_addr
[] __attribute__((visibility("hidden")));
52 /* Set to 1 to enable marker debug output */
53 static const int marker_debug
;
56 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
57 * and module markers and the hash table.
59 static DEFINE_MUTEX(markers_mutex
);
61 static LIST_HEAD(libs
);
64 void lock_markers(void)
66 mutex_lock(&markers_mutex
);
69 void unlock_markers(void)
71 mutex_unlock(&markers_mutex
);
75 * Marker hash table, containing the active markers.
76 * Protected by module_mutex.
78 #define MARKER_HASH_BITS 6
79 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
80 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
84 * It is used to make sure every handler has finished using its private data
85 * between two consecutive operation (add or remove) on a given marker. It is
86 * also used to delay the free of multiple probes array until a quiescent state
88 * marker entries modifications are protected by the markers_mutex.
91 struct hlist_node hlist
;
95 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
96 struct marker_probe_closure single
;
97 struct marker_probe_closure
*multi
;
98 int refcount
; /* Number of times armed. 0 if disarmed. */
104 unsigned char ptype
:1;
105 unsigned char format_allocated
:1;
106 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
109 #ifdef CONFIG_MARKERS_USERSPACE
110 static void marker_update_processes(void);
112 static void marker_update_processes(void)
118 * __mark_empty_function - Empty probe callback
119 * @mdata: marker data
120 * @probe_private: probe private data
121 * @call_private: call site private data
122 * @fmt: format string
123 * @...: variable argument list
125 * Empty callback provided as a probe to the markers. By providing this to a
126 * disabled marker, we make sure the execution flow is always valid even
127 * though the function pointer change and the marker enabling are two distinct
128 * operations that modifies the execution flow of preemptible code.
130 notrace
void __mark_empty_function(const struct marker
*mdata
,
131 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
134 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
137 * marker_probe_cb Callback that prepares the variable argument list for probes.
138 * @mdata: pointer of type struct marker
139 * @call_private: caller site private data
140 * @...: Variable argument list.
142 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
143 * need to put a full smp_rmb() in this branch. This is why we do not use
144 * rcu_dereference() for the pointer read.
146 notrace
void marker_probe_cb(const struct marker
*mdata
,
147 void *call_private
, struct registers
*regs
, ...)
153 * rcu_read_lock_sched does two things : disabling preemption to make
154 * sure the teardown of the callbacks can be done correctly when they
155 * are in modules and they insure RCU read coherency.
157 //ust// rcu_read_lock_sched_notrace();
158 ptype
= mdata
->ptype
;
159 if (likely(!ptype
)) {
160 marker_probe_func
*func
;
161 /* Must read the ptype before ptr. They are not data dependant,
162 * so we put an explicit smp_rmb() here. */
164 func
= mdata
->single
.func
;
165 /* Must read the ptr before private data. They are not data
166 * dependant, so we put an explicit smp_rmb() here. */
168 va_start(args
, regs
);
169 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
170 mdata
->format
, &args
);
173 struct marker_probe_closure
*multi
;
176 * Read mdata->ptype before mdata->multi.
179 multi
= mdata
->multi
;
181 * multi points to an array, therefore accessing the array
182 * depends on reading multi. However, even in this case,
183 * we must insure that the pointer is read _before_ the array
184 * data. Same as rcu_dereference, but we need a full smp_rmb()
185 * in the fast path, so put the explicit barrier here.
187 smp_read_barrier_depends();
188 for (i
= 0; multi
[i
].func
; i
++) {
189 va_start(args
, regs
);
190 multi
[i
].func(mdata
, multi
[i
].probe_private
,
191 regs
, call_private
, mdata
->format
, &args
);
195 //ust// rcu_read_unlock_sched_notrace();
197 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
200 * marker_probe_cb Callback that does not prepare the variable argument list.
201 * @mdata: pointer of type struct marker
202 * @call_private: caller site private data
203 * @...: Variable argument list.
205 * Should be connected to markers "MARK_NOARGS".
207 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
208 void *call_private
, struct registers
*regs
, ...)
210 va_list args
; /* not initialized */
213 //ust// rcu_read_lock_sched_notrace();
214 ptype
= mdata
->ptype
;
215 if (likely(!ptype
)) {
216 marker_probe_func
*func
;
217 /* Must read the ptype before ptr. They are not data dependant,
218 * so we put an explicit smp_rmb() here. */
220 func
= mdata
->single
.func
;
221 /* Must read the ptr before private data. They are not data
222 * dependant, so we put an explicit smp_rmb() here. */
224 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
225 mdata
->format
, &args
);
227 struct marker_probe_closure
*multi
;
230 * Read mdata->ptype before mdata->multi.
233 multi
= mdata
->multi
;
235 * multi points to an array, therefore accessing the array
236 * depends on reading multi. However, even in this case,
237 * we must insure that the pointer is read _before_ the array
238 * data. Same as rcu_dereference, but we need a full smp_rmb()
239 * in the fast path, so put the explicit barrier here.
241 smp_read_barrier_depends();
242 for (i
= 0; multi
[i
].func
; i
++)
243 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
244 call_private
, mdata
->format
, &args
);
246 //ust// rcu_read_unlock_sched_notrace();
249 static void free_old_closure(struct rcu_head
*head
)
251 struct marker_entry
*entry
= container_of(head
,
252 struct marker_entry
, rcu
);
253 kfree(entry
->oldptr
);
254 /* Make sure we free the data before setting the pending flag to 0 */
256 entry
->rcu_pending
= 0;
259 static void debug_print_probes(struct marker_entry
*entry
)
267 printk(KERN_DEBUG
"Single probe : %p %p\n",
269 entry
->single
.probe_private
);
271 for (i
= 0; entry
->multi
[i
].func
; i
++)
272 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
273 entry
->multi
[i
].func
,
274 entry
->multi
[i
].probe_private
);
278 static struct marker_probe_closure
*
279 marker_entry_add_probe(struct marker_entry
*entry
,
280 marker_probe_func
*probe
, void *probe_private
)
283 struct marker_probe_closure
*old
, *new;
287 debug_print_probes(entry
);
290 if (entry
->single
.func
== probe
&&
291 entry
->single
.probe_private
== probe_private
)
292 return ERR_PTR(-EBUSY
);
293 if (entry
->single
.func
== __mark_empty_function
) {
295 entry
->single
.func
= probe
;
296 entry
->single
.probe_private
= probe_private
;
299 debug_print_probes(entry
);
307 /* (N -> N+1), (N != 0, 1) probes */
308 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
309 if (old
[nr_probes
].func
== probe
310 && old
[nr_probes
].probe_private
312 return ERR_PTR(-EBUSY
);
314 /* + 2 : one for new probe, one for NULL func */
315 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
318 return ERR_PTR(-ENOMEM
);
320 new[0] = entry
->single
;
323 nr_probes
* sizeof(struct marker_probe_closure
));
324 new[nr_probes
].func
= probe
;
325 new[nr_probes
].probe_private
= probe_private
;
326 entry
->refcount
= nr_probes
+ 1;
329 debug_print_probes(entry
);
333 static struct marker_probe_closure
*
334 marker_entry_remove_probe(struct marker_entry
*entry
,
335 marker_probe_func
*probe
, void *probe_private
)
337 int nr_probes
= 0, nr_del
= 0, i
;
338 struct marker_probe_closure
*old
, *new;
342 debug_print_probes(entry
);
344 /* 0 -> N is an error */
345 WARN_ON(entry
->single
.func
== __mark_empty_function
);
347 WARN_ON(probe
&& entry
->single
.func
!= probe
);
348 WARN_ON(entry
->single
.probe_private
!= probe_private
);
349 entry
->single
.func
= __mark_empty_function
;
352 debug_print_probes(entry
);
355 /* (N -> M), (N > 1, M >= 0) probes */
356 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
357 if ((!probe
|| old
[nr_probes
].func
== probe
)
358 && old
[nr_probes
].probe_private
364 if (nr_probes
- nr_del
== 0) {
365 /* N -> 0, (N > 1) */
366 entry
->single
.func
= __mark_empty_function
;
369 } else if (nr_probes
- nr_del
== 1) {
370 /* N -> 1, (N > 1) */
371 for (i
= 0; old
[i
].func
; i
++)
372 if ((probe
&& old
[i
].func
!= probe
) ||
373 old
[i
].probe_private
!= probe_private
)
374 entry
->single
= old
[i
];
379 /* N -> M, (N > 1, M > 1) */
381 new = kzalloc((nr_probes
- nr_del
+ 1)
382 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
384 return ERR_PTR(-ENOMEM
);
385 for (i
= 0; old
[i
].func
; i
++)
386 if ((probe
&& old
[i
].func
!= probe
) ||
387 old
[i
].probe_private
!= probe_private
)
389 entry
->refcount
= nr_probes
- nr_del
;
393 debug_print_probes(entry
);
398 * Get marker if the marker is present in the marker hash table.
399 * Must be called with markers_mutex held.
400 * Returns NULL if not present.
402 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
404 struct hlist_head
*head
;
405 struct hlist_node
*node
;
406 struct marker_entry
*e
;
407 size_t channel_len
= strlen(channel
) + 1;
408 size_t name_len
= strlen(name
) + 1;
411 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
412 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
413 hlist_for_each_entry(e
, node
, head
, hlist
) {
414 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
421 * Add the marker to the marker hash table. Must be called with markers_mutex
424 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
427 struct hlist_head
*head
;
428 struct hlist_node
*node
;
429 struct marker_entry
*e
;
430 size_t channel_len
= strlen(channel
) + 1;
431 size_t name_len
= strlen(name
) + 1;
432 size_t format_len
= 0;
435 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
437 format_len
= strlen(format
) + 1;
438 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
439 hlist_for_each_entry(e
, node
, head
, hlist
) {
440 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
442 "Marker %s.%s busy\n", channel
, name
);
443 return ERR_PTR(-EBUSY
); /* Already there */
447 * Using kmalloc here to allocate a variable length element. Could
448 * cause some memory fragmentation if overused.
450 e
= kmalloc(sizeof(struct marker_entry
)
451 + channel_len
+ name_len
+ format_len
,
454 return ERR_PTR(-ENOMEM
);
455 memcpy(e
->channel
, channel
, channel_len
);
456 e
->name
= &e
->channel
[channel_len
];
457 memcpy(e
->name
, name
, name_len
);
459 e
->format
= &e
->name
[channel_len
+ name_len
];
460 memcpy(e
->format
, format
, format_len
);
461 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
462 e
->call
= marker_probe_cb_noarg
;
464 e
->call
= marker_probe_cb
;
465 trace_mark(metadata
, core_marker_format
,
466 "channel %s name %s format %s",
467 e
->channel
, e
->name
, e
->format
);
470 e
->call
= marker_probe_cb
;
472 e
->single
.func
= __mark_empty_function
;
473 e
->single
.probe_private
= NULL
;
476 e
->format_allocated
= 0;
479 hlist_add_head(&e
->hlist
, head
);
484 * Remove the marker from the marker hash table. Must be called with mutex_lock
487 static int remove_marker(const char *channel
, const char *name
)
489 struct hlist_head
*head
;
490 struct hlist_node
*node
;
491 struct marker_entry
*e
;
493 size_t channel_len
= strlen(channel
) + 1;
494 size_t name_len
= strlen(name
) + 1;
498 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
499 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
500 hlist_for_each_entry(e
, node
, head
, hlist
) {
501 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
508 if (e
->single
.func
!= __mark_empty_function
)
510 hlist_del(&e
->hlist
);
511 if (e
->format_allocated
)
513 ret
= ltt_channels_unregister(e
->channel
);
515 /* Make sure the call_rcu has been executed */
516 //ust// if (e->rcu_pending)
517 //ust// rcu_barrier_sched();
523 * Set the mark_entry format to the format found in the element.
525 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
527 entry
->format
= kstrdup(format
, GFP_KERNEL
);
530 entry
->format_allocated
= 1;
532 trace_mark(metadata
, core_marker_format
,
533 "channel %s name %s format %s",
534 entry
->channel
, entry
->name
, entry
->format
);
539 * Sets the probe callback corresponding to one marker.
541 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
545 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
548 if (strcmp(entry
->format
, elem
->format
) != 0) {
550 "Format mismatch for probe %s "
551 "(%s), marker (%s)\n",
558 ret
= marker_set_format(entry
, elem
->format
);
564 * probe_cb setup (statically known) is done here. It is
565 * asynchronous with the rest of execution, therefore we only
566 * pass from a "safe" callback (with argument) to an "unsafe"
567 * callback (does not set arguments).
569 elem
->call
= entry
->call
;
570 elem
->channel_id
= entry
->channel_id
;
571 elem
->event_id
= entry
->event_id
;
574 * We only update the single probe private data when the ptr is
575 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
577 WARN_ON(elem
->single
.func
!= __mark_empty_function
578 && elem
->single
.probe_private
!= entry
->single
.probe_private
580 elem
->single
.probe_private
= entry
->single
.probe_private
;
582 * Make sure the private data is valid when we update the
586 elem
->single
.func
= entry
->single
.func
;
588 * We also make sure that the new probe callbacks array is consistent
589 * before setting a pointer to it.
591 rcu_assign_pointer(elem
->multi
, entry
->multi
);
593 * Update the function or multi probe array pointer before setting the
597 elem
->ptype
= entry
->ptype
;
599 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
600 //ust// WARN_ON(!elem->tp_cb);
602 //ust// * It is ok to directly call the probe registration because type
603 //ust// * checking has been done in the __trace_mark_tp() macro.
606 //ust// if (active) {
608 //ust// * try_module_get should always succeed because we hold
609 //ust// * markers_mutex to get the tp_cb address.
611 //ust// ret = try_module_get(__module_text_address(
612 //ust// (unsigned long)elem->tp_cb));
613 //ust// BUG_ON(!ret);
614 //ust// ret = tracepoint_probe_register_noupdate(
615 //ust// elem->tp_name,
616 //ust// elem->tp_cb);
618 //ust// ret = tracepoint_probe_unregister_noupdate(
619 //ust// elem->tp_name,
620 //ust// elem->tp_cb);
622 //ust// * tracepoint_probe_update_all() must be called
623 //ust// * before the module containing tp_cb is unloaded.
625 //ust// module_put(__module_text_address(
626 //ust// (unsigned long)elem->tp_cb));
629 elem
->state__imv
= active
;
635 * Disable a marker and its probe callback.
636 * Note: only waiting an RCU period after setting elem->call to the empty
637 * function insures that the original callback is not used anymore. This insured
638 * by rcu_read_lock_sched around the call site.
640 static void disable_marker(struct marker
*elem
)
644 //ust// /* leave "call" as is. It is known statically. */
645 //ust// if (elem->tp_name && _imv_read(elem->state)) {
646 //ust// WARN_ON(!elem->tp_cb);
648 //ust// * It is ok to directly call the probe registration because type
649 //ust// * checking has been done in the __trace_mark_tp() macro.
651 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
652 //ust// elem->tp_cb);
653 //ust// WARN_ON(ret);
655 //ust// * tracepoint_probe_update_all() must be called
656 //ust// * before the module containing tp_cb is unloaded.
658 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
660 elem
->state__imv
= 0;
661 elem
->single
.func
= __mark_empty_function
;
662 /* Update the function before setting the ptype */
664 elem
->ptype
= 0; /* single probe */
666 * Leave the private data and channel_id/event_id there, because removal
667 * is racy and should be done only after an RCU period. These are never
668 * used until the next initialization anyway.
673 * marker_update_probe_range - Update a probe range
674 * @begin: beginning of the range
675 * @end: end of the range
677 * Updates the probe callback corresponding to a range of markers.
679 void marker_update_probe_range(struct marker
*begin
,
683 struct marker_entry
*mark_entry
;
685 mutex_lock(&markers_mutex
);
686 for (iter
= begin
; iter
< end
; iter
++) {
687 mark_entry
= get_marker(iter
->channel
, iter
->name
);
689 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
691 * ignore error, continue
694 /* This is added for UST. We emit a core_marker_id event
695 * for markers that are already registered to a probe
696 * upon library load. Otherwise, no core_marker_id will
697 * be generated for these markers. Is this the right thing
700 trace_mark(metadata
, core_marker_id
,
701 "channel %s name %s event_id %hu "
702 "int #1u%zu long #1u%zu pointer #1u%zu "
703 "size_t #1u%zu alignment #1u%u",
704 iter
->channel
, iter
->name
, mark_entry
->event_id
,
705 sizeof(int), sizeof(long), sizeof(void *),
706 sizeof(size_t), ltt_get_alignment());
708 disable_marker(iter
);
711 mutex_unlock(&markers_mutex
);
714 static void lib_update_markers(void)
718 /* FIXME: we should probably take a mutex here on libs */
719 //ust// mutex_lock(&module_mutex);
720 list_for_each_entry(lib
, &libs
, list
)
721 marker_update_probe_range(lib
->markers_start
,
722 lib
->markers_start
+ lib
->markers_count
);
723 //ust// mutex_unlock(&module_mutex);
727 * Update probes, removing the faulty probes.
729 * Internal callback only changed before the first probe is connected to it.
730 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
731 * transitions. All other transitions will leave the old private data valid.
732 * This makes the non-atomicity of the callback/private data updates valid.
734 * "special case" updates :
739 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
740 * Site effect : marker_set_format may delete the marker entry (creating a
743 static void marker_update_probes(void)
745 /* Core kernel markers */
746 //ust// marker_update_probe_range(__start___markers, __stop___markers);
747 /* Markers in modules. */
748 //ust// module_update_markers();
749 lib_update_markers();
750 //ust// tracepoint_probe_update_all();
751 /* Update immediate values */
753 //ust// module_imv_update(); /* FIXME: need to port for libs? */
754 marker_update_processes();
758 * marker_probe_register - Connect a probe to a marker
759 * @channel: marker channel
761 * @format: format string
762 * @probe: probe handler
763 * @probe_private: probe private data
765 * private data must be a valid allocated memory address, or NULL.
766 * Returns 0 if ok, error value on error.
767 * The probe address must at least be aligned on the architecture pointer size.
769 int marker_probe_register(const char *channel
, const char *name
,
770 const char *format
, marker_probe_func
*probe
,
773 struct marker_entry
*entry
;
774 int ret
= 0, ret_err
;
775 struct marker_probe_closure
*old
;
778 mutex_lock(&markers_mutex
);
779 entry
= get_marker(channel
, name
);
782 entry
= add_marker(channel
, name
, format
);
784 ret
= PTR_ERR(entry
);
787 ret
= ltt_channels_register(channel
);
789 goto error_remove_marker
;
790 ret
= ltt_channels_get_index_from_name(channel
);
792 goto error_unregister_channel
;
793 entry
->channel_id
= ret
;
794 ret
= ltt_channels_get_event_id(channel
, name
);
796 goto error_unregister_channel
;
797 entry
->event_id
= ret
;
799 trace_mark(metadata
, core_marker_id
,
800 "channel %s name %s event_id %hu "
801 "int #1u%zu long #1u%zu pointer #1u%zu "
802 "size_t #1u%zu alignment #1u%u",
803 channel
, name
, entry
->event_id
,
804 sizeof(int), sizeof(long), sizeof(void *),
805 sizeof(size_t), ltt_get_alignment());
808 ret
= marker_set_format(entry
, format
);
809 else if (strcmp(entry
->format
, format
))
816 * If we detect that a call_rcu is pending for this marker,
817 * make sure it's executed now.
819 //ust// if (entry->rcu_pending)
820 //ust// rcu_barrier_sched();
821 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
825 goto error_unregister_channel
;
829 mutex_unlock(&markers_mutex
);
831 /* Activate marker if necessary */
832 marker_update_probes();
834 mutex_lock(&markers_mutex
);
835 entry
= get_marker(channel
, name
);
838 //ust// if (entry->rcu_pending)
839 //ust// rcu_barrier_sched();
841 entry
->rcu_pending
= 1;
842 /* write rcu_pending before calling the RCU callback */
844 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
845 synchronize_rcu(); free_old_closure(&entry
->rcu
);
848 error_unregister_channel
:
849 ret_err
= ltt_channels_unregister(channel
);
852 ret_err
= remove_marker(channel
, name
);
855 mutex_unlock(&markers_mutex
);
858 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
861 * marker_probe_unregister - Disconnect a probe from a marker
862 * @channel: marker channel
864 * @probe: probe function pointer
865 * @probe_private: probe private data
867 * Returns the private data given to marker_probe_register, or an ERR_PTR().
868 * We do not need to call a synchronize_sched to make sure the probes have
869 * finished running before doing a module unload, because the module unload
870 * itself uses stop_machine(), which insures that every preempt disabled section
873 int marker_probe_unregister(const char *channel
, const char *name
,
874 marker_probe_func
*probe
, void *probe_private
)
876 struct marker_entry
*entry
;
877 struct marker_probe_closure
*old
;
880 mutex_lock(&markers_mutex
);
881 entry
= get_marker(channel
, name
);
884 //ust// if (entry->rcu_pending)
885 //ust// rcu_barrier_sched();
886 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
887 mutex_unlock(&markers_mutex
);
889 marker_update_probes();
891 mutex_lock(&markers_mutex
);
892 entry
= get_marker(channel
, name
);
895 //ust// if (entry->rcu_pending)
896 //ust// rcu_barrier_sched();
898 entry
->rcu_pending
= 1;
899 /* write rcu_pending before calling the RCU callback */
901 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
902 synchronize_rcu(); free_old_closure(&entry
->rcu
);
903 remove_marker(channel
, name
); /* Ignore busy error message */
906 mutex_unlock(&markers_mutex
);
909 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
911 static struct marker_entry
*
912 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
914 struct marker_entry
*entry
;
916 struct hlist_head
*head
;
917 struct hlist_node
*node
;
919 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
920 head
= &marker_table
[i
];
921 hlist_for_each_entry(entry
, node
, head
, hlist
) {
923 if (entry
->single
.func
== probe
924 && entry
->single
.probe_private
928 struct marker_probe_closure
*closure
;
929 closure
= entry
->multi
;
930 for (i
= 0; closure
[i
].func
; i
++) {
931 if (closure
[i
].func
== probe
&&
932 closure
[i
].probe_private
943 * marker_probe_unregister_private_data - Disconnect a probe from a marker
944 * @probe: probe function
945 * @probe_private: probe private data
947 * Unregister a probe by providing the registered private data.
948 * Only removes the first marker found in hash table.
949 * Return 0 on success or error value.
950 * We do not need to call a synchronize_sched to make sure the probes have
951 * finished running before doing a module unload, because the module unload
952 * itself uses stop_machine(), which insures that every preempt disabled section
955 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
958 struct marker_entry
*entry
;
960 struct marker_probe_closure
*old
;
961 const char *channel
= NULL
, *name
= NULL
;
963 mutex_lock(&markers_mutex
);
964 entry
= get_marker_from_private_data(probe
, probe_private
);
969 //ust// if (entry->rcu_pending)
970 //ust// rcu_barrier_sched();
971 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
972 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
973 name
= kstrdup(entry
->name
, GFP_KERNEL
);
974 mutex_unlock(&markers_mutex
);
976 marker_update_probes();
978 mutex_lock(&markers_mutex
);
979 entry
= get_marker(channel
, name
);
982 //ust// if (entry->rcu_pending)
983 //ust// rcu_barrier_sched();
985 entry
->rcu_pending
= 1;
986 /* write rcu_pending before calling the RCU callback */
988 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
989 synchronize_rcu(); free_old_closure(&entry
->rcu
);
990 /* Ignore busy error message */
991 remove_marker(channel
, name
);
993 mutex_unlock(&markers_mutex
);
998 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1001 * marker_get_private_data - Get a marker's probe private data
1002 * @channel: marker channel
1003 * @name: marker name
1004 * @probe: probe to match
1005 * @num: get the nth matching probe's private data
1007 * Returns the nth private data pointer (starting from 0) matching, or an
1009 * Returns the private data pointer, or an ERR_PTR.
1010 * The private data pointer should _only_ be dereferenced if the caller is the
1011 * owner of the data, or its content could vanish. This is mostly used to
1012 * confirm that a caller is the owner of a registered probe.
1014 void *marker_get_private_data(const char *channel
, const char *name
,
1015 marker_probe_func
*probe
, int num
)
1017 struct hlist_head
*head
;
1018 struct hlist_node
*node
;
1019 struct marker_entry
*e
;
1020 size_t channel_len
= strlen(channel
) + 1;
1021 size_t name_len
= strlen(name
) + 1;
1025 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1026 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1027 hlist_for_each_entry(e
, node
, head
, hlist
) {
1028 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1030 if (num
== 0 && e
->single
.func
== probe
)
1031 return e
->single
.probe_private
;
1033 struct marker_probe_closure
*closure
;
1036 for (i
= 0; closure
[i
].func
; i
++) {
1037 if (closure
[i
].func
!= probe
)
1040 return closure
[i
].probe_private
;
1046 return ERR_PTR(-ENOENT
);
1048 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1051 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1053 * Called when no channel users are active by the channel infrastructure.
1054 * Called with lock_markers() and channel mutex held.
1056 //ust// void markers_compact_event_ids(void)
1058 //ust// struct marker_entry *entry;
1059 //ust// unsigned int i;
1060 //ust// struct hlist_head *head;
1061 //ust// struct hlist_node *node;
1064 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1065 //ust// head = &marker_table[i];
1066 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1067 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1068 //ust// WARN_ON(ret < 0);
1069 //ust// entry->channel_id = ret;
1070 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1071 //ust// entry->name);
1072 //ust// WARN_ON(ret < 0);
1073 //ust// entry->event_id = ret;
1078 //ust//#ifdef CONFIG_MODULES
1081 * Returns 0 if current not found.
1082 * Returns 1 if current found.
1084 int lib_get_iter_markers(struct marker_iter
*iter
)
1086 struct lib
*iter_lib
;
1089 //ust// mutex_lock(&module_mutex);
1090 list_for_each_entry(iter_lib
, &libs
, list
) {
1091 if (iter_lib
< iter
->lib
)
1093 else if (iter_lib
> iter
->lib
)
1094 iter
->marker
= NULL
;
1095 found
= marker_get_iter_range(&iter
->marker
,
1096 iter_lib
->markers_start
,
1097 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1099 iter
->lib
= iter_lib
;
1103 //ust// mutex_unlock(&module_mutex);
1108 * marker_get_iter_range - Get a next marker iterator given a range.
1109 * @marker: current markers (in), next marker (out)
1110 * @begin: beginning of the range
1111 * @end: end of the range
1113 * Returns whether a next marker has been found (1) or not (0).
1114 * Will return the first marker in the range if the input marker is NULL.
1116 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1119 if (!*marker
&& begin
!= end
) {
1123 if (*marker
>= begin
&& *marker
< end
)
1127 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1129 static void marker_get_iter(struct marker_iter
*iter
)
1133 /* Core kernel markers */
1135 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1136 found
= marker_get_iter_range(&iter
->marker
,
1137 __start___markers
, __stop___markers
);
1141 /* Markers in modules. */
1142 found
= lib_get_iter_markers(iter
);
1145 marker_iter_reset(iter
);
1148 void marker_iter_start(struct marker_iter
*iter
)
1150 marker_get_iter(iter
);
1152 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1154 void marker_iter_next(struct marker_iter
*iter
)
1158 * iter->marker may be invalid because we blindly incremented it.
1159 * Make sure it is valid by marshalling on the markers, getting the
1160 * markers from following modules if necessary.
1162 marker_get_iter(iter
);
1164 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1166 void marker_iter_stop(struct marker_iter
*iter
)
1169 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1171 void marker_iter_reset(struct marker_iter
*iter
)
1174 iter
->marker
= NULL
;
1176 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1178 #ifdef CONFIG_MARKERS_USERSPACE
1180 * must be called with current->user_markers_mutex held
1182 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1184 struct user_marker
*umark
;
1185 struct hlist_node
*pos
, *n
;
1187 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1188 if (umark
->state
== state
) {
1189 hlist_del(&umark
->hlist
);
1195 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1196 //ust// char __user *state, int reg)
1198 //ust// struct user_marker *umark;
1200 //ust// struct marker_entry *entry;
1201 //ust// int ret = 0;
1203 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1204 //ust// current->comm, reg ? "registers" : "unregisters",
1205 //ust// name, state);
1207 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1208 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1209 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1210 //ust// umark->state = state;
1211 //ust// len = strncpy_from_user(umark->name, name,
1212 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1213 //ust// if (len < 0) {
1214 //ust// ret = -EFAULT;
1217 //ust// len = strncpy_from_user(umark->format, format,
1218 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1219 //ust// if (len < 0) {
1220 //ust// ret = -EFAULT;
1223 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1224 //ust// umark->format);
1225 //ust// mutex_lock(&markers_mutex);
1226 //ust// entry = get_marker("userspace", umark->name);
1227 //ust// if (entry) {
1228 //ust// if (entry->format &&
1229 //ust// strcmp(entry->format, umark->format) != 0) {
1230 //ust// printk(" error, wrong format in process %s",
1231 //ust// current->comm);
1232 //ust// ret = -EPERM;
1233 //ust// goto error_unlock;
1235 //ust// printk(" %s", !!entry->refcount
1236 //ust// ? "enabled" : "disabled");
1237 //ust// if (put_user(!!entry->refcount, state)) {
1238 //ust// ret = -EFAULT;
1239 //ust// goto error_unlock;
1241 //ust// printk("\n");
1243 //ust// printk(" disabled\n");
1244 //ust// if (put_user(0, umark->state)) {
1245 //ust// printk(KERN_WARNING
1246 //ust// "Marker in %s caused a fault\n",
1247 //ust// current->comm);
1248 //ust// goto error_unlock;
1251 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1252 //ust// hlist_add_head(&umark->hlist,
1253 //ust// ¤t->group_leader->user_markers);
1254 //ust// current->group_leader->user_markers_sequence++;
1255 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1256 //ust// mutex_unlock(&markers_mutex);
1258 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1259 //ust// free_user_marker(state,
1260 //ust// ¤t->group_leader->user_markers);
1261 //ust// current->group_leader->user_markers_sequence++;
1262 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1265 //ust// error_unlock:
1266 //ust// mutex_unlock(&markers_mutex);
1268 //ust// kfree(umark);
1275 //ust// * string : 0
1277 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1278 //ust// char __user *ubuf)
1280 //ust// long ret = -EPERM;
1284 //ust// switch (type) {
1285 //ust// case 0: /* String */
1286 //ust// ret = -ENOMEM;
1287 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1289 //ust// goto string_out;
1290 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1291 //ust// if (len < 0) {
1292 //ust// ret = -EFAULT;
1293 //ust// goto string_err;
1295 //ust// trace_mark(userspace, string, "string %s", page);
1297 //ust// free_page((unsigned long) page);
1306 //ust// static void marker_update_processes(void)
1308 //ust// struct task_struct *g, *t;
1311 //ust// * markers_mutex is taken to protect the p->user_markers read.
1313 //ust// mutex_lock(&markers_mutex);
1314 //ust// read_lock(&tasklist_lock);
1315 //ust// for_each_process(g) {
1316 //ust// WARN_ON(!thread_group_leader(g));
1317 //ust// if (hlist_empty(&g->user_markers))
1319 //ust// if (strcmp(g->comm, "testprog") == 0)
1320 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1323 //ust// /* TODO : implement this thread flag in each arch. */
1324 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1325 //ust// } while ((t = next_thread(t)) != g);
1327 //ust// read_unlock(&tasklist_lock);
1328 //ust// mutex_unlock(&markers_mutex);
1332 * Update current process.
1333 * Note that we have to wait a whole scheduler period before we are sure that
1334 * every running userspace threads have their markers updated.
1335 * (synchronize_sched() can be used to insure this).
1337 void marker_update_process(void)
1339 struct user_marker
*umark
;
1340 struct hlist_node
*pos
;
1341 struct marker_entry
*entry
;
1343 mutex_lock(&markers_mutex
);
1344 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1345 if (strcmp(current
->comm
, "testprog") == 0)
1346 printk(KERN_DEBUG
"do update pending for testprog\n");
1347 hlist_for_each_entry(umark
, pos
,
1348 ¤t
->group_leader
->user_markers
, hlist
) {
1349 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1350 umark
->name
, current
->comm
);
1351 entry
= get_marker("userspace", umark
->name
);
1353 if (entry
->format
&&
1354 strcmp(entry
->format
, umark
->format
) != 0) {
1356 " error, wrong format in process %s\n",
1360 if (put_user(!!entry
->refcount
, umark
->state
)) {
1362 "Marker in %s caused a fault\n",
1367 if (put_user(0, umark
->state
)) {
1369 "Marker in %s caused a fault\n",
1375 clear_thread_flag(TIF_MARKER_PENDING
);
1376 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1377 mutex_unlock(&markers_mutex
);
1381 * Called at process exit and upon do_execve().
1382 * We assume that when the leader exits, no more references can be done to the
1383 * leader structure by the other threads.
1385 void exit_user_markers(struct task_struct
*p
)
1387 struct user_marker
*umark
;
1388 struct hlist_node
*pos
, *n
;
1390 if (thread_group_leader(p
)) {
1391 mutex_lock(&markers_mutex
);
1392 mutex_lock(&p
->user_markers_mutex
);
1393 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1396 INIT_HLIST_HEAD(&p
->user_markers
);
1397 p
->user_markers_sequence
++;
1398 mutex_unlock(&p
->user_markers_mutex
);
1399 mutex_unlock(&markers_mutex
);
1403 int is_marker_enabled(const char *channel
, const char *name
)
1405 struct marker_entry
*entry
;
1407 mutex_lock(&markers_mutex
);
1408 entry
= get_marker(channel
, name
);
1409 mutex_unlock(&markers_mutex
);
1411 return entry
&& !!entry
->refcount
;
1415 int marker_module_notify(struct notifier_block
*self
,
1416 unsigned long val
, void *data
)
1418 struct module
*mod
= data
;
1421 case MODULE_STATE_COMING
:
1422 marker_update_probe_range(mod
->markers
,
1423 mod
->markers
+ mod
->num_markers
);
1425 case MODULE_STATE_GOING
:
1426 marker_update_probe_range(mod
->markers
,
1427 mod
->markers
+ mod
->num_markers
);
1433 struct notifier_block marker_module_nb
= {
1434 .notifier_call
= marker_module_notify
,
1438 //ust// static int init_markers(void)
1440 //ust// return register_module_notifier(&marker_module_nb);
1442 //ust// __initcall(init_markers);
1443 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1445 #endif /* CONFIG_MODULES */
1447 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1449 struct marker_entry
*entry
;
1450 struct ltt_probe_private_data call_data
;
1451 struct hlist_head
*head
;
1452 struct hlist_node
*node
;
1455 mutex_lock(&markers_mutex
);
1456 call_data
.trace
= trace
;
1457 call_data
.serializer
= NULL
;
1459 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1460 head
= &marker_table
[i
];
1461 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1462 __trace_mark(0, metadata
, core_marker_id
,
1464 "channel %s name %s event_id %hu "
1465 "int #1u%zu long #1u%zu pointer #1u%zu "
1466 "size_t #1u%zu alignment #1u%u",
1470 sizeof(int), sizeof(long),
1471 sizeof(void *), sizeof(size_t),
1472 ltt_get_alignment());
1474 __trace_mark(0, metadata
,
1477 "channel %s name %s format %s",
1483 mutex_unlock(&markers_mutex
);
1485 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1487 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1489 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1494 static void new_markers(struct marker
*start
, struct marker
*end
)
1498 for(m
=start
; m
< end
; m
++) {
1504 int marker_register_lib(struct marker
*markers_start
, struct marker_addr
*marker_addr_start
, int markers_count
)
1507 struct marker_addr
*addr
;
1509 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1511 pl
->markers_start
= markers_start
;
1512 pl
->markers_addr_start
= marker_addr_start
;
1513 pl
->markers_count
= markers_count
;
1516 for(addr
= marker_addr_start
; addr
< marker_addr_start
+ markers_count
; addr
++)
1517 addr
->marker
->location
= addr
->addr
;
1520 /* FIXME: maybe protect this with its own mutex? */
1522 list_add(&pl
->list
, &libs
);
1525 new_markers(markers_start
, markers_start
+ markers_count
);
1527 /* FIXME: update just the loaded lib */
1528 lib_update_markers();
1530 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1535 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1537 /*FIXME: implement; but before implementing, marker_register_lib must
1538 have appropriate locking. */
1543 static int initialized
= 0;
1545 void __attribute__((constructor
)) init_markers(void)
1548 marker_register_lib(__start___markers
, __start___marker_addr
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1549 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);