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 /* Activate marker if necessary */
827 marker_update_probes();
829 mutex_lock(&markers_mutex
);
830 entry
= get_marker(channel
, name
);
833 //ust// if (entry->rcu_pending)
834 //ust// rcu_barrier_sched();
836 entry
->rcu_pending
= 1;
837 /* write rcu_pending before calling the RCU callback */
839 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
840 synchronize_rcu(); free_old_closure(&entry
->rcu
);
843 error_unregister_channel
:
844 ret_err
= ltt_channels_unregister(channel
);
847 ret_err
= remove_marker(channel
, name
);
850 mutex_unlock(&markers_mutex
);
853 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
856 * marker_probe_unregister - Disconnect a probe from a marker
857 * @channel: marker channel
859 * @probe: probe function pointer
860 * @probe_private: probe private data
862 * Returns the private data given to marker_probe_register, or an ERR_PTR().
863 * We do not need to call a synchronize_sched to make sure the probes have
864 * finished running before doing a module unload, because the module unload
865 * itself uses stop_machine(), which insures that every preempt disabled section
868 int marker_probe_unregister(const char *channel
, const char *name
,
869 marker_probe_func
*probe
, void *probe_private
)
871 struct marker_entry
*entry
;
872 struct marker_probe_closure
*old
;
875 mutex_lock(&markers_mutex
);
876 entry
= get_marker(channel
, name
);
879 //ust// if (entry->rcu_pending)
880 //ust// rcu_barrier_sched();
881 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
882 mutex_unlock(&markers_mutex
);
884 marker_update_probes();
886 mutex_lock(&markers_mutex
);
887 entry
= get_marker(channel
, name
);
890 //ust// if (entry->rcu_pending)
891 //ust// rcu_barrier_sched();
893 entry
->rcu_pending
= 1;
894 /* write rcu_pending before calling the RCU callback */
896 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
897 synchronize_rcu(); free_old_closure(&entry
->rcu
);
898 remove_marker(channel
, name
); /* Ignore busy error message */
901 mutex_unlock(&markers_mutex
);
904 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
906 static struct marker_entry
*
907 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
909 struct marker_entry
*entry
;
911 struct hlist_head
*head
;
912 struct hlist_node
*node
;
914 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
915 head
= &marker_table
[i
];
916 hlist_for_each_entry(entry
, node
, head
, hlist
) {
918 if (entry
->single
.func
== probe
919 && entry
->single
.probe_private
923 struct marker_probe_closure
*closure
;
924 closure
= entry
->multi
;
925 for (i
= 0; closure
[i
].func
; i
++) {
926 if (closure
[i
].func
== probe
&&
927 closure
[i
].probe_private
938 * marker_probe_unregister_private_data - Disconnect a probe from a marker
939 * @probe: probe function
940 * @probe_private: probe private data
942 * Unregister a probe by providing the registered private data.
943 * Only removes the first marker found in hash table.
944 * Return 0 on success or error value.
945 * We do not need to call a synchronize_sched to make sure the probes have
946 * finished running before doing a module unload, because the module unload
947 * itself uses stop_machine(), which insures that every preempt disabled section
950 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
953 struct marker_entry
*entry
;
955 struct marker_probe_closure
*old
;
956 const char *channel
= NULL
, *name
= NULL
;
958 mutex_lock(&markers_mutex
);
959 entry
= get_marker_from_private_data(probe
, probe_private
);
964 //ust// if (entry->rcu_pending)
965 //ust// rcu_barrier_sched();
966 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
967 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
968 name
= kstrdup(entry
->name
, GFP_KERNEL
);
969 mutex_unlock(&markers_mutex
);
971 marker_update_probes();
973 mutex_lock(&markers_mutex
);
974 entry
= get_marker(channel
, name
);
977 //ust// if (entry->rcu_pending)
978 //ust// rcu_barrier_sched();
980 entry
->rcu_pending
= 1;
981 /* write rcu_pending before calling the RCU callback */
983 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
984 synchronize_rcu(); free_old_closure(&entry
->rcu
);
985 /* Ignore busy error message */
986 remove_marker(channel
, name
);
988 mutex_unlock(&markers_mutex
);
993 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
996 * marker_get_private_data - Get a marker's probe private data
997 * @channel: marker channel
999 * @probe: probe to match
1000 * @num: get the nth matching probe's private data
1002 * Returns the nth private data pointer (starting from 0) matching, or an
1004 * Returns the private data pointer, or an ERR_PTR.
1005 * The private data pointer should _only_ be dereferenced if the caller is the
1006 * owner of the data, or its content could vanish. This is mostly used to
1007 * confirm that a caller is the owner of a registered probe.
1009 void *marker_get_private_data(const char *channel
, const char *name
,
1010 marker_probe_func
*probe
, int num
)
1012 struct hlist_head
*head
;
1013 struct hlist_node
*node
;
1014 struct marker_entry
*e
;
1015 size_t channel_len
= strlen(channel
) + 1;
1016 size_t name_len
= strlen(name
) + 1;
1020 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1021 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1022 hlist_for_each_entry(e
, node
, head
, hlist
) {
1023 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1025 if (num
== 0 && e
->single
.func
== probe
)
1026 return e
->single
.probe_private
;
1028 struct marker_probe_closure
*closure
;
1031 for (i
= 0; closure
[i
].func
; i
++) {
1032 if (closure
[i
].func
!= probe
)
1035 return closure
[i
].probe_private
;
1041 return ERR_PTR(-ENOENT
);
1043 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1046 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1048 * Called when no channel users are active by the channel infrastructure.
1049 * Called with lock_markers() and channel mutex held.
1051 //ust// void markers_compact_event_ids(void)
1053 //ust// struct marker_entry *entry;
1054 //ust// unsigned int i;
1055 //ust// struct hlist_head *head;
1056 //ust// struct hlist_node *node;
1059 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1060 //ust// head = &marker_table[i];
1061 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1062 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1063 //ust// WARN_ON(ret < 0);
1064 //ust// entry->channel_id = ret;
1065 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1066 //ust// entry->name);
1067 //ust// WARN_ON(ret < 0);
1068 //ust// entry->event_id = ret;
1073 //ust//#ifdef CONFIG_MODULES
1076 * Returns 0 if current not found.
1077 * Returns 1 if current found.
1079 int lib_get_iter_markers(struct marker_iter
*iter
)
1081 struct lib
*iter_lib
;
1084 //ust// mutex_lock(&module_mutex);
1085 list_for_each_entry(iter_lib
, &libs
, list
) {
1086 if (iter_lib
< iter
->lib
)
1088 else if (iter_lib
> iter
->lib
)
1089 iter
->marker
= NULL
;
1090 found
= marker_get_iter_range(&iter
->marker
,
1091 iter_lib
->markers_start
,
1092 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1094 iter
->lib
= iter_lib
;
1098 //ust// mutex_unlock(&module_mutex);
1103 * marker_get_iter_range - Get a next marker iterator given a range.
1104 * @marker: current markers (in), next marker (out)
1105 * @begin: beginning of the range
1106 * @end: end of the range
1108 * Returns whether a next marker has been found (1) or not (0).
1109 * Will return the first marker in the range if the input marker is NULL.
1111 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1114 if (!*marker
&& begin
!= end
) {
1118 if (*marker
>= begin
&& *marker
< end
)
1122 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1124 static void marker_get_iter(struct marker_iter
*iter
)
1128 /* Core kernel markers */
1130 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1131 found
= marker_get_iter_range(&iter
->marker
,
1132 __start___markers
, __stop___markers
);
1136 /* Markers in modules. */
1137 found
= lib_get_iter_markers(iter
);
1140 marker_iter_reset(iter
);
1143 void marker_iter_start(struct marker_iter
*iter
)
1145 marker_get_iter(iter
);
1147 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1149 void marker_iter_next(struct marker_iter
*iter
)
1153 * iter->marker may be invalid because we blindly incremented it.
1154 * Make sure it is valid by marshalling on the markers, getting the
1155 * markers from following modules if necessary.
1157 marker_get_iter(iter
);
1159 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1161 void marker_iter_stop(struct marker_iter
*iter
)
1164 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1166 void marker_iter_reset(struct marker_iter
*iter
)
1169 iter
->marker
= NULL
;
1171 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1173 #ifdef CONFIG_MARKERS_USERSPACE
1175 * must be called with current->user_markers_mutex held
1177 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1179 struct user_marker
*umark
;
1180 struct hlist_node
*pos
, *n
;
1182 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1183 if (umark
->state
== state
) {
1184 hlist_del(&umark
->hlist
);
1190 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1191 //ust// char __user *state, int reg)
1193 //ust// struct user_marker *umark;
1195 //ust// struct marker_entry *entry;
1196 //ust// int ret = 0;
1198 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1199 //ust// current->comm, reg ? "registers" : "unregisters",
1200 //ust// name, state);
1202 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1203 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1204 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1205 //ust// umark->state = state;
1206 //ust// len = strncpy_from_user(umark->name, name,
1207 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1208 //ust// if (len < 0) {
1209 //ust// ret = -EFAULT;
1212 //ust// len = strncpy_from_user(umark->format, format,
1213 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1214 //ust// if (len < 0) {
1215 //ust// ret = -EFAULT;
1218 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1219 //ust// umark->format);
1220 //ust// mutex_lock(&markers_mutex);
1221 //ust// entry = get_marker("userspace", umark->name);
1222 //ust// if (entry) {
1223 //ust// if (entry->format &&
1224 //ust// strcmp(entry->format, umark->format) != 0) {
1225 //ust// printk(" error, wrong format in process %s",
1226 //ust// current->comm);
1227 //ust// ret = -EPERM;
1228 //ust// goto error_unlock;
1230 //ust// printk(" %s", !!entry->refcount
1231 //ust// ? "enabled" : "disabled");
1232 //ust// if (put_user(!!entry->refcount, state)) {
1233 //ust// ret = -EFAULT;
1234 //ust// goto error_unlock;
1236 //ust// printk("\n");
1238 //ust// printk(" disabled\n");
1239 //ust// if (put_user(0, umark->state)) {
1240 //ust// printk(KERN_WARNING
1241 //ust// "Marker in %s caused a fault\n",
1242 //ust// current->comm);
1243 //ust// goto error_unlock;
1246 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1247 //ust// hlist_add_head(&umark->hlist,
1248 //ust// ¤t->group_leader->user_markers);
1249 //ust// current->group_leader->user_markers_sequence++;
1250 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1251 //ust// mutex_unlock(&markers_mutex);
1253 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1254 //ust// free_user_marker(state,
1255 //ust// ¤t->group_leader->user_markers);
1256 //ust// current->group_leader->user_markers_sequence++;
1257 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1260 //ust// error_unlock:
1261 //ust// mutex_unlock(&markers_mutex);
1263 //ust// kfree(umark);
1270 //ust// * string : 0
1272 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1273 //ust// char __user *ubuf)
1275 //ust// long ret = -EPERM;
1279 //ust// switch (type) {
1280 //ust// case 0: /* String */
1281 //ust// ret = -ENOMEM;
1282 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1284 //ust// goto string_out;
1285 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1286 //ust// if (len < 0) {
1287 //ust// ret = -EFAULT;
1288 //ust// goto string_err;
1290 //ust// trace_mark(userspace, string, "string %s", page);
1292 //ust// free_page((unsigned long) page);
1301 //ust// static void marker_update_processes(void)
1303 //ust// struct task_struct *g, *t;
1306 //ust// * markers_mutex is taken to protect the p->user_markers read.
1308 //ust// mutex_lock(&markers_mutex);
1309 //ust// read_lock(&tasklist_lock);
1310 //ust// for_each_process(g) {
1311 //ust// WARN_ON(!thread_group_leader(g));
1312 //ust// if (hlist_empty(&g->user_markers))
1314 //ust// if (strcmp(g->comm, "testprog") == 0)
1315 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1318 //ust// /* TODO : implement this thread flag in each arch. */
1319 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1320 //ust// } while ((t = next_thread(t)) != g);
1322 //ust// read_unlock(&tasklist_lock);
1323 //ust// mutex_unlock(&markers_mutex);
1327 * Update current process.
1328 * Note that we have to wait a whole scheduler period before we are sure that
1329 * every running userspace threads have their markers updated.
1330 * (synchronize_sched() can be used to insure this).
1332 void marker_update_process(void)
1334 struct user_marker
*umark
;
1335 struct hlist_node
*pos
;
1336 struct marker_entry
*entry
;
1338 mutex_lock(&markers_mutex
);
1339 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1340 if (strcmp(current
->comm
, "testprog") == 0)
1341 printk(KERN_DEBUG
"do update pending for testprog\n");
1342 hlist_for_each_entry(umark
, pos
,
1343 ¤t
->group_leader
->user_markers
, hlist
) {
1344 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1345 umark
->name
, current
->comm
);
1346 entry
= get_marker("userspace", umark
->name
);
1348 if (entry
->format
&&
1349 strcmp(entry
->format
, umark
->format
) != 0) {
1351 " error, wrong format in process %s\n",
1355 if (put_user(!!entry
->refcount
, umark
->state
)) {
1357 "Marker in %s caused a fault\n",
1362 if (put_user(0, umark
->state
)) {
1364 "Marker in %s caused a fault\n",
1370 clear_thread_flag(TIF_MARKER_PENDING
);
1371 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1372 mutex_unlock(&markers_mutex
);
1376 * Called at process exit and upon do_execve().
1377 * We assume that when the leader exits, no more references can be done to the
1378 * leader structure by the other threads.
1380 void exit_user_markers(struct task_struct
*p
)
1382 struct user_marker
*umark
;
1383 struct hlist_node
*pos
, *n
;
1385 if (thread_group_leader(p
)) {
1386 mutex_lock(&markers_mutex
);
1387 mutex_lock(&p
->user_markers_mutex
);
1388 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1391 INIT_HLIST_HEAD(&p
->user_markers
);
1392 p
->user_markers_sequence
++;
1393 mutex_unlock(&p
->user_markers_mutex
);
1394 mutex_unlock(&markers_mutex
);
1398 int is_marker_enabled(const char *channel
, const char *name
)
1400 struct marker_entry
*entry
;
1402 mutex_lock(&markers_mutex
);
1403 entry
= get_marker(channel
, name
);
1404 mutex_unlock(&markers_mutex
);
1406 return entry
&& !!entry
->refcount
;
1410 int marker_module_notify(struct notifier_block
*self
,
1411 unsigned long val
, void *data
)
1413 struct module
*mod
= data
;
1416 case MODULE_STATE_COMING
:
1417 marker_update_probe_range(mod
->markers
,
1418 mod
->markers
+ mod
->num_markers
);
1420 case MODULE_STATE_GOING
:
1421 marker_update_probe_range(mod
->markers
,
1422 mod
->markers
+ mod
->num_markers
);
1428 struct notifier_block marker_module_nb
= {
1429 .notifier_call
= marker_module_notify
,
1433 //ust// static int init_markers(void)
1435 //ust// return register_module_notifier(&marker_module_nb);
1437 //ust// __initcall(init_markers);
1438 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1440 #endif /* CONFIG_MODULES */
1442 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1444 struct marker_entry
*entry
;
1445 struct ltt_probe_private_data call_data
;
1446 struct hlist_head
*head
;
1447 struct hlist_node
*node
;
1450 mutex_lock(&markers_mutex
);
1451 call_data
.trace
= trace
;
1452 call_data
.serializer
= NULL
;
1454 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1455 head
= &marker_table
[i
];
1456 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1457 __trace_mark(0, metadata
, core_marker_id
,
1459 "channel %s name %s event_id %hu "
1460 "int #1u%zu long #1u%zu pointer #1u%zu "
1461 "size_t #1u%zu alignment #1u%u",
1465 sizeof(int), sizeof(long),
1466 sizeof(void *), sizeof(size_t),
1467 ltt_get_alignment());
1469 __trace_mark(0, metadata
,
1472 "channel %s name %s format %s",
1478 mutex_unlock(&markers_mutex
);
1480 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1482 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1484 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1489 static void new_markers(struct marker
*start
, struct marker
*end
)
1493 for(m
=start
; m
< end
; m
++) {
1499 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1503 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1505 pl
->markers_start
= markers_start
;
1506 pl
->markers_count
= markers_count
;
1508 /* FIXME: maybe protect this with its own mutex? */
1510 list_add(&pl
->list
, &libs
);
1513 new_markers(markers_start
, markers_start
+ markers_count
);
1515 /* FIXME: update just the loaded lib */
1516 lib_update_markers();
1518 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1523 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1525 /*FIXME: implement; but before implementing, marker_register_lib must
1526 have appropriate locking. */
1531 static int initialized
= 0;
1533 void __attribute__((constructor
)) init_markers(void)
1536 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1537 DBG("markers_start: %p, markers_stop: %p\n", __start___markers
, __stop___markers
);