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 "kernelcompat.h"
41 #include "tracercore.h"
44 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
45 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
47 /* Set to 1 to enable marker debug output */
48 static const int marker_debug
;
51 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
52 * and module markers and the hash table.
54 static DEFINE_MUTEX(markers_mutex
);
56 static LIST_HEAD(libs
);
59 void lock_markers(void)
61 mutex_lock(&markers_mutex
);
64 void unlock_markers(void)
66 mutex_unlock(&markers_mutex
);
70 * Marker hash table, containing the active markers.
71 * Protected by module_mutex.
73 #define MARKER_HASH_BITS 6
74 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
75 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
79 * It is used to make sure every handler has finished using its private data
80 * between two consecutive operation (add or remove) on a given marker. It is
81 * also used to delay the free of multiple probes array until a quiescent state
83 * marker entries modifications are protected by the markers_mutex.
86 struct hlist_node hlist
;
90 void (*call
)(const struct marker
*mdata
, void *call_private
, ...);
91 struct marker_probe_closure single
;
92 struct marker_probe_closure
*multi
;
93 int refcount
; /* Number of times armed. 0 if disarmed. */
99 unsigned char ptype
:1;
100 unsigned char format_allocated
:1;
101 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
104 #ifdef CONFIG_MARKERS_USERSPACE
105 static void marker_update_processes(void);
107 static void marker_update_processes(void)
113 * __mark_empty_function - Empty probe callback
114 * @mdata: marker data
115 * @probe_private: probe private data
116 * @call_private: call site private data
117 * @fmt: format string
118 * @...: variable argument list
120 * Empty callback provided as a probe to the markers. By providing this to a
121 * disabled marker, we make sure the execution flow is always valid even
122 * though the function pointer change and the marker enabling are two distinct
123 * operations that modifies the execution flow of preemptible code.
125 notrace
void __mark_empty_function(const struct marker
*mdata
,
126 void *probe_private
, void *call_private
, const char *fmt
, va_list *args
)
129 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
132 * marker_probe_cb Callback that prepares the variable argument list for probes.
133 * @mdata: pointer of type struct marker
134 * @call_private: caller site private data
135 * @...: Variable argument list.
137 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
138 * need to put a full smp_rmb() in this branch. This is why we do not use
139 * rcu_dereference() for the pointer read.
141 notrace
void marker_probe_cb(const struct marker
*mdata
,
142 void *call_private
, ...)
148 * rcu_read_lock_sched does two things : disabling preemption to make
149 * sure the teardown of the callbacks can be done correctly when they
150 * are in modules and they insure RCU read coherency.
152 //ust// rcu_read_lock_sched_notrace();
153 ptype
= mdata
->ptype
;
154 if (likely(!ptype
)) {
155 marker_probe_func
*func
;
156 /* Must read the ptype before ptr. They are not data dependant,
157 * so we put an explicit smp_rmb() here. */
159 func
= mdata
->single
.func
;
160 /* Must read the ptr before private data. They are not data
161 * dependant, so we put an explicit smp_rmb() here. */
163 va_start(args
, call_private
);
164 func(mdata
, mdata
->single
.probe_private
, call_private
,
165 mdata
->format
, &args
);
168 struct marker_probe_closure
*multi
;
171 * Read mdata->ptype before mdata->multi.
174 multi
= mdata
->multi
;
176 * multi points to an array, therefore accessing the array
177 * depends on reading multi. However, even in this case,
178 * we must insure that the pointer is read _before_ the array
179 * data. Same as rcu_dereference, but we need a full smp_rmb()
180 * in the fast path, so put the explicit barrier here.
182 smp_read_barrier_depends();
183 for (i
= 0; multi
[i
].func
; i
++) {
184 va_start(args
, call_private
);
185 multi
[i
].func(mdata
, multi
[i
].probe_private
,
186 call_private
, mdata
->format
, &args
);
190 //ust// rcu_read_unlock_sched_notrace();
192 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
195 * marker_probe_cb Callback that does not prepare the variable argument list.
196 * @mdata: pointer of type struct marker
197 * @call_private: caller site private data
198 * @...: Variable argument list.
200 * Should be connected to markers "MARK_NOARGS".
202 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
203 void *call_private
, ...)
205 va_list args
; /* not initialized */
208 //ust// rcu_read_lock_sched_notrace();
209 ptype
= mdata
->ptype
;
210 if (likely(!ptype
)) {
211 marker_probe_func
*func
;
212 /* Must read the ptype before ptr. They are not data dependant,
213 * so we put an explicit smp_rmb() here. */
215 func
= mdata
->single
.func
;
216 /* Must read the ptr before private data. They are not data
217 * dependant, so we put an explicit smp_rmb() here. */
219 func(mdata
, mdata
->single
.probe_private
, call_private
,
220 mdata
->format
, &args
);
222 struct marker_probe_closure
*multi
;
225 * Read mdata->ptype before mdata->multi.
228 multi
= mdata
->multi
;
230 * multi points to an array, therefore accessing the array
231 * depends on reading multi. However, even in this case,
232 * we must insure that the pointer is read _before_ the array
233 * data. Same as rcu_dereference, but we need a full smp_rmb()
234 * in the fast path, so put the explicit barrier here.
236 smp_read_barrier_depends();
237 for (i
= 0; multi
[i
].func
; i
++)
238 multi
[i
].func(mdata
, multi
[i
].probe_private
,
239 call_private
, mdata
->format
, &args
);
241 //ust// rcu_read_unlock_sched_notrace();
244 static void free_old_closure(struct rcu_head
*head
)
246 struct marker_entry
*entry
= container_of(head
,
247 struct marker_entry
, rcu
);
248 kfree(entry
->oldptr
);
249 /* Make sure we free the data before setting the pending flag to 0 */
251 entry
->rcu_pending
= 0;
254 static void debug_print_probes(struct marker_entry
*entry
)
262 printk(KERN_DEBUG
"Single probe : %p %p\n",
264 entry
->single
.probe_private
);
266 for (i
= 0; entry
->multi
[i
].func
; i
++)
267 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
268 entry
->multi
[i
].func
,
269 entry
->multi
[i
].probe_private
);
273 static struct marker_probe_closure
*
274 marker_entry_add_probe(struct marker_entry
*entry
,
275 marker_probe_func
*probe
, void *probe_private
)
278 struct marker_probe_closure
*old
, *new;
282 debug_print_probes(entry
);
285 if (entry
->single
.func
== probe
&&
286 entry
->single
.probe_private
== probe_private
)
287 return ERR_PTR(-EBUSY
);
288 if (entry
->single
.func
== __mark_empty_function
) {
290 entry
->single
.func
= probe
;
291 entry
->single
.probe_private
= probe_private
;
294 debug_print_probes(entry
);
302 /* (N -> N+1), (N != 0, 1) probes */
303 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
304 if (old
[nr_probes
].func
== probe
305 && old
[nr_probes
].probe_private
307 return ERR_PTR(-EBUSY
);
309 /* + 2 : one for new probe, one for NULL func */
310 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
313 return ERR_PTR(-ENOMEM
);
315 new[0] = entry
->single
;
318 nr_probes
* sizeof(struct marker_probe_closure
));
319 new[nr_probes
].func
= probe
;
320 new[nr_probes
].probe_private
= probe_private
;
321 entry
->refcount
= nr_probes
+ 1;
324 debug_print_probes(entry
);
328 static struct marker_probe_closure
*
329 marker_entry_remove_probe(struct marker_entry
*entry
,
330 marker_probe_func
*probe
, void *probe_private
)
332 int nr_probes
= 0, nr_del
= 0, i
;
333 struct marker_probe_closure
*old
, *new;
337 debug_print_probes(entry
);
339 /* 0 -> N is an error */
340 WARN_ON(entry
->single
.func
== __mark_empty_function
);
342 WARN_ON(probe
&& entry
->single
.func
!= probe
);
343 WARN_ON(entry
->single
.probe_private
!= probe_private
);
344 entry
->single
.func
= __mark_empty_function
;
347 debug_print_probes(entry
);
350 /* (N -> M), (N > 1, M >= 0) probes */
351 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
352 if ((!probe
|| old
[nr_probes
].func
== probe
)
353 && old
[nr_probes
].probe_private
359 if (nr_probes
- nr_del
== 0) {
360 /* N -> 0, (N > 1) */
361 entry
->single
.func
= __mark_empty_function
;
364 } else if (nr_probes
- nr_del
== 1) {
365 /* N -> 1, (N > 1) */
366 for (i
= 0; old
[i
].func
; i
++)
367 if ((probe
&& old
[i
].func
!= probe
) ||
368 old
[i
].probe_private
!= probe_private
)
369 entry
->single
= old
[i
];
374 /* N -> M, (N > 1, M > 1) */
376 new = kzalloc((nr_probes
- nr_del
+ 1)
377 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
379 return ERR_PTR(-ENOMEM
);
380 for (i
= 0; old
[i
].func
; i
++)
381 if ((probe
&& old
[i
].func
!= probe
) ||
382 old
[i
].probe_private
!= probe_private
)
384 entry
->refcount
= nr_probes
- nr_del
;
388 debug_print_probes(entry
);
393 * Get marker if the marker is present in the marker hash table.
394 * Must be called with markers_mutex held.
395 * Returns NULL if not present.
397 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
399 struct hlist_head
*head
;
400 struct hlist_node
*node
;
401 struct marker_entry
*e
;
402 size_t channel_len
= strlen(channel
) + 1;
403 size_t name_len
= strlen(name
) + 1;
406 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
407 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
408 hlist_for_each_entry(e
, node
, head
, hlist
) {
409 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
416 * Add the marker to the marker hash table. Must be called with markers_mutex
419 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
422 struct hlist_head
*head
;
423 struct hlist_node
*node
;
424 struct marker_entry
*e
;
425 size_t channel_len
= strlen(channel
) + 1;
426 size_t name_len
= strlen(name
) + 1;
427 size_t format_len
= 0;
430 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
432 format_len
= strlen(format
) + 1;
433 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
434 hlist_for_each_entry(e
, node
, head
, hlist
) {
435 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
437 "Marker %s.%s busy\n", channel
, name
);
438 return ERR_PTR(-EBUSY
); /* Already there */
442 * Using kmalloc here to allocate a variable length element. Could
443 * cause some memory fragmentation if overused.
445 e
= kmalloc(sizeof(struct marker_entry
)
446 + channel_len
+ name_len
+ format_len
,
449 return ERR_PTR(-ENOMEM
);
450 memcpy(e
->channel
, channel
, channel_len
);
451 e
->name
= &e
->channel
[channel_len
];
452 memcpy(e
->name
, name
, name_len
);
454 e
->format
= &e
->name
[channel_len
+ name_len
];
455 memcpy(e
->format
, format
, format_len
);
456 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
457 e
->call
= marker_probe_cb_noarg
;
459 e
->call
= marker_probe_cb
;
460 trace_mark(metadata
, core_marker_format
,
461 "channel %s name %s format %s",
462 e
->channel
, e
->name
, e
->format
);
465 e
->call
= marker_probe_cb
;
467 e
->single
.func
= __mark_empty_function
;
468 e
->single
.probe_private
= NULL
;
471 e
->format_allocated
= 0;
474 hlist_add_head(&e
->hlist
, head
);
479 * Remove the marker from the marker hash table. Must be called with mutex_lock
482 static int remove_marker(const char *channel
, const char *name
)
484 struct hlist_head
*head
;
485 struct hlist_node
*node
;
486 struct marker_entry
*e
;
488 size_t channel_len
= strlen(channel
) + 1;
489 size_t name_len
= strlen(name
) + 1;
493 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
494 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
495 hlist_for_each_entry(e
, node
, head
, hlist
) {
496 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
503 if (e
->single
.func
!= __mark_empty_function
)
505 hlist_del(&e
->hlist
);
506 if (e
->format_allocated
)
508 ret
= ltt_channels_unregister(e
->channel
);
510 /* Make sure the call_rcu has been executed */
511 //ust// if (e->rcu_pending)
512 //ust// rcu_barrier_sched();
518 * Set the mark_entry format to the format found in the element.
520 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
522 entry
->format
= kstrdup(format
, GFP_KERNEL
);
525 entry
->format_allocated
= 1;
527 trace_mark(metadata
, core_marker_format
,
528 "channel %s name %s format %s",
529 entry
->channel
, entry
->name
, entry
->format
);
534 * Sets the probe callback corresponding to one marker.
536 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
540 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
543 if (strcmp(entry
->format
, elem
->format
) != 0) {
545 "Format mismatch for probe %s "
546 "(%s), marker (%s)\n",
553 ret
= marker_set_format(entry
, elem
->format
);
559 * probe_cb setup (statically known) is done here. It is
560 * asynchronous with the rest of execution, therefore we only
561 * pass from a "safe" callback (with argument) to an "unsafe"
562 * callback (does not set arguments).
564 elem
->call
= entry
->call
;
565 elem
->channel_id
= entry
->channel_id
;
566 elem
->event_id
= entry
->event_id
;
569 * We only update the single probe private data when the ptr is
570 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
572 WARN_ON(elem
->single
.func
!= __mark_empty_function
573 && elem
->single
.probe_private
!= entry
->single
.probe_private
575 elem
->single
.probe_private
= entry
->single
.probe_private
;
577 * Make sure the private data is valid when we update the
581 elem
->single
.func
= entry
->single
.func
;
583 * We also make sure that the new probe callbacks array is consistent
584 * before setting a pointer to it.
586 rcu_assign_pointer(elem
->multi
, entry
->multi
);
588 * Update the function or multi probe array pointer before setting the
592 elem
->ptype
= entry
->ptype
;
594 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
595 //ust// WARN_ON(!elem->tp_cb);
597 //ust// * It is ok to directly call the probe registration because type
598 //ust// * checking has been done in the __trace_mark_tp() macro.
601 //ust// if (active) {
603 //ust// * try_module_get should always succeed because we hold
604 //ust// * markers_mutex to get the tp_cb address.
606 //ust// ret = try_module_get(__module_text_address(
607 //ust// (unsigned long)elem->tp_cb));
608 //ust// BUG_ON(!ret);
609 //ust// ret = tracepoint_probe_register_noupdate(
610 //ust// elem->tp_name,
611 //ust// elem->tp_cb);
613 //ust// ret = tracepoint_probe_unregister_noupdate(
614 //ust// elem->tp_name,
615 //ust// elem->tp_cb);
617 //ust// * tracepoint_probe_update_all() must be called
618 //ust// * before the module containing tp_cb is unloaded.
620 //ust// module_put(__module_text_address(
621 //ust// (unsigned long)elem->tp_cb));
624 elem
->state__imv
= active
;
630 * Disable a marker and its probe callback.
631 * Note: only waiting an RCU period after setting elem->call to the empty
632 * function insures that the original callback is not used anymore. This insured
633 * by rcu_read_lock_sched around the call site.
635 static void disable_marker(struct marker
*elem
)
639 //ust// /* leave "call" as is. It is known statically. */
640 //ust// if (elem->tp_name && _imv_read(elem->state)) {
641 //ust// WARN_ON(!elem->tp_cb);
643 //ust// * It is ok to directly call the probe registration because type
644 //ust// * checking has been done in the __trace_mark_tp() macro.
646 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
647 //ust// elem->tp_cb);
648 //ust// WARN_ON(ret);
650 //ust// * tracepoint_probe_update_all() must be called
651 //ust// * before the module containing tp_cb is unloaded.
653 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
655 elem
->state__imv
= 0;
656 elem
->single
.func
= __mark_empty_function
;
657 /* Update the function before setting the ptype */
659 elem
->ptype
= 0; /* single probe */
661 * Leave the private data and channel_id/event_id there, because removal
662 * is racy and should be done only after an RCU period. These are never
663 * used until the next initialization anyway.
668 * marker_update_probe_range - Update a probe range
669 * @begin: beginning of the range
670 * @end: end of the range
672 * Updates the probe callback corresponding to a range of markers.
674 void marker_update_probe_range(struct marker
*begin
,
678 struct marker_entry
*mark_entry
;
680 mutex_lock(&markers_mutex
);
681 for (iter
= begin
; iter
< end
; iter
++) {
682 mark_entry
= get_marker(iter
->channel
, iter
->name
);
684 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
686 * ignore error, continue
689 /* This is added for UST. We emit a core_marker_id event
690 * for markers that are already registered to a probe
691 * upon library load. Otherwise, no core_marker_id will
692 * be generated for these markers. Is this the right thing
695 trace_mark(metadata
, core_marker_id
,
696 "channel %s name %s event_id %hu "
697 "int #1u%zu long #1u%zu pointer #1u%zu "
698 "size_t #1u%zu alignment #1u%u",
699 iter
->channel
, iter
->name
, mark_entry
->event_id
,
700 sizeof(int), sizeof(long), sizeof(void *),
701 sizeof(size_t), ltt_get_alignment());
703 disable_marker(iter
);
706 mutex_unlock(&markers_mutex
);
709 static void lib_update_markers(void)
713 /* FIXME: we should probably take a mutex here on libs */
714 //ust// mutex_lock(&module_mutex);
715 list_for_each_entry(lib
, &libs
, list
)
716 marker_update_probe_range(lib
->markers_start
,
717 lib
->markers_start
+ lib
->markers_count
);
718 //ust// mutex_unlock(&module_mutex);
722 * Update probes, removing the faulty probes.
724 * Internal callback only changed before the first probe is connected to it.
725 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
726 * transitions. All other transitions will leave the old private data valid.
727 * This makes the non-atomicity of the callback/private data updates valid.
729 * "special case" updates :
734 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
735 * Site effect : marker_set_format may delete the marker entry (creating a
738 static void marker_update_probes(void)
740 /* Core kernel markers */
741 //ust// marker_update_probe_range(__start___markers, __stop___markers);
742 /* Markers in modules. */
743 //ust// module_update_markers();
744 lib_update_markers();
745 //ust// tracepoint_probe_update_all();
746 /* Update immediate values */
748 //ust// module_imv_update(); /* FIXME: need to port for libs? */
749 marker_update_processes();
753 * marker_probe_register - Connect a probe to a marker
754 * @channel: marker channel
756 * @format: format string
757 * @probe: probe handler
758 * @probe_private: probe private data
760 * private data must be a valid allocated memory address, or NULL.
761 * Returns 0 if ok, error value on error.
762 * The probe address must at least be aligned on the architecture pointer size.
764 int marker_probe_register(const char *channel
, const char *name
,
765 const char *format
, marker_probe_func
*probe
,
768 struct marker_entry
*entry
;
769 int ret
= 0, ret_err
;
770 struct marker_probe_closure
*old
;
773 mutex_lock(&markers_mutex
);
774 entry
= get_marker(channel
, name
);
777 entry
= add_marker(channel
, name
, format
);
779 ret
= PTR_ERR(entry
);
782 ret
= ltt_channels_register(channel
);
784 goto error_remove_marker
;
785 ret
= ltt_channels_get_index_from_name(channel
);
787 goto error_unregister_channel
;
788 entry
->channel_id
= ret
;
789 ret
= ltt_channels_get_event_id(channel
, name
);
791 goto error_unregister_channel
;
792 entry
->event_id
= ret
;
794 trace_mark(metadata
, core_marker_id
,
795 "channel %s name %s event_id %hu "
796 "int #1u%zu long #1u%zu pointer #1u%zu "
797 "size_t #1u%zu alignment #1u%u",
798 channel
, name
, entry
->event_id
,
799 sizeof(int), sizeof(long), sizeof(void *),
800 sizeof(size_t), ltt_get_alignment());
803 ret
= marker_set_format(entry
, format
);
804 else if (strcmp(entry
->format
, format
))
811 * If we detect that a call_rcu is pending for this marker,
812 * make sure it's executed now.
814 //ust// if (entry->rcu_pending)
815 //ust// rcu_barrier_sched();
816 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
820 goto error_unregister_channel
;
824 mutex_unlock(&markers_mutex
);
826 marker_update_probes();
828 mutex_lock(&markers_mutex
);
829 entry
= get_marker(channel
, name
);
832 //ust// if (entry->rcu_pending)
833 //ust// rcu_barrier_sched();
835 entry
->rcu_pending
= 1;
836 /* write rcu_pending before calling the RCU callback */
838 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
839 synchronize_rcu(); free_old_closure(&entry
->rcu
);
842 error_unregister_channel
:
843 ret_err
= ltt_channels_unregister(channel
);
846 ret_err
= remove_marker(channel
, name
);
849 mutex_unlock(&markers_mutex
);
852 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
855 * marker_probe_unregister - Disconnect a probe from a marker
856 * @channel: marker channel
858 * @probe: probe function pointer
859 * @probe_private: probe private data
861 * Returns the private data given to marker_probe_register, or an ERR_PTR().
862 * We do not need to call a synchronize_sched to make sure the probes have
863 * finished running before doing a module unload, because the module unload
864 * itself uses stop_machine(), which insures that every preempt disabled section
867 int marker_probe_unregister(const char *channel
, const char *name
,
868 marker_probe_func
*probe
, void *probe_private
)
870 struct marker_entry
*entry
;
871 struct marker_probe_closure
*old
;
874 mutex_lock(&markers_mutex
);
875 entry
= get_marker(channel
, name
);
878 //ust// if (entry->rcu_pending)
879 //ust// rcu_barrier_sched();
880 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
881 mutex_unlock(&markers_mutex
);
883 marker_update_probes();
885 mutex_lock(&markers_mutex
);
886 entry
= get_marker(channel
, name
);
889 //ust// if (entry->rcu_pending)
890 //ust// rcu_barrier_sched();
892 entry
->rcu_pending
= 1;
893 /* write rcu_pending before calling the RCU callback */
895 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
896 synchronize_rcu(); free_old_closure(&entry
->rcu
);
897 remove_marker(channel
, name
); /* Ignore busy error message */
900 mutex_unlock(&markers_mutex
);
903 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
905 static struct marker_entry
*
906 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
908 struct marker_entry
*entry
;
910 struct hlist_head
*head
;
911 struct hlist_node
*node
;
913 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
914 head
= &marker_table
[i
];
915 hlist_for_each_entry(entry
, node
, head
, hlist
) {
917 if (entry
->single
.func
== probe
918 && entry
->single
.probe_private
922 struct marker_probe_closure
*closure
;
923 closure
= entry
->multi
;
924 for (i
= 0; closure
[i
].func
; i
++) {
925 if (closure
[i
].func
== probe
&&
926 closure
[i
].probe_private
937 * marker_probe_unregister_private_data - Disconnect a probe from a marker
938 * @probe: probe function
939 * @probe_private: probe private data
941 * Unregister a probe by providing the registered private data.
942 * Only removes the first marker found in hash table.
943 * Return 0 on success or error value.
944 * We do not need to call a synchronize_sched to make sure the probes have
945 * finished running before doing a module unload, because the module unload
946 * itself uses stop_machine(), which insures that every preempt disabled section
949 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
952 struct marker_entry
*entry
;
954 struct marker_probe_closure
*old
;
955 const char *channel
= NULL
, *name
= NULL
;
957 mutex_lock(&markers_mutex
);
958 entry
= get_marker_from_private_data(probe
, probe_private
);
963 //ust// if (entry->rcu_pending)
964 //ust// rcu_barrier_sched();
965 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
966 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
967 name
= kstrdup(entry
->name
, GFP_KERNEL
);
968 mutex_unlock(&markers_mutex
);
970 marker_update_probes();
972 mutex_lock(&markers_mutex
);
973 entry
= get_marker(channel
, name
);
976 //ust// if (entry->rcu_pending)
977 //ust// rcu_barrier_sched();
979 entry
->rcu_pending
= 1;
980 /* write rcu_pending before calling the RCU callback */
982 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
983 synchronize_rcu(); free_old_closure(&entry
->rcu
);
984 /* Ignore busy error message */
985 remove_marker(channel
, name
);
987 mutex_unlock(&markers_mutex
);
992 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
995 * marker_get_private_data - Get a marker's probe private data
996 * @channel: marker channel
998 * @probe: probe to match
999 * @num: get the nth matching probe's private data
1001 * Returns the nth private data pointer (starting from 0) matching, or an
1003 * Returns the private data pointer, or an ERR_PTR.
1004 * The private data pointer should _only_ be dereferenced if the caller is the
1005 * owner of the data, or its content could vanish. This is mostly used to
1006 * confirm that a caller is the owner of a registered probe.
1008 void *marker_get_private_data(const char *channel
, const char *name
,
1009 marker_probe_func
*probe
, int num
)
1011 struct hlist_head
*head
;
1012 struct hlist_node
*node
;
1013 struct marker_entry
*e
;
1014 size_t channel_len
= strlen(channel
) + 1;
1015 size_t name_len
= strlen(name
) + 1;
1019 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1020 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1021 hlist_for_each_entry(e
, node
, head
, hlist
) {
1022 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1024 if (num
== 0 && e
->single
.func
== probe
)
1025 return e
->single
.probe_private
;
1027 struct marker_probe_closure
*closure
;
1030 for (i
= 0; closure
[i
].func
; i
++) {
1031 if (closure
[i
].func
!= probe
)
1034 return closure
[i
].probe_private
;
1040 return ERR_PTR(-ENOENT
);
1042 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1045 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1047 * Called when no channel users are active by the channel infrastructure.
1048 * Called with lock_markers() and channel mutex held.
1050 //ust// void markers_compact_event_ids(void)
1052 //ust// struct marker_entry *entry;
1053 //ust// unsigned int i;
1054 //ust// struct hlist_head *head;
1055 //ust// struct hlist_node *node;
1058 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1059 //ust// head = &marker_table[i];
1060 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1061 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1062 //ust// WARN_ON(ret < 0);
1063 //ust// entry->channel_id = ret;
1064 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1065 //ust// entry->name);
1066 //ust// WARN_ON(ret < 0);
1067 //ust// entry->event_id = ret;
1072 //ust//#ifdef CONFIG_MODULES
1075 * Returns 0 if current not found.
1076 * Returns 1 if current found.
1078 int lib_get_iter_markers(struct marker_iter
*iter
)
1080 struct lib
*iter_lib
;
1083 //ust// mutex_lock(&module_mutex);
1084 list_for_each_entry(iter_lib
, &libs
, list
) {
1085 if (iter_lib
< iter
->lib
)
1087 else if (iter_lib
> iter
->lib
)
1088 iter
->marker
= NULL
;
1089 found
= marker_get_iter_range(&iter
->marker
,
1090 iter_lib
->markers_start
,
1091 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1093 iter
->lib
= iter_lib
;
1097 //ust// mutex_unlock(&module_mutex);
1102 * marker_get_iter_range - Get a next marker iterator given a range.
1103 * @marker: current markers (in), next marker (out)
1104 * @begin: beginning of the range
1105 * @end: end of the range
1107 * Returns whether a next marker has been found (1) or not (0).
1108 * Will return the first marker in the range if the input marker is NULL.
1110 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1113 if (!*marker
&& begin
!= end
) {
1117 if (*marker
>= begin
&& *marker
< end
)
1121 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1123 static void marker_get_iter(struct marker_iter
*iter
)
1127 /* Core kernel markers */
1129 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1130 found
= marker_get_iter_range(&iter
->marker
,
1131 __start___markers
, __stop___markers
);
1135 /* Markers in modules. */
1136 found
= lib_get_iter_markers(iter
);
1139 marker_iter_reset(iter
);
1142 void marker_iter_start(struct marker_iter
*iter
)
1144 marker_get_iter(iter
);
1146 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1148 void marker_iter_next(struct marker_iter
*iter
)
1152 * iter->marker may be invalid because we blindly incremented it.
1153 * Make sure it is valid by marshalling on the markers, getting the
1154 * markers from following modules if necessary.
1156 marker_get_iter(iter
);
1158 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1160 void marker_iter_stop(struct marker_iter
*iter
)
1163 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1165 void marker_iter_reset(struct marker_iter
*iter
)
1168 iter
->marker
= NULL
;
1170 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1172 #ifdef CONFIG_MARKERS_USERSPACE
1174 * must be called with current->user_markers_mutex held
1176 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1178 struct user_marker
*umark
;
1179 struct hlist_node
*pos
, *n
;
1181 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1182 if (umark
->state
== state
) {
1183 hlist_del(&umark
->hlist
);
1189 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1190 //ust// char __user *state, int reg)
1192 //ust// struct user_marker *umark;
1194 //ust// struct marker_entry *entry;
1195 //ust// int ret = 0;
1197 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1198 //ust// current->comm, reg ? "registers" : "unregisters",
1199 //ust// name, state);
1201 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1202 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1203 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1204 //ust// umark->state = state;
1205 //ust// len = strncpy_from_user(umark->name, name,
1206 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1207 //ust// if (len < 0) {
1208 //ust// ret = -EFAULT;
1211 //ust// len = strncpy_from_user(umark->format, format,
1212 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1213 //ust// if (len < 0) {
1214 //ust// ret = -EFAULT;
1217 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1218 //ust// umark->format);
1219 //ust// mutex_lock(&markers_mutex);
1220 //ust// entry = get_marker("userspace", umark->name);
1221 //ust// if (entry) {
1222 //ust// if (entry->format &&
1223 //ust// strcmp(entry->format, umark->format) != 0) {
1224 //ust// printk(" error, wrong format in process %s",
1225 //ust// current->comm);
1226 //ust// ret = -EPERM;
1227 //ust// goto error_unlock;
1229 //ust// printk(" %s", !!entry->refcount
1230 //ust// ? "enabled" : "disabled");
1231 //ust// if (put_user(!!entry->refcount, state)) {
1232 //ust// ret = -EFAULT;
1233 //ust// goto error_unlock;
1235 //ust// printk("\n");
1237 //ust// printk(" disabled\n");
1238 //ust// if (put_user(0, umark->state)) {
1239 //ust// printk(KERN_WARNING
1240 //ust// "Marker in %s caused a fault\n",
1241 //ust// current->comm);
1242 //ust// goto error_unlock;
1245 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1246 //ust// hlist_add_head(&umark->hlist,
1247 //ust// ¤t->group_leader->user_markers);
1248 //ust// current->group_leader->user_markers_sequence++;
1249 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1250 //ust// mutex_unlock(&markers_mutex);
1252 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1253 //ust// free_user_marker(state,
1254 //ust// ¤t->group_leader->user_markers);
1255 //ust// current->group_leader->user_markers_sequence++;
1256 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1259 //ust// error_unlock:
1260 //ust// mutex_unlock(&markers_mutex);
1262 //ust// kfree(umark);
1269 //ust// * string : 0
1271 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1272 //ust// char __user *ubuf)
1274 //ust// long ret = -EPERM;
1278 //ust// switch (type) {
1279 //ust// case 0: /* String */
1280 //ust// ret = -ENOMEM;
1281 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1283 //ust// goto string_out;
1284 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1285 //ust// if (len < 0) {
1286 //ust// ret = -EFAULT;
1287 //ust// goto string_err;
1289 //ust// trace_mark(userspace, string, "string %s", page);
1291 //ust// free_page((unsigned long) page);
1300 //ust// static void marker_update_processes(void)
1302 //ust// struct task_struct *g, *t;
1305 //ust// * markers_mutex is taken to protect the p->user_markers read.
1307 //ust// mutex_lock(&markers_mutex);
1308 //ust// read_lock(&tasklist_lock);
1309 //ust// for_each_process(g) {
1310 //ust// WARN_ON(!thread_group_leader(g));
1311 //ust// if (hlist_empty(&g->user_markers))
1313 //ust// if (strcmp(g->comm, "testprog") == 0)
1314 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1317 //ust// /* TODO : implement this thread flag in each arch. */
1318 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1319 //ust// } while ((t = next_thread(t)) != g);
1321 //ust// read_unlock(&tasklist_lock);
1322 //ust// mutex_unlock(&markers_mutex);
1326 * Update current process.
1327 * Note that we have to wait a whole scheduler period before we are sure that
1328 * every running userspace threads have their markers updated.
1329 * (synchronize_sched() can be used to insure this).
1331 void marker_update_process(void)
1333 struct user_marker
*umark
;
1334 struct hlist_node
*pos
;
1335 struct marker_entry
*entry
;
1337 mutex_lock(&markers_mutex
);
1338 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1339 if (strcmp(current
->comm
, "testprog") == 0)
1340 printk(KERN_DEBUG
"do update pending for testprog\n");
1341 hlist_for_each_entry(umark
, pos
,
1342 ¤t
->group_leader
->user_markers
, hlist
) {
1343 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1344 umark
->name
, current
->comm
);
1345 entry
= get_marker("userspace", umark
->name
);
1347 if (entry
->format
&&
1348 strcmp(entry
->format
, umark
->format
) != 0) {
1350 " error, wrong format in process %s\n",
1354 if (put_user(!!entry
->refcount
, umark
->state
)) {
1356 "Marker in %s caused a fault\n",
1361 if (put_user(0, umark
->state
)) {
1363 "Marker in %s caused a fault\n",
1369 clear_thread_flag(TIF_MARKER_PENDING
);
1370 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1371 mutex_unlock(&markers_mutex
);
1375 * Called at process exit and upon do_execve().
1376 * We assume that when the leader exits, no more references can be done to the
1377 * leader structure by the other threads.
1379 void exit_user_markers(struct task_struct
*p
)
1381 struct user_marker
*umark
;
1382 struct hlist_node
*pos
, *n
;
1384 if (thread_group_leader(p
)) {
1385 mutex_lock(&markers_mutex
);
1386 mutex_lock(&p
->user_markers_mutex
);
1387 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1390 INIT_HLIST_HEAD(&p
->user_markers
);
1391 p
->user_markers_sequence
++;
1392 mutex_unlock(&p
->user_markers_mutex
);
1393 mutex_unlock(&markers_mutex
);
1397 int is_marker_enabled(const char *channel
, const char *name
)
1399 struct marker_entry
*entry
;
1401 mutex_lock(&markers_mutex
);
1402 entry
= get_marker(channel
, name
);
1403 mutex_unlock(&markers_mutex
);
1405 return entry
&& !!entry
->refcount
;
1409 int marker_module_notify(struct notifier_block
*self
,
1410 unsigned long val
, void *data
)
1412 struct module
*mod
= data
;
1415 case MODULE_STATE_COMING
:
1416 marker_update_probe_range(mod
->markers
,
1417 mod
->markers
+ mod
->num_markers
);
1419 case MODULE_STATE_GOING
:
1420 marker_update_probe_range(mod
->markers
,
1421 mod
->markers
+ mod
->num_markers
);
1427 struct notifier_block marker_module_nb
= {
1428 .notifier_call
= marker_module_notify
,
1432 //ust// static int init_markers(void)
1434 //ust// return register_module_notifier(&marker_module_nb);
1436 //ust// __initcall(init_markers);
1437 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1439 #endif /* CONFIG_MODULES */
1441 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1443 struct marker_entry
*entry
;
1444 struct ltt_probe_private_data call_data
;
1445 struct hlist_head
*head
;
1446 struct hlist_node
*node
;
1449 mutex_lock(&markers_mutex
);
1450 call_data
.trace
= trace
;
1451 call_data
.serializer
= NULL
;
1453 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1454 head
= &marker_table
[i
];
1455 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1456 __trace_mark(0, metadata
, core_marker_id
,
1458 "channel %s name %s event_id %hu "
1459 "int #1u%zu long #1u%zu pointer #1u%zu "
1460 "size_t #1u%zu alignment #1u%u",
1464 sizeof(int), sizeof(long),
1465 sizeof(void *), sizeof(size_t),
1466 ltt_get_alignment());
1468 __trace_mark(0, metadata
,
1471 "channel %s name %s format %s",
1477 mutex_unlock(&markers_mutex
);
1479 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1481 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1483 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1488 static void new_markers(struct marker
*start
, struct marker
*end
)
1492 for(m
=start
; m
< end
; m
++) {
1498 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1502 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1504 pl
->markers_start
= markers_start
;
1505 pl
->markers_count
= markers_count
;
1507 /* FIXME: maybe protect this with its own mutex? */
1509 list_add(&pl
->list
, &libs
);
1512 new_markers(markers_start
, markers_start
+ markers_count
);
1514 /* FIXME: update just the loaded lib */
1515 lib_update_markers();
1517 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1522 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1524 /*FIXME: implement; but before implementing, marker_register_lib must
1525 have appropriate locking. */
1530 static int initialized
= 0;
1532 void __attribute__((constructor
)) init_markers(void)
1535 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1536 DBG("markers_start: %p, markers_stop: %p\n", __start___markers
, __stop___markers
);