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 #ifdef CONFIG_UST_GDB_INTEGRATION
51 extern struct marker_addr __start___marker_addr
[] __attribute__((visibility("hidden")));
52 extern struct marker_addr __stop___marker_addr
[] __attribute__((visibility("hidden")));
55 /* Set to 1 to enable marker debug output */
56 static const int marker_debug
;
59 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
60 * and module markers and the hash table.
62 static DEFINE_MUTEX(markers_mutex
);
64 static LIST_HEAD(libs
);
67 void lock_markers(void)
69 mutex_lock(&markers_mutex
);
72 void unlock_markers(void)
74 mutex_unlock(&markers_mutex
);
78 * Marker hash table, containing the active markers.
79 * Protected by module_mutex.
81 #define MARKER_HASH_BITS 6
82 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
83 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
87 * It is used to make sure every handler has finished using its private data
88 * between two consecutive operation (add or remove) on a given marker. It is
89 * also used to delay the free of multiple probes array until a quiescent state
91 * marker entries modifications are protected by the markers_mutex.
94 struct hlist_node hlist
;
98 void (*call
)(const struct marker
*mdata
, void *call_private
, struct registers
*regs
, ...);
99 struct marker_probe_closure single
;
100 struct marker_probe_closure
*multi
;
101 int refcount
; /* Number of times armed. 0 if disarmed. */
107 unsigned char ptype
:1;
108 unsigned char format_allocated
:1;
109 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
112 #ifdef CONFIG_MARKERS_USERSPACE
113 static void marker_update_processes(void);
115 static void marker_update_processes(void)
121 * __mark_empty_function - Empty probe callback
122 * @mdata: marker data
123 * @probe_private: probe private data
124 * @call_private: call site private data
125 * @fmt: format string
126 * @...: variable argument list
128 * Empty callback provided as a probe to the markers. By providing this to a
129 * disabled marker, we make sure the execution flow is always valid even
130 * though the function pointer change and the marker enabling are two distinct
131 * operations that modifies the execution flow of preemptible code.
133 notrace
void __mark_empty_function(const struct marker
*mdata
,
134 void *probe_private
, struct registers
*regs
, void *call_private
, const char *fmt
, va_list *args
)
137 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
140 * marker_probe_cb Callback that prepares the variable argument list for probes.
141 * @mdata: pointer of type struct marker
142 * @call_private: caller site private data
143 * @...: Variable argument list.
145 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
146 * need to put a full smp_rmb() in this branch. This is why we do not use
147 * rcu_dereference() for the pointer read.
149 notrace
void marker_probe_cb(const struct marker
*mdata
,
150 void *call_private
, struct registers
*regs
, ...)
156 * rcu_read_lock_sched does two things : disabling preemption to make
157 * sure the teardown of the callbacks can be done correctly when they
158 * are in modules and they insure RCU read coherency.
160 //ust// rcu_read_lock_sched_notrace();
161 ptype
= mdata
->ptype
;
162 if (likely(!ptype
)) {
163 marker_probe_func
*func
;
164 /* Must read the ptype before ptr. They are not data dependant,
165 * so we put an explicit smp_rmb() here. */
167 func
= mdata
->single
.func
;
168 /* Must read the ptr before private data. They are not data
169 * dependant, so we put an explicit smp_rmb() here. */
171 va_start(args
, regs
);
172 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
173 mdata
->format
, &args
);
176 struct marker_probe_closure
*multi
;
179 * Read mdata->ptype before mdata->multi.
182 multi
= mdata
->multi
;
184 * multi points to an array, therefore accessing the array
185 * depends on reading multi. However, even in this case,
186 * we must insure that the pointer is read _before_ the array
187 * data. Same as rcu_dereference, but we need a full smp_rmb()
188 * in the fast path, so put the explicit barrier here.
190 smp_read_barrier_depends();
191 for (i
= 0; multi
[i
].func
; i
++) {
192 va_start(args
, regs
);
193 multi
[i
].func(mdata
, multi
[i
].probe_private
,
194 regs
, call_private
, mdata
->format
, &args
);
198 //ust// rcu_read_unlock_sched_notrace();
200 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
203 * marker_probe_cb Callback that does not prepare the variable argument list.
204 * @mdata: pointer of type struct marker
205 * @call_private: caller site private data
206 * @...: Variable argument list.
208 * Should be connected to markers "MARK_NOARGS".
210 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
211 void *call_private
, struct registers
*regs
, ...)
213 va_list args
; /* not initialized */
216 //ust// rcu_read_lock_sched_notrace();
217 ptype
= mdata
->ptype
;
218 if (likely(!ptype
)) {
219 marker_probe_func
*func
;
220 /* Must read the ptype before ptr. They are not data dependant,
221 * so we put an explicit smp_rmb() here. */
223 func
= mdata
->single
.func
;
224 /* Must read the ptr before private data. They are not data
225 * dependant, so we put an explicit smp_rmb() here. */
227 func(mdata
, mdata
->single
.probe_private
, regs
, call_private
,
228 mdata
->format
, &args
);
230 struct marker_probe_closure
*multi
;
233 * Read mdata->ptype before mdata->multi.
236 multi
= mdata
->multi
;
238 * multi points to an array, therefore accessing the array
239 * depends on reading multi. However, even in this case,
240 * we must insure that the pointer is read _before_ the array
241 * data. Same as rcu_dereference, but we need a full smp_rmb()
242 * in the fast path, so put the explicit barrier here.
244 smp_read_barrier_depends();
245 for (i
= 0; multi
[i
].func
; i
++)
246 multi
[i
].func(mdata
, multi
[i
].probe_private
, regs
,
247 call_private
, mdata
->format
, &args
);
249 //ust// rcu_read_unlock_sched_notrace();
252 static void free_old_closure(struct rcu_head
*head
)
254 struct marker_entry
*entry
= container_of(head
,
255 struct marker_entry
, rcu
);
256 kfree(entry
->oldptr
);
257 /* Make sure we free the data before setting the pending flag to 0 */
259 entry
->rcu_pending
= 0;
262 static void debug_print_probes(struct marker_entry
*entry
)
270 printk(KERN_DEBUG
"Single probe : %p %p\n",
272 entry
->single
.probe_private
);
274 for (i
= 0; entry
->multi
[i
].func
; i
++)
275 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
276 entry
->multi
[i
].func
,
277 entry
->multi
[i
].probe_private
);
281 static struct marker_probe_closure
*
282 marker_entry_add_probe(struct marker_entry
*entry
,
283 marker_probe_func
*probe
, void *probe_private
)
286 struct marker_probe_closure
*old
, *new;
290 debug_print_probes(entry
);
293 if (entry
->single
.func
== probe
&&
294 entry
->single
.probe_private
== probe_private
)
295 return ERR_PTR(-EBUSY
);
296 if (entry
->single
.func
== __mark_empty_function
) {
298 entry
->single
.func
= probe
;
299 entry
->single
.probe_private
= probe_private
;
302 debug_print_probes(entry
);
310 /* (N -> N+1), (N != 0, 1) probes */
311 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
312 if (old
[nr_probes
].func
== probe
313 && old
[nr_probes
].probe_private
315 return ERR_PTR(-EBUSY
);
317 /* + 2 : one for new probe, one for NULL func */
318 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
321 return ERR_PTR(-ENOMEM
);
323 new[0] = entry
->single
;
326 nr_probes
* sizeof(struct marker_probe_closure
));
327 new[nr_probes
].func
= probe
;
328 new[nr_probes
].probe_private
= probe_private
;
329 entry
->refcount
= nr_probes
+ 1;
332 debug_print_probes(entry
);
336 static struct marker_probe_closure
*
337 marker_entry_remove_probe(struct marker_entry
*entry
,
338 marker_probe_func
*probe
, void *probe_private
)
340 int nr_probes
= 0, nr_del
= 0, i
;
341 struct marker_probe_closure
*old
, *new;
345 debug_print_probes(entry
);
347 /* 0 -> N is an error */
348 WARN_ON(entry
->single
.func
== __mark_empty_function
);
350 WARN_ON(probe
&& entry
->single
.func
!= probe
);
351 WARN_ON(entry
->single
.probe_private
!= probe_private
);
352 entry
->single
.func
= __mark_empty_function
;
355 debug_print_probes(entry
);
358 /* (N -> M), (N > 1, M >= 0) probes */
359 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
360 if ((!probe
|| old
[nr_probes
].func
== probe
)
361 && old
[nr_probes
].probe_private
367 if (nr_probes
- nr_del
== 0) {
368 /* N -> 0, (N > 1) */
369 entry
->single
.func
= __mark_empty_function
;
372 } else if (nr_probes
- nr_del
== 1) {
373 /* N -> 1, (N > 1) */
374 for (i
= 0; old
[i
].func
; i
++)
375 if ((probe
&& old
[i
].func
!= probe
) ||
376 old
[i
].probe_private
!= probe_private
)
377 entry
->single
= old
[i
];
382 /* N -> M, (N > 1, M > 1) */
384 new = kzalloc((nr_probes
- nr_del
+ 1)
385 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
387 return ERR_PTR(-ENOMEM
);
388 for (i
= 0; old
[i
].func
; i
++)
389 if ((probe
&& old
[i
].func
!= probe
) ||
390 old
[i
].probe_private
!= probe_private
)
392 entry
->refcount
= nr_probes
- nr_del
;
396 debug_print_probes(entry
);
401 * Get marker if the marker is present in the marker hash table.
402 * Must be called with markers_mutex held.
403 * Returns NULL if not present.
405 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
407 struct hlist_head
*head
;
408 struct hlist_node
*node
;
409 struct marker_entry
*e
;
410 size_t channel_len
= strlen(channel
) + 1;
411 size_t name_len
= strlen(name
) + 1;
414 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
415 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
416 hlist_for_each_entry(e
, node
, head
, hlist
) {
417 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
424 * Add the marker to the marker hash table. Must be called with markers_mutex
427 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
430 struct hlist_head
*head
;
431 struct hlist_node
*node
;
432 struct marker_entry
*e
;
433 size_t channel_len
= strlen(channel
) + 1;
434 size_t name_len
= strlen(name
) + 1;
435 size_t format_len
= 0;
438 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
440 format_len
= strlen(format
) + 1;
441 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
442 hlist_for_each_entry(e
, node
, head
, hlist
) {
443 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
445 "Marker %s.%s busy\n", channel
, name
);
446 return ERR_PTR(-EBUSY
); /* Already there */
450 * Using kmalloc here to allocate a variable length element. Could
451 * cause some memory fragmentation if overused.
453 e
= kmalloc(sizeof(struct marker_entry
)
454 + channel_len
+ name_len
+ format_len
,
457 return ERR_PTR(-ENOMEM
);
458 memcpy(e
->channel
, channel
, channel_len
);
459 e
->name
= &e
->channel
[channel_len
];
460 memcpy(e
->name
, name
, name_len
);
462 e
->format
= &e
->name
[channel_len
+ name_len
];
463 memcpy(e
->format
, format
, format_len
);
464 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
465 e
->call
= marker_probe_cb_noarg
;
467 e
->call
= marker_probe_cb
;
468 trace_mark(metadata
, core_marker_format
,
469 "channel %s name %s format %s",
470 e
->channel
, e
->name
, e
->format
);
473 e
->call
= marker_probe_cb
;
475 e
->single
.func
= __mark_empty_function
;
476 e
->single
.probe_private
= NULL
;
479 e
->format_allocated
= 0;
482 hlist_add_head(&e
->hlist
, head
);
487 * Remove the marker from the marker hash table. Must be called with mutex_lock
490 static int remove_marker(const char *channel
, const char *name
)
492 struct hlist_head
*head
;
493 struct hlist_node
*node
;
494 struct marker_entry
*e
;
496 size_t channel_len
= strlen(channel
) + 1;
497 size_t name_len
= strlen(name
) + 1;
501 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
502 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
503 hlist_for_each_entry(e
, node
, head
, hlist
) {
504 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
511 if (e
->single
.func
!= __mark_empty_function
)
513 hlist_del(&e
->hlist
);
514 if (e
->format_allocated
)
516 ret
= ltt_channels_unregister(e
->channel
);
518 /* Make sure the call_rcu has been executed */
519 //ust// if (e->rcu_pending)
520 //ust// rcu_barrier_sched();
526 * Set the mark_entry format to the format found in the element.
528 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
530 entry
->format
= kstrdup(format
, GFP_KERNEL
);
533 entry
->format_allocated
= 1;
535 trace_mark(metadata
, core_marker_format
,
536 "channel %s name %s format %s",
537 entry
->channel
, entry
->name
, entry
->format
);
542 * Sets the probe callback corresponding to one marker.
544 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
548 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
551 if (strcmp(entry
->format
, elem
->format
) != 0) {
553 "Format mismatch for probe %s "
554 "(%s), marker (%s)\n",
561 ret
= marker_set_format(entry
, elem
->format
);
567 * probe_cb setup (statically known) is done here. It is
568 * asynchronous with the rest of execution, therefore we only
569 * pass from a "safe" callback (with argument) to an "unsafe"
570 * callback (does not set arguments).
572 elem
->call
= entry
->call
;
573 elem
->channel_id
= entry
->channel_id
;
574 elem
->event_id
= entry
->event_id
;
577 * We only update the single probe private data when the ptr is
578 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
580 WARN_ON(elem
->single
.func
!= __mark_empty_function
581 && elem
->single
.probe_private
!= entry
->single
.probe_private
583 elem
->single
.probe_private
= entry
->single
.probe_private
;
585 * Make sure the private data is valid when we update the
589 elem
->single
.func
= entry
->single
.func
;
591 * We also make sure that the new probe callbacks array is consistent
592 * before setting a pointer to it.
594 rcu_assign_pointer(elem
->multi
, entry
->multi
);
596 * Update the function or multi probe array pointer before setting the
600 elem
->ptype
= entry
->ptype
;
602 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
603 //ust// WARN_ON(!elem->tp_cb);
605 //ust// * It is ok to directly call the probe registration because type
606 //ust// * checking has been done in the __trace_mark_tp() macro.
609 //ust// if (active) {
611 //ust// * try_module_get should always succeed because we hold
612 //ust// * markers_mutex to get the tp_cb address.
614 //ust// ret = try_module_get(__module_text_address(
615 //ust// (unsigned long)elem->tp_cb));
616 //ust// BUG_ON(!ret);
617 //ust// ret = tracepoint_probe_register_noupdate(
618 //ust// elem->tp_name,
619 //ust// elem->tp_cb);
621 //ust// ret = tracepoint_probe_unregister_noupdate(
622 //ust// elem->tp_name,
623 //ust// elem->tp_cb);
625 //ust// * tracepoint_probe_update_all() must be called
626 //ust// * before the module containing tp_cb is unloaded.
628 //ust// module_put(__module_text_address(
629 //ust// (unsigned long)elem->tp_cb));
632 elem
->state__imv
= active
;
638 * Disable a marker and its probe callback.
639 * Note: only waiting an RCU period after setting elem->call to the empty
640 * function insures that the original callback is not used anymore. This insured
641 * by rcu_read_lock_sched around the call site.
643 static void disable_marker(struct marker
*elem
)
647 //ust// /* leave "call" as is. It is known statically. */
648 //ust// if (elem->tp_name && _imv_read(elem->state)) {
649 //ust// WARN_ON(!elem->tp_cb);
651 //ust// * It is ok to directly call the probe registration because type
652 //ust// * checking has been done in the __trace_mark_tp() macro.
654 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
655 //ust// elem->tp_cb);
656 //ust// WARN_ON(ret);
658 //ust// * tracepoint_probe_update_all() must be called
659 //ust// * before the module containing tp_cb is unloaded.
661 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
663 elem
->state__imv
= 0;
664 elem
->single
.func
= __mark_empty_function
;
665 /* Update the function before setting the ptype */
667 elem
->ptype
= 0; /* single probe */
669 * Leave the private data and channel_id/event_id there, because removal
670 * is racy and should be done only after an RCU period. These are never
671 * used until the next initialization anyway.
676 * is_marker_enabled - Check if a marker is enabled
677 * @channel: channel name
680 * Returns 1 if the marker is enabled, 0 if disabled.
682 int is_marker_enabled(const char *channel
, const char *name
)
684 struct marker_entry
*entry
;
686 mutex_lock(&markers_mutex
);
687 entry
= get_marker(channel
, name
);
688 mutex_unlock(&markers_mutex
);
690 return entry
&& !!entry
->refcount
;
694 * marker_update_probe_range - Update a probe range
695 * @begin: beginning of the range
696 * @end: end of the range
698 * Updates the probe callback corresponding to a range of markers.
700 void marker_update_probe_range(struct marker
*begin
,
704 struct marker_entry
*mark_entry
;
706 mutex_lock(&markers_mutex
);
707 for (iter
= begin
; iter
< end
; iter
++) {
708 mark_entry
= get_marker(iter
->channel
, iter
->name
);
710 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
712 * ignore error, continue
715 /* This is added for UST. We emit a core_marker_id event
716 * for markers that are already registered to a probe
717 * upon library load. Otherwise, no core_marker_id will
718 * be generated for these markers. Is this the right thing
721 trace_mark(metadata
, core_marker_id
,
722 "channel %s name %s event_id %hu "
723 "int #1u%zu long #1u%zu pointer #1u%zu "
724 "size_t #1u%zu alignment #1u%u",
725 iter
->channel
, iter
->name
, mark_entry
->event_id
,
726 sizeof(int), sizeof(long), sizeof(void *),
727 sizeof(size_t), ltt_get_alignment());
729 disable_marker(iter
);
732 mutex_unlock(&markers_mutex
);
735 static void lib_update_markers(void)
739 /* FIXME: we should probably take a mutex here on libs */
740 //ust// mutex_lock(&module_mutex);
741 list_for_each_entry(lib
, &libs
, list
)
742 marker_update_probe_range(lib
->markers_start
,
743 lib
->markers_start
+ lib
->markers_count
);
744 //ust// mutex_unlock(&module_mutex);
748 * Update probes, removing the faulty probes.
750 * Internal callback only changed before the first probe is connected to it.
751 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
752 * transitions. All other transitions will leave the old private data valid.
753 * This makes the non-atomicity of the callback/private data updates valid.
755 * "special case" updates :
760 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
761 * Site effect : marker_set_format may delete the marker entry (creating a
764 static void marker_update_probes(void)
766 /* Core kernel markers */
767 //ust// marker_update_probe_range(__start___markers, __stop___markers);
768 /* Markers in modules. */
769 //ust// module_update_markers();
770 lib_update_markers();
771 //ust// tracepoint_probe_update_all();
772 /* Update immediate values */
774 //ust// module_imv_update(); /* FIXME: need to port for libs? */
775 marker_update_processes();
779 * marker_probe_register - Connect a probe to a marker
780 * @channel: marker channel
782 * @format: format string
783 * @probe: probe handler
784 * @probe_private: probe private data
786 * private data must be a valid allocated memory address, or NULL.
787 * Returns 0 if ok, error value on error.
788 * The probe address must at least be aligned on the architecture pointer size.
790 int marker_probe_register(const char *channel
, const char *name
,
791 const char *format
, marker_probe_func
*probe
,
794 struct marker_entry
*entry
;
795 int ret
= 0, ret_err
;
796 struct marker_probe_closure
*old
;
799 mutex_lock(&markers_mutex
);
800 entry
= get_marker(channel
, name
);
803 entry
= add_marker(channel
, name
, format
);
805 ret
= PTR_ERR(entry
);
808 ret
= ltt_channels_register(channel
);
810 goto error_remove_marker
;
811 ret
= ltt_channels_get_index_from_name(channel
);
813 goto error_unregister_channel
;
814 entry
->channel_id
= ret
;
815 ret
= ltt_channels_get_event_id(channel
, name
);
817 goto error_unregister_channel
;
818 entry
->event_id
= ret
;
820 trace_mark(metadata
, core_marker_id
,
821 "channel %s name %s event_id %hu "
822 "int #1u%zu long #1u%zu pointer #1u%zu "
823 "size_t #1u%zu alignment #1u%u",
824 channel
, name
, entry
->event_id
,
825 sizeof(int), sizeof(long), sizeof(void *),
826 sizeof(size_t), ltt_get_alignment());
829 ret
= marker_set_format(entry
, format
);
830 else if (strcmp(entry
->format
, format
))
837 * If we detect that a call_rcu is pending for this marker,
838 * make sure it's executed now.
840 //ust// if (entry->rcu_pending)
841 //ust// rcu_barrier_sched();
842 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
846 goto error_unregister_channel
;
850 mutex_unlock(&markers_mutex
);
852 /* Activate marker if necessary */
853 marker_update_probes();
855 mutex_lock(&markers_mutex
);
856 entry
= get_marker(channel
, name
);
859 //ust// if (entry->rcu_pending)
860 //ust// rcu_barrier_sched();
862 entry
->rcu_pending
= 1;
863 /* write rcu_pending before calling the RCU callback */
865 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
866 synchronize_rcu(); free_old_closure(&entry
->rcu
);
869 error_unregister_channel
:
870 ret_err
= ltt_channels_unregister(channel
);
873 ret_err
= remove_marker(channel
, name
);
876 mutex_unlock(&markers_mutex
);
879 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
882 * marker_probe_unregister - Disconnect a probe from a marker
883 * @channel: marker channel
885 * @probe: probe function pointer
886 * @probe_private: probe private data
888 * Returns the private data given to marker_probe_register, or an ERR_PTR().
889 * We do not need to call a synchronize_sched to make sure the probes have
890 * finished running before doing a module unload, because the module unload
891 * itself uses stop_machine(), which insures that every preempt disabled section
894 int marker_probe_unregister(const char *channel
, const char *name
,
895 marker_probe_func
*probe
, void *probe_private
)
897 struct marker_entry
*entry
;
898 struct marker_probe_closure
*old
;
901 mutex_lock(&markers_mutex
);
902 entry
= get_marker(channel
, name
);
905 //ust// if (entry->rcu_pending)
906 //ust// rcu_barrier_sched();
907 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
908 mutex_unlock(&markers_mutex
);
910 marker_update_probes();
912 mutex_lock(&markers_mutex
);
913 entry
= get_marker(channel
, name
);
916 //ust// if (entry->rcu_pending)
917 //ust// rcu_barrier_sched();
919 entry
->rcu_pending
= 1;
920 /* write rcu_pending before calling the RCU callback */
922 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
923 synchronize_rcu(); free_old_closure(&entry
->rcu
);
924 remove_marker(channel
, name
); /* Ignore busy error message */
927 mutex_unlock(&markers_mutex
);
930 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
932 static struct marker_entry
*
933 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
935 struct marker_entry
*entry
;
937 struct hlist_head
*head
;
938 struct hlist_node
*node
;
940 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
941 head
= &marker_table
[i
];
942 hlist_for_each_entry(entry
, node
, head
, hlist
) {
944 if (entry
->single
.func
== probe
945 && entry
->single
.probe_private
949 struct marker_probe_closure
*closure
;
950 closure
= entry
->multi
;
951 for (i
= 0; closure
[i
].func
; i
++) {
952 if (closure
[i
].func
== probe
&&
953 closure
[i
].probe_private
964 * marker_probe_unregister_private_data - Disconnect a probe from a marker
965 * @probe: probe function
966 * @probe_private: probe private data
968 * Unregister a probe by providing the registered private data.
969 * Only removes the first marker found in hash table.
970 * Return 0 on success or error value.
971 * We do not need to call a synchronize_sched to make sure the probes have
972 * finished running before doing a module unload, because the module unload
973 * itself uses stop_machine(), which insures that every preempt disabled section
976 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
979 struct marker_entry
*entry
;
981 struct marker_probe_closure
*old
;
982 const char *channel
= NULL
, *name
= NULL
;
984 mutex_lock(&markers_mutex
);
985 entry
= get_marker_from_private_data(probe
, probe_private
);
990 //ust// if (entry->rcu_pending)
991 //ust// rcu_barrier_sched();
992 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
993 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
994 name
= kstrdup(entry
->name
, GFP_KERNEL
);
995 mutex_unlock(&markers_mutex
);
997 marker_update_probes();
999 mutex_lock(&markers_mutex
);
1000 entry
= get_marker(channel
, name
);
1003 //ust// if (entry->rcu_pending)
1004 //ust// rcu_barrier_sched();
1005 entry
->oldptr
= old
;
1006 entry
->rcu_pending
= 1;
1007 /* write rcu_pending before calling the RCU callback */
1009 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
1010 synchronize_rcu(); free_old_closure(&entry
->rcu
);
1011 /* Ignore busy error message */
1012 remove_marker(channel
, name
);
1014 mutex_unlock(&markers_mutex
);
1019 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1022 * marker_get_private_data - Get a marker's probe private data
1023 * @channel: marker channel
1024 * @name: marker name
1025 * @probe: probe to match
1026 * @num: get the nth matching probe's private data
1028 * Returns the nth private data pointer (starting from 0) matching, or an
1030 * Returns the private data pointer, or an ERR_PTR.
1031 * The private data pointer should _only_ be dereferenced if the caller is the
1032 * owner of the data, or its content could vanish. This is mostly used to
1033 * confirm that a caller is the owner of a registered probe.
1035 void *marker_get_private_data(const char *channel
, const char *name
,
1036 marker_probe_func
*probe
, int num
)
1038 struct hlist_head
*head
;
1039 struct hlist_node
*node
;
1040 struct marker_entry
*e
;
1041 size_t channel_len
= strlen(channel
) + 1;
1042 size_t name_len
= strlen(name
) + 1;
1046 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1047 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1048 hlist_for_each_entry(e
, node
, head
, hlist
) {
1049 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1051 if (num
== 0 && e
->single
.func
== probe
)
1052 return e
->single
.probe_private
;
1054 struct marker_probe_closure
*closure
;
1057 for (i
= 0; closure
[i
].func
; i
++) {
1058 if (closure
[i
].func
!= probe
)
1061 return closure
[i
].probe_private
;
1067 return ERR_PTR(-ENOENT
);
1069 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1072 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1074 * Called when no channel users are active by the channel infrastructure.
1075 * Called with lock_markers() and channel mutex held.
1077 //ust// void markers_compact_event_ids(void)
1079 //ust// struct marker_entry *entry;
1080 //ust// unsigned int i;
1081 //ust// struct hlist_head *head;
1082 //ust// struct hlist_node *node;
1085 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1086 //ust// head = &marker_table[i];
1087 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1088 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1089 //ust// WARN_ON(ret < 0);
1090 //ust// entry->channel_id = ret;
1091 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1092 //ust// entry->name);
1093 //ust// WARN_ON(ret < 0);
1094 //ust// entry->event_id = ret;
1099 //ust//#ifdef CONFIG_MODULES
1102 * Returns 0 if current not found.
1103 * Returns 1 if current found.
1105 int lib_get_iter_markers(struct marker_iter
*iter
)
1107 struct lib
*iter_lib
;
1110 //ust// mutex_lock(&module_mutex);
1111 list_for_each_entry(iter_lib
, &libs
, list
) {
1112 if (iter_lib
< iter
->lib
)
1114 else if (iter_lib
> iter
->lib
)
1115 iter
->marker
= NULL
;
1116 found
= marker_get_iter_range(&iter
->marker
,
1117 iter_lib
->markers_start
,
1118 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1120 iter
->lib
= iter_lib
;
1124 //ust// mutex_unlock(&module_mutex);
1129 * marker_get_iter_range - Get a next marker iterator given a range.
1130 * @marker: current markers (in), next marker (out)
1131 * @begin: beginning of the range
1132 * @end: end of the range
1134 * Returns whether a next marker has been found (1) or not (0).
1135 * Will return the first marker in the range if the input marker is NULL.
1137 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1140 if (!*marker
&& begin
!= end
) {
1144 if (*marker
>= begin
&& *marker
< end
)
1148 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1150 static void marker_get_iter(struct marker_iter
*iter
)
1154 /* Core kernel markers */
1156 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1157 found
= marker_get_iter_range(&iter
->marker
,
1158 __start___markers
, __stop___markers
);
1162 /* Markers in modules. */
1163 found
= lib_get_iter_markers(iter
);
1166 marker_iter_reset(iter
);
1169 void marker_iter_start(struct marker_iter
*iter
)
1171 marker_get_iter(iter
);
1173 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1175 void marker_iter_next(struct marker_iter
*iter
)
1179 * iter->marker may be invalid because we blindly incremented it.
1180 * Make sure it is valid by marshalling on the markers, getting the
1181 * markers from following modules if necessary.
1183 marker_get_iter(iter
);
1185 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1187 void marker_iter_stop(struct marker_iter
*iter
)
1190 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1192 void marker_iter_reset(struct marker_iter
*iter
)
1195 iter
->marker
= NULL
;
1197 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1199 #ifdef CONFIG_MARKERS_USERSPACE
1201 * must be called with current->user_markers_mutex held
1203 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1205 struct user_marker
*umark
;
1206 struct hlist_node
*pos
, *n
;
1208 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1209 if (umark
->state
== state
) {
1210 hlist_del(&umark
->hlist
);
1216 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1217 //ust// char __user *state, int reg)
1219 //ust// struct user_marker *umark;
1221 //ust// struct marker_entry *entry;
1222 //ust// int ret = 0;
1224 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1225 //ust// current->comm, reg ? "registers" : "unregisters",
1226 //ust// name, state);
1228 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1229 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1230 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1231 //ust// umark->state = state;
1232 //ust// len = strncpy_from_user(umark->name, name,
1233 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1234 //ust// if (len < 0) {
1235 //ust// ret = -EFAULT;
1238 //ust// len = strncpy_from_user(umark->format, format,
1239 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1240 //ust// if (len < 0) {
1241 //ust// ret = -EFAULT;
1244 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1245 //ust// umark->format);
1246 //ust// mutex_lock(&markers_mutex);
1247 //ust// entry = get_marker("userspace", umark->name);
1248 //ust// if (entry) {
1249 //ust// if (entry->format &&
1250 //ust// strcmp(entry->format, umark->format) != 0) {
1251 //ust// printk(" error, wrong format in process %s",
1252 //ust// current->comm);
1253 //ust// ret = -EPERM;
1254 //ust// goto error_unlock;
1256 //ust// printk(" %s", !!entry->refcount
1257 //ust// ? "enabled" : "disabled");
1258 //ust// if (put_user(!!entry->refcount, state)) {
1259 //ust// ret = -EFAULT;
1260 //ust// goto error_unlock;
1262 //ust// printk("\n");
1264 //ust// printk(" disabled\n");
1265 //ust// if (put_user(0, umark->state)) {
1266 //ust// printk(KERN_WARNING
1267 //ust// "Marker in %s caused a fault\n",
1268 //ust// current->comm);
1269 //ust// goto error_unlock;
1272 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1273 //ust// hlist_add_head(&umark->hlist,
1274 //ust// ¤t->group_leader->user_markers);
1275 //ust// current->group_leader->user_markers_sequence++;
1276 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1277 //ust// mutex_unlock(&markers_mutex);
1279 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1280 //ust// free_user_marker(state,
1281 //ust// ¤t->group_leader->user_markers);
1282 //ust// current->group_leader->user_markers_sequence++;
1283 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1286 //ust// error_unlock:
1287 //ust// mutex_unlock(&markers_mutex);
1289 //ust// kfree(umark);
1296 //ust// * string : 0
1298 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1299 //ust// char __user *ubuf)
1301 //ust// long ret = -EPERM;
1305 //ust// switch (type) {
1306 //ust// case 0: /* String */
1307 //ust// ret = -ENOMEM;
1308 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1310 //ust// goto string_out;
1311 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1312 //ust// if (len < 0) {
1313 //ust// ret = -EFAULT;
1314 //ust// goto string_err;
1316 //ust// trace_mark(userspace, string, "string %s", page);
1318 //ust// free_page((unsigned long) page);
1327 //ust// static void marker_update_processes(void)
1329 //ust// struct task_struct *g, *t;
1332 //ust// * markers_mutex is taken to protect the p->user_markers read.
1334 //ust// mutex_lock(&markers_mutex);
1335 //ust// read_lock(&tasklist_lock);
1336 //ust// for_each_process(g) {
1337 //ust// WARN_ON(!thread_group_leader(g));
1338 //ust// if (hlist_empty(&g->user_markers))
1340 //ust// if (strcmp(g->comm, "testprog") == 0)
1341 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1344 //ust// /* TODO : implement this thread flag in each arch. */
1345 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1346 //ust// } while ((t = next_thread(t)) != g);
1348 //ust// read_unlock(&tasklist_lock);
1349 //ust// mutex_unlock(&markers_mutex);
1353 * Update current process.
1354 * Note that we have to wait a whole scheduler period before we are sure that
1355 * every running userspace threads have their markers updated.
1356 * (synchronize_sched() can be used to insure this).
1358 void marker_update_process(void)
1360 struct user_marker
*umark
;
1361 struct hlist_node
*pos
;
1362 struct marker_entry
*entry
;
1364 mutex_lock(&markers_mutex
);
1365 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1366 if (strcmp(current
->comm
, "testprog") == 0)
1367 printk(KERN_DEBUG
"do update pending for testprog\n");
1368 hlist_for_each_entry(umark
, pos
,
1369 ¤t
->group_leader
->user_markers
, hlist
) {
1370 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1371 umark
->name
, current
->comm
);
1372 entry
= get_marker("userspace", umark
->name
);
1374 if (entry
->format
&&
1375 strcmp(entry
->format
, umark
->format
) != 0) {
1377 " error, wrong format in process %s\n",
1381 if (put_user(!!entry
->refcount
, umark
->state
)) {
1383 "Marker in %s caused a fault\n",
1388 if (put_user(0, umark
->state
)) {
1390 "Marker in %s caused a fault\n",
1396 clear_thread_flag(TIF_MARKER_PENDING
);
1397 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1398 mutex_unlock(&markers_mutex
);
1402 * Called at process exit and upon do_execve().
1403 * We assume that when the leader exits, no more references can be done to the
1404 * leader structure by the other threads.
1406 void exit_user_markers(struct task_struct
*p
)
1408 struct user_marker
*umark
;
1409 struct hlist_node
*pos
, *n
;
1411 if (thread_group_leader(p
)) {
1412 mutex_lock(&markers_mutex
);
1413 mutex_lock(&p
->user_markers_mutex
);
1414 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1417 INIT_HLIST_HEAD(&p
->user_markers
);
1418 p
->user_markers_sequence
++;
1419 mutex_unlock(&p
->user_markers_mutex
);
1420 mutex_unlock(&markers_mutex
);
1424 int is_marker_enabled(const char *channel
, const char *name
)
1426 struct marker_entry
*entry
;
1428 mutex_lock(&markers_mutex
);
1429 entry
= get_marker(channel
, name
);
1430 mutex_unlock(&markers_mutex
);
1432 return entry
&& !!entry
->refcount
;
1436 int marker_module_notify(struct notifier_block
*self
,
1437 unsigned long val
, void *data
)
1439 struct module
*mod
= data
;
1442 case MODULE_STATE_COMING
:
1443 marker_update_probe_range(mod
->markers
,
1444 mod
->markers
+ mod
->num_markers
);
1446 case MODULE_STATE_GOING
:
1447 marker_update_probe_range(mod
->markers
,
1448 mod
->markers
+ mod
->num_markers
);
1454 struct notifier_block marker_module_nb
= {
1455 .notifier_call
= marker_module_notify
,
1459 //ust// static int init_markers(void)
1461 //ust// return register_module_notifier(&marker_module_nb);
1463 //ust// __initcall(init_markers);
1464 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1466 #endif /* CONFIG_MODULES */
1468 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1470 struct marker_entry
*entry
;
1471 struct ltt_probe_private_data call_data
;
1472 struct hlist_head
*head
;
1473 struct hlist_node
*node
;
1476 mutex_lock(&markers_mutex
);
1477 call_data
.trace
= trace
;
1478 call_data
.serializer
= NULL
;
1480 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1481 head
= &marker_table
[i
];
1482 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1483 __trace_mark(0, metadata
, core_marker_id
,
1485 "channel %s name %s event_id %hu "
1486 "int #1u%zu long #1u%zu pointer #1u%zu "
1487 "size_t #1u%zu alignment #1u%u",
1491 sizeof(int), sizeof(long),
1492 sizeof(void *), sizeof(size_t),
1493 ltt_get_alignment());
1495 __trace_mark(0, metadata
,
1498 "channel %s name %s format %s",
1504 mutex_unlock(&markers_mutex
);
1506 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1508 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1510 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1515 static void new_markers(struct marker
*start
, struct marker
*end
)
1519 for(m
=start
; m
< end
; m
++) {
1525 int marker_register_lib(struct marker
*markers_start
, struct marker_addr
*marker_addr_start
, int markers_count
)
1528 struct marker_addr
*addr
;
1530 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1532 pl
->markers_start
= markers_start
;
1533 #ifdef CONFIG_UST_GDB_INTEGRATION
1534 pl
->markers_addr_start
= marker_addr_start
;
1536 pl
->markers_count
= markers_count
;
1538 #ifdef CONFIG_UST_GDB_INTEGRATION
1540 for(addr
= marker_addr_start
; addr
< marker_addr_start
+ markers_count
; addr
++)
1541 addr
->marker
->location
= addr
->addr
;
1545 /* FIXME: maybe protect this with its own mutex? */
1547 list_add(&pl
->list
, &libs
);
1550 new_markers(markers_start
, markers_start
+ markers_count
);
1552 /* FIXME: update just the loaded lib */
1553 lib_update_markers();
1555 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1560 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1562 /*FIXME: implement; but before implementing, marker_register_lib must
1563 have appropriate locking. */
1568 static int initialized
= 0;
1570 void __attribute__((constructor
)) init_markers(void)
1573 #ifdef CONFIG_UST_GDB_INTEGRATION
1574 marker_register_lib(__start___markers
, __start___marker_addr
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1576 marker_register_lib(__start___markers
, NULL
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1578 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);