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 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/types.h>
21 #include <linux/jhash.h>
22 #include <linux/list.h>
23 #include <linux/rcupdate.h>
24 #include <linux/marker.h>
25 #include <linux/err.h>
26 #include <linux/slab.h>
28 extern struct marker __start___markers
[];
29 extern struct marker __stop___markers
[];
31 /* Set to 1 to enable marker debug output */
32 static const int marker_debug
;
35 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
36 * and module markers and the hash table.
38 static DEFINE_MUTEX(markers_mutex
);
41 * Marker hash table, containing the active markers.
42 * Protected by module_mutex.
44 #define MARKER_HASH_BITS 6
45 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
46 static struct hlist_head marker_table
[MARKER_TABLE_SIZE
];
50 * It is used to make sure every handler has finished using its private data
51 * between two consecutive operation (add or remove) on a given marker. It is
52 * also used to delay the free of multiple probes array until a quiescent state
54 * marker entries modifications are protected by the markers_mutex.
57 struct hlist_node hlist
;
60 void (*call
)(const struct marker
*mdata
, void *call_private
, ...);
61 struct marker_probe_closure single
;
62 struct marker_probe_closure
*multi
;
63 int refcount
; /* Number of times armed. 0 if disarmed. */
67 unsigned char ptype
:1;
68 unsigned char format_allocated
:1;
69 char name
[0]; /* Contains name'\0'format'\0' */
73 * __mark_empty_function - Empty probe callback
74 * @probe_private: probe private data
75 * @call_private: call site private data
77 * @...: variable argument list
79 * Empty callback provided as a probe to the markers. By providing this to a
80 * disabled marker, we make sure the execution flow is always valid even
81 * though the function pointer change and the marker enabling are two distinct
82 * operations that modifies the execution flow of preemptible code.
84 void __mark_empty_function(void *probe_private
, void *call_private
,
85 const char *fmt
, va_list *args
)
88 EXPORT_SYMBOL_GPL(__mark_empty_function
);
91 * marker_probe_cb Callback that prepares the variable argument list for probes.
92 * @mdata: pointer of type struct marker
93 * @call_private: caller site private data
94 * @...: Variable argument list.
96 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
97 * need to put a full smp_rmb() in this branch. This is why we do not use
98 * rcu_dereference() for the pointer read.
100 void marker_probe_cb(const struct marker
*mdata
, void *call_private
, ...)
106 * rcu_read_lock_sched does two things : disabling preemption to make
107 * sure the teardown of the callbacks can be done correctly when they
108 * are in modules and they insure RCU read coherency.
110 rcu_read_lock_sched();
111 ptype
= mdata
->ptype
;
112 if (likely(!ptype
)) {
113 marker_probe_func
*func
;
114 /* Must read the ptype before ptr. They are not data dependant,
115 * so we put an explicit smp_rmb() here. */
117 func
= mdata
->single
.func
;
118 /* Must read the ptr before private data. They are not data
119 * dependant, so we put an explicit smp_rmb() here. */
121 va_start(args
, call_private
);
122 func(mdata
->single
.probe_private
, call_private
, mdata
->format
,
126 struct marker_probe_closure
*multi
;
129 * Read mdata->ptype before mdata->multi.
132 multi
= mdata
->multi
;
134 * multi points to an array, therefore accessing the array
135 * depends on reading multi. However, even in this case,
136 * we must insure that the pointer is read _before_ the array
137 * data. Same as rcu_dereference, but we need a full smp_rmb()
138 * in the fast path, so put the explicit barrier here.
140 smp_read_barrier_depends();
141 for (i
= 0; multi
[i
].func
; i
++) {
142 va_start(args
, call_private
);
143 multi
[i
].func(multi
[i
].probe_private
, call_private
,
144 mdata
->format
, &args
);
148 rcu_read_unlock_sched();
150 EXPORT_SYMBOL_GPL(marker_probe_cb
);
153 * marker_probe_cb Callback that does not prepare the variable argument list.
154 * @mdata: pointer of type struct marker
155 * @call_private: caller site private data
156 * @...: Variable argument list.
158 * Should be connected to markers "MARK_NOARGS".
160 static void marker_probe_cb_noarg(const struct marker
*mdata
, void *call_private
, ...)
162 va_list args
; /* not initialized */
165 rcu_read_lock_sched();
166 ptype
= mdata
->ptype
;
167 if (likely(!ptype
)) {
168 marker_probe_func
*func
;
169 /* Must read the ptype before ptr. They are not data dependant,
170 * so we put an explicit smp_rmb() here. */
172 func
= mdata
->single
.func
;
173 /* Must read the ptr before private data. They are not data
174 * dependant, so we put an explicit smp_rmb() here. */
176 func(mdata
->single
.probe_private
, call_private
, mdata
->format
,
179 struct marker_probe_closure
*multi
;
182 * Read mdata->ptype before mdata->multi.
185 multi
= mdata
->multi
;
187 * multi points to an array, therefore accessing the array
188 * depends on reading multi. However, even in this case,
189 * we must insure that the pointer is read _before_ the array
190 * data. Same as rcu_dereference, but we need a full smp_rmb()
191 * in the fast path, so put the explicit barrier here.
193 smp_read_barrier_depends();
194 for (i
= 0; multi
[i
].func
; i
++)
195 multi
[i
].func(multi
[i
].probe_private
, call_private
,
196 mdata
->format
, &args
);
198 rcu_read_unlock_sched();
201 static void free_old_closure(struct rcu_head
*head
)
203 struct marker_entry
*entry
= container_of(head
,
204 struct marker_entry
, rcu
);
205 kfree(entry
->oldptr
);
206 /* Make sure we free the data before setting the pending flag to 0 */
208 entry
->rcu_pending
= 0;
211 static void debug_print_probes(struct marker_entry
*entry
)
219 printk(KERN_DEBUG
"Single probe : %p %p\n",
221 entry
->single
.probe_private
);
223 for (i
= 0; entry
->multi
[i
].func
; i
++)
224 printk(KERN_DEBUG
"Multi probe %d : %p %p\n", i
,
225 entry
->multi
[i
].func
,
226 entry
->multi
[i
].probe_private
);
230 static struct marker_probe_closure
*
231 marker_entry_add_probe(struct marker_entry
*entry
,
232 marker_probe_func
*probe
, void *probe_private
)
235 struct marker_probe_closure
*old
, *new;
239 debug_print_probes(entry
);
242 if (entry
->single
.func
== probe
&&
243 entry
->single
.probe_private
== probe_private
)
244 return ERR_PTR(-EBUSY
);
245 if (entry
->single
.func
== __mark_empty_function
) {
247 entry
->single
.func
= probe
;
248 entry
->single
.probe_private
= probe_private
;
251 debug_print_probes(entry
);
259 /* (N -> N+1), (N != 0, 1) probes */
260 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
261 if (old
[nr_probes
].func
== probe
262 && old
[nr_probes
].probe_private
264 return ERR_PTR(-EBUSY
);
266 /* + 2 : one for new probe, one for NULL func */
267 new = kzalloc((nr_probes
+ 2) * sizeof(struct marker_probe_closure
),
270 return ERR_PTR(-ENOMEM
);
272 new[0] = entry
->single
;
275 nr_probes
* sizeof(struct marker_probe_closure
));
276 new[nr_probes
].func
= probe
;
277 new[nr_probes
].probe_private
= probe_private
;
278 entry
->refcount
= nr_probes
+ 1;
281 debug_print_probes(entry
);
285 static struct marker_probe_closure
*
286 marker_entry_remove_probe(struct marker_entry
*entry
,
287 marker_probe_func
*probe
, void *probe_private
)
289 int nr_probes
= 0, nr_del
= 0, i
;
290 struct marker_probe_closure
*old
, *new;
294 debug_print_probes(entry
);
296 /* 0 -> N is an error */
297 WARN_ON(entry
->single
.func
== __mark_empty_function
);
299 WARN_ON(probe
&& entry
->single
.func
!= probe
);
300 WARN_ON(entry
->single
.probe_private
!= probe_private
);
301 entry
->single
.func
= __mark_empty_function
;
304 debug_print_probes(entry
);
307 /* (N -> M), (N > 1, M >= 0) probes */
308 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
309 if ((!probe
|| old
[nr_probes
].func
== probe
)
310 && old
[nr_probes
].probe_private
316 if (nr_probes
- nr_del
== 0) {
317 /* N -> 0, (N > 1) */
318 entry
->single
.func
= __mark_empty_function
;
321 } else if (nr_probes
- nr_del
== 1) {
322 /* N -> 1, (N > 1) */
323 for (i
= 0; old
[i
].func
; i
++)
324 if ((probe
&& old
[i
].func
!= probe
) ||
325 old
[i
].probe_private
!= probe_private
)
326 entry
->single
= old
[i
];
331 /* N -> M, (N > 1, M > 1) */
333 new = kzalloc((nr_probes
- nr_del
+ 1)
334 * sizeof(struct marker_probe_closure
), GFP_KERNEL
);
336 return ERR_PTR(-ENOMEM
);
337 for (i
= 0; old
[i
].func
; i
++)
338 if ((probe
&& old
[i
].func
!= probe
) ||
339 old
[i
].probe_private
!= probe_private
)
341 entry
->refcount
= nr_probes
- nr_del
;
345 debug_print_probes(entry
);
350 * Get marker if the marker is present in the marker hash table.
351 * Must be called with markers_mutex held.
352 * Returns NULL if not present.
354 static struct marker_entry
*get_marker(const char *name
)
356 struct hlist_head
*head
;
357 struct hlist_node
*node
;
358 struct marker_entry
*e
;
359 u32 hash
= jhash(name
, strlen(name
), 0);
361 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
362 hlist_for_each_entry(e
, node
, head
, hlist
) {
363 if (!strcmp(name
, e
->name
))
370 * Add the marker to the marker hash table. Must be called with markers_mutex
373 static struct marker_entry
*add_marker(const char *name
, const char *format
)
375 struct hlist_head
*head
;
376 struct hlist_node
*node
;
377 struct marker_entry
*e
;
378 size_t name_len
= strlen(name
) + 1;
379 size_t format_len
= 0;
380 u32 hash
= jhash(name
, name_len
-1, 0);
383 format_len
= strlen(format
) + 1;
384 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
385 hlist_for_each_entry(e
, node
, head
, hlist
) {
386 if (!strcmp(name
, e
->name
)) {
388 "Marker %s busy\n", name
);
389 return ERR_PTR(-EBUSY
); /* Already there */
393 * Using kmalloc here to allocate a variable length element. Could
394 * cause some memory fragmentation if overused.
396 e
= kmalloc(sizeof(struct marker_entry
) + name_len
+ format_len
,
399 return ERR_PTR(-ENOMEM
);
400 memcpy(&e
->name
[0], name
, name_len
);
402 e
->format
= &e
->name
[name_len
];
403 memcpy(e
->format
, format
, format_len
);
404 if (strcmp(e
->format
, MARK_NOARGS
) == 0)
405 e
->call
= marker_probe_cb_noarg
;
407 e
->call
= marker_probe_cb
;
408 trace_mark(core_marker_format
, "name %s format %s",
412 e
->call
= marker_probe_cb
;
414 e
->single
.func
= __mark_empty_function
;
415 e
->single
.probe_private
= NULL
;
418 e
->format_allocated
= 0;
421 hlist_add_head(&e
->hlist
, head
);
426 * Remove the marker from the marker hash table. Must be called with mutex_lock
429 static int remove_marker(const char *name
)
431 struct hlist_head
*head
;
432 struct hlist_node
*node
;
433 struct marker_entry
*e
;
435 size_t len
= strlen(name
) + 1;
436 u32 hash
= jhash(name
, len
-1, 0);
438 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
439 hlist_for_each_entry(e
, node
, head
, hlist
) {
440 if (!strcmp(name
, e
->name
)) {
447 if (e
->single
.func
!= __mark_empty_function
)
449 hlist_del(&e
->hlist
);
450 if (e
->format_allocated
)
452 /* Make sure the call_rcu has been executed */
460 * Set the mark_entry format to the format found in the element.
462 static int marker_set_format(struct marker_entry
*entry
, const char *format
)
464 entry
->format
= kstrdup(format
, GFP_KERNEL
);
467 entry
->format_allocated
= 1;
469 trace_mark(core_marker_format
, "name %s format %s",
470 entry
->name
, entry
->format
);
475 * Sets the probe callback corresponding to one marker.
477 static int set_marker(struct marker_entry
*entry
, struct marker
*elem
,
481 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
484 if (strcmp(entry
->format
, elem
->format
) != 0) {
486 "Format mismatch for probe %s "
487 "(%s), marker (%s)\n",
494 ret
= marker_set_format(entry
, elem
->format
);
500 * probe_cb setup (statically known) is done here. It is
501 * asynchronous with the rest of execution, therefore we only
502 * pass from a "safe" callback (with argument) to an "unsafe"
503 * callback (does not set arguments).
505 elem
->call
= entry
->call
;
508 * We only update the single probe private data when the ptr is
509 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
511 WARN_ON(elem
->single
.func
!= __mark_empty_function
512 && elem
->single
.probe_private
!= entry
->single
.probe_private
514 elem
->single
.probe_private
= entry
->single
.probe_private
;
516 * Make sure the private data is valid when we update the
520 elem
->single
.func
= entry
->single
.func
;
522 * We also make sure that the new probe callbacks array is consistent
523 * before setting a pointer to it.
525 rcu_assign_pointer(elem
->multi
, entry
->multi
);
527 * Update the function or multi probe array pointer before setting the
531 elem
->ptype
= entry
->ptype
;
532 elem
->state
= active
;
538 * Disable a marker and its probe callback.
539 * Note: only waiting an RCU period after setting elem->call to the empty
540 * function insures that the original callback is not used anymore. This insured
541 * by rcu_read_lock_sched around the call site.
543 static void disable_marker(struct marker
*elem
)
545 /* leave "call" as is. It is known statically. */
547 elem
->single
.func
= __mark_empty_function
;
548 /* Update the function before setting the ptype */
550 elem
->ptype
= 0; /* single probe */
552 * Leave the private data and id there, because removal is racy and
553 * should be done only after an RCU period. These are never used until
554 * the next initialization anyway.
559 * marker_update_probe_range - Update a probe range
560 * @begin: beginning of the range
561 * @end: end of the range
563 * Updates the probe callback corresponding to a range of markers.
565 void marker_update_probe_range(struct marker
*begin
,
569 struct marker_entry
*mark_entry
;
571 mutex_lock(&markers_mutex
);
572 for (iter
= begin
; iter
< end
; iter
++) {
573 mark_entry
= get_marker(iter
->name
);
575 set_marker(mark_entry
, iter
, !!mark_entry
->refcount
);
577 * ignore error, continue
580 disable_marker(iter
);
583 mutex_unlock(&markers_mutex
);
587 * Update probes, removing the faulty probes.
589 * Internal callback only changed before the first probe is connected to it.
590 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
591 * transitions. All other transitions will leave the old private data valid.
592 * This makes the non-atomicity of the callback/private data updates valid.
594 * "special case" updates :
599 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
600 * Site effect : marker_set_format may delete the marker entry (creating a
603 static void marker_update_probes(void)
605 /* Core kernel markers */
606 marker_update_probe_range(__start___markers
, __stop___markers
);
607 /* Markers in modules. */
608 module_update_markers();
612 * marker_probe_register - Connect a probe to a marker
614 * @format: format string
615 * @probe: probe handler
616 * @probe_private: probe private data
618 * private data must be a valid allocated memory address, or NULL.
619 * Returns 0 if ok, error value on error.
620 * The probe address must at least be aligned on the architecture pointer size.
622 int marker_probe_register(const char *name
, const char *format
,
623 marker_probe_func
*probe
, void *probe_private
)
625 struct marker_entry
*entry
;
627 struct marker_probe_closure
*old
;
629 mutex_lock(&markers_mutex
);
630 entry
= get_marker(name
);
632 entry
= add_marker(name
, format
);
634 ret
= PTR_ERR(entry
);
637 ret
= marker_set_format(entry
, format
);
638 else if (strcmp(entry
->format
, format
))
645 * If we detect that a call_rcu is pending for this marker,
646 * make sure it's executed now.
648 if (entry
->rcu_pending
)
650 old
= marker_entry_add_probe(entry
, probe
, probe_private
);
655 mutex_unlock(&markers_mutex
);
656 marker_update_probes(); /* may update entry */
657 mutex_lock(&markers_mutex
);
658 entry
= get_marker(name
);
660 if (entry
->rcu_pending
)
663 entry
->rcu_pending
= 1;
664 /* write rcu_pending before calling the RCU callback */
666 call_rcu_sched(&entry
->rcu
, free_old_closure
);
668 mutex_unlock(&markers_mutex
);
671 EXPORT_SYMBOL_GPL(marker_probe_register
);
674 * marker_probe_unregister - Disconnect a probe from a marker
676 * @probe: probe function pointer
677 * @probe_private: probe private data
679 * Returns the private data given to marker_probe_register, or an ERR_PTR().
680 * We do not need to call a synchronize_sched to make sure the probes have
681 * finished running before doing a module unload, because the module unload
682 * itself uses stop_machine(), which insures that every preempt disabled section
685 int marker_probe_unregister(const char *name
,
686 marker_probe_func
*probe
, void *probe_private
)
688 struct marker_entry
*entry
;
689 struct marker_probe_closure
*old
;
692 mutex_lock(&markers_mutex
);
693 entry
= get_marker(name
);
696 if (entry
->rcu_pending
)
698 old
= marker_entry_remove_probe(entry
, probe
, probe_private
);
699 mutex_unlock(&markers_mutex
);
700 marker_update_probes(); /* may update entry */
701 mutex_lock(&markers_mutex
);
702 entry
= get_marker(name
);
705 if (entry
->rcu_pending
)
708 entry
->rcu_pending
= 1;
709 /* write rcu_pending before calling the RCU callback */
711 call_rcu_sched(&entry
->rcu
, free_old_closure
);
712 remove_marker(name
); /* Ignore busy error message */
715 mutex_unlock(&markers_mutex
);
718 EXPORT_SYMBOL_GPL(marker_probe_unregister
);
720 static struct marker_entry
*
721 get_marker_from_private_data(marker_probe_func
*probe
, void *probe_private
)
723 struct marker_entry
*entry
;
725 struct hlist_head
*head
;
726 struct hlist_node
*node
;
728 for (i
= 0; i
< MARKER_TABLE_SIZE
; i
++) {
729 head
= &marker_table
[i
];
730 hlist_for_each_entry(entry
, node
, head
, hlist
) {
732 if (entry
->single
.func
== probe
733 && entry
->single
.probe_private
737 struct marker_probe_closure
*closure
;
738 closure
= entry
->multi
;
739 for (i
= 0; closure
[i
].func
; i
++) {
740 if (closure
[i
].func
== probe
&&
741 closure
[i
].probe_private
752 * marker_probe_unregister_private_data - Disconnect a probe from a marker
753 * @probe: probe function
754 * @probe_private: probe private data
756 * Unregister a probe by providing the registered private data.
757 * Only removes the first marker found in hash table.
758 * Return 0 on success or error value.
759 * We do not need to call a synchronize_sched to make sure the probes have
760 * finished running before doing a module unload, because the module unload
761 * itself uses stop_machine(), which insures that every preempt disabled section
764 int marker_probe_unregister_private_data(marker_probe_func
*probe
,
767 struct marker_entry
*entry
;
769 struct marker_probe_closure
*old
;
771 mutex_lock(&markers_mutex
);
772 entry
= get_marker_from_private_data(probe
, probe_private
);
777 if (entry
->rcu_pending
)
779 old
= marker_entry_remove_probe(entry
, NULL
, probe_private
);
780 mutex_unlock(&markers_mutex
);
781 marker_update_probes(); /* may update entry */
782 mutex_lock(&markers_mutex
);
783 entry
= get_marker_from_private_data(probe
, probe_private
);
785 if (entry
->rcu_pending
)
788 entry
->rcu_pending
= 1;
789 /* write rcu_pending before calling the RCU callback */
791 call_rcu_sched(&entry
->rcu
, free_old_closure
);
792 remove_marker(entry
->name
); /* Ignore busy error message */
794 mutex_unlock(&markers_mutex
);
797 EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data
);
800 * marker_get_private_data - Get a marker's probe private data
802 * @probe: probe to match
803 * @num: get the nth matching probe's private data
805 * Returns the nth private data pointer (starting from 0) matching, or an
807 * Returns the private data pointer, or an ERR_PTR.
808 * The private data pointer should _only_ be dereferenced if the caller is the
809 * owner of the data, or its content could vanish. This is mostly used to
810 * confirm that a caller is the owner of a registered probe.
812 void *marker_get_private_data(const char *name
, marker_probe_func
*probe
,
815 struct hlist_head
*head
;
816 struct hlist_node
*node
;
817 struct marker_entry
*e
;
818 size_t name_len
= strlen(name
) + 1;
819 u32 hash
= jhash(name
, name_len
-1, 0);
822 head
= &marker_table
[hash
& ((1 << MARKER_HASH_BITS
)-1)];
823 hlist_for_each_entry(e
, node
, head
, hlist
) {
824 if (!strcmp(name
, e
->name
)) {
826 if (num
== 0 && e
->single
.func
== probe
)
827 return e
->single
.probe_private
;
829 struct marker_probe_closure
*closure
;
832 for (i
= 0; closure
[i
].func
; i
++) {
833 if (closure
[i
].func
!= probe
)
836 return closure
[i
].probe_private
;
842 return ERR_PTR(-ENOENT
);
844 EXPORT_SYMBOL_GPL(marker_get_private_data
);