markers: remove exported symbol marker_probe_cb_noarg()
[deliverable/linux.git] / kernel / marker.c
CommitLineData
8256e47c
MD
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>
1aeb272c 26#include <linux/slab.h>
8256e47c
MD
27
28extern struct marker __start___markers[];
29extern struct marker __stop___markers[];
30
fb40bd78 31/* Set to 1 to enable marker debug output */
ab883af5 32static const int marker_debug;
fb40bd78 33
8256e47c 34/*
314de8a9 35 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
fb40bd78 36 * and module markers and the hash table.
8256e47c
MD
37 */
38static DEFINE_MUTEX(markers_mutex);
39
8256e47c
MD
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
fb40bd78
MD
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 */
8256e47c
MD
55struct marker_entry {
56 struct hlist_node hlist;
57 char *format;
dc102a8f
MD
58 /* Probe wrapper */
59 void (*call)(const struct marker *mdata, void *call_private, ...);
fb40bd78
MD
60 struct marker_probe_closure single;
61 struct marker_probe_closure *multi;
8256e47c 62 int refcount; /* Number of times armed. 0 if disarmed. */
ed86a590
MD
63 struct rcu_head rcu;
64 void *oldptr;
1b7ae37c 65 int rcu_pending;
de4fc64f 66 unsigned char ptype:1;
0eec481e 67 unsigned char format_allocated:1;
8256e47c
MD
68 char name[0]; /* Contains name'\0'format'\0' */
69};
70
71static struct hlist_head marker_table[MARKER_TABLE_SIZE];
72
73/**
74 * __mark_empty_function - Empty probe callback
fb40bd78
MD
75 * @probe_private: probe private data
76 * @call_private: call site private data
8256e47c
MD
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 */
fb40bd78
MD
85void __mark_empty_function(void *probe_private, void *call_private,
86 const char *fmt, va_list *args)
8256e47c
MD
87{
88}
89EXPORT_SYMBOL_GPL(__mark_empty_function);
90
fb40bd78
MD
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
fb40bd78
MD
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 */
dc102a8f 101void marker_probe_cb(const struct marker *mdata, void *call_private, ...)
fb40bd78
MD
102{
103 va_list args;
104 char ptype;
105
106 /*
e2d3b75d
MD
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.
fb40bd78 110 */
e2d3b75d 111 rcu_read_lock_sched();
58336114 112 ptype = mdata->ptype;
fb40bd78
MD
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();
58336114 118 func = mdata->single.func;
fb40bd78
MD
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();
dc102a8f
MD
122 va_start(args, call_private);
123 func(mdata->single.probe_private, call_private, mdata->format,
124 &args);
fb40bd78
MD
125 va_end(args);
126 } else {
127 struct marker_probe_closure *multi;
128 int i;
5def9a3a
MD
129 /*
130 * Read mdata->ptype before mdata->multi.
131 */
132 smp_rmb();
133 multi = mdata->multi;
fb40bd78
MD
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();
fb40bd78 142 for (i = 0; multi[i].func; i++) {
dc102a8f
MD
143 va_start(args, call_private);
144 multi[i].func(multi[i].probe_private, call_private,
145 mdata->format, &args);
fb40bd78
MD
146 va_end(args);
147 }
148 }
e2d3b75d 149 rcu_read_unlock_sched();
fb40bd78
MD
150}
151EXPORT_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
fb40bd78
MD
157 * @...: Variable argument list.
158 *
159 * Should be connected to markers "MARK_NOARGS".
160 */
505e371d 161static void marker_probe_cb_noarg(const struct marker *mdata, void *call_private, ...)
fb40bd78
MD
162{
163 va_list args; /* not initialized */
164 char ptype;
165
e2d3b75d 166 rcu_read_lock_sched();
58336114 167 ptype = mdata->ptype;
fb40bd78
MD
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();
58336114 173 func = mdata->single.func;
fb40bd78
MD
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();
dc102a8f
MD
177 func(mdata->single.probe_private, call_private, mdata->format,
178 &args);
fb40bd78
MD
179 } else {
180 struct marker_probe_closure *multi;
181 int i;
5def9a3a
MD
182 /*
183 * Read mdata->ptype before mdata->multi.
184 */
185 smp_rmb();
186 multi = mdata->multi;
fb40bd78
MD
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();
fb40bd78 195 for (i = 0; multi[i].func; i++)
dc102a8f
MD
196 multi[i].func(multi[i].probe_private, call_private,
197 mdata->format, &args);
fb40bd78 198 }
e2d3b75d 199 rcu_read_unlock_sched();
fb40bd78 200}
fb40bd78 201
ed86a590
MD
202static 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
fb40bd78
MD
212static 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
231static struct marker_probe_closure *
232marker_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
286static struct marker_probe_closure *
287marker_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
8256e47c
MD
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 */
355static 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 */
fb40bd78 374static struct marker_entry *add_marker(const char *name, const char *format)
8256e47c
MD
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
fb40bd78
MD
389 "Marker %s busy\n", name);
390 return ERR_PTR(-EBUSY); /* Already there */
8256e47c
MD
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)
fb40bd78 400 return ERR_PTR(-ENOMEM);
8256e47c
MD
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);
fb40bd78
MD
405 if (strcmp(e->format, MARK_NOARGS) == 0)
406 e->call = marker_probe_cb_noarg;
407 else
408 e->call = marker_probe_cb;
8256e47c
MD
409 trace_mark(core_marker_format, "name %s format %s",
410 e->name, e->format);
fb40bd78 411 } else {
8256e47c 412 e->format = NULL;
fb40bd78
MD
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;
0eec481e 419 e->format_allocated = 0;
8256e47c 420 e->refcount = 0;
ed86a590 421 e->rcu_pending = 0;
8256e47c 422 hlist_add_head(&e->hlist, head);
fb40bd78 423 return e;
8256e47c
MD
424}
425
426/*
427 * Remove the marker from the marker hash table. Must be called with mutex_lock
428 * held.
429 */
fb40bd78 430static int remove_marker(const char *name)
8256e47c
MD
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;
8256e47c
MD
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 }
fb40bd78
MD
446 if (!found)
447 return -ENOENT;
448 if (e->single.func != __mark_empty_function)
449 return -EBUSY;
450 hlist_del(&e->hlist);
0eec481e
LJ
451 if (e->format_allocated)
452 kfree(e->format);
ed86a590
MD
453 /* Make sure the call_rcu has been executed */
454 if (e->rcu_pending)
455 rcu_barrier_sched();
fb40bd78
MD
456 kfree(e);
457 return 0;
8256e47c
MD
458}
459
460/*
461 * Set the mark_entry format to the format found in the element.
462 */
0eec481e 463static int marker_set_format(struct marker_entry *entry, const char *format)
8256e47c 464{
0eec481e
LJ
465 entry->format = kstrdup(format, GFP_KERNEL);
466 if (!entry->format)
8256e47c 467 return -ENOMEM;
0eec481e
LJ
468 entry->format_allocated = 1;
469
8256e47c 470 trace_mark(core_marker_format, "name %s format %s",
0eec481e 471 entry->name, entry->format);
8256e47c
MD
472 return 0;
473}
474
475/*
476 * Sets the probe callback corresponding to one marker.
477 */
0eec481e 478static int set_marker(struct marker_entry *entry, struct marker *elem,
fb40bd78 479 int active)
8256e47c
MD
480{
481 int ret;
0eec481e 482 WARN_ON(strcmp(entry->name, elem->name) != 0);
8256e47c 483
0eec481e
LJ
484 if (entry->format) {
485 if (strcmp(entry->format, elem->format) != 0) {
8256e47c
MD
486 printk(KERN_NOTICE
487 "Format mismatch for probe %s "
488 "(%s), marker (%s)\n",
0eec481e
LJ
489 entry->name,
490 entry->format,
8256e47c
MD
491 elem->format);
492 return -EPERM;
493 }
494 } else {
495 ret = marker_set_format(entry, elem->format);
496 if (ret)
497 return ret;
498 }
fb40bd78
MD
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 */
0eec481e 506 elem->call = entry->call;
fb40bd78
MD
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
0eec481e
LJ
513 && elem->single.probe_private != entry->single.probe_private
514 && !elem->ptype);
515 elem->single.probe_private = entry->single.probe_private;
fb40bd78
MD
516 /*
517 * Make sure the private data is valid when we update the
518 * single probe ptr.
519 */
520 smp_wmb();
0eec481e 521 elem->single.func = entry->single.func;
fb40bd78
MD
522 /*
523 * We also make sure that the new probe callbacks array is consistent
524 * before setting a pointer to it.
525 */
0eec481e 526 rcu_assign_pointer(elem->multi, entry->multi);
fb40bd78
MD
527 /*
528 * Update the function or multi probe array pointer before setting the
529 * ptype.
530 */
531 smp_wmb();
0eec481e 532 elem->ptype = entry->ptype;
fb40bd78
MD
533 elem->state = active;
534
8256e47c
MD
535 return 0;
536}
537
538/*
539 * Disable a marker and its probe callback.
fd3c36f8
MD
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
e2d3b75d 542 * by rcu_read_lock_sched around the call site.
8256e47c
MD
543 */
544static void disable_marker(struct marker *elem)
545{
fb40bd78 546 /* leave "call" as is. It is known statically. */
8256e47c 547 elem->state = 0;
fb40bd78
MD
548 elem->single.func = __mark_empty_function;
549 /* Update the function before setting the ptype */
550 smp_wmb();
551 elem->ptype = 0; /* single probe */
8256e47c
MD
552 /*
553 * Leave the private data and id there, because removal is racy and
fd3c36f8
MD
554 * should be done only after an RCU period. These are never used until
555 * the next initialization anyway.
8256e47c
MD
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
8256e47c
MD
563 *
564 * Updates the probe callback corresponding to a range of markers.
8256e47c
MD
565 */
566void marker_update_probe_range(struct marker *begin,
fb40bd78 567 struct marker *end)
8256e47c
MD
568{
569 struct marker *iter;
570 struct marker_entry *mark_entry;
571
314de8a9 572 mutex_lock(&markers_mutex);
8256e47c
MD
573 for (iter = begin; iter < end; iter++) {
574 mark_entry = get_marker(iter->name);
fb40bd78 575 if (mark_entry) {
0eec481e 576 set_marker(mark_entry, iter, !!mark_entry->refcount);
8256e47c
MD
577 /*
578 * ignore error, continue
579 */
8256e47c
MD
580 } else {
581 disable_marker(iter);
582 }
583 }
314de8a9 584 mutex_unlock(&markers_mutex);
8256e47c
MD
585}
586
587/*
588 * Update probes, removing the faulty probes.
fb40bd78
MD
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).
8256e47c 603 */
fb40bd78 604static void marker_update_probes(void)
8256e47c 605{
8256e47c 606 /* Core kernel markers */
fb40bd78 607 marker_update_probe_range(__start___markers, __stop___markers);
8256e47c 608 /* Markers in modules. */
fb40bd78 609 module_update_markers();
8256e47c
MD
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
fb40bd78 617 * @probe_private: probe private data
8256e47c
MD
618 *
619 * private data must be a valid allocated memory address, or NULL.
620 * Returns 0 if ok, error value on error.
fb40bd78 621 * The probe address must at least be aligned on the architecture pointer size.
8256e47c
MD
622 */
623int marker_probe_register(const char *name, const char *format,
fb40bd78 624 marker_probe_func *probe, void *probe_private)
8256e47c
MD
625{
626 struct marker_entry *entry;
314de8a9 627 int ret = 0;
fb40bd78 628 struct marker_probe_closure *old;
8256e47c
MD
629
630 mutex_lock(&markers_mutex);
631 entry = get_marker(name);
fb40bd78
MD
632 if (!entry) {
633 entry = add_marker(name, format);
48043bcd 634 if (IS_ERR(entry))
fb40bd78 635 ret = PTR_ERR(entry);
48043bcd
LJ
636 } else if (format) {
637 if (!entry->format)
0eec481e 638 ret = marker_set_format(entry, format);
48043bcd
LJ
639 else if (strcmp(entry->format, format))
640 ret = -EPERM;
8256e47c 641 }
48043bcd
LJ
642 if (ret)
643 goto end;
644
ed86a590
MD
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();
fb40bd78
MD
651 old = marker_entry_add_probe(entry, probe, probe_private);
652 if (IS_ERR(old)) {
653 ret = PTR_ERR(old);
8256e47c 654 goto end;
fb40bd78 655 }
314de8a9 656 mutex_unlock(&markers_mutex);
fb40bd78
MD
657 marker_update_probes(); /* may update entry */
658 mutex_lock(&markers_mutex);
659 entry = get_marker(name);
660 WARN_ON(!entry);
ed86a590
MD
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);
8256e47c
MD
668end:
669 mutex_unlock(&markers_mutex);
8256e47c
MD
670 return ret;
671}
672EXPORT_SYMBOL_GPL(marker_probe_register);
673
674/**
675 * marker_probe_unregister - Disconnect a probe from a marker
676 * @name: marker name
fb40bd78
MD
677 * @probe: probe function pointer
678 * @probe_private: probe private data
8256e47c
MD
679 *
680 * Returns the private data given to marker_probe_register, or an ERR_PTR().
fb40bd78
MD
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.
8256e47c 685 */
fb40bd78
MD
686int marker_probe_unregister(const char *name,
687 marker_probe_func *probe, void *probe_private)
8256e47c 688{
8256e47c 689 struct marker_entry *entry;
fb40bd78 690 struct marker_probe_closure *old;
544adb41 691 int ret = -ENOENT;
8256e47c
MD
692
693 mutex_lock(&markers_mutex);
694 entry = get_marker(name);
544adb41 695 if (!entry)
8256e47c 696 goto end;
ed86a590
MD
697 if (entry->rcu_pending)
698 rcu_barrier_sched();
fb40bd78 699 old = marker_entry_remove_probe(entry, probe, probe_private);
314de8a9 700 mutex_unlock(&markers_mutex);
fb40bd78
MD
701 marker_update_probes(); /* may update entry */
702 mutex_lock(&markers_mutex);
703 entry = get_marker(name);
544adb41
JJ
704 if (!entry)
705 goto end;
ed86a590
MD
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);
fb40bd78 713 remove_marker(name); /* Ignore busy error message */
544adb41 714 ret = 0;
8256e47c
MD
715end:
716 mutex_unlock(&markers_mutex);
fb40bd78 717 return ret;
8256e47c
MD
718}
719EXPORT_SYMBOL_GPL(marker_probe_unregister);
720
fb40bd78
MD
721static struct marker_entry *
722get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
8256e47c 723{
8256e47c 724 struct marker_entry *entry;
8256e47c 725 unsigned int i;
fb40bd78
MD
726 struct hlist_head *head;
727 struct hlist_node *node;
8256e47c 728
8256e47c
MD
729 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
730 head = &marker_table[i];
731 hlist_for_each_entry(entry, node, head, hlist) {
fb40bd78
MD
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 }
8256e47c
MD
746 }
747 }
748 }
fb40bd78 749 return NULL;
8256e47c 750}
8256e47c
MD
751
752/**
fb40bd78
MD
753 * marker_probe_unregister_private_data - Disconnect a probe from a marker
754 * @probe: probe function
755 * @probe_private: probe private data
8256e47c 756 *
fb40bd78
MD
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.
8256e47c 764 */
fb40bd78
MD
765int marker_probe_unregister_private_data(marker_probe_func *probe,
766 void *probe_private)
8256e47c
MD
767{
768 struct marker_entry *entry;
314de8a9 769 int ret = 0;
fb40bd78 770 struct marker_probe_closure *old;
8256e47c
MD
771
772 mutex_lock(&markers_mutex);
fb40bd78 773 entry = get_marker_from_private_data(probe, probe_private);
8256e47c
MD
774 if (!entry) {
775 ret = -ENOENT;
776 goto end;
777 }
ed86a590
MD
778 if (entry->rcu_pending)
779 rcu_barrier_sched();
fb40bd78 780 old = marker_entry_remove_probe(entry, NULL, probe_private);
8256e47c 781 mutex_unlock(&markers_mutex);
fb40bd78 782 marker_update_probes(); /* may update entry */
8256e47c 783 mutex_lock(&markers_mutex);
fb40bd78
MD
784 entry = get_marker_from_private_data(probe, probe_private);
785 WARN_ON(!entry);
ed86a590
MD
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);
fb40bd78 793 remove_marker(entry->name); /* Ignore busy error message */
8256e47c
MD
794end:
795 mutex_unlock(&markers_mutex);
8256e47c
MD
796 return ret;
797}
fb40bd78 798EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
8256e47c
MD
799
800/**
801 * marker_get_private_data - Get a marker's probe private data
802 * @name: marker name
fb40bd78
MD
803 * @probe: probe to match
804 * @num: get the nth matching probe's private data
8256e47c 805 *
fb40bd78
MD
806 * Returns the nth private data pointer (starting from 0) matching, or an
807 * ERR_PTR.
8256e47c
MD
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 */
fb40bd78
MD
813void *marker_get_private_data(const char *name, marker_probe_func *probe,
814 int num)
8256e47c
MD
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);
fb40bd78 821 int i;
8256e47c
MD
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)) {
fb40bd78
MD
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 }
8256e47c
MD
842 }
843 }
844 return ERR_PTR(-ENOENT);
845}
846EXPORT_SYMBOL_GPL(marker_get_private_data);
This page took 0.149403 seconds and 5 git commands to generate.