2 * Copyright (C) 2007 Mathieu Desnoyers
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
34 #include "kernelcompat.h"
37 #include "tracercore.h"
41 extern struct marker __start___markers
[] __attribute__((visibility("hidden")));
42 extern struct marker __stop___markers
[] __attribute__((visibility("hidden")));
44 /* Set to 1 to enable marker debug output */
45 static const int marker_debug
;
48 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
49 * and module markers and the hash table.
51 static DEFINE_MUTEX(markers_mutex
);
53 void lock_markers(void)
55 mutex_lock(&markers_mutex
);
58 void unlock_markers(void)
60 mutex_unlock(&markers_mutex
);
64 * Marker hash table, containing the active markers.
65 * Protected by module_mutex.
67 #define MARKER_HASH_BITS 6
68 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
69 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
73 * It is used to make sure every handler has finished using its private data
74 * between two consecutive operation (add or remove) on a given marker. It is
75 * also used to delay the free of multiple probes array until a quiescent state
77 * marker entries modifications are protected by the markers_mutex.
80 struct hlist_node hlist
;
84 void (*call
)(const struct marker
*mdata
, void *call_private
, ...);
85 struct marker_probe_closure single
;
86 struct marker_probe_closure
*multi
;
87 int refcount
; /* Number of times armed. 0 if disarmed. */
93 unsigned char ptype
:1;
94 unsigned char format_allocated
:1;
95 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
98 #ifdef CONFIG_MARKERS_USERSPACE
99 static void marker_update_processes(void);
101 static void marker_update_processes(void)
107 * __mark_empty_function - Empty probe callback
108 * @mdata: marker data
109 * @probe_private: probe private data
110 * @call_private: call site private data
111 * @fmt: format string
112 * @...: variable argument list
114 * Empty callback provided as a probe to the markers. By providing this to a
115 * disabled marker, we make sure the execution flow is always valid even
116 * though the function pointer change and the marker enabling are two distinct
117 * operations that modifies the execution flow of preemptible code.
119 notrace
void __mark_empty_function(const struct marker
*mdata
,
120 void *probe_private
, void *call_private
, const char *fmt
, va_list *args
)
123 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
126 * marker_probe_cb Callback that prepares the variable argument list for probes.
127 * @mdata: pointer of type struct marker
128 * @call_private: caller site private data
129 * @...: Variable argument list.
131 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
132 * need to put a full smp_rmb() in this branch. This is why we do not use
133 * rcu_dereference() for the pointer read.
135 notrace
void marker_probe_cb(const struct marker
*mdata
,
136 void *call_private
, ...)
142 * rcu_read_lock_sched does two things : disabling preemption to make
143 * sure the teardown of the callbacks can be done correctly when they
144 * are in modules and they insure RCU read coherency.
146 //ust// rcu_read_lock_sched_notrace();
147 ptype
= mdata
->ptype
;
148 if (likely(!ptype
)) {
149 marker_probe_func
*func
;
150 /* Must read the ptype before ptr. They are not data dependant,
151 * so we put an explicit smp_rmb() here. */
153 func
= mdata
->single
.func
;
154 /* Must read the ptr before private data. They are not data
155 * dependant, so we put an explicit smp_rmb() here. */
157 va_start(args
, call_private
);
158 func(mdata
, mdata
->single
.probe_private
, call_private
,
159 mdata
->format
, &args
);
162 struct marker_probe_closure
*multi
;
165 * Read mdata->ptype before mdata->multi.
168 multi
= mdata
->multi
;
170 * multi points to an array, therefore accessing the array
171 * depends on reading multi. However, even in this case,
172 * we must insure that the pointer is read _before_ the array
173 * data. Same as rcu_dereference, but we need a full smp_rmb()
174 * in the fast path, so put the explicit barrier here.
176 smp_read_barrier_depends();
177 for (i
= 0; multi
[i
].func
; i
++) {
178 va_start(args
, call_private
);
179 multi
[i
].func(mdata
, multi
[i
].probe_private
,
180 call_private
, mdata
->format
, &args
);
184 //ust// rcu_read_unlock_sched_notrace();
186 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
189 * marker_probe_cb Callback that does not prepare the variable argument list.
190 * @mdata: pointer of type struct marker
191 * @call_private: caller site private data
192 * @...: Variable argument list.
194 * Should be connected to markers "MARK_NOARGS".
196 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
197 void *call_private
, ...)
199 va_list args
; /* not initialized */
202 //ust// rcu_read_lock_sched_notrace();
203 ptype
= mdata
->ptype
;
204 if (likely(!ptype
)) {
205 marker_probe_func
*func
;
206 /* Must read the ptype before ptr. They are not data dependant,
207 * so we put an explicit smp_rmb() here. */
209 func
= mdata
->single
.func
;
210 /* Must read the ptr before private data. They are not data
211 * dependant, so we put an explicit smp_rmb() here. */
213 func(mdata
, mdata
->single
.probe_private
, call_private
,
214 mdata
->format
, &args
);
216 struct marker_probe_closure
*multi
;
219 * Read mdata->ptype before mdata->multi.
222 multi
= mdata
->multi
;
224 * multi points to an array, therefore accessing the array
225 * depends on reading multi. However, even in this case,
226 * we must insure that the pointer is read _before_ the array
227 * data. Same as rcu_dereference, but we need a full smp_rmb()
228 * in the fast path, so put the explicit barrier here.
230 smp_read_barrier_depends();
231 for (i
= 0; multi
[i
].func
; i
++)
232 multi
[i
].func(mdata
, multi
[i
].probe_private
,
233 call_private
, mdata
->format
, &args
);
235 //ust// rcu_read_unlock_sched_notrace();
238 static void free_old_closure(struct rcu_head
*head
)
240 struct marker_entry
*entry
= container_of(head
,
241 struct marker_entry
, rcu
);
242 kfree(entry
->oldptr
);
243 /* Make sure we free the data before setting the pending flag to 0 */
245 entry
->rcu_pending
= 0;
248 static void debug_print_probes(struct marker_entry
*entry
)
256 printk(KERN_DEBUG
"Single probe : %p %p\n",
258 entry
->single
.probe_private
);
260 for (i
= 0; entry
->multi
[i
].func
; i
++)
261 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
262 entry
->multi
[i
].func
,
263 entry
->multi
[i
].probe_private
);
267 static struct marker_probe_closure
*
268 marker_entry_add_probe(struct marker_entry
*entry
,
269 marker_probe_func
*probe
, void *probe_private
)
272 struct marker_probe_closure
*old
, *new;
276 debug_print_probes(entry
);
279 if (entry
->single
.func
== probe
&&
280 entry
->single
.probe_private
== probe_private
)
281 return ERR_PTR(-EBUSY
);
282 if (entry
->single
.func
== __mark_empty_function
) {
284 entry
->single
.func
= probe
;
285 entry
->single
.probe_private
= probe_private
;
288 debug_print_probes(entry
);
296 /* (N -> N+1), (N != 0, 1) probes */
297 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
298 if (old
[nr_probes
].func
== probe
299 && old
[nr_probes
].probe_private
301 return ERR_PTR(-EBUSY
);
303 /* + 2 : one for new probe, one for NULL func */
304 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
307 return ERR_PTR(-ENOMEM
);
309 new[0] = entry
->single
;
312 nr_probes
* sizeof(struct marker_probe_closure
));
313 new[nr_probes
].func
= probe
;
314 new[nr_probes
].probe_private
= probe_private
;
315 entry
->refcount
= nr_probes
+ 1;
318 debug_print_probes(entry
);
322 static struct marker_probe_closure
*
323 marker_entry_remove_probe(struct marker_entry
*entry
,
324 marker_probe_func
*probe
, void *probe_private
)
326 int nr_probes
= 0, nr_del
= 0, i
;
327 struct marker_probe_closure
*old
, *new;
331 debug_print_probes(entry
);
333 /* 0 -> N is an error */
334 WARN_ON(entry
->single
.func
== __mark_empty_function
);
336 WARN_ON(probe
&& entry
->single
.func
!= probe
);
337 WARN_ON(entry
->single
.probe_private
!= probe_private
);
338 entry
->single
.func
= __mark_empty_function
;
341 debug_print_probes(entry
);
344 /* (N -> M), (N > 1, M >= 0) probes */
345 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
346 if ((!probe
|| old
[nr_probes
].func
== probe
)
347 && old
[nr_probes
].probe_private
353 if (nr_probes
- nr_del
== 0) {
354 /* N -> 0, (N > 1) */
355 entry
->single
.func
= __mark_empty_function
;
358 } else if (nr_probes
- nr_del
== 1) {
359 /* N -> 1, (N > 1) */
360 for (i
= 0; old
[i
].func
; i
++)
361 if ((probe
&& old
[i
].func
!= probe
) ||
362 old
[i
].probe_private
!= probe_private
)
363 entry
->single
= old
[i
];
368 /* N -> M, (N > 1, M > 1) */
370 new = kzalloc((nr_probes
- nr_del
+ 1)
371 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
373 return ERR_PTR(-ENOMEM
);
374 for (i
= 0; old
[i
].func
; i
++)
375 if ((probe
&& old
[i
].func
!= probe
) ||
376 old
[i
].probe_private
!= probe_private
)
378 entry
->refcount
= nr_probes
- nr_del
;
382 debug_print_probes(entry
);
387 * Get marker if the marker is present in the marker hash table.
388 * Must be called with markers_mutex held.
389 * Returns NULL if not present.
391 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
393 struct hlist_head
*head
;
394 struct hlist_node
*node
;
395 struct marker_entry
*e
;
396 size_t channel_len
= strlen(channel
) + 1;
397 size_t name_len
= strlen(name
) + 1;
400 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
401 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
402 hlist_for_each_entry(e
, node
, head
, hlist
) {
403 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
410 * Add the marker to the marker hash table. Must be called with markers_mutex
413 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
416 struct hlist_head
*head
;
417 struct hlist_node
*node
;
418 struct marker_entry
*e
;
419 size_t channel_len
= strlen(channel
) + 1;
420 size_t name_len
= strlen(name
) + 1;
421 size_t format_len
= 0;
424 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
426 format_len
= strlen(format
) + 1;
427 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
428 hlist_for_each_entry(e
, node
, head
, hlist
) {
429 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
431 "Marker %s.%s busy\n", channel
, name
);
432 return ERR_PTR(-EBUSY
); /* Already there */
436 * Using kmalloc here to allocate a variable length element. Could
437 * cause some memory fragmentation if overused.
439 e
= kmalloc(sizeof(struct marker_entry
)
440 + channel_len
+ name_len
+ format_len
,
443 return ERR_PTR(-ENOMEM
);
444 memcpy(e
->channel
, channel
, channel_len
);
445 e
->name
= &e
->channel
[channel_len
];
446 memcpy(e
->name
, name
, name_len
);
448 e
->format
= &e
->name
[channel_len
+ name_len
];
449 memcpy(e
->format
, format
, format_len
);
450 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
451 e
->call
= marker_probe_cb_noarg
;
453 e
->call
= marker_probe_cb
;
454 trace_mark(metadata
, core_marker_format
,
455 "channel %s name %s format %s",
456 e
->channel
, e
->name
, e
->format
);
459 e
->call
= marker_probe_cb
;
461 e
->single
.func
= __mark_empty_function
;
462 e
->single
.probe_private
= NULL
;
465 e
->format_allocated
= 0;
468 hlist_add_head(&e
->hlist
, head
);
473 * Remove the marker from the marker hash table. Must be called with mutex_lock
476 static int remove_marker(const char *channel
, const char *name
)
478 struct hlist_head
*head
;
479 struct hlist_node
*node
;
480 struct marker_entry
*e
;
482 size_t channel_len
= strlen(channel
) + 1;
483 size_t name_len
= strlen(name
) + 1;
487 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
488 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
489 hlist_for_each_entry(e
, node
, head
, hlist
) {
490 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
497 if (e
->single
.func
!= __mark_empty_function
)
499 hlist_del(&e
->hlist
);
500 if (e
->format_allocated
)
502 ret
= ltt_channels_unregister(e
->channel
);
504 /* Make sure the call_rcu has been executed */
505 //ust// if (e->rcu_pending)
506 //ust// rcu_barrier_sched();
512 * Set the mark_entry format to the format found in the element.
514 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
516 entry
->format
= kstrdup(format
, GFP_KERNEL
);
519 entry
->format_allocated
= 1;
521 trace_mark(metadata
, core_marker_format
,
522 "channel %s name %s format %s",
523 entry
->channel
, entry
->name
, entry
->format
);
528 * Sets the probe callback corresponding to one marker.
530 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
534 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
537 if (strcmp(entry
->format
, elem
->format
) != 0) {
539 "Format mismatch for probe %s "
540 "(%s), marker (%s)\n",
547 ret
= marker_set_format(entry
, elem
->format
);
553 * probe_cb setup (statically known) is done here. It is
554 * asynchronous with the rest of execution, therefore we only
555 * pass from a "safe" callback (with argument) to an "unsafe"
556 * callback (does not set arguments).
558 elem
->call
= entry
->call
;
559 elem
->channel_id
= entry
->channel_id
;
560 elem
->event_id
= entry
->event_id
;
563 * We only update the single probe private data when the ptr is
564 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
566 WARN_ON(elem
->single
.func
!= __mark_empty_function
567 && elem
->single
.probe_private
!= entry
->single
.probe_private
569 elem
->single
.probe_private
= entry
->single
.probe_private
;
571 * Make sure the private data is valid when we update the
575 elem
->single
.func
= entry
->single
.func
;
577 * We also make sure that the new probe callbacks array is consistent
578 * before setting a pointer to it.
580 rcu_assign_pointer(elem
->multi
, entry
->multi
);
582 * Update the function or multi probe array pointer before setting the
586 elem
->ptype
= entry
->ptype
;
588 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
589 //ust// WARN_ON(!elem->tp_cb);
591 //ust// * It is ok to directly call the probe registration because type
592 //ust// * checking has been done in the __trace_mark_tp() macro.
595 //ust// if (active) {
597 //ust// * try_module_get should always succeed because we hold
598 //ust// * markers_mutex to get the tp_cb address.
600 //ust// ret = try_module_get(__module_text_address(
601 //ust// (unsigned long)elem->tp_cb));
602 //ust// BUG_ON(!ret);
603 //ust// ret = tracepoint_probe_register_noupdate(
604 //ust// elem->tp_name,
605 //ust// elem->tp_cb);
607 //ust// ret = tracepoint_probe_unregister_noupdate(
608 //ust// elem->tp_name,
609 //ust// elem->tp_cb);
611 //ust// * tracepoint_probe_update_all() must be called
612 //ust// * before the module containing tp_cb is unloaded.
614 //ust// module_put(__module_text_address(
615 //ust// (unsigned long)elem->tp_cb));
618 elem
->state__imv
= active
;
624 * Disable a marker and its probe callback.
625 * Note: only waiting an RCU period after setting elem->call to the empty
626 * function insures that the original callback is not used anymore. This insured
627 * by rcu_read_lock_sched around the call site.
629 static void disable_marker(struct marker
*elem
)
633 /* leave "call" as is. It is known statically. */
634 //ust// if (elem->tp_name && _imv_read(elem->state)) {
635 //ust// WARN_ON(!elem->tp_cb);
637 //ust// * It is ok to directly call the probe registration because type
638 //ust// * checking has been done in the __trace_mark_tp() macro.
640 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
641 //ust// elem->tp_cb);
642 //ust// WARN_ON(ret);
644 //ust// * tracepoint_probe_update_all() must be called
645 //ust// * before the module containing tp_cb is unloaded.
647 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
649 elem
->state__imv
= 0;
650 elem
->single
.func
= __mark_empty_function
;
651 /* Update the function before setting the ptype */
653 elem
->ptype
= 0; /* single probe */
655 * Leave the private data and channel_id/event_id there, because removal
656 * is racy and should be done only after an RCU period. These are never
657 * used until the next initialization anyway.
662 * marker_update_probe_range - Update a probe range
663 * @begin: beginning of the range
664 * @end: end of the range
666 * Updates the probe callback corresponding to a range of markers.
668 void marker_update_probe_range(struct marker
*begin
,
672 struct marker_entry
*mark_entry
;
674 mutex_lock(&markers_mutex
);
675 for (iter
= begin
; iter
< end
; iter
++) {
676 mark_entry
= get_marker(iter
->channel
, iter
->name
);
678 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
680 * ignore error, continue
683 /* This is added for UST. We emit a core_marker_id event
684 * for markers that are already registered to a probe
685 * upon library load. Otherwise, no core_marker_id will
686 * be generated for these markers. Is this the right thing
689 trace_mark(metadata
, core_marker_id
,
690 "channel %s name %s event_id %hu "
691 "int #1u%zu long #1u%zu pointer #1u%zu "
692 "size_t #1u%zu alignment #1u%u",
693 iter
->channel
, iter
->name
, mark_entry
->event_id
,
694 sizeof(int), sizeof(long), sizeof(void *),
695 sizeof(size_t), ltt_get_alignment());
697 disable_marker(iter
);
700 mutex_unlock(&markers_mutex
);
704 * Update probes, removing the faulty probes.
706 * Internal callback only changed before the first probe is connected to it.
707 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
708 * transitions. All other transitions will leave the old private data valid.
709 * This makes the non-atomicity of the callback/private data updates valid.
711 * "special case" updates :
716 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
717 * Site effect : marker_set_format may delete the marker entry (creating a
720 static void marker_update_probes(void)
722 /* Core kernel markers */
723 //ust// marker_update_probe_range(__start___markers, __stop___markers);
724 /* Markers in modules. */
725 //ust// module_update_markers();
726 lib_update_markers();
727 //ust// tracepoint_probe_update_all();
728 /* Update immediate values */
730 //ust// module_imv_update(); /* FIXME: need to port for libs? */
731 marker_update_processes();
735 * marker_probe_register - Connect a probe to a marker
736 * @channel: marker channel
738 * @format: format string
739 * @probe: probe handler
740 * @probe_private: probe private data
742 * private data must be a valid allocated memory address, or NULL.
743 * Returns 0 if ok, error value on error.
744 * The probe address must at least be aligned on the architecture pointer size.
746 int marker_probe_register(const char *channel
, const char *name
,
747 const char *format
, marker_probe_func
*probe
,
750 struct marker_entry
*entry
;
751 int ret
= 0, ret_err
;
752 struct marker_probe_closure
*old
;
755 mutex_lock(&markers_mutex
);
756 entry
= get_marker(channel
, name
);
759 entry
= add_marker(channel
, name
, format
);
761 ret
= PTR_ERR(entry
);
764 ret
= ltt_channels_register(channel
);
766 goto error_remove_marker
;
767 ret
= ltt_channels_get_index_from_name(channel
);
769 goto error_unregister_channel
;
770 entry
->channel_id
= ret
;
771 ret
= ltt_channels_get_event_id(channel
, name
);
773 goto error_unregister_channel
;
774 entry
->event_id
= ret
;
776 trace_mark(metadata
, core_marker_id
,
777 "channel %s name %s event_id %hu "
778 "int #1u%zu long #1u%zu pointer #1u%zu "
779 "size_t #1u%zu alignment #1u%u",
780 channel
, name
, entry
->event_id
,
781 sizeof(int), sizeof(long), sizeof(void *),
782 sizeof(size_t), ltt_get_alignment());
785 ret
= marker_set_format(entry
, format
);
786 else if (strcmp(entry
->format
, format
))
793 * If we detect that a call_rcu is pending for this marker,
794 * make sure it's executed now.
796 //ust// if (entry->rcu_pending)
797 //ust// rcu_barrier_sched();
798 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
802 goto error_unregister_channel
;
806 mutex_unlock(&markers_mutex
);
808 marker_update_probes();
810 mutex_lock(&markers_mutex
);
811 entry
= get_marker(channel
, name
);
814 //ust// if (entry->rcu_pending)
815 //ust// rcu_barrier_sched();
817 entry
->rcu_pending
= 1;
818 /* write rcu_pending before calling the RCU callback */
820 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
821 synchronize_rcu(); free_old_closure(&entry
->rcu
);
824 error_unregister_channel
:
825 ret_err
= ltt_channels_unregister(channel
);
828 ret_err
= remove_marker(channel
, name
);
831 mutex_unlock(&markers_mutex
);
834 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
837 * marker_probe_unregister - Disconnect a probe from a marker
838 * @channel: marker channel
840 * @probe: probe function pointer
841 * @probe_private: probe private data
843 * Returns the private data given to marker_probe_register, or an ERR_PTR().
844 * We do not need to call a synchronize_sched to make sure the probes have
845 * finished running before doing a module unload, because the module unload
846 * itself uses stop_machine(), which insures that every preempt disabled section
849 int marker_probe_unregister(const char *channel
, const char *name
,
850 marker_probe_func
*probe
, void *probe_private
)
852 struct marker_entry
*entry
;
853 struct marker_probe_closure
*old
;
856 mutex_lock(&markers_mutex
);
857 entry
= get_marker(channel
, name
);
860 //ust// if (entry->rcu_pending)
861 //ust// rcu_barrier_sched();
862 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
863 mutex_unlock(&markers_mutex
);
865 marker_update_probes();
867 mutex_lock(&markers_mutex
);
868 entry
= get_marker(channel
, name
);
871 //ust// if (entry->rcu_pending)
872 //ust// rcu_barrier_sched();
874 entry
->rcu_pending
= 1;
875 /* write rcu_pending before calling the RCU callback */
877 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
878 synchronize_rcu(); free_old_closure(&entry
->rcu
);
879 remove_marker(channel
, name
); /* Ignore busy error message */
882 mutex_unlock(&markers_mutex
);
885 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
887 static struct marker_entry
*
888 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
890 struct marker_entry
*entry
;
892 struct hlist_head
*head
;
893 struct hlist_node
*node
;
895 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
896 head
= &marker_table
[i
];
897 hlist_for_each_entry(entry
, node
, head
, hlist
) {
899 if (entry
->single
.func
== probe
900 && entry
->single
.probe_private
904 struct marker_probe_closure
*closure
;
905 closure
= entry
->multi
;
906 for (i
= 0; closure
[i
].func
; i
++) {
907 if (closure
[i
].func
== probe
&&
908 closure
[i
].probe_private
919 * marker_probe_unregister_private_data - Disconnect a probe from a marker
920 * @probe: probe function
921 * @probe_private: probe private data
923 * Unregister a probe by providing the registered private data.
924 * Only removes the first marker found in hash table.
925 * Return 0 on success or error value.
926 * We do not need to call a synchronize_sched to make sure the probes have
927 * finished running before doing a module unload, because the module unload
928 * itself uses stop_machine(), which insures that every preempt disabled section
931 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
934 struct marker_entry
*entry
;
936 struct marker_probe_closure
*old
;
937 const char *channel
= NULL
, *name
= NULL
;
939 mutex_lock(&markers_mutex
);
940 entry
= get_marker_from_private_data(probe
, probe_private
);
945 //ust// if (entry->rcu_pending)
946 //ust// rcu_barrier_sched();
947 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
948 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
949 name
= kstrdup(entry
->name
, GFP_KERNEL
);
950 mutex_unlock(&markers_mutex
);
952 marker_update_probes();
954 mutex_lock(&markers_mutex
);
955 entry
= get_marker(channel
, name
);
958 //ust// if (entry->rcu_pending)
959 //ust// rcu_barrier_sched();
961 entry
->rcu_pending
= 1;
962 /* write rcu_pending before calling the RCU callback */
964 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
965 synchronize_rcu(); free_old_closure(&entry
->rcu
);
966 /* Ignore busy error message */
967 remove_marker(channel
, name
);
969 mutex_unlock(&markers_mutex
);
974 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
977 * marker_get_private_data - Get a marker's probe private data
978 * @channel: marker channel
980 * @probe: probe to match
981 * @num: get the nth matching probe's private data
983 * Returns the nth private data pointer (starting from 0) matching, or an
985 * Returns the private data pointer, or an ERR_PTR.
986 * The private data pointer should _only_ be dereferenced if the caller is the
987 * owner of the data, or its content could vanish. This is mostly used to
988 * confirm that a caller is the owner of a registered probe.
990 void *marker_get_private_data(const char *channel
, const char *name
,
991 marker_probe_func
*probe
, int num
)
993 struct hlist_head
*head
;
994 struct hlist_node
*node
;
995 struct marker_entry
*e
;
996 size_t channel_len
= strlen(channel
) + 1;
997 size_t name_len
= strlen(name
) + 1;
1001 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1002 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1003 hlist_for_each_entry(e
, node
, head
, hlist
) {
1004 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1006 if (num
== 0 && e
->single
.func
== probe
)
1007 return e
->single
.probe_private
;
1009 struct marker_probe_closure
*closure
;
1012 for (i
= 0; closure
[i
].func
; i
++) {
1013 if (closure
[i
].func
!= probe
)
1016 return closure
[i
].probe_private
;
1022 return ERR_PTR(-ENOENT
);
1024 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1027 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1029 * Called when no channel users are active by the channel infrastructure.
1030 * Called with lock_markers() and channel mutex held.
1032 //ust// void markers_compact_event_ids(void)
1034 //ust// struct marker_entry *entry;
1035 //ust// unsigned int i;
1036 //ust// struct hlist_head *head;
1037 //ust// struct hlist_node *node;
1040 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1041 //ust// head = &marker_table[i];
1042 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1043 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1044 //ust// WARN_ON(ret < 0);
1045 //ust// entry->channel_id = ret;
1046 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1047 //ust// entry->name);
1048 //ust// WARN_ON(ret < 0);
1049 //ust// entry->event_id = ret;
1054 //ust//#ifdef CONFIG_MODULES
1057 * marker_get_iter_range - Get a next marker iterator given a range.
1058 * @marker: current markers (in), next marker (out)
1059 * @begin: beginning of the range
1060 * @end: end of the range
1062 * Returns whether a next marker has been found (1) or not (0).
1063 * Will return the first marker in the range if the input marker is NULL.
1065 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1068 if (!*marker
&& begin
!= end
) {
1072 if (*marker
>= begin
&& *marker
< end
)
1076 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1078 static void marker_get_iter(struct marker_iter
*iter
)
1082 /* Core kernel markers */
1084 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1085 found
= marker_get_iter_range(&iter
->marker
,
1086 __start___markers
, __stop___markers
);
1090 /* Markers in modules. */
1091 found
= lib_get_iter_markers(iter
);
1094 marker_iter_reset(iter
);
1097 void marker_iter_start(struct marker_iter
*iter
)
1099 marker_get_iter(iter
);
1101 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1103 void marker_iter_next(struct marker_iter
*iter
)
1107 * iter->marker may be invalid because we blindly incremented it.
1108 * Make sure it is valid by marshalling on the markers, getting the
1109 * markers from following modules if necessary.
1111 marker_get_iter(iter
);
1113 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1115 void marker_iter_stop(struct marker_iter
*iter
)
1118 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1120 void marker_iter_reset(struct marker_iter
*iter
)
1123 iter
->marker
= NULL
;
1125 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1127 #ifdef CONFIG_MARKERS_USERSPACE
1129 * must be called with current->user_markers_mutex held
1131 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1133 struct user_marker
*umark
;
1134 struct hlist_node
*pos
, *n
;
1136 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1137 if (umark
->state
== state
) {
1138 hlist_del(&umark
->hlist
);
1144 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1145 //ust// char __user *state, int reg)
1147 //ust// struct user_marker *umark;
1149 //ust// struct marker_entry *entry;
1150 //ust// int ret = 0;
1152 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1153 //ust// current->comm, reg ? "registers" : "unregisters",
1154 //ust// name, state);
1156 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1157 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1158 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1159 //ust// umark->state = state;
1160 //ust// len = strncpy_from_user(umark->name, name,
1161 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1162 //ust// if (len < 0) {
1163 //ust// ret = -EFAULT;
1166 //ust// len = strncpy_from_user(umark->format, format,
1167 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1168 //ust// if (len < 0) {
1169 //ust// ret = -EFAULT;
1172 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1173 //ust// umark->format);
1174 //ust// mutex_lock(&markers_mutex);
1175 //ust// entry = get_marker("userspace", umark->name);
1176 //ust// if (entry) {
1177 //ust// if (entry->format &&
1178 //ust// strcmp(entry->format, umark->format) != 0) {
1179 //ust// printk(" error, wrong format in process %s",
1180 //ust// current->comm);
1181 //ust// ret = -EPERM;
1182 //ust// goto error_unlock;
1184 //ust// printk(" %s", !!entry->refcount
1185 //ust// ? "enabled" : "disabled");
1186 //ust// if (put_user(!!entry->refcount, state)) {
1187 //ust// ret = -EFAULT;
1188 //ust// goto error_unlock;
1190 //ust// printk("\n");
1192 //ust// printk(" disabled\n");
1193 //ust// if (put_user(0, umark->state)) {
1194 //ust// printk(KERN_WARNING
1195 //ust// "Marker in %s caused a fault\n",
1196 //ust// current->comm);
1197 //ust// goto error_unlock;
1200 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1201 //ust// hlist_add_head(&umark->hlist,
1202 //ust// ¤t->group_leader->user_markers);
1203 //ust// current->group_leader->user_markers_sequence++;
1204 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1205 //ust// mutex_unlock(&markers_mutex);
1207 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1208 //ust// free_user_marker(state,
1209 //ust// ¤t->group_leader->user_markers);
1210 //ust// current->group_leader->user_markers_sequence++;
1211 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1214 //ust// error_unlock:
1215 //ust// mutex_unlock(&markers_mutex);
1217 //ust// kfree(umark);
1224 //ust// * string : 0
1226 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1227 //ust// char __user *ubuf)
1229 //ust// long ret = -EPERM;
1233 //ust// switch (type) {
1234 //ust// case 0: /* String */
1235 //ust// ret = -ENOMEM;
1236 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1238 //ust// goto string_out;
1239 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1240 //ust// if (len < 0) {
1241 //ust// ret = -EFAULT;
1242 //ust// goto string_err;
1244 //ust// trace_mark(userspace, string, "string %s", page);
1246 //ust// free_page((unsigned long) page);
1255 //ust// static void marker_update_processes(void)
1257 //ust// struct task_struct *g, *t;
1260 //ust// * markers_mutex is taken to protect the p->user_markers read.
1262 //ust// mutex_lock(&markers_mutex);
1263 //ust// read_lock(&tasklist_lock);
1264 //ust// for_each_process(g) {
1265 //ust// WARN_ON(!thread_group_leader(g));
1266 //ust// if (hlist_empty(&g->user_markers))
1268 //ust// if (strcmp(g->comm, "testprog") == 0)
1269 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1272 //ust// /* TODO : implement this thread flag in each arch. */
1273 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1274 //ust// } while ((t = next_thread(t)) != g);
1276 //ust// read_unlock(&tasklist_lock);
1277 //ust// mutex_unlock(&markers_mutex);
1281 * Update current process.
1282 * Note that we have to wait a whole scheduler period before we are sure that
1283 * every running userspace threads have their markers updated.
1284 * (synchronize_sched() can be used to insure this).
1286 void marker_update_process(void)
1288 struct user_marker
*umark
;
1289 struct hlist_node
*pos
;
1290 struct marker_entry
*entry
;
1292 mutex_lock(&markers_mutex
);
1293 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1294 if (strcmp(current
->comm
, "testprog") == 0)
1295 printk(KERN_DEBUG
"do update pending for testprog\n");
1296 hlist_for_each_entry(umark
, pos
,
1297 ¤t
->group_leader
->user_markers
, hlist
) {
1298 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1299 umark
->name
, current
->comm
);
1300 entry
= get_marker("userspace", umark
->name
);
1302 if (entry
->format
&&
1303 strcmp(entry
->format
, umark
->format
) != 0) {
1305 " error, wrong format in process %s\n",
1309 if (put_user(!!entry
->refcount
, umark
->state
)) {
1311 "Marker in %s caused a fault\n",
1316 if (put_user(0, umark
->state
)) {
1318 "Marker in %s caused a fault\n",
1324 clear_thread_flag(TIF_MARKER_PENDING
);
1325 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1326 mutex_unlock(&markers_mutex
);
1330 * Called at process exit and upon do_execve().
1331 * We assume that when the leader exits, no more references can be done to the
1332 * leader structure by the other threads.
1334 void exit_user_markers(struct task_struct
*p
)
1336 struct user_marker
*umark
;
1337 struct hlist_node
*pos
, *n
;
1339 if (thread_group_leader(p
)) {
1340 mutex_lock(&markers_mutex
);
1341 mutex_lock(&p
->user_markers_mutex
);
1342 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1345 INIT_HLIST_HEAD(&p
->user_markers
);
1346 p
->user_markers_sequence
++;
1347 mutex_unlock(&p
->user_markers_mutex
);
1348 mutex_unlock(&markers_mutex
);
1352 int is_marker_enabled(const char *channel
, const char *name
)
1354 struct marker_entry
*entry
;
1356 mutex_lock(&markers_mutex
);
1357 entry
= get_marker(channel
, name
);
1358 mutex_unlock(&markers_mutex
);
1360 return entry
&& !!entry
->refcount
;
1364 int marker_module_notify(struct notifier_block
*self
,
1365 unsigned long val
, void *data
)
1367 struct module
*mod
= data
;
1370 case MODULE_STATE_COMING
:
1371 marker_update_probe_range(mod
->markers
,
1372 mod
->markers
+ mod
->num_markers
);
1374 case MODULE_STATE_GOING
:
1375 marker_update_probe_range(mod
->markers
,
1376 mod
->markers
+ mod
->num_markers
);
1382 struct notifier_block marker_module_nb
= {
1383 .notifier_call
= marker_module_notify
,
1387 //ust// static int init_markers(void)
1389 //ust// return register_module_notifier(&marker_module_nb);
1391 //ust// __initcall(init_markers);
1392 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1394 #endif /* CONFIG_MODULES */
1396 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1398 struct marker_iter iter
;
1399 struct ltt_probe_private_data call_data
;
1400 const char *channel
;
1402 call_data
.trace
= trace
;
1403 call_data
.serializer
= NULL
;
1405 marker_iter_reset(&iter
);
1406 marker_iter_start(&iter
);
1407 for (; iter
.marker
!= NULL
; marker_iter_next(&iter
)) {
1408 if (!_imv_read(iter
.marker
->state
))
1410 channel
= ltt_channels_get_name_from_index(
1411 iter
.marker
->channel_id
);
1412 __trace_mark(0, metadata
, core_marker_id
,
1414 "channel %s name %s event_id %hu "
1415 "int #1u%zu long #1u%zu pointer #1u%zu "
1416 "size_t #1u%zu alignment #1u%u",
1419 iter
.marker
->event_id
,
1420 sizeof(int), sizeof(long),
1421 sizeof(void *), sizeof(size_t),
1422 ltt_get_alignment());
1423 if (iter
.marker
->format
)
1424 __trace_mark(0, metadata
,
1427 "channel %s name %s format %s",
1430 iter
.marker
->format
);
1432 marker_iter_stop(&iter
);
1434 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1437 static LIST_HEAD(libs
);
1440 * Returns 0 if current not found.
1441 * Returns 1 if current found.
1443 int lib_get_iter_markers(struct marker_iter
*iter
)
1445 struct lib
*iter_lib
;
1448 //ust// mutex_lock(&module_mutex);
1449 list_for_each_entry(iter_lib
, &libs
, list
) {
1450 if (iter_lib
< iter
->lib
)
1452 else if (iter_lib
> iter
->lib
)
1453 iter
->marker
= NULL
;
1454 found
= marker_get_iter_range(&iter
->marker
,
1455 iter_lib
->markers_start
,
1456 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1458 iter
->lib
= iter_lib
;
1462 //ust// mutex_unlock(&module_mutex);
1466 void lib_update_markers(void)
1470 //ust// mutex_lock(&module_mutex);
1471 list_for_each_entry(lib
, &libs
, list
)
1472 marker_update_probe_range(lib
->markers_start
,
1473 lib
->markers_start
+ lib
->markers_count
);
1474 //ust// mutex_unlock(&module_mutex);
1477 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1479 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1484 static void new_markers(struct marker
*start
, struct marker
*end
)
1488 for(m
=start
; m
< end
; m
++) {
1494 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1498 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1500 pl
->markers_start
= markers_start
;
1501 pl
->markers_count
= markers_count
;
1503 /* FIXME: maybe protect this with its own mutex? */
1505 list_add(&pl
->list
, &libs
);
1508 new_markers(markers_start
, markers_start
+ markers_count
);
1510 /* FIXME: update just the loaded lib */
1511 lib_update_markers();
1513 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1518 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1520 /*FIXME: implement; but before implementing, marker_register_lib must
1521 have appropriate locking. */
1526 static int initialized
= 0;
1528 void __attribute__((constructor
)) init_markers(void)
1531 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1532 printf("markers_start: %p, markers_stop: %p\n", __start___markers
, __stop___markers
);