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