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 * marker_update_probe_range - Update a probe range
677 * @begin: beginning of the range
678 * @end: end of the range
680 * Updates the probe callback corresponding to a range of markers.
682 void marker_update_probe_range(struct marker
*begin
,
686 struct marker_entry
*mark_entry
;
688 mutex_lock(&markers_mutex
);
689 for (iter
= begin
; iter
< end
; iter
++) {
690 mark_entry
= get_marker(iter
->channel
, iter
->name
);
692 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
694 * ignore error, continue
697 /* This is added for UST. We emit a core_marker_id event
698 * for markers that are already registered to a probe
699 * upon library load. Otherwise, no core_marker_id will
700 * be generated for these markers. Is this the right thing
703 trace_mark(metadata
, core_marker_id
,
704 "channel %s name %s event_id %hu "
705 "int #1u%zu long #1u%zu pointer #1u%zu "
706 "size_t #1u%zu alignment #1u%u",
707 iter
->channel
, iter
->name
, mark_entry
->event_id
,
708 sizeof(int), sizeof(long), sizeof(void *),
709 sizeof(size_t), ltt_get_alignment());
711 disable_marker(iter
);
714 mutex_unlock(&markers_mutex
);
717 static void lib_update_markers(void)
721 /* FIXME: we should probably take a mutex here on libs */
722 //ust// mutex_lock(&module_mutex);
723 list_for_each_entry(lib
, &libs
, list
)
724 marker_update_probe_range(lib
->markers_start
,
725 lib
->markers_start
+ lib
->markers_count
);
726 //ust// mutex_unlock(&module_mutex);
730 * Update probes, removing the faulty probes.
732 * Internal callback only changed before the first probe is connected to it.
733 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
734 * transitions. All other transitions will leave the old private data valid.
735 * This makes the non-atomicity of the callback/private data updates valid.
737 * "special case" updates :
742 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
743 * Site effect : marker_set_format may delete the marker entry (creating a
746 static void marker_update_probes(void)
748 /* Core kernel markers */
749 //ust// marker_update_probe_range(__start___markers, __stop___markers);
750 /* Markers in modules. */
751 //ust// module_update_markers();
752 lib_update_markers();
753 //ust// tracepoint_probe_update_all();
754 /* Update immediate values */
756 //ust// module_imv_update(); /* FIXME: need to port for libs? */
757 marker_update_processes();
761 * marker_probe_register - Connect a probe to a marker
762 * @channel: marker channel
764 * @format: format string
765 * @probe: probe handler
766 * @probe_private: probe private data
768 * private data must be a valid allocated memory address, or NULL.
769 * Returns 0 if ok, error value on error.
770 * The probe address must at least be aligned on the architecture pointer size.
772 int marker_probe_register(const char *channel
, const char *name
,
773 const char *format
, marker_probe_func
*probe
,
776 struct marker_entry
*entry
;
777 int ret
= 0, ret_err
;
778 struct marker_probe_closure
*old
;
781 mutex_lock(&markers_mutex
);
782 entry
= get_marker(channel
, name
);
785 entry
= add_marker(channel
, name
, format
);
787 ret
= PTR_ERR(entry
);
790 ret
= ltt_channels_register(channel
);
792 goto error_remove_marker
;
793 ret
= ltt_channels_get_index_from_name(channel
);
795 goto error_unregister_channel
;
796 entry
->channel_id
= ret
;
797 ret
= ltt_channels_get_event_id(channel
, name
);
799 goto error_unregister_channel
;
800 entry
->event_id
= ret
;
802 trace_mark(metadata
, core_marker_id
,
803 "channel %s name %s event_id %hu "
804 "int #1u%zu long #1u%zu pointer #1u%zu "
805 "size_t #1u%zu alignment #1u%u",
806 channel
, name
, entry
->event_id
,
807 sizeof(int), sizeof(long), sizeof(void *),
808 sizeof(size_t), ltt_get_alignment());
811 ret
= marker_set_format(entry
, format
);
812 else if (strcmp(entry
->format
, format
))
819 * If we detect that a call_rcu is pending for this marker,
820 * make sure it's executed now.
822 //ust// if (entry->rcu_pending)
823 //ust// rcu_barrier_sched();
824 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
828 goto error_unregister_channel
;
832 mutex_unlock(&markers_mutex
);
834 /* Activate marker if necessary */
835 marker_update_probes();
837 mutex_lock(&markers_mutex
);
838 entry
= get_marker(channel
, name
);
841 //ust// if (entry->rcu_pending)
842 //ust// rcu_barrier_sched();
844 entry
->rcu_pending
= 1;
845 /* write rcu_pending before calling the RCU callback */
847 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
848 synchronize_rcu(); free_old_closure(&entry
->rcu
);
851 error_unregister_channel
:
852 ret_err
= ltt_channels_unregister(channel
);
855 ret_err
= remove_marker(channel
, name
);
858 mutex_unlock(&markers_mutex
);
861 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
864 * marker_probe_unregister - Disconnect a probe from a marker
865 * @channel: marker channel
867 * @probe: probe function pointer
868 * @probe_private: probe private data
870 * Returns the private data given to marker_probe_register, or an ERR_PTR().
871 * We do not need to call a synchronize_sched to make sure the probes have
872 * finished running before doing a module unload, because the module unload
873 * itself uses stop_machine(), which insures that every preempt disabled section
876 int marker_probe_unregister(const char *channel
, const char *name
,
877 marker_probe_func
*probe
, void *probe_private
)
879 struct marker_entry
*entry
;
880 struct marker_probe_closure
*old
;
883 mutex_lock(&markers_mutex
);
884 entry
= get_marker(channel
, name
);
887 //ust// if (entry->rcu_pending)
888 //ust// rcu_barrier_sched();
889 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
890 mutex_unlock(&markers_mutex
);
892 marker_update_probes();
894 mutex_lock(&markers_mutex
);
895 entry
= get_marker(channel
, name
);
898 //ust// if (entry->rcu_pending)
899 //ust// rcu_barrier_sched();
901 entry
->rcu_pending
= 1;
902 /* write rcu_pending before calling the RCU callback */
904 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
905 synchronize_rcu(); free_old_closure(&entry
->rcu
);
906 remove_marker(channel
, name
); /* Ignore busy error message */
909 mutex_unlock(&markers_mutex
);
912 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
914 static struct marker_entry
*
915 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
917 struct marker_entry
*entry
;
919 struct hlist_head
*head
;
920 struct hlist_node
*node
;
922 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
923 head
= &marker_table
[i
];
924 hlist_for_each_entry(entry
, node
, head
, hlist
) {
926 if (entry
->single
.func
== probe
927 && entry
->single
.probe_private
931 struct marker_probe_closure
*closure
;
932 closure
= entry
->multi
;
933 for (i
= 0; closure
[i
].func
; i
++) {
934 if (closure
[i
].func
== probe
&&
935 closure
[i
].probe_private
946 * marker_probe_unregister_private_data - Disconnect a probe from a marker
947 * @probe: probe function
948 * @probe_private: probe private data
950 * Unregister a probe by providing the registered private data.
951 * Only removes the first marker found in hash table.
952 * Return 0 on success or error value.
953 * We do not need to call a synchronize_sched to make sure the probes have
954 * finished running before doing a module unload, because the module unload
955 * itself uses stop_machine(), which insures that every preempt disabled section
958 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
961 struct marker_entry
*entry
;
963 struct marker_probe_closure
*old
;
964 const char *channel
= NULL
, *name
= NULL
;
966 mutex_lock(&markers_mutex
);
967 entry
= get_marker_from_private_data(probe
, probe_private
);
972 //ust// if (entry->rcu_pending)
973 //ust// rcu_barrier_sched();
974 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
975 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
976 name
= kstrdup(entry
->name
, GFP_KERNEL
);
977 mutex_unlock(&markers_mutex
);
979 marker_update_probes();
981 mutex_lock(&markers_mutex
);
982 entry
= get_marker(channel
, name
);
985 //ust// if (entry->rcu_pending)
986 //ust// rcu_barrier_sched();
988 entry
->rcu_pending
= 1;
989 /* write rcu_pending before calling the RCU callback */
991 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
992 synchronize_rcu(); free_old_closure(&entry
->rcu
);
993 /* Ignore busy error message */
994 remove_marker(channel
, name
);
996 mutex_unlock(&markers_mutex
);
1001 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1004 * marker_get_private_data - Get a marker's probe private data
1005 * @channel: marker channel
1006 * @name: marker name
1007 * @probe: probe to match
1008 * @num: get the nth matching probe's private data
1010 * Returns the nth private data pointer (starting from 0) matching, or an
1012 * Returns the private data pointer, or an ERR_PTR.
1013 * The private data pointer should _only_ be dereferenced if the caller is the
1014 * owner of the data, or its content could vanish. This is mostly used to
1015 * confirm that a caller is the owner of a registered probe.
1017 void *marker_get_private_data(const char *channel
, const char *name
,
1018 marker_probe_func
*probe
, int num
)
1020 struct hlist_head
*head
;
1021 struct hlist_node
*node
;
1022 struct marker_entry
*e
;
1023 size_t channel_len
= strlen(channel
) + 1;
1024 size_t name_len
= strlen(name
) + 1;
1028 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1029 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1030 hlist_for_each_entry(e
, node
, head
, hlist
) {
1031 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1033 if (num
== 0 && e
->single
.func
== probe
)
1034 return e
->single
.probe_private
;
1036 struct marker_probe_closure
*closure
;
1039 for (i
= 0; closure
[i
].func
; i
++) {
1040 if (closure
[i
].func
!= probe
)
1043 return closure
[i
].probe_private
;
1049 return ERR_PTR(-ENOENT
);
1051 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1054 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1056 * Called when no channel users are active by the channel infrastructure.
1057 * Called with lock_markers() and channel mutex held.
1059 //ust// void markers_compact_event_ids(void)
1061 //ust// struct marker_entry *entry;
1062 //ust// unsigned int i;
1063 //ust// struct hlist_head *head;
1064 //ust// struct hlist_node *node;
1067 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1068 //ust// head = &marker_table[i];
1069 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1070 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1071 //ust// WARN_ON(ret < 0);
1072 //ust// entry->channel_id = ret;
1073 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1074 //ust// entry->name);
1075 //ust// WARN_ON(ret < 0);
1076 //ust// entry->event_id = ret;
1081 //ust//#ifdef CONFIG_MODULES
1084 * Returns 0 if current not found.
1085 * Returns 1 if current found.
1087 int lib_get_iter_markers(struct marker_iter
*iter
)
1089 struct lib
*iter_lib
;
1092 //ust// mutex_lock(&module_mutex);
1093 list_for_each_entry(iter_lib
, &libs
, list
) {
1094 if (iter_lib
< iter
->lib
)
1096 else if (iter_lib
> iter
->lib
)
1097 iter
->marker
= NULL
;
1098 found
= marker_get_iter_range(&iter
->marker
,
1099 iter_lib
->markers_start
,
1100 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1102 iter
->lib
= iter_lib
;
1106 //ust// mutex_unlock(&module_mutex);
1111 * marker_get_iter_range - Get a next marker iterator given a range.
1112 * @marker: current markers (in), next marker (out)
1113 * @begin: beginning of the range
1114 * @end: end of the range
1116 * Returns whether a next marker has been found (1) or not (0).
1117 * Will return the first marker in the range if the input marker is NULL.
1119 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1122 if (!*marker
&& begin
!= end
) {
1126 if (*marker
>= begin
&& *marker
< end
)
1130 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1132 static void marker_get_iter(struct marker_iter
*iter
)
1136 /* Core kernel markers */
1138 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1139 found
= marker_get_iter_range(&iter
->marker
,
1140 __start___markers
, __stop___markers
);
1144 /* Markers in modules. */
1145 found
= lib_get_iter_markers(iter
);
1148 marker_iter_reset(iter
);
1151 void marker_iter_start(struct marker_iter
*iter
)
1153 marker_get_iter(iter
);
1155 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1157 void marker_iter_next(struct marker_iter
*iter
)
1161 * iter->marker may be invalid because we blindly incremented it.
1162 * Make sure it is valid by marshalling on the markers, getting the
1163 * markers from following modules if necessary.
1165 marker_get_iter(iter
);
1167 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1169 void marker_iter_stop(struct marker_iter
*iter
)
1172 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1174 void marker_iter_reset(struct marker_iter
*iter
)
1177 iter
->marker
= NULL
;
1179 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1181 #ifdef CONFIG_MARKERS_USERSPACE
1183 * must be called with current->user_markers_mutex held
1185 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1187 struct user_marker
*umark
;
1188 struct hlist_node
*pos
, *n
;
1190 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1191 if (umark
->state
== state
) {
1192 hlist_del(&umark
->hlist
);
1198 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1199 //ust// char __user *state, int reg)
1201 //ust// struct user_marker *umark;
1203 //ust// struct marker_entry *entry;
1204 //ust// int ret = 0;
1206 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1207 //ust// current->comm, reg ? "registers" : "unregisters",
1208 //ust// name, state);
1210 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1211 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1212 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1213 //ust// umark->state = state;
1214 //ust// len = strncpy_from_user(umark->name, name,
1215 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1216 //ust// if (len < 0) {
1217 //ust// ret = -EFAULT;
1220 //ust// len = strncpy_from_user(umark->format, format,
1221 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1222 //ust// if (len < 0) {
1223 //ust// ret = -EFAULT;
1226 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1227 //ust// umark->format);
1228 //ust// mutex_lock(&markers_mutex);
1229 //ust// entry = get_marker("userspace", umark->name);
1230 //ust// if (entry) {
1231 //ust// if (entry->format &&
1232 //ust// strcmp(entry->format, umark->format) != 0) {
1233 //ust// printk(" error, wrong format in process %s",
1234 //ust// current->comm);
1235 //ust// ret = -EPERM;
1236 //ust// goto error_unlock;
1238 //ust// printk(" %s", !!entry->refcount
1239 //ust// ? "enabled" : "disabled");
1240 //ust// if (put_user(!!entry->refcount, state)) {
1241 //ust// ret = -EFAULT;
1242 //ust// goto error_unlock;
1244 //ust// printk("\n");
1246 //ust// printk(" disabled\n");
1247 //ust// if (put_user(0, umark->state)) {
1248 //ust// printk(KERN_WARNING
1249 //ust// "Marker in %s caused a fault\n",
1250 //ust// current->comm);
1251 //ust// goto error_unlock;
1254 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1255 //ust// hlist_add_head(&umark->hlist,
1256 //ust// ¤t->group_leader->user_markers);
1257 //ust// current->group_leader->user_markers_sequence++;
1258 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1259 //ust// mutex_unlock(&markers_mutex);
1261 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1262 //ust// free_user_marker(state,
1263 //ust// ¤t->group_leader->user_markers);
1264 //ust// current->group_leader->user_markers_sequence++;
1265 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1268 //ust// error_unlock:
1269 //ust// mutex_unlock(&markers_mutex);
1271 //ust// kfree(umark);
1278 //ust// * string : 0
1280 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1281 //ust// char __user *ubuf)
1283 //ust// long ret = -EPERM;
1287 //ust// switch (type) {
1288 //ust// case 0: /* String */
1289 //ust// ret = -ENOMEM;
1290 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1292 //ust// goto string_out;
1293 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1294 //ust// if (len < 0) {
1295 //ust// ret = -EFAULT;
1296 //ust// goto string_err;
1298 //ust// trace_mark(userspace, string, "string %s", page);
1300 //ust// free_page((unsigned long) page);
1309 //ust// static void marker_update_processes(void)
1311 //ust// struct task_struct *g, *t;
1314 //ust// * markers_mutex is taken to protect the p->user_markers read.
1316 //ust// mutex_lock(&markers_mutex);
1317 //ust// read_lock(&tasklist_lock);
1318 //ust// for_each_process(g) {
1319 //ust// WARN_ON(!thread_group_leader(g));
1320 //ust// if (hlist_empty(&g->user_markers))
1322 //ust// if (strcmp(g->comm, "testprog") == 0)
1323 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1326 //ust// /* TODO : implement this thread flag in each arch. */
1327 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1328 //ust// } while ((t = next_thread(t)) != g);
1330 //ust// read_unlock(&tasklist_lock);
1331 //ust// mutex_unlock(&markers_mutex);
1335 * Update current process.
1336 * Note that we have to wait a whole scheduler period before we are sure that
1337 * every running userspace threads have their markers updated.
1338 * (synchronize_sched() can be used to insure this).
1340 void marker_update_process(void)
1342 struct user_marker
*umark
;
1343 struct hlist_node
*pos
;
1344 struct marker_entry
*entry
;
1346 mutex_lock(&markers_mutex
);
1347 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1348 if (strcmp(current
->comm
, "testprog") == 0)
1349 printk(KERN_DEBUG
"do update pending for testprog\n");
1350 hlist_for_each_entry(umark
, pos
,
1351 ¤t
->group_leader
->user_markers
, hlist
) {
1352 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1353 umark
->name
, current
->comm
);
1354 entry
= get_marker("userspace", umark
->name
);
1356 if (entry
->format
&&
1357 strcmp(entry
->format
, umark
->format
) != 0) {
1359 " error, wrong format in process %s\n",
1363 if (put_user(!!entry
->refcount
, umark
->state
)) {
1365 "Marker in %s caused a fault\n",
1370 if (put_user(0, umark
->state
)) {
1372 "Marker in %s caused a fault\n",
1378 clear_thread_flag(TIF_MARKER_PENDING
);
1379 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1380 mutex_unlock(&markers_mutex
);
1384 * Called at process exit and upon do_execve().
1385 * We assume that when the leader exits, no more references can be done to the
1386 * leader structure by the other threads.
1388 void exit_user_markers(struct task_struct
*p
)
1390 struct user_marker
*umark
;
1391 struct hlist_node
*pos
, *n
;
1393 if (thread_group_leader(p
)) {
1394 mutex_lock(&markers_mutex
);
1395 mutex_lock(&p
->user_markers_mutex
);
1396 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1399 INIT_HLIST_HEAD(&p
->user_markers
);
1400 p
->user_markers_sequence
++;
1401 mutex_unlock(&p
->user_markers_mutex
);
1402 mutex_unlock(&markers_mutex
);
1406 int is_marker_enabled(const char *channel
, const char *name
)
1408 struct marker_entry
*entry
;
1410 mutex_lock(&markers_mutex
);
1411 entry
= get_marker(channel
, name
);
1412 mutex_unlock(&markers_mutex
);
1414 return entry
&& !!entry
->refcount
;
1418 int marker_module_notify(struct notifier_block
*self
,
1419 unsigned long val
, void *data
)
1421 struct module
*mod
= data
;
1424 case MODULE_STATE_COMING
:
1425 marker_update_probe_range(mod
->markers
,
1426 mod
->markers
+ mod
->num_markers
);
1428 case MODULE_STATE_GOING
:
1429 marker_update_probe_range(mod
->markers
,
1430 mod
->markers
+ mod
->num_markers
);
1436 struct notifier_block marker_module_nb
= {
1437 .notifier_call
= marker_module_notify
,
1441 //ust// static int init_markers(void)
1443 //ust// return register_module_notifier(&marker_module_nb);
1445 //ust// __initcall(init_markers);
1446 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1448 #endif /* CONFIG_MODULES */
1450 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1452 struct marker_entry
*entry
;
1453 struct ltt_probe_private_data call_data
;
1454 struct hlist_head
*head
;
1455 struct hlist_node
*node
;
1458 mutex_lock(&markers_mutex
);
1459 call_data
.trace
= trace
;
1460 call_data
.serializer
= NULL
;
1462 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1463 head
= &marker_table
[i
];
1464 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1465 __trace_mark(0, metadata
, core_marker_id
,
1467 "channel %s name %s event_id %hu "
1468 "int #1u%zu long #1u%zu pointer #1u%zu "
1469 "size_t #1u%zu alignment #1u%u",
1473 sizeof(int), sizeof(long),
1474 sizeof(void *), sizeof(size_t),
1475 ltt_get_alignment());
1477 __trace_mark(0, metadata
,
1480 "channel %s name %s format %s",
1486 mutex_unlock(&markers_mutex
);
1488 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1490 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1492 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1497 static void new_markers(struct marker
*start
, struct marker
*end
)
1501 for(m
=start
; m
< end
; m
++) {
1507 int marker_register_lib(struct marker
*markers_start
, struct marker_addr
*marker_addr_start
, int markers_count
)
1510 struct marker_addr
*addr
;
1512 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1514 pl
->markers_start
= markers_start
;
1515 #ifdef CONFIG_UST_GDB_INTEGRATION
1516 pl
->markers_addr_start
= marker_addr_start
;
1518 pl
->markers_count
= markers_count
;
1521 for(addr
= marker_addr_start
; addr
< marker_addr_start
+ markers_count
; addr
++)
1522 addr
->marker
->location
= addr
->addr
;
1525 /* FIXME: maybe protect this with its own mutex? */
1527 list_add(&pl
->list
, &libs
);
1530 new_markers(markers_start
, markers_start
+ markers_count
);
1532 /* FIXME: update just the loaded lib */
1533 lib_update_markers();
1535 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1540 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1542 /*FIXME: implement; but before implementing, marker_register_lib must
1543 have appropriate locking. */
1548 static int initialized
= 0;
1550 void __attribute__((constructor
)) init_markers(void)
1553 #ifdef CONFIG_UST_GDB_INTEGRATION
1554 marker_register_lib(__start___markers
, __start___marker_addr
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1556 marker_register_lib(__start___markers
, NULL
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1558 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);