Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / kernel / marker.c
blob775f329441fbc3a832fc07ff10f148d1476efc95
1 /*
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>
27 extern struct marker __start___markers[];
28 extern struct marker __stop___markers[];
30 /* Set to 1 to enable marker debug output */
31 const int marker_debug;
34 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
35 * and module markers and the hash table.
37 static DEFINE_MUTEX(markers_mutex);
40 * Marker hash table, containing the active markers.
41 * Protected by module_mutex.
43 #define MARKER_HASH_BITS 6
44 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
47 * Note about RCU :
48 * It is used to make sure every handler has finished using its private data
49 * between two consecutive operation (add or remove) on a given marker. It is
50 * also used to delay the free of multiple probes array until a quiescent state
51 * is reached.
52 * marker entries modifications are protected by the markers_mutex.
54 struct marker_entry {
55 struct hlist_node hlist;
56 char *format;
57 void (*call)(const struct marker *mdata, /* Probe wrapper */
58 void *call_private, const char *fmt, ...);
59 struct marker_probe_closure single;
60 struct marker_probe_closure *multi;
61 int refcount; /* Number of times armed. 0 if disarmed. */
62 struct rcu_head rcu;
63 void *oldptr;
64 <<<<<<< HEAD:kernel/marker.c
65 char rcu_pending:1;
66 char ptype:1;
67 =======
68 unsigned char rcu_pending:1;
69 unsigned char ptype:1;
70 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/marker.c
71 char name[0]; /* Contains name'\0'format'\0' */
74 static struct hlist_head marker_table[MARKER_TABLE_SIZE];
76 /**
77 * __mark_empty_function - Empty probe callback
78 * @probe_private: probe private data
79 * @call_private: call site private data
80 * @fmt: format string
81 * @...: variable argument list
83 * Empty callback provided as a probe to the markers. By providing this to a
84 * disabled marker, we make sure the execution flow is always valid even
85 * though the function pointer change and the marker enabling are two distinct
86 * operations that modifies the execution flow of preemptible code.
88 void __mark_empty_function(void *probe_private, void *call_private,
89 const char *fmt, va_list *args)
92 EXPORT_SYMBOL_GPL(__mark_empty_function);
95 * marker_probe_cb Callback that prepares the variable argument list for probes.
96 * @mdata: pointer of type struct marker
97 * @call_private: caller site private data
98 * @fmt: format string
99 * @...: Variable argument list.
101 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
102 * need to put a full smp_rmb() in this branch. This is why we do not use
103 * rcu_dereference() for the pointer read.
105 void marker_probe_cb(const struct marker *mdata, void *call_private,
106 const char *fmt, ...)
108 va_list args;
109 char ptype;
112 * disabling preemption to make sure the teardown of the callbacks can
113 * be done correctly when they are in modules and they insure RCU read
114 * coherency.
116 preempt_disable();
117 ptype = ACCESS_ONCE(mdata->ptype);
118 if (likely(!ptype)) {
119 marker_probe_func *func;
120 /* Must read the ptype before ptr. They are not data dependant,
121 * so we put an explicit smp_rmb() here. */
122 smp_rmb();
123 func = ACCESS_ONCE(mdata->single.func);
124 /* Must read the ptr before private data. They are not data
125 * dependant, so we put an explicit smp_rmb() here. */
126 smp_rmb();
127 va_start(args, fmt);
128 func(mdata->single.probe_private, call_private, fmt, &args);
129 va_end(args);
130 } else {
131 struct marker_probe_closure *multi;
132 int i;
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 multi = ACCESS_ONCE(mdata->multi);
142 for (i = 0; multi[i].func; i++) {
143 va_start(args, fmt);
144 multi[i].func(multi[i].probe_private, call_private, fmt,
145 &args);
146 va_end(args);
149 preempt_enable();
151 EXPORT_SYMBOL_GPL(marker_probe_cb);
154 * marker_probe_cb Callback that does not prepare the variable argument list.
155 * @mdata: pointer of type struct marker
156 * @call_private: caller site private data
157 * @fmt: format string
158 * @...: Variable argument list.
160 * Should be connected to markers "MARK_NOARGS".
162 void marker_probe_cb_noarg(const struct marker *mdata,
163 void *call_private, const char *fmt, ...)
165 va_list args; /* not initialized */
166 char ptype;
168 preempt_disable();
169 ptype = ACCESS_ONCE(mdata->ptype);
170 if (likely(!ptype)) {
171 marker_probe_func *func;
172 /* Must read the ptype before ptr. They are not data dependant,
173 * so we put an explicit smp_rmb() here. */
174 smp_rmb();
175 func = ACCESS_ONCE(mdata->single.func);
176 /* Must read the ptr before private data. They are not data
177 * dependant, so we put an explicit smp_rmb() here. */
178 smp_rmb();
179 func(mdata->single.probe_private, call_private, fmt, &args);
180 } else {
181 struct marker_probe_closure *multi;
182 int i;
184 * multi points to an array, therefore accessing the array
185 * depends on reading multi. However, even in this case,
186 * we must insure that the pointer is read _before_ the array
187 * data. Same as rcu_dereference, but we need a full smp_rmb()
188 * in the fast path, so put the explicit barrier here.
190 smp_read_barrier_depends();
191 multi = ACCESS_ONCE(mdata->multi);
192 for (i = 0; multi[i].func; i++)
193 multi[i].func(multi[i].probe_private, call_private, fmt,
194 &args);
196 preempt_enable();
198 EXPORT_SYMBOL_GPL(marker_probe_cb_noarg);
200 static void free_old_closure(struct rcu_head *head)
202 struct marker_entry *entry = container_of(head,
203 struct marker_entry, rcu);
204 kfree(entry->oldptr);
205 /* Make sure we free the data before setting the pending flag to 0 */
206 smp_wmb();
207 entry->rcu_pending = 0;
210 static void debug_print_probes(struct marker_entry *entry)
212 int i;
214 if (!marker_debug)
215 return;
217 if (!entry->ptype) {
218 printk(KERN_DEBUG "Single probe : %p %p\n",
219 entry->single.func,
220 entry->single.probe_private);
221 } else {
222 for (i = 0; entry->multi[i].func; i++)
223 printk(KERN_DEBUG "Multi probe %d : %p %p\n", i,
224 entry->multi[i].func,
225 entry->multi[i].probe_private);
229 static struct marker_probe_closure *
230 marker_entry_add_probe(struct marker_entry *entry,
231 marker_probe_func *probe, void *probe_private)
233 int nr_probes = 0;
234 struct marker_probe_closure *old, *new;
236 WARN_ON(!probe);
238 debug_print_probes(entry);
239 old = entry->multi;
240 if (!entry->ptype) {
241 if (entry->single.func == probe &&
242 entry->single.probe_private == probe_private)
243 return ERR_PTR(-EBUSY);
244 if (entry->single.func == __mark_empty_function) {
245 /* 0 -> 1 probes */
246 entry->single.func = probe;
247 entry->single.probe_private = probe_private;
248 entry->refcount = 1;
249 entry->ptype = 0;
250 debug_print_probes(entry);
251 return NULL;
252 } else {
253 /* 1 -> 2 probes */
254 nr_probes = 1;
255 old = NULL;
257 } else {
258 /* (N -> N+1), (N != 0, 1) probes */
259 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
260 if (old[nr_probes].func == probe
261 && old[nr_probes].probe_private
262 == probe_private)
263 return ERR_PTR(-EBUSY);
265 /* + 2 : one for new probe, one for NULL func */
266 new = kzalloc((nr_probes + 2) * sizeof(struct marker_probe_closure),
267 GFP_KERNEL);
268 if (new == NULL)
269 return ERR_PTR(-ENOMEM);
270 if (!old)
271 new[0] = entry->single;
272 else
273 memcpy(new, old,
274 nr_probes * sizeof(struct marker_probe_closure));
275 new[nr_probes].func = probe;
276 new[nr_probes].probe_private = probe_private;
277 entry->refcount = nr_probes + 1;
278 entry->multi = new;
279 entry->ptype = 1;
280 debug_print_probes(entry);
281 return old;
284 static struct marker_probe_closure *
285 marker_entry_remove_probe(struct marker_entry *entry,
286 marker_probe_func *probe, void *probe_private)
288 int nr_probes = 0, nr_del = 0, i;
289 struct marker_probe_closure *old, *new;
291 old = entry->multi;
293 debug_print_probes(entry);
294 if (!entry->ptype) {
295 /* 0 -> N is an error */
296 WARN_ON(entry->single.func == __mark_empty_function);
297 /* 1 -> 0 probes */
298 WARN_ON(probe && entry->single.func != probe);
299 WARN_ON(entry->single.probe_private != probe_private);
300 entry->single.func = __mark_empty_function;
301 entry->refcount = 0;
302 entry->ptype = 0;
303 debug_print_probes(entry);
304 return NULL;
305 } else {
306 /* (N -> M), (N > 1, M >= 0) probes */
307 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
308 if ((!probe || old[nr_probes].func == probe)
309 && old[nr_probes].probe_private
310 == probe_private)
311 nr_del++;
315 if (nr_probes - nr_del == 0) {
316 /* N -> 0, (N > 1) */
317 entry->single.func = __mark_empty_function;
318 entry->refcount = 0;
319 entry->ptype = 0;
320 } else if (nr_probes - nr_del == 1) {
321 /* N -> 1, (N > 1) */
322 for (i = 0; old[i].func; i++)
323 if ((probe && old[i].func != probe) ||
324 old[i].probe_private != probe_private)
325 entry->single = old[i];
326 entry->refcount = 1;
327 entry->ptype = 0;
328 } else {
329 int j = 0;
330 /* N -> M, (N > 1, M > 1) */
331 /* + 1 for NULL */
332 new = kzalloc((nr_probes - nr_del + 1)
333 * sizeof(struct marker_probe_closure), GFP_KERNEL);
334 if (new == NULL)
335 return ERR_PTR(-ENOMEM);
336 for (i = 0; old[i].func; i++)
337 if ((probe && old[i].func != probe) ||
338 old[i].probe_private != probe_private)
339 new[j++] = old[i];
340 entry->refcount = nr_probes - nr_del;
341 entry->ptype = 1;
342 entry->multi = new;
344 debug_print_probes(entry);
345 return old;
349 * Get marker if the marker is present in the marker hash table.
350 * Must be called with markers_mutex held.
351 * Returns NULL if not present.
353 static struct marker_entry *get_marker(const char *name)
355 struct hlist_head *head;
356 struct hlist_node *node;
357 struct marker_entry *e;
358 u32 hash = jhash(name, strlen(name), 0);
360 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
361 hlist_for_each_entry(e, node, head, hlist) {
362 if (!strcmp(name, e->name))
363 return e;
365 return NULL;
369 * Add the marker to the marker hash table. Must be called with markers_mutex
370 * held.
372 static struct marker_entry *add_marker(const char *name, const char *format)
374 struct hlist_head *head;
375 struct hlist_node *node;
376 struct marker_entry *e;
377 size_t name_len = strlen(name) + 1;
378 size_t format_len = 0;
379 u32 hash = jhash(name, name_len-1, 0);
381 if (format)
382 format_len = strlen(format) + 1;
383 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
384 hlist_for_each_entry(e, node, head, hlist) {
385 if (!strcmp(name, e->name)) {
386 printk(KERN_NOTICE
387 "Marker %s busy\n", name);
388 return ERR_PTR(-EBUSY); /* Already there */
392 * Using kmalloc here to allocate a variable length element. Could
393 * cause some memory fragmentation if overused.
395 e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
396 GFP_KERNEL);
397 if (!e)
398 return ERR_PTR(-ENOMEM);
399 memcpy(&e->name[0], name, name_len);
400 if (format) {
401 e->format = &e->name[name_len];
402 memcpy(e->format, format, format_len);
403 if (strcmp(e->format, MARK_NOARGS) == 0)
404 e->call = marker_probe_cb_noarg;
405 else
406 e->call = marker_probe_cb;
407 trace_mark(core_marker_format, "name %s format %s",
408 e->name, e->format);
409 } else {
410 e->format = NULL;
411 e->call = marker_probe_cb;
413 e->single.func = __mark_empty_function;
414 e->single.probe_private = NULL;
415 e->multi = NULL;
416 e->ptype = 0;
417 e->refcount = 0;
418 e->rcu_pending = 0;
419 hlist_add_head(&e->hlist, head);
420 return e;
424 * Remove the marker from the marker hash table. Must be called with mutex_lock
425 * held.
427 static int remove_marker(const char *name)
429 struct hlist_head *head;
430 struct hlist_node *node;
431 struct marker_entry *e;
432 int found = 0;
433 size_t len = strlen(name) + 1;
434 u32 hash = jhash(name, len-1, 0);
436 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
437 hlist_for_each_entry(e, node, head, hlist) {
438 if (!strcmp(name, e->name)) {
439 found = 1;
440 break;
443 if (!found)
444 return -ENOENT;
445 if (e->single.func != __mark_empty_function)
446 return -EBUSY;
447 hlist_del(&e->hlist);
448 /* Make sure the call_rcu has been executed */
449 if (e->rcu_pending)
450 rcu_barrier();
451 kfree(e);
452 return 0;
456 * Set the mark_entry format to the format found in the element.
458 static int marker_set_format(struct marker_entry **entry, const char *format)
460 struct marker_entry *e;
461 size_t name_len = strlen((*entry)->name) + 1;
462 size_t format_len = strlen(format) + 1;
465 e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
466 GFP_KERNEL);
467 if (!e)
468 return -ENOMEM;
469 memcpy(&e->name[0], (*entry)->name, name_len);
470 e->format = &e->name[name_len];
471 memcpy(e->format, format, format_len);
472 if (strcmp(e->format, MARK_NOARGS) == 0)
473 e->call = marker_probe_cb_noarg;
474 else
475 e->call = marker_probe_cb;
476 e->single = (*entry)->single;
477 e->multi = (*entry)->multi;
478 e->ptype = (*entry)->ptype;
479 e->refcount = (*entry)->refcount;
480 e->rcu_pending = 0;
481 hlist_add_before(&e->hlist, &(*entry)->hlist);
482 hlist_del(&(*entry)->hlist);
483 /* Make sure the call_rcu has been executed */
484 if ((*entry)->rcu_pending)
485 rcu_barrier();
486 kfree(*entry);
487 *entry = e;
488 trace_mark(core_marker_format, "name %s format %s",
489 e->name, e->format);
490 return 0;
494 * Sets the probe callback corresponding to one marker.
496 static int set_marker(struct marker_entry **entry, struct marker *elem,
497 int active)
499 int ret;
500 WARN_ON(strcmp((*entry)->name, elem->name) != 0);
502 if ((*entry)->format) {
503 if (strcmp((*entry)->format, elem->format) != 0) {
504 printk(KERN_NOTICE
505 "Format mismatch for probe %s "
506 "(%s), marker (%s)\n",
507 (*entry)->name,
508 (*entry)->format,
509 elem->format);
510 return -EPERM;
512 } else {
513 ret = marker_set_format(entry, elem->format);
514 if (ret)
515 return ret;
519 * probe_cb setup (statically known) is done here. It is
520 * asynchronous with the rest of execution, therefore we only
521 * pass from a "safe" callback (with argument) to an "unsafe"
522 * callback (does not set arguments).
524 elem->call = (*entry)->call;
526 * Sanity check :
527 * We only update the single probe private data when the ptr is
528 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
530 WARN_ON(elem->single.func != __mark_empty_function
531 && elem->single.probe_private
532 != (*entry)->single.probe_private &&
533 !elem->ptype);
534 elem->single.probe_private = (*entry)->single.probe_private;
536 * Make sure the private data is valid when we update the
537 * single probe ptr.
539 smp_wmb();
540 elem->single.func = (*entry)->single.func;
542 * We also make sure that the new probe callbacks array is consistent
543 * before setting a pointer to it.
545 rcu_assign_pointer(elem->multi, (*entry)->multi);
547 * Update the function or multi probe array pointer before setting the
548 * ptype.
550 smp_wmb();
551 elem->ptype = (*entry)->ptype;
552 elem->state = active;
554 return 0;
558 * Disable a marker and its probe callback.
559 * Note: only after a synchronize_sched() issued after setting elem->call to the
560 * empty function insures that the original callback is not used anymore. This
561 * insured by preemption disabling around the call site.
563 static void disable_marker(struct marker *elem)
565 /* leave "call" as is. It is known statically. */
566 elem->state = 0;
567 elem->single.func = __mark_empty_function;
568 /* Update the function before setting the ptype */
569 smp_wmb();
570 elem->ptype = 0; /* single probe */
572 * Leave the private data and id there, because removal is racy and
573 * should be done only after a synchronize_sched(). These are never used
574 * until the next initialization anyway.
579 * marker_update_probe_range - Update a probe range
580 * @begin: beginning of the range
581 * @end: end of the range
583 * Updates the probe callback corresponding to a range of markers.
585 void marker_update_probe_range(struct marker *begin,
586 struct marker *end)
588 struct marker *iter;
589 struct marker_entry *mark_entry;
591 mutex_lock(&markers_mutex);
592 for (iter = begin; iter < end; iter++) {
593 mark_entry = get_marker(iter->name);
594 if (mark_entry) {
595 set_marker(&mark_entry, iter,
596 !!mark_entry->refcount);
598 * ignore error, continue
600 } else {
601 disable_marker(iter);
604 mutex_unlock(&markers_mutex);
608 * Update probes, removing the faulty probes.
609 * Issues a synchronize_sched() when no reference to the module passed
610 * as parameter is found in the probes so the probe module can be
611 * safely unloaded from now on.
613 * Internal callback only changed before the first probe is connected to it.
614 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
615 * transitions. All other transitions will leave the old private data valid.
616 * This makes the non-atomicity of the callback/private data updates valid.
618 * "special case" updates :
619 * 0 -> 1 callback
620 * 1 -> 0 callback
621 * 1 -> 2 callbacks
622 * 2 -> 1 callbacks
623 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
624 * Site effect : marker_set_format may delete the marker entry (creating a
625 * replacement).
627 static void marker_update_probes(void)
629 /* Core kernel markers */
630 marker_update_probe_range(__start___markers, __stop___markers);
631 /* Markers in modules. */
632 module_update_markers();
636 * marker_probe_register - Connect a probe to a marker
637 * @name: marker name
638 * @format: format string
639 * @probe: probe handler
640 * @probe_private: probe private data
642 * private data must be a valid allocated memory address, or NULL.
643 * Returns 0 if ok, error value on error.
644 * The probe address must at least be aligned on the architecture pointer size.
646 int marker_probe_register(const char *name, const char *format,
647 marker_probe_func *probe, void *probe_private)
649 struct marker_entry *entry;
650 int ret = 0;
651 struct marker_probe_closure *old;
653 mutex_lock(&markers_mutex);
654 entry = get_marker(name);
655 if (!entry) {
656 entry = add_marker(name, format);
657 if (IS_ERR(entry)) {
658 ret = PTR_ERR(entry);
659 goto end;
663 * If we detect that a call_rcu is pending for this marker,
664 * make sure it's executed now.
666 if (entry->rcu_pending)
667 rcu_barrier();
668 old = marker_entry_add_probe(entry, probe, probe_private);
669 if (IS_ERR(old)) {
670 ret = PTR_ERR(old);
671 goto end;
673 mutex_unlock(&markers_mutex);
674 marker_update_probes(); /* may update entry */
675 mutex_lock(&markers_mutex);
676 entry = get_marker(name);
677 WARN_ON(!entry);
678 entry->oldptr = old;
679 entry->rcu_pending = 1;
680 /* write rcu_pending before calling the RCU callback */
681 smp_wmb();
682 call_rcu(&entry->rcu, free_old_closure);
683 end:
684 mutex_unlock(&markers_mutex);
685 return ret;
687 EXPORT_SYMBOL_GPL(marker_probe_register);
690 * marker_probe_unregister - Disconnect a probe from a marker
691 * @name: marker name
692 * @probe: probe function pointer
693 * @probe_private: probe private data
695 * Returns the private data given to marker_probe_register, or an ERR_PTR().
696 * We do not need to call a synchronize_sched to make sure the probes have
697 * finished running before doing a module unload, because the module unload
698 * itself uses stop_machine(), which insures that every preempt disabled section
699 * have finished.
701 int marker_probe_unregister(const char *name,
702 marker_probe_func *probe, void *probe_private)
704 struct marker_entry *entry;
705 struct marker_probe_closure *old;
706 <<<<<<< HEAD:kernel/marker.c
707 int ret = 0;
708 =======
709 int ret = -ENOENT;
710 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/marker.c
712 mutex_lock(&markers_mutex);
713 entry = get_marker(name);
714 <<<<<<< HEAD:kernel/marker.c
715 if (!entry) {
716 ret = -ENOENT;
717 =======
718 if (!entry)
719 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/marker.c
720 goto end;
721 <<<<<<< HEAD:kernel/marker.c
723 =======
724 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/marker.c
725 if (entry->rcu_pending)
726 rcu_barrier();
727 old = marker_entry_remove_probe(entry, probe, probe_private);
728 mutex_unlock(&markers_mutex);
729 marker_update_probes(); /* may update entry */
730 mutex_lock(&markers_mutex);
731 entry = get_marker(name);
732 <<<<<<< HEAD:kernel/marker.c
733 =======
734 if (!entry)
735 goto end;
736 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/marker.c
737 entry->oldptr = old;
738 entry->rcu_pending = 1;
739 /* write rcu_pending before calling the RCU callback */
740 smp_wmb();
741 call_rcu(&entry->rcu, free_old_closure);
742 remove_marker(name); /* Ignore busy error message */
743 <<<<<<< HEAD:kernel/marker.c
744 =======
745 ret = 0;
746 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/marker.c
747 end:
748 mutex_unlock(&markers_mutex);
749 return ret;
751 EXPORT_SYMBOL_GPL(marker_probe_unregister);
753 static struct marker_entry *
754 get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
756 struct marker_entry *entry;
757 unsigned int i;
758 struct hlist_head *head;
759 struct hlist_node *node;
761 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
762 head = &marker_table[i];
763 hlist_for_each_entry(entry, node, head, hlist) {
764 if (!entry->ptype) {
765 if (entry->single.func == probe
766 && entry->single.probe_private
767 == probe_private)
768 return entry;
769 } else {
770 struct marker_probe_closure *closure;
771 closure = entry->multi;
772 for (i = 0; closure[i].func; i++) {
773 if (closure[i].func == probe &&
774 closure[i].probe_private
775 == probe_private)
776 return entry;
781 return NULL;
785 * marker_probe_unregister_private_data - Disconnect a probe from a marker
786 * @probe: probe function
787 * @probe_private: probe private data
789 * Unregister a probe by providing the registered private data.
790 * Only removes the first marker found in hash table.
791 * Return 0 on success or error value.
792 * We do not need to call a synchronize_sched to make sure the probes have
793 * finished running before doing a module unload, because the module unload
794 * itself uses stop_machine(), which insures that every preempt disabled section
795 * have finished.
797 int marker_probe_unregister_private_data(marker_probe_func *probe,
798 void *probe_private)
800 struct marker_entry *entry;
801 int ret = 0;
802 struct marker_probe_closure *old;
804 mutex_lock(&markers_mutex);
805 entry = get_marker_from_private_data(probe, probe_private);
806 if (!entry) {
807 ret = -ENOENT;
808 goto end;
810 if (entry->rcu_pending)
811 rcu_barrier();
812 old = marker_entry_remove_probe(entry, NULL, probe_private);
813 mutex_unlock(&markers_mutex);
814 marker_update_probes(); /* may update entry */
815 mutex_lock(&markers_mutex);
816 entry = get_marker_from_private_data(probe, probe_private);
817 WARN_ON(!entry);
818 entry->oldptr = old;
819 entry->rcu_pending = 1;
820 /* write rcu_pending before calling the RCU callback */
821 smp_wmb();
822 call_rcu(&entry->rcu, free_old_closure);
823 remove_marker(entry->name); /* Ignore busy error message */
824 end:
825 mutex_unlock(&markers_mutex);
826 return ret;
828 EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
831 * marker_get_private_data - Get a marker's probe private data
832 * @name: marker name
833 * @probe: probe to match
834 * @num: get the nth matching probe's private data
836 * Returns the nth private data pointer (starting from 0) matching, or an
837 * ERR_PTR.
838 * Returns the private data pointer, or an ERR_PTR.
839 * The private data pointer should _only_ be dereferenced if the caller is the
840 * owner of the data, or its content could vanish. This is mostly used to
841 * confirm that a caller is the owner of a registered probe.
843 void *marker_get_private_data(const char *name, marker_probe_func *probe,
844 int num)
846 struct hlist_head *head;
847 struct hlist_node *node;
848 struct marker_entry *e;
849 size_t name_len = strlen(name) + 1;
850 u32 hash = jhash(name, name_len-1, 0);
851 int i;
853 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
854 hlist_for_each_entry(e, node, head, hlist) {
855 if (!strcmp(name, e->name)) {
856 if (!e->ptype) {
857 if (num == 0 && e->single.func == probe)
858 return e->single.probe_private;
859 else
860 break;
861 } else {
862 struct marker_probe_closure *closure;
863 int match = 0;
864 closure = e->multi;
865 for (i = 0; closure[i].func; i++) {
866 if (closure[i].func != probe)
867 continue;
868 if (match++ == num)
869 return closure[i].probe_private;
874 return ERR_PTR(-ENOENT);
876 EXPORT_SYMBOL_GPL(marker_get_private_data);