blob: 23192646555fbaf7b76d282eca706565745865fe [file] [log] [blame]
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001/*
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>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070026#include <linux/slab.h>
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070027
28extern struct marker __start___markers[];
29extern struct marker __stop___markers[];
30
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080031/* Set to 1 to enable marker debug output */
Adrian Bunkab883af2008-04-30 00:54:30 -070032static const int marker_debug;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070033
34/*
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080035 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
36 * and module markers and the hash table.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070037 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080038static DEFINE_MUTEX(markers_mutex);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070039
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
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080047/*
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 */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070055struct marker_entry {
56 struct hlist_node hlist;
57 char *format;
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +020058 /* Probe wrapper */
59 void (*call)(const struct marker *mdata, void *call_private, ...);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080060 struct marker_probe_closure single;
61 struct marker_probe_closure *multi;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070062 int refcount; /* Number of times armed. 0 if disarmed. */
Mathieu Desnoyersed86a592008-10-01 12:03:25 -040063 struct rcu_head rcu;
64 void *oldptr;
Lai Jiangshan1b7ae372008-10-10 14:43:57 +080065 int rcu_pending;
Harvey Harrisonde4fc642008-02-23 15:23:33 -080066 unsigned char ptype:1;
Lai Jiangshan0eec4812008-10-15 14:56:37 +080067 unsigned char format_allocated:1;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070068 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
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080075 * @probe_private: probe private data
76 * @call_private: call site private data
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070077 * @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 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080085void __mark_empty_function(void *probe_private, void *call_private,
86 const char *fmt, va_list *args)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070087{
88}
89EXPORT_SYMBOL_GPL(__mark_empty_function);
90
91/*
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080092 * 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
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080095 * @...: 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 */
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200101void marker_probe_cb(const struct marker *mdata, void *call_private, ...)
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800102{
103 va_list args;
104 char ptype;
105
106 /*
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400107 * 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.
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800110 */
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400111 rcu_read_lock_sched();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700112 ptype = mdata->ptype;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800113 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();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700118 func = mdata->single.func;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800119 /* Must read the ptr before private data. They are not data
120 * dependant, so we put an explicit smp_rmb() here. */
121 smp_rmb();
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200122 va_start(args, call_private);
123 func(mdata->single.probe_private, call_private, mdata->format,
124 &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800125 va_end(args);
126 } else {
127 struct marker_probe_closure *multi;
128 int i;
129 /*
Mathieu Desnoyers5def9a32008-07-29 22:33:31 -0700130 * Read mdata->ptype before mdata->multi.
131 */
132 smp_rmb();
133 multi = mdata->multi;
134 /*
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800135 * 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();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800142 for (i = 0; multi[i].func; i++) {
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200143 va_start(args, call_private);
144 multi[i].func(multi[i].probe_private, call_private,
145 mdata->format, &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800146 va_end(args);
147 }
148 }
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400149 rcu_read_unlock_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800150}
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
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800157 * @...: Variable argument list.
158 *
159 * Should be connected to markers "MARK_NOARGS".
160 */
Lai Jiangshan505e371d2008-10-15 14:56:42 +0800161static void marker_probe_cb_noarg(const struct marker *mdata, void *call_private, ...)
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800162{
163 va_list args; /* not initialized */
164 char ptype;
165
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400166 rcu_read_lock_sched();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700167 ptype = mdata->ptype;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800168 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();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700173 func = mdata->single.func;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800174 /* Must read the ptr before private data. They are not data
175 * dependant, so we put an explicit smp_rmb() here. */
176 smp_rmb();
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200177 func(mdata->single.probe_private, call_private, mdata->format,
178 &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800179 } else {
180 struct marker_probe_closure *multi;
181 int i;
182 /*
Mathieu Desnoyers5def9a32008-07-29 22:33:31 -0700183 * Read mdata->ptype before mdata->multi.
184 */
185 smp_rmb();
186 multi = mdata->multi;
187 /*
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800188 * 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();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800195 for (i = 0; multi[i].func; i++)
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200196 multi[i].func(multi[i].probe_private, call_private,
197 mdata->format, &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800198 }
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400199 rcu_read_unlock_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800200}
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800201
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400202static 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
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800212static 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
350/*
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700351 * 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 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800374static struct marker_entry *add_marker(const char *name, const char *format)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700375{
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
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800389 "Marker %s busy\n", name);
390 return ERR_PTR(-EBUSY); /* Already there */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700391 }
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)
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800400 return ERR_PTR(-ENOMEM);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700401 memcpy(&e->name[0], name, name_len);
402 if (format) {
403 e->format = &e->name[name_len];
404 memcpy(e->format, format, format_len);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800405 if (strcmp(e->format, MARK_NOARGS) == 0)
406 e->call = marker_probe_cb_noarg;
407 else
408 e->call = marker_probe_cb;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700409 trace_mark(core_marker_format, "name %s format %s",
410 e->name, e->format);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800411 } else {
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700412 e->format = NULL;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800413 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;
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800419 e->format_allocated = 0;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700420 e->refcount = 0;
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400421 e->rcu_pending = 0;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700422 hlist_add_head(&e->hlist, head);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800423 return e;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700424}
425
426/*
427 * Remove the marker from the marker hash table. Must be called with mutex_lock
428 * held.
429 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800430static int remove_marker(const char *name)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700431{
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;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700437 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 }
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800446 if (!found)
447 return -ENOENT;
448 if (e->single.func != __mark_empty_function)
449 return -EBUSY;
450 hlist_del(&e->hlist);
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800451 if (e->format_allocated)
452 kfree(e->format);
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400453 /* Make sure the call_rcu has been executed */
454 if (e->rcu_pending)
455 rcu_barrier_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800456 kfree(e);
457 return 0;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700458}
459
460/*
461 * Set the mark_entry format to the format found in the element.
462 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800463static int marker_set_format(struct marker_entry *entry, const char *format)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700464{
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800465 entry->format = kstrdup(format, GFP_KERNEL);
466 if (!entry->format)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700467 return -ENOMEM;
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800468 entry->format_allocated = 1;
469
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700470 trace_mark(core_marker_format, "name %s format %s",
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800471 entry->name, entry->format);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700472 return 0;
473}
474
475/*
476 * Sets the probe callback corresponding to one marker.
477 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800478static int set_marker(struct marker_entry *entry, struct marker *elem,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800479 int active)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700480{
481 int ret;
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800482 WARN_ON(strcmp(entry->name, elem->name) != 0);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700483
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800484 if (entry->format) {
485 if (strcmp(entry->format, elem->format) != 0) {
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700486 printk(KERN_NOTICE
487 "Format mismatch for probe %s "
488 "(%s), marker (%s)\n",
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800489 entry->name,
490 entry->format,
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700491 elem->format);
492 return -EPERM;
493 }
494 } else {
495 ret = marker_set_format(entry, elem->format);
496 if (ret)
497 return ret;
498 }
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800499
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 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800506 elem->call = entry->call;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800507 /*
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
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800513 && elem->single.probe_private != entry->single.probe_private
514 && !elem->ptype);
515 elem->single.probe_private = entry->single.probe_private;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800516 /*
517 * Make sure the private data is valid when we update the
518 * single probe ptr.
519 */
520 smp_wmb();
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800521 elem->single.func = entry->single.func;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800522 /*
523 * We also make sure that the new probe callbacks array is consistent
524 * before setting a pointer to it.
525 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800526 rcu_assign_pointer(elem->multi, entry->multi);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800527 /*
528 * Update the function or multi probe array pointer before setting the
529 * ptype.
530 */
531 smp_wmb();
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800532 elem->ptype = entry->ptype;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800533 elem->state = active;
534
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700535 return 0;
536}
537
538/*
539 * Disable a marker and its probe callback.
Mathieu Desnoyersfd3c36f2008-03-24 12:29:47 -0700540 * 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
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400542 * by rcu_read_lock_sched around the call site.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700543 */
544static void disable_marker(struct marker *elem)
545{
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800546 /* leave "call" as is. It is known statically. */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700547 elem->state = 0;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800548 elem->single.func = __mark_empty_function;
549 /* Update the function before setting the ptype */
550 smp_wmb();
551 elem->ptype = 0; /* single probe */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700552 /*
553 * Leave the private data and id there, because removal is racy and
Mathieu Desnoyersfd3c36f2008-03-24 12:29:47 -0700554 * should be done only after an RCU period. These are never used until
555 * the next initialization anyway.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700556 */
557}
558
559/**
560 * marker_update_probe_range - Update a probe range
561 * @begin: beginning of the range
562 * @end: end of the range
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700563 *
564 * Updates the probe callback corresponding to a range of markers.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700565 */
566void marker_update_probe_range(struct marker *begin,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800567 struct marker *end)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700568{
569 struct marker *iter;
570 struct marker_entry *mark_entry;
571
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800572 mutex_lock(&markers_mutex);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700573 for (iter = begin; iter < end; iter++) {
574 mark_entry = get_marker(iter->name);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800575 if (mark_entry) {
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800576 set_marker(mark_entry, iter, !!mark_entry->refcount);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700577 /*
578 * ignore error, continue
579 */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700580 } else {
581 disable_marker(iter);
582 }
583 }
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800584 mutex_unlock(&markers_mutex);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700585}
586
587/*
588 * Update probes, removing the faulty probes.
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800589 *
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).
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700603 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800604static void marker_update_probes(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700605{
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700606 /* Core kernel markers */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800607 marker_update_probe_range(__start___markers, __stop___markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700608 /* Markers in modules. */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800609 module_update_markers();
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700610}
611
612/**
613 * marker_probe_register - Connect a probe to a marker
614 * @name: marker name
615 * @format: format string
616 * @probe: probe handler
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800617 * @probe_private: probe private data
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700618 *
619 * private data must be a valid allocated memory address, or NULL.
620 * Returns 0 if ok, error value on error.
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800621 * The probe address must at least be aligned on the architecture pointer size.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700622 */
623int marker_probe_register(const char *name, const char *format,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800624 marker_probe_func *probe, void *probe_private)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700625{
626 struct marker_entry *entry;
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800627 int ret = 0;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800628 struct marker_probe_closure *old;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700629
630 mutex_lock(&markers_mutex);
631 entry = get_marker(name);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800632 if (!entry) {
633 entry = add_marker(name, format);
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800634 if (IS_ERR(entry))
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800635 ret = PTR_ERR(entry);
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800636 } else if (format) {
637 if (!entry->format)
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800638 ret = marker_set_format(entry, format);
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800639 else if (strcmp(entry->format, format))
640 ret = -EPERM;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800641 }
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800642 if (ret)
643 goto end;
644
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400645 /*
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();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800651 old = marker_entry_add_probe(entry, probe, probe_private);
652 if (IS_ERR(old)) {
653 ret = PTR_ERR(old);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700654 goto end;
655 }
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800656 mutex_unlock(&markers_mutex);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800657 marker_update_probes(); /* may update entry */
658 mutex_lock(&markers_mutex);
659 entry = get_marker(name);
660 WARN_ON(!entry);
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400661 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);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700668end:
669 mutex_unlock(&markers_mutex);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700670 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
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800677 * @probe: probe function pointer
678 * @probe_private: probe private data
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700679 *
680 * Returns the private data given to marker_probe_register, or an ERR_PTR().
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800681 * 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.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700685 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800686int marker_probe_unregister(const char *name,
687 marker_probe_func *probe, void *probe_private)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700688{
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700689 struct marker_entry *entry;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800690 struct marker_probe_closure *old;
Jesper Juhl544adb42008-03-04 14:29:00 -0800691 int ret = -ENOENT;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700692
693 mutex_lock(&markers_mutex);
694 entry = get_marker(name);
Jesper Juhl544adb42008-03-04 14:29:00 -0800695 if (!entry)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700696 goto end;
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400697 if (entry->rcu_pending)
698 rcu_barrier_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800699 old = marker_entry_remove_probe(entry, probe, probe_private);
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800700 mutex_unlock(&markers_mutex);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800701 marker_update_probes(); /* may update entry */
702 mutex_lock(&markers_mutex);
703 entry = get_marker(name);
Jesper Juhl544adb42008-03-04 14:29:00 -0800704 if (!entry)
705 goto end;
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400706 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);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800713 remove_marker(name); /* Ignore busy error message */
Jesper Juhl544adb42008-03-04 14:29:00 -0800714 ret = 0;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700715end:
716 mutex_unlock(&markers_mutex);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800717 return ret;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700718}
719EXPORT_SYMBOL_GPL(marker_probe_unregister);
720
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800721static struct marker_entry *
722get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700723{
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800724 struct marker_entry *entry;
725 unsigned int i;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700726 struct hlist_head *head;
727 struct hlist_node *node;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700728
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700729 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
730 head = &marker_table[i];
731 hlist_for_each_entry(entry, node, head, hlist) {
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800732 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 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700746 }
747 }
748 }
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800749 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 */
765int 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;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700776 goto end;
777 }
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400778 if (entry->rcu_pending)
779 rcu_barrier_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800780 old = marker_entry_remove_probe(entry, NULL, probe_private);
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800781 mutex_unlock(&markers_mutex);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800782 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);
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400786 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);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800793 remove_marker(entry->name); /* Ignore busy error message */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700794end:
795 mutex_unlock(&markers_mutex);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800796 return ret;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700797}
798EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
799
800/**
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700801 * marker_get_private_data - Get a marker's probe private data
802 * @name: marker name
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800803 * @probe: probe to match
804 * @num: get the nth matching probe's private data
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700805 *
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800806 * Returns the nth private data pointer (starting from 0) matching, or an
807 * ERR_PTR.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700808 * 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 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800813void *marker_get_private_data(const char *name, marker_probe_func *probe,
814 int num)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700815{
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);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800821 int i;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700822
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)) {
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800826 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 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700842 }
843 }
844 return ERR_PTR(-ENOENT);
845}
846EXPORT_SYMBOL_GPL(marker_get_private_data);