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 void lock_markers(void)
58 mutex_lock(&markers_mutex
);
61 void unlock_markers(void)
63 mutex_unlock(&markers_mutex
);
67 * Marker hash table, containing the active markers.
68 * Protected by module_mutex.
70 #define MARKER_HASH_BITS 6
71 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
72 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
76 * It is used to make sure every handler has finished using its private data
77 * between two consecutive operation (add or remove) on a given marker. It is
78 * also used to delay the free of multiple probes array until a quiescent state
80 * marker entries modifications are protected by the markers_mutex.
83 struct hlist_node hlist
;
87 void (*call
)(const struct marker
*mdata
, void *call_private
, ...);
88 struct marker_probe_closure single
;
89 struct marker_probe_closure
*multi
;
90 int refcount
; /* Number of times armed. 0 if disarmed. */
96 unsigned char ptype
:1;
97 unsigned char format_allocated
:1;
98 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
101 #ifdef CONFIG_MARKERS_USERSPACE
102 static void marker_update_processes(void);
104 static void marker_update_processes(void)
110 * __mark_empty_function - Empty probe callback
111 * @mdata: marker data
112 * @probe_private: probe private data
113 * @call_private: call site private data
114 * @fmt: format string
115 * @...: variable argument list
117 * Empty callback provided as a probe to the markers. By providing this to a
118 * disabled marker, we make sure the execution flow is always valid even
119 * though the function pointer change and the marker enabling are two distinct
120 * operations that modifies the execution flow of preemptible code.
122 notrace
void __mark_empty_function(const struct marker
*mdata
,
123 void *probe_private
, void *call_private
, const char *fmt
, va_list *args
)
126 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
129 * marker_probe_cb Callback that prepares the variable argument list for probes.
130 * @mdata: pointer of type struct marker
131 * @call_private: caller site private data
132 * @...: Variable argument list.
134 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
135 * need to put a full smp_rmb() in this branch. This is why we do not use
136 * rcu_dereference() for the pointer read.
138 notrace
void marker_probe_cb(const struct marker
*mdata
,
139 void *call_private
, ...)
145 * rcu_read_lock_sched does two things : disabling preemption to make
146 * sure the teardown of the callbacks can be done correctly when they
147 * are in modules and they insure RCU read coherency.
149 //ust// rcu_read_lock_sched_notrace();
150 ptype
= mdata
->ptype
;
151 if (likely(!ptype
)) {
152 marker_probe_func
*func
;
153 /* Must read the ptype before ptr. They are not data dependant,
154 * so we put an explicit smp_rmb() here. */
156 func
= mdata
->single
.func
;
157 /* Must read the ptr before private data. They are not data
158 * dependant, so we put an explicit smp_rmb() here. */
160 va_start(args
, call_private
);
161 func(mdata
, mdata
->single
.probe_private
, call_private
,
162 mdata
->format
, &args
);
165 struct marker_probe_closure
*multi
;
168 * Read mdata->ptype before mdata->multi.
171 multi
= mdata
->multi
;
173 * multi points to an array, therefore accessing the array
174 * depends on reading multi. However, even in this case,
175 * we must insure that the pointer is read _before_ the array
176 * data. Same as rcu_dereference, but we need a full smp_rmb()
177 * in the fast path, so put the explicit barrier here.
179 smp_read_barrier_depends();
180 for (i
= 0; multi
[i
].func
; i
++) {
181 va_start(args
, call_private
);
182 multi
[i
].func(mdata
, multi
[i
].probe_private
,
183 call_private
, mdata
->format
, &args
);
187 //ust// rcu_read_unlock_sched_notrace();
189 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
192 * marker_probe_cb Callback that does not prepare the variable argument list.
193 * @mdata: pointer of type struct marker
194 * @call_private: caller site private data
195 * @...: Variable argument list.
197 * Should be connected to markers "MARK_NOARGS".
199 static notrace
void marker_probe_cb_noarg(const struct marker
*mdata
,
200 void *call_private
, ...)
202 va_list args
; /* not initialized */
205 //ust// rcu_read_lock_sched_notrace();
206 ptype
= mdata
->ptype
;
207 if (likely(!ptype
)) {
208 marker_probe_func
*func
;
209 /* Must read the ptype before ptr. They are not data dependant,
210 * so we put an explicit smp_rmb() here. */
212 func
= mdata
->single
.func
;
213 /* Must read the ptr before private data. They are not data
214 * dependant, so we put an explicit smp_rmb() here. */
216 func(mdata
, mdata
->single
.probe_private
, call_private
,
217 mdata
->format
, &args
);
219 struct marker_probe_closure
*multi
;
222 * Read mdata->ptype before mdata->multi.
225 multi
= mdata
->multi
;
227 * multi points to an array, therefore accessing the array
228 * depends on reading multi. However, even in this case,
229 * we must insure that the pointer is read _before_ the array
230 * data. Same as rcu_dereference, but we need a full smp_rmb()
231 * in the fast path, so put the explicit barrier here.
233 smp_read_barrier_depends();
234 for (i
= 0; multi
[i
].func
; i
++)
235 multi
[i
].func(mdata
, multi
[i
].probe_private
,
236 call_private
, mdata
->format
, &args
);
238 //ust// rcu_read_unlock_sched_notrace();
241 static void free_old_closure(struct rcu_head
*head
)
243 struct marker_entry
*entry
= container_of(head
,
244 struct marker_entry
, rcu
);
245 kfree(entry
->oldptr
);
246 /* Make sure we free the data before setting the pending flag to 0 */
248 entry
->rcu_pending
= 0;
251 static void debug_print_probes(struct marker_entry
*entry
)
259 printk(KERN_DEBUG
"Single probe : %p %p\n",
261 entry
->single
.probe_private
);
263 for (i
= 0; entry
->multi
[i
].func
; i
++)
264 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
265 entry
->multi
[i
].func
,
266 entry
->multi
[i
].probe_private
);
270 static struct marker_probe_closure
*
271 marker_entry_add_probe(struct marker_entry
*entry
,
272 marker_probe_func
*probe
, void *probe_private
)
275 struct marker_probe_closure
*old
, *new;
279 debug_print_probes(entry
);
282 if (entry
->single
.func
== probe
&&
283 entry
->single
.probe_private
== probe_private
)
284 return ERR_PTR(-EBUSY
);
285 if (entry
->single
.func
== __mark_empty_function
) {
287 entry
->single
.func
= probe
;
288 entry
->single
.probe_private
= probe_private
;
291 debug_print_probes(entry
);
299 /* (N -> N+1), (N != 0, 1) probes */
300 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
301 if (old
[nr_probes
].func
== probe
302 && old
[nr_probes
].probe_private
304 return ERR_PTR(-EBUSY
);
306 /* + 2 : one for new probe, one for NULL func */
307 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
310 return ERR_PTR(-ENOMEM
);
312 new[0] = entry
->single
;
315 nr_probes
* sizeof(struct marker_probe_closure
));
316 new[nr_probes
].func
= probe
;
317 new[nr_probes
].probe_private
= probe_private
;
318 entry
->refcount
= nr_probes
+ 1;
321 debug_print_probes(entry
);
325 static struct marker_probe_closure
*
326 marker_entry_remove_probe(struct marker_entry
*entry
,
327 marker_probe_func
*probe
, void *probe_private
)
329 int nr_probes
= 0, nr_del
= 0, i
;
330 struct marker_probe_closure
*old
, *new;
334 debug_print_probes(entry
);
336 /* 0 -> N is an error */
337 WARN_ON(entry
->single
.func
== __mark_empty_function
);
339 WARN_ON(probe
&& entry
->single
.func
!= probe
);
340 WARN_ON(entry
->single
.probe_private
!= probe_private
);
341 entry
->single
.func
= __mark_empty_function
;
344 debug_print_probes(entry
);
347 /* (N -> M), (N > 1, M >= 0) probes */
348 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
349 if ((!probe
|| old
[nr_probes
].func
== probe
)
350 && old
[nr_probes
].probe_private
356 if (nr_probes
- nr_del
== 0) {
357 /* N -> 0, (N > 1) */
358 entry
->single
.func
= __mark_empty_function
;
361 } else if (nr_probes
- nr_del
== 1) {
362 /* N -> 1, (N > 1) */
363 for (i
= 0; old
[i
].func
; i
++)
364 if ((probe
&& old
[i
].func
!= probe
) ||
365 old
[i
].probe_private
!= probe_private
)
366 entry
->single
= old
[i
];
371 /* N -> M, (N > 1, M > 1) */
373 new = kzalloc((nr_probes
- nr_del
+ 1)
374 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
376 return ERR_PTR(-ENOMEM
);
377 for (i
= 0; old
[i
].func
; i
++)
378 if ((probe
&& old
[i
].func
!= probe
) ||
379 old
[i
].probe_private
!= probe_private
)
381 entry
->refcount
= nr_probes
- nr_del
;
385 debug_print_probes(entry
);
390 * Get marker if the marker is present in the marker hash table.
391 * Must be called with markers_mutex held.
392 * Returns NULL if not present.
394 static struct marker_entry
*get_marker(const char *channel
, const char *name
)
396 struct hlist_head
*head
;
397 struct hlist_node
*node
;
398 struct marker_entry
*e
;
399 size_t channel_len
= strlen(channel
) + 1;
400 size_t name_len
= strlen(name
) + 1;
403 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
404 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
405 hlist_for_each_entry(e
, node
, head
, hlist
) {
406 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
413 * Add the marker to the marker hash table. Must be called with markers_mutex
416 static struct marker_entry
*add_marker(const char *channel
, const char *name
,
419 struct hlist_head
*head
;
420 struct hlist_node
*node
;
421 struct marker_entry
*e
;
422 size_t channel_len
= strlen(channel
) + 1;
423 size_t name_len
= strlen(name
) + 1;
424 size_t format_len
= 0;
427 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
429 format_len
= strlen(format
) + 1;
430 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
431 hlist_for_each_entry(e
, node
, head
, hlist
) {
432 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
434 "Marker %s.%s busy\n", channel
, name
);
435 return ERR_PTR(-EBUSY
); /* Already there */
439 * Using kmalloc here to allocate a variable length element. Could
440 * cause some memory fragmentation if overused.
442 e
= kmalloc(sizeof(struct marker_entry
)
443 + channel_len
+ name_len
+ format_len
,
446 return ERR_PTR(-ENOMEM
);
447 memcpy(e
->channel
, channel
, channel_len
);
448 e
->name
= &e
->channel
[channel_len
];
449 memcpy(e
->name
, name
, name_len
);
451 e
->format
= &e
->name
[channel_len
+ name_len
];
452 memcpy(e
->format
, format
, format_len
);
453 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
454 e
->call
= marker_probe_cb_noarg
;
456 e
->call
= marker_probe_cb
;
457 trace_mark(metadata
, core_marker_format
,
458 "channel %s name %s format %s",
459 e
->channel
, e
->name
, e
->format
);
462 e
->call
= marker_probe_cb
;
464 e
->single
.func
= __mark_empty_function
;
465 e
->single
.probe_private
= NULL
;
468 e
->format_allocated
= 0;
471 hlist_add_head(&e
->hlist
, head
);
476 * Remove the marker from the marker hash table. Must be called with mutex_lock
479 static int remove_marker(const char *channel
, const char *name
)
481 struct hlist_head
*head
;
482 struct hlist_node
*node
;
483 struct marker_entry
*e
;
485 size_t channel_len
= strlen(channel
) + 1;
486 size_t name_len
= strlen(name
) + 1;
490 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
491 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
492 hlist_for_each_entry(e
, node
, head
, hlist
) {
493 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
500 if (e
->single
.func
!= __mark_empty_function
)
502 hlist_del(&e
->hlist
);
503 if (e
->format_allocated
)
505 ret
= ltt_channels_unregister(e
->channel
);
507 /* Make sure the call_rcu has been executed */
508 //ust// if (e->rcu_pending)
509 //ust// rcu_barrier_sched();
515 * Set the mark_entry format to the format found in the element.
517 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
519 entry
->format
= kstrdup(format
, GFP_KERNEL
);
522 entry
->format_allocated
= 1;
524 trace_mark(metadata
, core_marker_format
,
525 "channel %s name %s format %s",
526 entry
->channel
, entry
->name
, entry
->format
);
531 * Sets the probe callback corresponding to one marker.
533 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
537 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
540 if (strcmp(entry
->format
, elem
->format
) != 0) {
542 "Format mismatch for probe %s "
543 "(%s), marker (%s)\n",
550 ret
= marker_set_format(entry
, elem
->format
);
556 * probe_cb setup (statically known) is done here. It is
557 * asynchronous with the rest of execution, therefore we only
558 * pass from a "safe" callback (with argument) to an "unsafe"
559 * callback (does not set arguments).
561 elem
->call
= entry
->call
;
562 elem
->channel_id
= entry
->channel_id
;
563 elem
->event_id
= entry
->event_id
;
566 * We only update the single probe private data when the ptr is
567 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
569 WARN_ON(elem
->single
.func
!= __mark_empty_function
570 && elem
->single
.probe_private
!= entry
->single
.probe_private
572 elem
->single
.probe_private
= entry
->single
.probe_private
;
574 * Make sure the private data is valid when we update the
578 elem
->single
.func
= entry
->single
.func
;
580 * We also make sure that the new probe callbacks array is consistent
581 * before setting a pointer to it.
583 rcu_assign_pointer(elem
->multi
, entry
->multi
);
585 * Update the function or multi probe array pointer before setting the
589 elem
->ptype
= entry
->ptype
;
591 //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) {
592 //ust// WARN_ON(!elem->tp_cb);
594 //ust// * It is ok to directly call the probe registration because type
595 //ust// * checking has been done in the __trace_mark_tp() macro.
598 //ust// if (active) {
600 //ust// * try_module_get should always succeed because we hold
601 //ust// * markers_mutex to get the tp_cb address.
603 //ust// ret = try_module_get(__module_text_address(
604 //ust// (unsigned long)elem->tp_cb));
605 //ust// BUG_ON(!ret);
606 //ust// ret = tracepoint_probe_register_noupdate(
607 //ust// elem->tp_name,
608 //ust// elem->tp_cb);
610 //ust// ret = tracepoint_probe_unregister_noupdate(
611 //ust// elem->tp_name,
612 //ust// elem->tp_cb);
614 //ust// * tracepoint_probe_update_all() must be called
615 //ust// * before the module containing tp_cb is unloaded.
617 //ust// module_put(__module_text_address(
618 //ust// (unsigned long)elem->tp_cb));
621 elem
->state__imv
= active
;
627 * Disable a marker and its probe callback.
628 * Note: only waiting an RCU period after setting elem->call to the empty
629 * function insures that the original callback is not used anymore. This insured
630 * by rcu_read_lock_sched around the call site.
632 static void disable_marker(struct marker
*elem
)
636 /* leave "call" as is. It is known statically. */
637 //ust// if (elem->tp_name && _imv_read(elem->state)) {
638 //ust// WARN_ON(!elem->tp_cb);
640 //ust// * It is ok to directly call the probe registration because type
641 //ust// * checking has been done in the __trace_mark_tp() macro.
643 //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
644 //ust// elem->tp_cb);
645 //ust// WARN_ON(ret);
647 //ust// * tracepoint_probe_update_all() must be called
648 //ust// * before the module containing tp_cb is unloaded.
650 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
652 elem
->state__imv
= 0;
653 elem
->single
.func
= __mark_empty_function
;
654 /* Update the function before setting the ptype */
656 elem
->ptype
= 0; /* single probe */
658 * Leave the private data and channel_id/event_id there, because removal
659 * is racy and should be done only after an RCU period. These are never
660 * used until the next initialization anyway.
665 * marker_update_probe_range - Update a probe range
666 * @begin: beginning of the range
667 * @end: end of the range
669 * Updates the probe callback corresponding to a range of markers.
671 void marker_update_probe_range(struct marker
*begin
,
675 struct marker_entry
*mark_entry
;
677 mutex_lock(&markers_mutex
);
678 for (iter
= begin
; iter
< end
; iter
++) {
679 mark_entry
= get_marker(iter
->channel
, iter
->name
);
681 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
683 * ignore error, continue
686 /* This is added for UST. We emit a core_marker_id event
687 * for markers that are already registered to a probe
688 * upon library load. Otherwise, no core_marker_id will
689 * be generated for these markers. Is this the right thing
692 trace_mark(metadata
, core_marker_id
,
693 "channel %s name %s event_id %hu "
694 "int #1u%zu long #1u%zu pointer #1u%zu "
695 "size_t #1u%zu alignment #1u%u",
696 iter
->channel
, iter
->name
, mark_entry
->event_id
,
697 sizeof(int), sizeof(long), sizeof(void *),
698 sizeof(size_t), ltt_get_alignment());
700 disable_marker(iter
);
703 mutex_unlock(&markers_mutex
);
707 * Update probes, removing the faulty probes.
709 * Internal callback only changed before the first probe is connected to it.
710 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
711 * transitions. All other transitions will leave the old private data valid.
712 * This makes the non-atomicity of the callback/private data updates valid.
714 * "special case" updates :
719 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
720 * Site effect : marker_set_format may delete the marker entry (creating a
723 static void marker_update_probes(void)
725 /* Core kernel markers */
726 //ust// marker_update_probe_range(__start___markers, __stop___markers);
727 /* Markers in modules. */
728 //ust// module_update_markers();
729 lib_update_markers();
730 //ust// tracepoint_probe_update_all();
731 /* Update immediate values */
733 //ust// module_imv_update(); /* FIXME: need to port for libs? */
734 marker_update_processes();
738 * marker_probe_register - Connect a probe to a marker
739 * @channel: marker channel
741 * @format: format string
742 * @probe: probe handler
743 * @probe_private: probe private data
745 * private data must be a valid allocated memory address, or NULL.
746 * Returns 0 if ok, error value on error.
747 * The probe address must at least be aligned on the architecture pointer size.
749 int marker_probe_register(const char *channel
, const char *name
,
750 const char *format
, marker_probe_func
*probe
,
753 struct marker_entry
*entry
;
754 int ret
= 0, ret_err
;
755 struct marker_probe_closure
*old
;
758 mutex_lock(&markers_mutex
);
759 entry
= get_marker(channel
, name
);
762 entry
= add_marker(channel
, name
, format
);
764 ret
= PTR_ERR(entry
);
767 ret
= ltt_channels_register(channel
);
769 goto error_remove_marker
;
770 ret
= ltt_channels_get_index_from_name(channel
);
772 goto error_unregister_channel
;
773 entry
->channel_id
= ret
;
774 ret
= ltt_channels_get_event_id(channel
, name
);
776 goto error_unregister_channel
;
777 entry
->event_id
= ret
;
779 trace_mark(metadata
, core_marker_id
,
780 "channel %s name %s event_id %hu "
781 "int #1u%zu long #1u%zu pointer #1u%zu "
782 "size_t #1u%zu alignment #1u%u",
783 channel
, name
, entry
->event_id
,
784 sizeof(int), sizeof(long), sizeof(void *),
785 sizeof(size_t), ltt_get_alignment());
788 ret
= marker_set_format(entry
, format
);
789 else if (strcmp(entry
->format
, format
))
796 * If we detect that a call_rcu is pending for this marker,
797 * make sure it's executed now.
799 //ust// if (entry->rcu_pending)
800 //ust// rcu_barrier_sched();
801 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
805 goto error_unregister_channel
;
809 mutex_unlock(&markers_mutex
);
811 marker_update_probes();
813 mutex_lock(&markers_mutex
);
814 entry
= get_marker(channel
, name
);
817 //ust// if (entry->rcu_pending)
818 //ust// rcu_barrier_sched();
820 entry
->rcu_pending
= 1;
821 /* write rcu_pending before calling the RCU callback */
823 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
824 synchronize_rcu(); free_old_closure(&entry
->rcu
);
827 error_unregister_channel
:
828 ret_err
= ltt_channels_unregister(channel
);
831 ret_err
= remove_marker(channel
, name
);
834 mutex_unlock(&markers_mutex
);
837 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
840 * marker_probe_unregister - Disconnect a probe from a marker
841 * @channel: marker channel
843 * @probe: probe function pointer
844 * @probe_private: probe private data
846 * Returns the private data given to marker_probe_register, or an ERR_PTR().
847 * We do not need to call a synchronize_sched to make sure the probes have
848 * finished running before doing a module unload, because the module unload
849 * itself uses stop_machine(), which insures that every preempt disabled section
852 int marker_probe_unregister(const char *channel
, const char *name
,
853 marker_probe_func
*probe
, void *probe_private
)
855 struct marker_entry
*entry
;
856 struct marker_probe_closure
*old
;
859 mutex_lock(&markers_mutex
);
860 entry
= get_marker(channel
, name
);
863 //ust// if (entry->rcu_pending)
864 //ust// rcu_barrier_sched();
865 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
866 mutex_unlock(&markers_mutex
);
868 marker_update_probes();
870 mutex_lock(&markers_mutex
);
871 entry
= get_marker(channel
, name
);
874 //ust// if (entry->rcu_pending)
875 //ust// rcu_barrier_sched();
877 entry
->rcu_pending
= 1;
878 /* write rcu_pending before calling the RCU callback */
880 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
881 synchronize_rcu(); free_old_closure(&entry
->rcu
);
882 remove_marker(channel
, name
); /* Ignore busy error message */
885 mutex_unlock(&markers_mutex
);
888 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
890 static struct marker_entry
*
891 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
893 struct marker_entry
*entry
;
895 struct hlist_head
*head
;
896 struct hlist_node
*node
;
898 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
899 head
= &marker_table
[i
];
900 hlist_for_each_entry(entry
, node
, head
, hlist
) {
902 if (entry
->single
.func
== probe
903 && entry
->single
.probe_private
907 struct marker_probe_closure
*closure
;
908 closure
= entry
->multi
;
909 for (i
= 0; closure
[i
].func
; i
++) {
910 if (closure
[i
].func
== probe
&&
911 closure
[i
].probe_private
922 * marker_probe_unregister_private_data - Disconnect a probe from a marker
923 * @probe: probe function
924 * @probe_private: probe private data
926 * Unregister a probe by providing the registered private data.
927 * Only removes the first marker found in hash table.
928 * Return 0 on success or error value.
929 * We do not need to call a synchronize_sched to make sure the probes have
930 * finished running before doing a module unload, because the module unload
931 * itself uses stop_machine(), which insures that every preempt disabled section
934 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
937 struct marker_entry
*entry
;
939 struct marker_probe_closure
*old
;
940 const char *channel
= NULL
, *name
= NULL
;
942 mutex_lock(&markers_mutex
);
943 entry
= get_marker_from_private_data(probe
, probe_private
);
948 //ust// if (entry->rcu_pending)
949 //ust// rcu_barrier_sched();
950 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
951 channel
= kstrdup(entry
->channel
, GFP_KERNEL
);
952 name
= kstrdup(entry
->name
, GFP_KERNEL
);
953 mutex_unlock(&markers_mutex
);
955 marker_update_probes();
957 mutex_lock(&markers_mutex
);
958 entry
= get_marker(channel
, name
);
961 //ust// if (entry->rcu_pending)
962 //ust// rcu_barrier_sched();
964 entry
->rcu_pending
= 1;
965 /* write rcu_pending before calling the RCU callback */
967 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
968 synchronize_rcu(); free_old_closure(&entry
->rcu
);
969 /* Ignore busy error message */
970 remove_marker(channel
, name
);
972 mutex_unlock(&markers_mutex
);
977 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
980 * marker_get_private_data - Get a marker's probe private data
981 * @channel: marker channel
983 * @probe: probe to match
984 * @num: get the nth matching probe's private data
986 * Returns the nth private data pointer (starting from 0) matching, or an
988 * Returns the private data pointer, or an ERR_PTR.
989 * The private data pointer should _only_ be dereferenced if the caller is the
990 * owner of the data, or its content could vanish. This is mostly used to
991 * confirm that a caller is the owner of a registered probe.
993 void *marker_get_private_data(const char *channel
, const char *name
,
994 marker_probe_func
*probe
, int num
)
996 struct hlist_head
*head
;
997 struct hlist_node
*node
;
998 struct marker_entry
*e
;
999 size_t channel_len
= strlen(channel
) + 1;
1000 size_t name_len
= strlen(name
) + 1;
1004 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
1005 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
1006 hlist_for_each_entry(e
, node
, head
, hlist
) {
1007 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
1009 if (num
== 0 && e
->single
.func
== probe
)
1010 return e
->single
.probe_private
;
1012 struct marker_probe_closure
*closure
;
1015 for (i
= 0; closure
[i
].func
; i
++) {
1016 if (closure
[i
].func
!= probe
)
1019 return closure
[i
].probe_private
;
1025 return ERR_PTR(-ENOENT
);
1027 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1030 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1032 * Called when no channel users are active by the channel infrastructure.
1033 * Called with lock_markers() and channel mutex held.
1035 //ust// void markers_compact_event_ids(void)
1037 //ust// struct marker_entry *entry;
1038 //ust// unsigned int i;
1039 //ust// struct hlist_head *head;
1040 //ust// struct hlist_node *node;
1043 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1044 //ust// head = &marker_table[i];
1045 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1046 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1047 //ust// WARN_ON(ret < 0);
1048 //ust// entry->channel_id = ret;
1049 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1050 //ust// entry->name);
1051 //ust// WARN_ON(ret < 0);
1052 //ust// entry->event_id = ret;
1057 //ust//#ifdef CONFIG_MODULES
1060 * marker_get_iter_range - Get a next marker iterator given a range.
1061 * @marker: current markers (in), next marker (out)
1062 * @begin: beginning of the range
1063 * @end: end of the range
1065 * Returns whether a next marker has been found (1) or not (0).
1066 * Will return the first marker in the range if the input marker is NULL.
1068 int marker_get_iter_range(struct marker
**marker
, struct marker
*begin
,
1071 if (!*marker
&& begin
!= end
) {
1075 if (*marker
>= begin
&& *marker
< end
)
1079 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1081 static void marker_get_iter(struct marker_iter
*iter
)
1085 /* Core kernel markers */
1087 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1088 found
= marker_get_iter_range(&iter
->marker
,
1089 __start___markers
, __stop___markers
);
1093 /* Markers in modules. */
1094 found
= lib_get_iter_markers(iter
);
1097 marker_iter_reset(iter
);
1100 void marker_iter_start(struct marker_iter
*iter
)
1102 marker_get_iter(iter
);
1104 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1106 void marker_iter_next(struct marker_iter
*iter
)
1110 * iter->marker may be invalid because we blindly incremented it.
1111 * Make sure it is valid by marshalling on the markers, getting the
1112 * markers from following modules if necessary.
1114 marker_get_iter(iter
);
1116 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1118 void marker_iter_stop(struct marker_iter
*iter
)
1121 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1123 void marker_iter_reset(struct marker_iter
*iter
)
1126 iter
->marker
= NULL
;
1128 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1130 #ifdef CONFIG_MARKERS_USERSPACE
1132 * must be called with current->user_markers_mutex held
1134 static void free_user_marker(char __user
*state
, struct hlist_head
*head
)
1136 struct user_marker
*umark
;
1137 struct hlist_node
*pos
, *n
;
1139 hlist_for_each_entry_safe(umark
, pos
, n
, head
, hlist
) {
1140 if (umark
->state
== state
) {
1141 hlist_del(&umark
->hlist
);
1147 //ust// asmlinkage long sys_marker(char __user *name, char __user *format,
1148 //ust// char __user *state, int reg)
1150 //ust// struct user_marker *umark;
1152 //ust// struct marker_entry *entry;
1153 //ust// int ret = 0;
1155 //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n",
1156 //ust// current->comm, reg ? "registers" : "unregisters",
1157 //ust// name, state);
1159 //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL);
1160 //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0';
1161 //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0';
1162 //ust// umark->state = state;
1163 //ust// len = strncpy_from_user(umark->name, name,
1164 //ust// MAX_USER_MARKER_NAME_LEN - 1);
1165 //ust// if (len < 0) {
1166 //ust// ret = -EFAULT;
1169 //ust// len = strncpy_from_user(umark->format, format,
1170 //ust// MAX_USER_MARKER_FORMAT_LEN - 1);
1171 //ust// if (len < 0) {
1172 //ust// ret = -EFAULT;
1175 //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name,
1176 //ust// umark->format);
1177 //ust// mutex_lock(&markers_mutex);
1178 //ust// entry = get_marker("userspace", umark->name);
1179 //ust// if (entry) {
1180 //ust// if (entry->format &&
1181 //ust// strcmp(entry->format, umark->format) != 0) {
1182 //ust// printk(" error, wrong format in process %s",
1183 //ust// current->comm);
1184 //ust// ret = -EPERM;
1185 //ust// goto error_unlock;
1187 //ust// printk(" %s", !!entry->refcount
1188 //ust// ? "enabled" : "disabled");
1189 //ust// if (put_user(!!entry->refcount, state)) {
1190 //ust// ret = -EFAULT;
1191 //ust// goto error_unlock;
1193 //ust// printk("\n");
1195 //ust// printk(" disabled\n");
1196 //ust// if (put_user(0, umark->state)) {
1197 //ust// printk(KERN_WARNING
1198 //ust// "Marker in %s caused a fault\n",
1199 //ust// current->comm);
1200 //ust// goto error_unlock;
1203 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1204 //ust// hlist_add_head(&umark->hlist,
1205 //ust// ¤t->group_leader->user_markers);
1206 //ust// current->group_leader->user_markers_sequence++;
1207 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1208 //ust// mutex_unlock(&markers_mutex);
1210 //ust// mutex_lock(¤t->group_leader->user_markers_mutex);
1211 //ust// free_user_marker(state,
1212 //ust// ¤t->group_leader->user_markers);
1213 //ust// current->group_leader->user_markers_sequence++;
1214 //ust// mutex_unlock(¤t->group_leader->user_markers_mutex);
1217 //ust// error_unlock:
1218 //ust// mutex_unlock(&markers_mutex);
1220 //ust// kfree(umark);
1227 //ust// * string : 0
1229 //ust// asmlinkage long sys_trace(int type, uint16_t id,
1230 //ust// char __user *ubuf)
1232 //ust// long ret = -EPERM;
1236 //ust// switch (type) {
1237 //ust// case 0: /* String */
1238 //ust// ret = -ENOMEM;
1239 //ust// page = (char *)__get_free_page(GFP_TEMPORARY);
1241 //ust// goto string_out;
1242 //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE);
1243 //ust// if (len < 0) {
1244 //ust// ret = -EFAULT;
1245 //ust// goto string_err;
1247 //ust// trace_mark(userspace, string, "string %s", page);
1249 //ust// free_page((unsigned long) page);
1258 //ust// static void marker_update_processes(void)
1260 //ust// struct task_struct *g, *t;
1263 //ust// * markers_mutex is taken to protect the p->user_markers read.
1265 //ust// mutex_lock(&markers_mutex);
1266 //ust// read_lock(&tasklist_lock);
1267 //ust// for_each_process(g) {
1268 //ust// WARN_ON(!thread_group_leader(g));
1269 //ust// if (hlist_empty(&g->user_markers))
1271 //ust// if (strcmp(g->comm, "testprog") == 0)
1272 //ust// printk(KERN_DEBUG "set update pending for testprog\n");
1275 //ust// /* TODO : implement this thread flag in each arch. */
1276 //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING);
1277 //ust// } while ((t = next_thread(t)) != g);
1279 //ust// read_unlock(&tasklist_lock);
1280 //ust// mutex_unlock(&markers_mutex);
1284 * Update current process.
1285 * Note that we have to wait a whole scheduler period before we are sure that
1286 * every running userspace threads have their markers updated.
1287 * (synchronize_sched() can be used to insure this).
1289 void marker_update_process(void)
1291 struct user_marker
*umark
;
1292 struct hlist_node
*pos
;
1293 struct marker_entry
*entry
;
1295 mutex_lock(&markers_mutex
);
1296 mutex_lock(¤t
->group_leader
->user_markers_mutex
);
1297 if (strcmp(current
->comm
, "testprog") == 0)
1298 printk(KERN_DEBUG
"do update pending for testprog\n");
1299 hlist_for_each_entry(umark
, pos
,
1300 ¤t
->group_leader
->user_markers
, hlist
) {
1301 printk(KERN_DEBUG
"Updating marker %s in %s\n",
1302 umark
->name
, current
->comm
);
1303 entry
= get_marker("userspace", umark
->name
);
1305 if (entry
->format
&&
1306 strcmp(entry
->format
, umark
->format
) != 0) {
1308 " error, wrong format in process %s\n",
1312 if (put_user(!!entry
->refcount
, umark
->state
)) {
1314 "Marker in %s caused a fault\n",
1319 if (put_user(0, umark
->state
)) {
1321 "Marker in %s caused a fault\n",
1327 clear_thread_flag(TIF_MARKER_PENDING
);
1328 mutex_unlock(¤t
->group_leader
->user_markers_mutex
);
1329 mutex_unlock(&markers_mutex
);
1333 * Called at process exit and upon do_execve().
1334 * We assume that when the leader exits, no more references can be done to the
1335 * leader structure by the other threads.
1337 void exit_user_markers(struct task_struct
*p
)
1339 struct user_marker
*umark
;
1340 struct hlist_node
*pos
, *n
;
1342 if (thread_group_leader(p
)) {
1343 mutex_lock(&markers_mutex
);
1344 mutex_lock(&p
->user_markers_mutex
);
1345 hlist_for_each_entry_safe(umark
, pos
, n
, &p
->user_markers
,
1348 INIT_HLIST_HEAD(&p
->user_markers
);
1349 p
->user_markers_sequence
++;
1350 mutex_unlock(&p
->user_markers_mutex
);
1351 mutex_unlock(&markers_mutex
);
1355 int is_marker_enabled(const char *channel
, const char *name
)
1357 struct marker_entry
*entry
;
1359 mutex_lock(&markers_mutex
);
1360 entry
= get_marker(channel
, name
);
1361 mutex_unlock(&markers_mutex
);
1363 return entry
&& !!entry
->refcount
;
1367 int marker_module_notify(struct notifier_block
*self
,
1368 unsigned long val
, void *data
)
1370 struct module
*mod
= data
;
1373 case MODULE_STATE_COMING
:
1374 marker_update_probe_range(mod
->markers
,
1375 mod
->markers
+ mod
->num_markers
);
1377 case MODULE_STATE_GOING
:
1378 marker_update_probe_range(mod
->markers
,
1379 mod
->markers
+ mod
->num_markers
);
1385 struct notifier_block marker_module_nb
= {
1386 .notifier_call
= marker_module_notify
,
1390 //ust// static int init_markers(void)
1392 //ust// return register_module_notifier(&marker_module_nb);
1394 //ust// __initcall(init_markers);
1395 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1397 #endif /* CONFIG_MODULES */
1399 void ltt_dump_marker_state(struct ltt_trace_struct
*trace
)
1401 struct marker_entry
*entry
;
1402 struct ltt_probe_private_data call_data
;
1403 struct hlist_head
*head
;
1404 struct hlist_node
*node
;
1407 mutex_lock(&markers_mutex
);
1408 call_data
.trace
= trace
;
1409 call_data
.serializer
= NULL
;
1411 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
1412 head
= &marker_table
[i
];
1413 hlist_for_each_entry(entry
, node
, head
, hlist
) {
1414 __trace_mark(0, metadata
, core_marker_id
,
1416 "channel %s name %s event_id %hu "
1417 "int #1u%zu long #1u%zu pointer #1u%zu "
1418 "size_t #1u%zu alignment #1u%u",
1422 sizeof(int), sizeof(long),
1423 sizeof(void *), sizeof(size_t),
1424 ltt_get_alignment());
1426 __trace_mark(0, metadata
,
1429 "channel %s name %s format %s",
1435 mutex_unlock(&markers_mutex
);
1437 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1440 static LIST_HEAD(libs
);
1443 * Returns 0 if current not found.
1444 * Returns 1 if current found.
1446 int lib_get_iter_markers(struct marker_iter
*iter
)
1448 struct lib
*iter_lib
;
1451 //ust// mutex_lock(&module_mutex);
1452 list_for_each_entry(iter_lib
, &libs
, list
) {
1453 if (iter_lib
< iter
->lib
)
1455 else if (iter_lib
> iter
->lib
)
1456 iter
->marker
= NULL
;
1457 found
= marker_get_iter_range(&iter
->marker
,
1458 iter_lib
->markers_start
,
1459 iter_lib
->markers_start
+ iter_lib
->markers_count
);
1461 iter
->lib
= iter_lib
;
1465 //ust// mutex_unlock(&module_mutex);
1469 void lib_update_markers(void)
1473 //ust// mutex_lock(&module_mutex);
1474 list_for_each_entry(lib
, &libs
, list
)
1475 marker_update_probe_range(lib
->markers_start
,
1476 lib
->markers_start
+ lib
->markers_count
);
1477 //ust// mutex_unlock(&module_mutex);
1480 static void (*new_marker_cb
)(struct marker
*) = NULL
;
1482 void marker_set_new_marker_cb(void (*cb
)(struct marker
*))
1487 static void new_markers(struct marker
*start
, struct marker
*end
)
1491 for(m
=start
; m
< end
; m
++) {
1497 int marker_register_lib(struct marker
*markers_start
, int markers_count
)
1501 pl
= (struct lib
*) malloc(sizeof(struct lib
));
1503 pl
->markers_start
= markers_start
;
1504 pl
->markers_count
= markers_count
;
1506 /* FIXME: maybe protect this with its own mutex? */
1508 list_add(&pl
->list
, &libs
);
1511 new_markers(markers_start
, markers_start
+ markers_count
);
1513 /* FIXME: update just the loaded lib */
1514 lib_update_markers();
1516 DBG("just registered a markers section from %p and having %d markers", markers_start
, markers_count
);
1521 int marker_unregister_lib(struct marker
*markers_start
, int markers_count
)
1523 /*FIXME: implement; but before implementing, marker_register_lib must
1524 have appropriate locking. */
1529 static int initialized
= 0;
1531 void __attribute__((constructor
)) init_markers(void)
1534 marker_register_lib(__start___markers
, (((long)__stop___markers
)-((long)__start___markers
))/sizeof(struct marker
));
1535 printf("markers_start: %p, markers_stop: %p\n", __start___markers
, __stop___markers
);