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