blob: de683a7799e713a63e5d740df89413a606de445a [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 Bunkab883af52008-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)
Lai Jiangshan4de62742008-10-15 14:56:47 +080046static struct hlist_head marker_table[MARKER_TABLE_SIZE];
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070047
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080048/*
49 * Note about RCU :
50 * It is used to make sure every handler has finished using its private data
51 * between two consecutive operation (add or remove) on a given marker. It is
52 * also used to delay the free of multiple probes array until a quiescent state
53 * is reached.
54 * marker entries modifications are protected by the markers_mutex.
55 */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070056struct marker_entry {
57 struct hlist_node hlist;
58 char *format;
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +020059 /* Probe wrapper */
60 void (*call)(const struct marker *mdata, void *call_private, ...);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080061 struct marker_probe_closure single;
62 struct marker_probe_closure *multi;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070063 int refcount; /* Number of times armed. 0 if disarmed. */
Mathieu Desnoyersed86a592008-10-01 12:03:25 -040064 struct rcu_head rcu;
65 void *oldptr;
Lai Jiangshan1b7ae372008-10-10 14:43:57 +080066 int rcu_pending;
Harvey Harrisonde4fc64f2008-02-23 15:23:33 -080067 unsigned char ptype:1;
Lai Jiangshan0eec4812008-10-15 14:56:37 +080068 unsigned char format_allocated:1;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070069 char name[0]; /* Contains name'\0'format'\0' */
70};
71
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070072/**
73 * __mark_empty_function - Empty probe callback
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080074 * @probe_private: probe private data
75 * @call_private: call site private data
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070076 * @fmt: format string
77 * @...: variable argument list
78 *
79 * Empty callback provided as a probe to the markers. By providing this to a
80 * disabled marker, we make sure the execution flow is always valid even
81 * though the function pointer change and the marker enabling are two distinct
82 * operations that modifies the execution flow of preemptible code.
83 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080084void __mark_empty_function(void *probe_private, void *call_private,
85 const char *fmt, va_list *args)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -070086{
87}
88EXPORT_SYMBOL_GPL(__mark_empty_function);
89
90/*
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080091 * marker_probe_cb Callback that prepares the variable argument list for probes.
92 * @mdata: pointer of type struct marker
93 * @call_private: caller site private data
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -080094 * @...: Variable argument list.
95 *
96 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
97 * need to put a full smp_rmb() in this branch. This is why we do not use
98 * rcu_dereference() for the pointer read.
99 */
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200100void marker_probe_cb(const struct marker *mdata, void *call_private, ...)
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800101{
102 va_list args;
103 char ptype;
104
105 /*
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400106 * rcu_read_lock_sched does two things : disabling preemption to make
107 * sure the teardown of the callbacks can be done correctly when they
108 * are in modules and they insure RCU read coherency.
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800109 */
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400110 rcu_read_lock_sched();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700111 ptype = mdata->ptype;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800112 if (likely(!ptype)) {
113 marker_probe_func *func;
114 /* Must read the ptype before ptr. They are not data dependant,
115 * so we put an explicit smp_rmb() here. */
116 smp_rmb();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700117 func = mdata->single.func;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800118 /* Must read the ptr before private data. They are not data
119 * dependant, so we put an explicit smp_rmb() here. */
120 smp_rmb();
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200121 va_start(args, call_private);
122 func(mdata->single.probe_private, call_private, mdata->format,
123 &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800124 va_end(args);
125 } else {
126 struct marker_probe_closure *multi;
127 int i;
128 /*
Mathieu Desnoyers5def9a32008-07-29 22:33:31 -0700129 * Read mdata->ptype before mdata->multi.
130 */
131 smp_rmb();
132 multi = mdata->multi;
133 /*
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800134 * multi points to an array, therefore accessing the array
135 * depends on reading multi. However, even in this case,
136 * we must insure that the pointer is read _before_ the array
137 * data. Same as rcu_dereference, but we need a full smp_rmb()
138 * in the fast path, so put the explicit barrier here.
139 */
140 smp_read_barrier_depends();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800141 for (i = 0; multi[i].func; i++) {
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200142 va_start(args, call_private);
143 multi[i].func(multi[i].probe_private, call_private,
144 mdata->format, &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800145 va_end(args);
146 }
147 }
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400148 rcu_read_unlock_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800149}
150EXPORT_SYMBOL_GPL(marker_probe_cb);
151
152/*
153 * marker_probe_cb Callback that does not prepare the variable argument list.
154 * @mdata: pointer of type struct marker
155 * @call_private: caller site private data
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800156 * @...: Variable argument list.
157 *
158 * Should be connected to markers "MARK_NOARGS".
159 */
Lai Jiangshan505e371d2008-10-15 14:56:42 +0800160static void marker_probe_cb_noarg(const struct marker *mdata, void *call_private, ...)
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800161{
162 va_list args; /* not initialized */
163 char ptype;
164
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400165 rcu_read_lock_sched();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700166 ptype = mdata->ptype;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800167 if (likely(!ptype)) {
168 marker_probe_func *func;
169 /* Must read the ptype before ptr. They are not data dependant,
170 * so we put an explicit smp_rmb() here. */
171 smp_rmb();
Mathieu Desnoyers58336112008-03-24 12:29:49 -0700172 func = mdata->single.func;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800173 /* Must read the ptr before private data. They are not data
174 * dependant, so we put an explicit smp_rmb() here. */
175 smp_rmb();
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200176 func(mdata->single.probe_private, call_private, mdata->format,
177 &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800178 } else {
179 struct marker_probe_closure *multi;
180 int i;
181 /*
Mathieu Desnoyers5def9a32008-07-29 22:33:31 -0700182 * Read mdata->ptype before mdata->multi.
183 */
184 smp_rmb();
185 multi = mdata->multi;
186 /*
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800187 * multi points to an array, therefore accessing the array
188 * depends on reading multi. However, even in this case,
189 * we must insure that the pointer is read _before_ the array
190 * data. Same as rcu_dereference, but we need a full smp_rmb()
191 * in the fast path, so put the explicit barrier here.
192 */
193 smp_read_barrier_depends();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800194 for (i = 0; multi[i].func; i++)
Mathieu Desnoyersdc102a82008-05-12 21:21:09 +0200195 multi[i].func(multi[i].probe_private, call_private,
196 mdata->format, &args);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800197 }
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400198 rcu_read_unlock_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800199}
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800200
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400201static void free_old_closure(struct rcu_head *head)
202{
203 struct marker_entry *entry = container_of(head,
204 struct marker_entry, rcu);
205 kfree(entry->oldptr);
206 /* Make sure we free the data before setting the pending flag to 0 */
207 smp_wmb();
208 entry->rcu_pending = 0;
209}
210
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800211static void debug_print_probes(struct marker_entry *entry)
212{
213 int i;
214
215 if (!marker_debug)
216 return;
217
218 if (!entry->ptype) {
219 printk(KERN_DEBUG "Single probe : %p %p\n",
220 entry->single.func,
221 entry->single.probe_private);
222 } else {
223 for (i = 0; entry->multi[i].func; i++)
224 printk(KERN_DEBUG "Multi probe %d : %p %p\n", i,
225 entry->multi[i].func,
226 entry->multi[i].probe_private);
227 }
228}
229
230static struct marker_probe_closure *
231marker_entry_add_probe(struct marker_entry *entry,
232 marker_probe_func *probe, void *probe_private)
233{
234 int nr_probes = 0;
235 struct marker_probe_closure *old, *new;
236
237 WARN_ON(!probe);
238
239 debug_print_probes(entry);
240 old = entry->multi;
241 if (!entry->ptype) {
242 if (entry->single.func == probe &&
243 entry->single.probe_private == probe_private)
244 return ERR_PTR(-EBUSY);
245 if (entry->single.func == __mark_empty_function) {
246 /* 0 -> 1 probes */
247 entry->single.func = probe;
248 entry->single.probe_private = probe_private;
249 entry->refcount = 1;
250 entry->ptype = 0;
251 debug_print_probes(entry);
252 return NULL;
253 } else {
254 /* 1 -> 2 probes */
255 nr_probes = 1;
256 old = NULL;
257 }
258 } else {
259 /* (N -> N+1), (N != 0, 1) probes */
260 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
261 if (old[nr_probes].func == probe
262 && old[nr_probes].probe_private
263 == probe_private)
264 return ERR_PTR(-EBUSY);
265 }
266 /* + 2 : one for new probe, one for NULL func */
267 new = kzalloc((nr_probes + 2) * sizeof(struct marker_probe_closure),
268 GFP_KERNEL);
269 if (new == NULL)
270 return ERR_PTR(-ENOMEM);
271 if (!old)
272 new[0] = entry->single;
273 else
274 memcpy(new, old,
275 nr_probes * sizeof(struct marker_probe_closure));
276 new[nr_probes].func = probe;
277 new[nr_probes].probe_private = probe_private;
278 entry->refcount = nr_probes + 1;
279 entry->multi = new;
280 entry->ptype = 1;
281 debug_print_probes(entry);
282 return old;
283}
284
285static struct marker_probe_closure *
286marker_entry_remove_probe(struct marker_entry *entry,
287 marker_probe_func *probe, void *probe_private)
288{
289 int nr_probes = 0, nr_del = 0, i;
290 struct marker_probe_closure *old, *new;
291
292 old = entry->multi;
293
294 debug_print_probes(entry);
295 if (!entry->ptype) {
296 /* 0 -> N is an error */
297 WARN_ON(entry->single.func == __mark_empty_function);
298 /* 1 -> 0 probes */
299 WARN_ON(probe && entry->single.func != probe);
300 WARN_ON(entry->single.probe_private != probe_private);
301 entry->single.func = __mark_empty_function;
302 entry->refcount = 0;
303 entry->ptype = 0;
304 debug_print_probes(entry);
305 return NULL;
306 } else {
307 /* (N -> M), (N > 1, M >= 0) probes */
308 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
309 if ((!probe || old[nr_probes].func == probe)
310 && old[nr_probes].probe_private
311 == probe_private)
312 nr_del++;
313 }
314 }
315
316 if (nr_probes - nr_del == 0) {
317 /* N -> 0, (N > 1) */
318 entry->single.func = __mark_empty_function;
319 entry->refcount = 0;
320 entry->ptype = 0;
321 } else if (nr_probes - nr_del == 1) {
322 /* N -> 1, (N > 1) */
323 for (i = 0; old[i].func; i++)
324 if ((probe && old[i].func != probe) ||
325 old[i].probe_private != probe_private)
326 entry->single = old[i];
327 entry->refcount = 1;
328 entry->ptype = 0;
329 } else {
330 int j = 0;
331 /* N -> M, (N > 1, M > 1) */
332 /* + 1 for NULL */
333 new = kzalloc((nr_probes - nr_del + 1)
334 * sizeof(struct marker_probe_closure), GFP_KERNEL);
335 if (new == NULL)
336 return ERR_PTR(-ENOMEM);
337 for (i = 0; old[i].func; i++)
338 if ((probe && old[i].func != probe) ||
339 old[i].probe_private != probe_private)
340 new[j++] = old[i];
341 entry->refcount = nr_probes - nr_del;
342 entry->ptype = 1;
343 entry->multi = new;
344 }
345 debug_print_probes(entry);
346 return old;
347}
348
349/*
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700350 * Get marker if the marker is present in the marker hash table.
351 * Must be called with markers_mutex held.
352 * Returns NULL if not present.
353 */
354static struct marker_entry *get_marker(const char *name)
355{
356 struct hlist_head *head;
357 struct hlist_node *node;
358 struct marker_entry *e;
359 u32 hash = jhash(name, strlen(name), 0);
360
361 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
362 hlist_for_each_entry(e, node, head, hlist) {
363 if (!strcmp(name, e->name))
364 return e;
365 }
366 return NULL;
367}
368
369/*
370 * Add the marker to the marker hash table. Must be called with markers_mutex
371 * held.
372 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800373static struct marker_entry *add_marker(const char *name, const char *format)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700374{
375 struct hlist_head *head;
376 struct hlist_node *node;
377 struct marker_entry *e;
378 size_t name_len = strlen(name) + 1;
379 size_t format_len = 0;
380 u32 hash = jhash(name, name_len-1, 0);
381
382 if (format)
383 format_len = strlen(format) + 1;
384 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
385 hlist_for_each_entry(e, node, head, hlist) {
386 if (!strcmp(name, e->name)) {
387 printk(KERN_NOTICE
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800388 "Marker %s busy\n", name);
389 return ERR_PTR(-EBUSY); /* Already there */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700390 }
391 }
392 /*
393 * Using kmalloc here to allocate a variable length element. Could
394 * cause some memory fragmentation if overused.
395 */
396 e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
397 GFP_KERNEL);
398 if (!e)
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800399 return ERR_PTR(-ENOMEM);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700400 memcpy(&e->name[0], name, name_len);
401 if (format) {
402 e->format = &e->name[name_len];
403 memcpy(e->format, format, format_len);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800404 if (strcmp(e->format, MARK_NOARGS) == 0)
405 e->call = marker_probe_cb_noarg;
406 else
407 e->call = marker_probe_cb;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700408 trace_mark(core_marker_format, "name %s format %s",
409 e->name, e->format);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800410 } else {
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700411 e->format = NULL;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800412 e->call = marker_probe_cb;
413 }
414 e->single.func = __mark_empty_function;
415 e->single.probe_private = NULL;
416 e->multi = NULL;
417 e->ptype = 0;
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800418 e->format_allocated = 0;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700419 e->refcount = 0;
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400420 e->rcu_pending = 0;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700421 hlist_add_head(&e->hlist, head);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800422 return e;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700423}
424
425/*
426 * Remove the marker from the marker hash table. Must be called with mutex_lock
427 * held.
428 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800429static int remove_marker(const char *name)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700430{
431 struct hlist_head *head;
432 struct hlist_node *node;
433 struct marker_entry *e;
434 int found = 0;
435 size_t len = strlen(name) + 1;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700436 u32 hash = jhash(name, len-1, 0);
437
438 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
439 hlist_for_each_entry(e, node, head, hlist) {
440 if (!strcmp(name, e->name)) {
441 found = 1;
442 break;
443 }
444 }
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800445 if (!found)
446 return -ENOENT;
447 if (e->single.func != __mark_empty_function)
448 return -EBUSY;
449 hlist_del(&e->hlist);
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800450 if (e->format_allocated)
451 kfree(e->format);
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400452 /* Make sure the call_rcu has been executed */
453 if (e->rcu_pending)
454 rcu_barrier_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800455 kfree(e);
456 return 0;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700457}
458
459/*
460 * Set the mark_entry format to the format found in the element.
461 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800462static int marker_set_format(struct marker_entry *entry, const char *format)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700463{
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800464 entry->format = kstrdup(format, GFP_KERNEL);
465 if (!entry->format)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700466 return -ENOMEM;
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800467 entry->format_allocated = 1;
468
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700469 trace_mark(core_marker_format, "name %s format %s",
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800470 entry->name, entry->format);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700471 return 0;
472}
473
474/*
475 * Sets the probe callback corresponding to one marker.
476 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800477static int set_marker(struct marker_entry *entry, struct marker *elem,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800478 int active)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700479{
480 int ret;
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800481 WARN_ON(strcmp(entry->name, elem->name) != 0);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700482
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800483 if (entry->format) {
484 if (strcmp(entry->format, elem->format) != 0) {
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700485 printk(KERN_NOTICE
486 "Format mismatch for probe %s "
487 "(%s), marker (%s)\n",
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800488 entry->name,
489 entry->format,
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700490 elem->format);
491 return -EPERM;
492 }
493 } else {
494 ret = marker_set_format(entry, elem->format);
495 if (ret)
496 return ret;
497 }
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800498
499 /*
500 * probe_cb setup (statically known) is done here. It is
501 * asynchronous with the rest of execution, therefore we only
502 * pass from a "safe" callback (with argument) to an "unsafe"
503 * callback (does not set arguments).
504 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800505 elem->call = entry->call;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800506 /*
507 * Sanity check :
508 * We only update the single probe private data when the ptr is
509 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
510 */
511 WARN_ON(elem->single.func != __mark_empty_function
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800512 && elem->single.probe_private != entry->single.probe_private
513 && !elem->ptype);
514 elem->single.probe_private = entry->single.probe_private;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800515 /*
516 * Make sure the private data is valid when we update the
517 * single probe ptr.
518 */
519 smp_wmb();
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800520 elem->single.func = entry->single.func;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800521 /*
522 * We also make sure that the new probe callbacks array is consistent
523 * before setting a pointer to it.
524 */
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800525 rcu_assign_pointer(elem->multi, entry->multi);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800526 /*
527 * Update the function or multi probe array pointer before setting the
528 * ptype.
529 */
530 smp_wmb();
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800531 elem->ptype = entry->ptype;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800532 elem->state = active;
533
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700534 return 0;
535}
536
537/*
538 * Disable a marker and its probe callback.
Mathieu Desnoyersfd3c36f2008-03-24 12:29:47 -0700539 * Note: only waiting an RCU period after setting elem->call to the empty
540 * function insures that the original callback is not used anymore. This insured
Mathieu Desnoyerse2d3b75d2008-09-29 11:08:03 -0400541 * by rcu_read_lock_sched around the call site.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700542 */
543static void disable_marker(struct marker *elem)
544{
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800545 /* leave "call" as is. It is known statically. */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700546 elem->state = 0;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800547 elem->single.func = __mark_empty_function;
548 /* Update the function before setting the ptype */
549 smp_wmb();
550 elem->ptype = 0; /* single probe */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700551 /*
552 * Leave the private data and id there, because removal is racy and
Mathieu Desnoyersfd3c36f2008-03-24 12:29:47 -0700553 * should be done only after an RCU period. These are never used until
554 * the next initialization anyway.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700555 */
556}
557
558/**
559 * marker_update_probe_range - Update a probe range
560 * @begin: beginning of the range
561 * @end: end of the range
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700562 *
563 * Updates the probe callback corresponding to a range of markers.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700564 */
565void marker_update_probe_range(struct marker *begin,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800566 struct marker *end)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700567{
568 struct marker *iter;
569 struct marker_entry *mark_entry;
570
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800571 mutex_lock(&markers_mutex);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700572 for (iter = begin; iter < end; iter++) {
573 mark_entry = get_marker(iter->name);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800574 if (mark_entry) {
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800575 set_marker(mark_entry, iter, !!mark_entry->refcount);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700576 /*
577 * ignore error, continue
578 */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700579 } else {
580 disable_marker(iter);
581 }
582 }
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800583 mutex_unlock(&markers_mutex);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700584}
585
586/*
587 * Update probes, removing the faulty probes.
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800588 *
589 * Internal callback only changed before the first probe is connected to it.
590 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
591 * transitions. All other transitions will leave the old private data valid.
592 * This makes the non-atomicity of the callback/private data updates valid.
593 *
594 * "special case" updates :
595 * 0 -> 1 callback
596 * 1 -> 0 callback
597 * 1 -> 2 callbacks
598 * 2 -> 1 callbacks
599 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
600 * Site effect : marker_set_format may delete the marker entry (creating a
601 * replacement).
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700602 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800603static void marker_update_probes(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700604{
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700605 /* Core kernel markers */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800606 marker_update_probe_range(__start___markers, __stop___markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700607 /* Markers in modules. */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800608 module_update_markers();
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700609}
610
611/**
612 * marker_probe_register - Connect a probe to a marker
613 * @name: marker name
614 * @format: format string
615 * @probe: probe handler
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800616 * @probe_private: probe private data
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700617 *
618 * private data must be a valid allocated memory address, or NULL.
619 * Returns 0 if ok, error value on error.
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800620 * The probe address must at least be aligned on the architecture pointer size.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700621 */
622int marker_probe_register(const char *name, const char *format,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800623 marker_probe_func *probe, void *probe_private)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700624{
625 struct marker_entry *entry;
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800626 int ret = 0;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800627 struct marker_probe_closure *old;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700628
629 mutex_lock(&markers_mutex);
630 entry = get_marker(name);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800631 if (!entry) {
632 entry = add_marker(name, format);
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800633 if (IS_ERR(entry))
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800634 ret = PTR_ERR(entry);
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800635 } else if (format) {
636 if (!entry->format)
Lai Jiangshan0eec4812008-10-15 14:56:37 +0800637 ret = marker_set_format(entry, format);
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800638 else if (strcmp(entry->format, format))
639 ret = -EPERM;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800640 }
Lai Jiangshan48043bc2008-10-08 10:23:36 +0800641 if (ret)
642 goto end;
643
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400644 /*
645 * If we detect that a call_rcu is pending for this marker,
646 * make sure it's executed now.
647 */
648 if (entry->rcu_pending)
649 rcu_barrier_sched();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800650 old = marker_entry_add_probe(entry, probe, probe_private);
651 if (IS_ERR(old)) {
652 ret = PTR_ERR(old);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700653 goto end;
654 }
Mathieu Desnoyers314de8a2007-11-14 16:59:48 -0800655 mutex_unlock(&markers_mutex);
Mathieu Desnoyers2bdba312008-11-14 17:47:35 -0500656 marker_update_probes();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800657 mutex_lock(&markers_mutex);
658 entry = get_marker(name);
Mathieu Desnoyers2bdba312008-11-14 17:47:35 -0500659 if (!entry)
660 goto end;
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 Desnoyers2bdba312008-11-14 17:47:35 -0500701 marker_update_probes();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800702 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 Desnoyers2bdba312008-11-14 17:47:35 -0500782 marker_update_probes();
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800783 mutex_lock(&markers_mutex);
784 entry = get_marker_from_private_data(probe, probe_private);
Mathieu Desnoyers2bdba312008-11-14 17:47:35 -0500785 if (!entry)
786 goto end;
Mathieu Desnoyersed86a592008-10-01 12:03:25 -0400787 if (entry->rcu_pending)
788 rcu_barrier_sched();
789 entry->oldptr = old;
790 entry->rcu_pending = 1;
791 /* write rcu_pending before calling the RCU callback */
792 smp_wmb();
793 call_rcu_sched(&entry->rcu, free_old_closure);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800794 remove_marker(entry->name); /* Ignore busy error message */
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700795end:
796 mutex_unlock(&markers_mutex);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800797 return ret;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700798}
799EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
800
801/**
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700802 * marker_get_private_data - Get a marker's probe private data
803 * @name: marker name
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800804 * @probe: probe to match
805 * @num: get the nth matching probe's private data
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700806 *
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800807 * Returns the nth private data pointer (starting from 0) matching, or an
808 * ERR_PTR.
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700809 * Returns the private data pointer, or an ERR_PTR.
810 * The private data pointer should _only_ be dereferenced if the caller is the
811 * owner of the data, or its content could vanish. This is mostly used to
812 * confirm that a caller is the owner of a registered probe.
813 */
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800814void *marker_get_private_data(const char *name, marker_probe_func *probe,
815 int num)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700816{
817 struct hlist_head *head;
818 struct hlist_node *node;
819 struct marker_entry *e;
820 size_t name_len = strlen(name) + 1;
821 u32 hash = jhash(name, name_len-1, 0);
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800822 int i;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700823
824 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
825 hlist_for_each_entry(e, node, head, hlist) {
826 if (!strcmp(name, e->name)) {
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800827 if (!e->ptype) {
828 if (num == 0 && e->single.func == probe)
829 return e->single.probe_private;
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -0800830 } else {
831 struct marker_probe_closure *closure;
832 int match = 0;
833 closure = e->multi;
834 for (i = 0; closure[i].func; i++) {
835 if (closure[i].func != probe)
836 continue;
837 if (match++ == num)
838 return closure[i].probe_private;
839 }
840 }
Zhaolei5d9881e2008-10-22 11:38:01 +0800841 break;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -0700842 }
843 }
844 return ERR_PTR(-ENOENT);
845}
846EXPORT_SYMBOL_GPL(marker_get_private_data);