Commit | Line | Data |
---|---|---|
68c1021b PMF |
1 | /* |
2 | * Copyright (C) 2007 Mathieu Desnoyers | |
3 | * | |
34e4b7db PMF |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; either | |
7 | * version 2.1 of the License, or (at your option) any later version. | |
68c1021b | 8 | * |
34e4b7db | 9 | * This library is distributed in the hope that it will be useful, |
68c1021b | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
34e4b7db PMF |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. | |
68c1021b | 13 | * |
34e4b7db PMF |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
68c1021b | 17 | */ |
59b161cd PMF |
18 | //ust// #include <linux/module.h> |
19 | //ust// #include <linux/mutex.h> | |
20 | //ust// #include <linux/types.h> | |
b4512257 PMF |
21 | //#include "jhash.h" |
22 | //#include "list.h" | |
23 | //#include "rcupdate.h" | |
59b161cd PMF |
24 | //ust// #include <linux/marker.h> |
25 | #include <errno.h> | |
26 | //ust// #include <linux/slab.h> | |
27 | //ust// #include <linux/immediate.h> | |
28 | //ust// #include <linux/sched.h> | |
29 | //ust// #include <linux/uaccess.h> | |
30 | //ust// #include <linux/user_marker.h> | |
31 | //ust// #include <linux/ltt-tracer.h> | |
32 | ||
769d0157 | 33 | #define _LGPL_SOURCE |
b7ea1a1c | 34 | #include <urcu-bp.h> |
769d0157 | 35 | |
59b161cd | 36 | #include "kernelcompat.h" |
769d0157 | 37 | |
93d0f2ea | 38 | #include <ust/marker.h> |
59b161cd PMF |
39 | #include "usterr.h" |
40 | #include "channels.h" | |
41 | #include "tracercore.h" | |
9c67dc50 | 42 | #include "tracer.h" |
68c1021b | 43 | |
c463904d PMF |
44 | extern struct marker __start___markers[] __attribute__((visibility("hidden"))); |
45 | extern struct marker __stop___markers[] __attribute__((visibility("hidden"))); | |
68c1021b PMF |
46 | |
47 | /* Set to 1 to enable marker debug output */ | |
48 | static const int marker_debug; | |
49 | ||
50 | /* | |
51 | * markers_mutex nests inside module_mutex. Markers mutex protects the builtin | |
52 | * and module markers and the hash table. | |
53 | */ | |
54 | static DEFINE_MUTEX(markers_mutex); | |
55 | ||
772030fe PMF |
56 | static LIST_HEAD(libs); |
57 | ||
58 | ||
68c1021b PMF |
59 | void lock_markers(void) |
60 | { | |
61 | mutex_lock(&markers_mutex); | |
62 | } | |
63 | ||
64 | void unlock_markers(void) | |
65 | { | |
66 | mutex_unlock(&markers_mutex); | |
67 | } | |
68 | ||
69 | /* | |
70 | * Marker hash table, containing the active markers. | |
71 | * Protected by module_mutex. | |
72 | */ | |
73 | #define MARKER_HASH_BITS 6 | |
74 | #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS) | |
75 | static struct hlist_head marker_table[MARKER_TABLE_SIZE]; | |
76 | ||
77 | /* | |
78 | * Note about RCU : | |
79 | * It is used to make sure every handler has finished using its private data | |
80 | * between two consecutive operation (add or remove) on a given marker. It is | |
81 | * also used to delay the free of multiple probes array until a quiescent state | |
82 | * is reached. | |
83 | * marker entries modifications are protected by the markers_mutex. | |
84 | */ | |
85 | struct marker_entry { | |
86 | struct hlist_node hlist; | |
87 | char *format; | |
88 | char *name; | |
89 | /* Probe wrapper */ | |
90 | void (*call)(const struct marker *mdata, void *call_private, ...); | |
91 | struct marker_probe_closure single; | |
92 | struct marker_probe_closure *multi; | |
93 | int refcount; /* Number of times armed. 0 if disarmed. */ | |
94 | struct rcu_head rcu; | |
95 | void *oldptr; | |
96 | int rcu_pending; | |
97 | u16 channel_id; | |
98 | u16 event_id; | |
99 | unsigned char ptype:1; | |
100 | unsigned char format_allocated:1; | |
101 | char channel[0]; /* Contains channel'\0'name'\0'format'\0' */ | |
102 | }; | |
103 | ||
104 | #ifdef CONFIG_MARKERS_USERSPACE | |
105 | static void marker_update_processes(void); | |
106 | #else | |
107 | static void marker_update_processes(void) | |
108 | { | |
109 | } | |
110 | #endif | |
111 | ||
112 | /** | |
113 | * __mark_empty_function - Empty probe callback | |
114 | * @mdata: marker data | |
115 | * @probe_private: probe private data | |
116 | * @call_private: call site private data | |
117 | * @fmt: format string | |
118 | * @...: variable argument list | |
119 | * | |
120 | * Empty callback provided as a probe to the markers. By providing this to a | |
121 | * disabled marker, we make sure the execution flow is always valid even | |
122 | * though the function pointer change and the marker enabling are two distinct | |
123 | * operations that modifies the execution flow of preemptible code. | |
124 | */ | |
125 | notrace void __mark_empty_function(const struct marker *mdata, | |
126 | void *probe_private, void *call_private, const char *fmt, va_list *args) | |
127 | { | |
128 | } | |
59b161cd | 129 | //ust// EXPORT_SYMBOL_GPL(__mark_empty_function); |
68c1021b PMF |
130 | |
131 | /* | |
132 | * marker_probe_cb Callback that prepares the variable argument list for probes. | |
133 | * @mdata: pointer of type struct marker | |
134 | * @call_private: caller site private data | |
135 | * @...: Variable argument list. | |
136 | * | |
137 | * Since we do not use "typical" pointer based RCU in the 1 argument case, we | |
138 | * need to put a full smp_rmb() in this branch. This is why we do not use | |
139 | * rcu_dereference() for the pointer read. | |
140 | */ | |
141 | notrace void marker_probe_cb(const struct marker *mdata, | |
142 | void *call_private, ...) | |
143 | { | |
144 | va_list args; | |
145 | char ptype; | |
146 | ||
147 | /* | |
148 | * rcu_read_lock_sched does two things : disabling preemption to make | |
149 | * sure the teardown of the callbacks can be done correctly when they | |
150 | * are in modules and they insure RCU read coherency. | |
151 | */ | |
59b161cd | 152 | //ust// rcu_read_lock_sched_notrace(); |
68c1021b PMF |
153 | ptype = mdata->ptype; |
154 | if (likely(!ptype)) { | |
155 | marker_probe_func *func; | |
156 | /* Must read the ptype before ptr. They are not data dependant, | |
157 | * so we put an explicit smp_rmb() here. */ | |
158 | smp_rmb(); | |
159 | func = mdata->single.func; | |
160 | /* Must read the ptr before private data. They are not data | |
161 | * dependant, so we put an explicit smp_rmb() here. */ | |
162 | smp_rmb(); | |
163 | va_start(args, call_private); | |
164 | func(mdata, mdata->single.probe_private, call_private, | |
165 | mdata->format, &args); | |
166 | va_end(args); | |
167 | } else { | |
168 | struct marker_probe_closure *multi; | |
169 | int i; | |
170 | /* | |
171 | * Read mdata->ptype before mdata->multi. | |
172 | */ | |
173 | smp_rmb(); | |
174 | multi = mdata->multi; | |
175 | /* | |
176 | * multi points to an array, therefore accessing the array | |
177 | * depends on reading multi. However, even in this case, | |
178 | * we must insure that the pointer is read _before_ the array | |
179 | * data. Same as rcu_dereference, but we need a full smp_rmb() | |
180 | * in the fast path, so put the explicit barrier here. | |
181 | */ | |
182 | smp_read_barrier_depends(); | |
183 | for (i = 0; multi[i].func; i++) { | |
184 | va_start(args, call_private); | |
185 | multi[i].func(mdata, multi[i].probe_private, | |
186 | call_private, mdata->format, &args); | |
187 | va_end(args); | |
188 | } | |
189 | } | |
59b161cd | 190 | //ust// rcu_read_unlock_sched_notrace(); |
68c1021b | 191 | } |
59b161cd | 192 | //ust// EXPORT_SYMBOL_GPL(marker_probe_cb); |
68c1021b PMF |
193 | |
194 | /* | |
195 | * marker_probe_cb Callback that does not prepare the variable argument list. | |
196 | * @mdata: pointer of type struct marker | |
197 | * @call_private: caller site private data | |
198 | * @...: Variable argument list. | |
199 | * | |
200 | * Should be connected to markers "MARK_NOARGS". | |
201 | */ | |
202 | static notrace void marker_probe_cb_noarg(const struct marker *mdata, | |
203 | void *call_private, ...) | |
204 | { | |
205 | va_list args; /* not initialized */ | |
206 | char ptype; | |
207 | ||
59b161cd | 208 | //ust// rcu_read_lock_sched_notrace(); |
68c1021b PMF |
209 | ptype = mdata->ptype; |
210 | if (likely(!ptype)) { | |
211 | marker_probe_func *func; | |
212 | /* Must read the ptype before ptr. They are not data dependant, | |
213 | * so we put an explicit smp_rmb() here. */ | |
214 | smp_rmb(); | |
215 | func = mdata->single.func; | |
216 | /* Must read the ptr before private data. They are not data | |
217 | * dependant, so we put an explicit smp_rmb() here. */ | |
218 | smp_rmb(); | |
219 | func(mdata, mdata->single.probe_private, call_private, | |
220 | mdata->format, &args); | |
221 | } else { | |
222 | struct marker_probe_closure *multi; | |
223 | int i; | |
224 | /* | |
225 | * Read mdata->ptype before mdata->multi. | |
226 | */ | |
227 | smp_rmb(); | |
228 | multi = mdata->multi; | |
229 | /* | |
230 | * multi points to an array, therefore accessing the array | |
231 | * depends on reading multi. However, even in this case, | |
232 | * we must insure that the pointer is read _before_ the array | |
233 | * data. Same as rcu_dereference, but we need a full smp_rmb() | |
234 | * in the fast path, so put the explicit barrier here. | |
235 | */ | |
236 | smp_read_barrier_depends(); | |
237 | for (i = 0; multi[i].func; i++) | |
238 | multi[i].func(mdata, multi[i].probe_private, | |
239 | call_private, mdata->format, &args); | |
240 | } | |
59b161cd | 241 | //ust// rcu_read_unlock_sched_notrace(); |
68c1021b PMF |
242 | } |
243 | ||
244 | static void free_old_closure(struct rcu_head *head) | |
245 | { | |
246 | struct marker_entry *entry = container_of(head, | |
247 | struct marker_entry, rcu); | |
248 | kfree(entry->oldptr); | |
249 | /* Make sure we free the data before setting the pending flag to 0 */ | |
250 | smp_wmb(); | |
251 | entry->rcu_pending = 0; | |
252 | } | |
253 | ||
254 | static void debug_print_probes(struct marker_entry *entry) | |
255 | { | |
256 | int i; | |
257 | ||
258 | if (!marker_debug) | |
259 | return; | |
260 | ||
261 | if (!entry->ptype) { | |
262 | printk(KERN_DEBUG "Single probe : %p %p\n", | |
263 | entry->single.func, | |
264 | entry->single.probe_private); | |
265 | } else { | |
266 | for (i = 0; entry->multi[i].func; i++) | |
267 | printk(KERN_DEBUG "Multi probe %d : %p %p\n", i, | |
268 | entry->multi[i].func, | |
269 | entry->multi[i].probe_private); | |
270 | } | |
271 | } | |
272 | ||
273 | static struct marker_probe_closure * | |
274 | marker_entry_add_probe(struct marker_entry *entry, | |
275 | marker_probe_func *probe, void *probe_private) | |
276 | { | |
277 | int nr_probes = 0; | |
278 | struct marker_probe_closure *old, *new; | |
279 | ||
280 | WARN_ON(!probe); | |
281 | ||
282 | debug_print_probes(entry); | |
283 | old = entry->multi; | |
284 | if (!entry->ptype) { | |
285 | if (entry->single.func == probe && | |
286 | entry->single.probe_private == probe_private) | |
287 | return ERR_PTR(-EBUSY); | |
288 | if (entry->single.func == __mark_empty_function) { | |
289 | /* 0 -> 1 probes */ | |
290 | entry->single.func = probe; | |
291 | entry->single.probe_private = probe_private; | |
292 | entry->refcount = 1; | |
293 | entry->ptype = 0; | |
294 | debug_print_probes(entry); | |
295 | return NULL; | |
296 | } else { | |
297 | /* 1 -> 2 probes */ | |
298 | nr_probes = 1; | |
299 | old = NULL; | |
300 | } | |
301 | } else { | |
302 | /* (N -> N+1), (N != 0, 1) probes */ | |
303 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) | |
304 | if (old[nr_probes].func == probe | |
305 | && old[nr_probes].probe_private | |
306 | == probe_private) | |
307 | return ERR_PTR(-EBUSY); | |
308 | } | |
309 | /* + 2 : one for new probe, one for NULL func */ | |
310 | new = kzalloc((nr_probes + 2) * sizeof(struct marker_probe_closure), | |
311 | GFP_KERNEL); | |
312 | if (new == NULL) | |
313 | return ERR_PTR(-ENOMEM); | |
314 | if (!old) | |
315 | new[0] = entry->single; | |
316 | else | |
317 | memcpy(new, old, | |
318 | nr_probes * sizeof(struct marker_probe_closure)); | |
319 | new[nr_probes].func = probe; | |
320 | new[nr_probes].probe_private = probe_private; | |
321 | entry->refcount = nr_probes + 1; | |
322 | entry->multi = new; | |
323 | entry->ptype = 1; | |
324 | debug_print_probes(entry); | |
325 | return old; | |
326 | } | |
327 | ||
328 | static struct marker_probe_closure * | |
329 | marker_entry_remove_probe(struct marker_entry *entry, | |
330 | marker_probe_func *probe, void *probe_private) | |
331 | { | |
332 | int nr_probes = 0, nr_del = 0, i; | |
333 | struct marker_probe_closure *old, *new; | |
334 | ||
335 | old = entry->multi; | |
336 | ||
337 | debug_print_probes(entry); | |
338 | if (!entry->ptype) { | |
339 | /* 0 -> N is an error */ | |
340 | WARN_ON(entry->single.func == __mark_empty_function); | |
341 | /* 1 -> 0 probes */ | |
342 | WARN_ON(probe && entry->single.func != probe); | |
343 | WARN_ON(entry->single.probe_private != probe_private); | |
344 | entry->single.func = __mark_empty_function; | |
345 | entry->refcount = 0; | |
346 | entry->ptype = 0; | |
347 | debug_print_probes(entry); | |
348 | return NULL; | |
349 | } else { | |
350 | /* (N -> M), (N > 1, M >= 0) probes */ | |
351 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { | |
352 | if ((!probe || old[nr_probes].func == probe) | |
353 | && old[nr_probes].probe_private | |
354 | == probe_private) | |
355 | nr_del++; | |
356 | } | |
357 | } | |
358 | ||
359 | if (nr_probes - nr_del == 0) { | |
360 | /* N -> 0, (N > 1) */ | |
361 | entry->single.func = __mark_empty_function; | |
362 | entry->refcount = 0; | |
363 | entry->ptype = 0; | |
364 | } else if (nr_probes - nr_del == 1) { | |
365 | /* N -> 1, (N > 1) */ | |
366 | for (i = 0; old[i].func; i++) | |
367 | if ((probe && old[i].func != probe) || | |
368 | old[i].probe_private != probe_private) | |
369 | entry->single = old[i]; | |
370 | entry->refcount = 1; | |
371 | entry->ptype = 0; | |
372 | } else { | |
373 | int j = 0; | |
374 | /* N -> M, (N > 1, M > 1) */ | |
375 | /* + 1 for NULL */ | |
376 | new = kzalloc((nr_probes - nr_del + 1) | |
377 | * sizeof(struct marker_probe_closure), GFP_KERNEL); | |
378 | if (new == NULL) | |
379 | return ERR_PTR(-ENOMEM); | |
380 | for (i = 0; old[i].func; i++) | |
381 | if ((probe && old[i].func != probe) || | |
382 | old[i].probe_private != probe_private) | |
383 | new[j++] = old[i]; | |
384 | entry->refcount = nr_probes - nr_del; | |
385 | entry->ptype = 1; | |
386 | entry->multi = new; | |
387 | } | |
388 | debug_print_probes(entry); | |
389 | return old; | |
390 | } | |
391 | ||
392 | /* | |
393 | * Get marker if the marker is present in the marker hash table. | |
394 | * Must be called with markers_mutex held. | |
395 | * Returns NULL if not present. | |
396 | */ | |
397 | static struct marker_entry *get_marker(const char *channel, const char *name) | |
398 | { | |
399 | struct hlist_head *head; | |
400 | struct hlist_node *node; | |
401 | struct marker_entry *e; | |
402 | size_t channel_len = strlen(channel) + 1; | |
403 | size_t name_len = strlen(name) + 1; | |
404 | u32 hash; | |
405 | ||
406 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
407 | head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)]; | |
408 | hlist_for_each_entry(e, node, head, hlist) { | |
409 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) | |
410 | return e; | |
411 | } | |
412 | return NULL; | |
413 | } | |
414 | ||
415 | /* | |
416 | * Add the marker to the marker hash table. Must be called with markers_mutex | |
417 | * held. | |
418 | */ | |
419 | static struct marker_entry *add_marker(const char *channel, const char *name, | |
420 | const char *format) | |
421 | { | |
422 | struct hlist_head *head; | |
423 | struct hlist_node *node; | |
424 | struct marker_entry *e; | |
425 | size_t channel_len = strlen(channel) + 1; | |
426 | size_t name_len = strlen(name) + 1; | |
427 | size_t format_len = 0; | |
428 | u32 hash; | |
429 | ||
430 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
431 | if (format) | |
432 | format_len = strlen(format) + 1; | |
433 | head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)]; | |
434 | hlist_for_each_entry(e, node, head, hlist) { | |
435 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) { | |
436 | printk(KERN_NOTICE | |
437 | "Marker %s.%s busy\n", channel, name); | |
438 | return ERR_PTR(-EBUSY); /* Already there */ | |
439 | } | |
440 | } | |
441 | /* | |
442 | * Using kmalloc here to allocate a variable length element. Could | |
443 | * cause some memory fragmentation if overused. | |
444 | */ | |
445 | e = kmalloc(sizeof(struct marker_entry) | |
446 | + channel_len + name_len + format_len, | |
447 | GFP_KERNEL); | |
448 | if (!e) | |
449 | return ERR_PTR(-ENOMEM); | |
450 | memcpy(e->channel, channel, channel_len); | |
451 | e->name = &e->channel[channel_len]; | |
452 | memcpy(e->name, name, name_len); | |
453 | if (format) { | |
454 | e->format = &e->name[channel_len + name_len]; | |
455 | memcpy(e->format, format, format_len); | |
456 | if (strcmp(e->format, MARK_NOARGS) == 0) | |
457 | e->call = marker_probe_cb_noarg; | |
458 | else | |
459 | e->call = marker_probe_cb; | |
460 | trace_mark(metadata, core_marker_format, | |
461 | "channel %s name %s format %s", | |
462 | e->channel, e->name, e->format); | |
463 | } else { | |
464 | e->format = NULL; | |
465 | e->call = marker_probe_cb; | |
466 | } | |
467 | e->single.func = __mark_empty_function; | |
468 | e->single.probe_private = NULL; | |
469 | e->multi = NULL; | |
470 | e->ptype = 0; | |
471 | e->format_allocated = 0; | |
472 | e->refcount = 0; | |
473 | e->rcu_pending = 0; | |
474 | hlist_add_head(&e->hlist, head); | |
475 | return e; | |
476 | } | |
477 | ||
478 | /* | |
479 | * Remove the marker from the marker hash table. Must be called with mutex_lock | |
480 | * held. | |
481 | */ | |
482 | static int remove_marker(const char *channel, const char *name) | |
483 | { | |
484 | struct hlist_head *head; | |
485 | struct hlist_node *node; | |
486 | struct marker_entry *e; | |
487 | int found = 0; | |
488 | size_t channel_len = strlen(channel) + 1; | |
489 | size_t name_len = strlen(name) + 1; | |
490 | u32 hash; | |
491 | int ret; | |
492 | ||
493 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
494 | head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)]; | |
495 | hlist_for_each_entry(e, node, head, hlist) { | |
496 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) { | |
497 | found = 1; | |
498 | break; | |
499 | } | |
500 | } | |
501 | if (!found) | |
502 | return -ENOENT; | |
503 | if (e->single.func != __mark_empty_function) | |
504 | return -EBUSY; | |
505 | hlist_del(&e->hlist); | |
506 | if (e->format_allocated) | |
507 | kfree(e->format); | |
508 | ret = ltt_channels_unregister(e->channel); | |
509 | WARN_ON(ret); | |
510 | /* Make sure the call_rcu has been executed */ | |
6cb88bc0 PMF |
511 | //ust// if (e->rcu_pending) |
512 | //ust// rcu_barrier_sched(); | |
68c1021b PMF |
513 | kfree(e); |
514 | return 0; | |
515 | } | |
516 | ||
517 | /* | |
518 | * Set the mark_entry format to the format found in the element. | |
519 | */ | |
520 | static int marker_set_format(struct marker_entry *entry, const char *format) | |
521 | { | |
522 | entry->format = kstrdup(format, GFP_KERNEL); | |
523 | if (!entry->format) | |
524 | return -ENOMEM; | |
525 | entry->format_allocated = 1; | |
526 | ||
527 | trace_mark(metadata, core_marker_format, | |
528 | "channel %s name %s format %s", | |
529 | entry->channel, entry->name, entry->format); | |
530 | return 0; | |
531 | } | |
532 | ||
533 | /* | |
534 | * Sets the probe callback corresponding to one marker. | |
535 | */ | |
536 | static int set_marker(struct marker_entry *entry, struct marker *elem, | |
537 | int active) | |
538 | { | |
539 | int ret = 0; | |
540 | WARN_ON(strcmp(entry->name, elem->name) != 0); | |
541 | ||
542 | if (entry->format) { | |
543 | if (strcmp(entry->format, elem->format) != 0) { | |
544 | printk(KERN_NOTICE | |
545 | "Format mismatch for probe %s " | |
546 | "(%s), marker (%s)\n", | |
547 | entry->name, | |
548 | entry->format, | |
549 | elem->format); | |
550 | return -EPERM; | |
551 | } | |
552 | } else { | |
553 | ret = marker_set_format(entry, elem->format); | |
554 | if (ret) | |
555 | return ret; | |
556 | } | |
557 | ||
558 | /* | |
559 | * probe_cb setup (statically known) is done here. It is | |
560 | * asynchronous with the rest of execution, therefore we only | |
561 | * pass from a "safe" callback (with argument) to an "unsafe" | |
562 | * callback (does not set arguments). | |
563 | */ | |
564 | elem->call = entry->call; | |
565 | elem->channel_id = entry->channel_id; | |
566 | elem->event_id = entry->event_id; | |
567 | /* | |
568 | * Sanity check : | |
569 | * We only update the single probe private data when the ptr is | |
570 | * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1) | |
571 | */ | |
572 | WARN_ON(elem->single.func != __mark_empty_function | |
573 | && elem->single.probe_private != entry->single.probe_private | |
574 | && !elem->ptype); | |
575 | elem->single.probe_private = entry->single.probe_private; | |
576 | /* | |
577 | * Make sure the private data is valid when we update the | |
578 | * single probe ptr. | |
579 | */ | |
580 | smp_wmb(); | |
581 | elem->single.func = entry->single.func; | |
582 | /* | |
583 | * We also make sure that the new probe callbacks array is consistent | |
584 | * before setting a pointer to it. | |
585 | */ | |
586 | rcu_assign_pointer(elem->multi, entry->multi); | |
587 | /* | |
588 | * Update the function or multi probe array pointer before setting the | |
589 | * ptype. | |
590 | */ | |
591 | smp_wmb(); | |
592 | elem->ptype = entry->ptype; | |
593 | ||
59b161cd PMF |
594 | //ust// if (elem->tp_name && (active ^ _imv_read(elem->state))) { |
595 | //ust// WARN_ON(!elem->tp_cb); | |
596 | //ust// /* | |
597 | //ust// * It is ok to directly call the probe registration because type | |
598 | //ust// * checking has been done in the __trace_mark_tp() macro. | |
599 | //ust// */ | |
600 | //ust// | |
601 | //ust// if (active) { | |
602 | //ust// /* | |
603 | //ust// * try_module_get should always succeed because we hold | |
604 | //ust// * markers_mutex to get the tp_cb address. | |
605 | //ust// */ | |
606 | //ust// ret = try_module_get(__module_text_address( | |
607 | //ust// (unsigned long)elem->tp_cb)); | |
608 | //ust// BUG_ON(!ret); | |
609 | //ust// ret = tracepoint_probe_register_noupdate( | |
610 | //ust// elem->tp_name, | |
611 | //ust// elem->tp_cb); | |
612 | //ust// } else { | |
613 | //ust// ret = tracepoint_probe_unregister_noupdate( | |
614 | //ust// elem->tp_name, | |
615 | //ust// elem->tp_cb); | |
616 | //ust// /* | |
617 | //ust// * tracepoint_probe_update_all() must be called | |
618 | //ust// * before the module containing tp_cb is unloaded. | |
619 | //ust// */ | |
620 | //ust// module_put(__module_text_address( | |
621 | //ust// (unsigned long)elem->tp_cb)); | |
622 | //ust// } | |
623 | //ust// } | |
68c1021b PMF |
624 | elem->state__imv = active; |
625 | ||
626 | return ret; | |
627 | } | |
628 | ||
629 | /* | |
630 | * Disable a marker and its probe callback. | |
631 | * Note: only waiting an RCU period after setting elem->call to the empty | |
632 | * function insures that the original callback is not used anymore. This insured | |
633 | * by rcu_read_lock_sched around the call site. | |
634 | */ | |
635 | static void disable_marker(struct marker *elem) | |
636 | { | |
772030fe PMF |
637 | //ust// int ret; |
638 | //ust// | |
639 | //ust// /* leave "call" as is. It is known statically. */ | |
59b161cd PMF |
640 | //ust// if (elem->tp_name && _imv_read(elem->state)) { |
641 | //ust// WARN_ON(!elem->tp_cb); | |
642 | //ust// /* | |
643 | //ust// * It is ok to directly call the probe registration because type | |
644 | //ust// * checking has been done in the __trace_mark_tp() macro. | |
645 | //ust// */ | |
646 | //ust// ret = tracepoint_probe_unregister_noupdate(elem->tp_name, | |
647 | //ust// elem->tp_cb); | |
648 | //ust// WARN_ON(ret); | |
649 | //ust// /* | |
650 | //ust// * tracepoint_probe_update_all() must be called | |
651 | //ust// * before the module containing tp_cb is unloaded. | |
652 | //ust// */ | |
653 | //ust// module_put(__module_text_address((unsigned long)elem->tp_cb)); | |
654 | //ust// } | |
68c1021b PMF |
655 | elem->state__imv = 0; |
656 | elem->single.func = __mark_empty_function; | |
657 | /* Update the function before setting the ptype */ | |
658 | smp_wmb(); | |
659 | elem->ptype = 0; /* single probe */ | |
660 | /* | |
661 | * Leave the private data and channel_id/event_id there, because removal | |
662 | * is racy and should be done only after an RCU period. These are never | |
663 | * used until the next initialization anyway. | |
664 | */ | |
665 | } | |
666 | ||
667 | /** | |
668 | * marker_update_probe_range - Update a probe range | |
669 | * @begin: beginning of the range | |
670 | * @end: end of the range | |
671 | * | |
672 | * Updates the probe callback corresponding to a range of markers. | |
673 | */ | |
674 | void marker_update_probe_range(struct marker *begin, | |
675 | struct marker *end) | |
676 | { | |
677 | struct marker *iter; | |
678 | struct marker_entry *mark_entry; | |
679 | ||
680 | mutex_lock(&markers_mutex); | |
681 | for (iter = begin; iter < end; iter++) { | |
682 | mark_entry = get_marker(iter->channel, iter->name); | |
683 | if (mark_entry) { | |
684 | set_marker(mark_entry, iter, !!mark_entry->refcount); | |
685 | /* | |
686 | * ignore error, continue | |
687 | */ | |
4db647c5 PMF |
688 | |
689 | /* This is added for UST. We emit a core_marker_id event | |
690 | * for markers that are already registered to a probe | |
691 | * upon library load. Otherwise, no core_marker_id will | |
692 | * be generated for these markers. Is this the right thing | |
693 | * to do? | |
694 | */ | |
695 | trace_mark(metadata, core_marker_id, | |
696 | "channel %s name %s event_id %hu " | |
697 | "int #1u%zu long #1u%zu pointer #1u%zu " | |
698 | "size_t #1u%zu alignment #1u%u", | |
699 | iter->channel, iter->name, mark_entry->event_id, | |
700 | sizeof(int), sizeof(long), sizeof(void *), | |
701 | sizeof(size_t), ltt_get_alignment()); | |
68c1021b PMF |
702 | } else { |
703 | disable_marker(iter); | |
704 | } | |
705 | } | |
706 | mutex_unlock(&markers_mutex); | |
707 | } | |
708 | ||
772030fe PMF |
709 | static void lib_update_markers(void) |
710 | { | |
711 | struct lib *lib; | |
712 | ||
713 | /* FIXME: we should probably take a mutex here on libs */ | |
714 | //ust// mutex_lock(&module_mutex); | |
715 | list_for_each_entry(lib, &libs, list) | |
716 | marker_update_probe_range(lib->markers_start, | |
717 | lib->markers_start + lib->markers_count); | |
718 | //ust// mutex_unlock(&module_mutex); | |
719 | } | |
720 | ||
68c1021b PMF |
721 | /* |
722 | * Update probes, removing the faulty probes. | |
723 | * | |
724 | * Internal callback only changed before the first probe is connected to it. | |
725 | * Single probe private data can only be changed on 0 -> 1 and 2 -> 1 | |
726 | * transitions. All other transitions will leave the old private data valid. | |
727 | * This makes the non-atomicity of the callback/private data updates valid. | |
728 | * | |
729 | * "special case" updates : | |
730 | * 0 -> 1 callback | |
731 | * 1 -> 0 callback | |
732 | * 1 -> 2 callbacks | |
733 | * 2 -> 1 callbacks | |
734 | * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates. | |
735 | * Site effect : marker_set_format may delete the marker entry (creating a | |
736 | * replacement). | |
737 | */ | |
738 | static void marker_update_probes(void) | |
739 | { | |
740 | /* Core kernel markers */ | |
c463904d | 741 | //ust// marker_update_probe_range(__start___markers, __stop___markers); |
68c1021b | 742 | /* Markers in modules. */ |
59b161cd | 743 | //ust// module_update_markers(); |
c463904d | 744 | lib_update_markers(); |
b6bf28ec | 745 | //ust// tracepoint_probe_update_all(); |
68c1021b PMF |
746 | /* Update immediate values */ |
747 | core_imv_update(); | |
474d745f | 748 | //ust// module_imv_update(); /* FIXME: need to port for libs? */ |
68c1021b PMF |
749 | marker_update_processes(); |
750 | } | |
751 | ||
752 | /** | |
753 | * marker_probe_register - Connect a probe to a marker | |
754 | * @channel: marker channel | |
755 | * @name: marker name | |
756 | * @format: format string | |
757 | * @probe: probe handler | |
758 | * @probe_private: probe private data | |
759 | * | |
760 | * private data must be a valid allocated memory address, or NULL. | |
761 | * Returns 0 if ok, error value on error. | |
762 | * The probe address must at least be aligned on the architecture pointer size. | |
763 | */ | |
764 | int marker_probe_register(const char *channel, const char *name, | |
765 | const char *format, marker_probe_func *probe, | |
766 | void *probe_private) | |
767 | { | |
768 | struct marker_entry *entry; | |
769 | int ret = 0, ret_err; | |
770 | struct marker_probe_closure *old; | |
771 | int first_probe = 0; | |
772 | ||
773 | mutex_lock(&markers_mutex); | |
774 | entry = get_marker(channel, name); | |
775 | if (!entry) { | |
776 | first_probe = 1; | |
777 | entry = add_marker(channel, name, format); | |
778 | if (IS_ERR(entry)) | |
779 | ret = PTR_ERR(entry); | |
780 | if (ret) | |
781 | goto end; | |
782 | ret = ltt_channels_register(channel); | |
783 | if (ret) | |
784 | goto error_remove_marker; | |
785 | ret = ltt_channels_get_index_from_name(channel); | |
786 | if (ret < 0) | |
787 | goto error_unregister_channel; | |
788 | entry->channel_id = ret; | |
789 | ret = ltt_channels_get_event_id(channel, name); | |
790 | if (ret < 0) | |
791 | goto error_unregister_channel; | |
792 | entry->event_id = ret; | |
793 | ret = 0; | |
794 | trace_mark(metadata, core_marker_id, | |
795 | "channel %s name %s event_id %hu " | |
796 | "int #1u%zu long #1u%zu pointer #1u%zu " | |
797 | "size_t #1u%zu alignment #1u%u", | |
798 | channel, name, entry->event_id, | |
799 | sizeof(int), sizeof(long), sizeof(void *), | |
800 | sizeof(size_t), ltt_get_alignment()); | |
801 | } else if (format) { | |
802 | if (!entry->format) | |
803 | ret = marker_set_format(entry, format); | |
804 | else if (strcmp(entry->format, format)) | |
805 | ret = -EPERM; | |
806 | if (ret) | |
807 | goto end; | |
808 | } | |
809 | ||
810 | /* | |
811 | * If we detect that a call_rcu is pending for this marker, | |
812 | * make sure it's executed now. | |
813 | */ | |
6cb88bc0 PMF |
814 | //ust// if (entry->rcu_pending) |
815 | //ust// rcu_barrier_sched(); | |
68c1021b PMF |
816 | old = marker_entry_add_probe(entry, probe, probe_private); |
817 | if (IS_ERR(old)) { | |
818 | ret = PTR_ERR(old); | |
819 | if (first_probe) | |
820 | goto error_unregister_channel; | |
821 | else | |
822 | goto end; | |
823 | } | |
824 | mutex_unlock(&markers_mutex); | |
825 | ||
826 | marker_update_probes(); | |
827 | ||
828 | mutex_lock(&markers_mutex); | |
829 | entry = get_marker(channel, name); | |
830 | if (!entry) | |
831 | goto end; | |
6cb88bc0 PMF |
832 | //ust// if (entry->rcu_pending) |
833 | //ust// rcu_barrier_sched(); | |
68c1021b PMF |
834 | entry->oldptr = old; |
835 | entry->rcu_pending = 1; | |
836 | /* write rcu_pending before calling the RCU callback */ | |
837 | smp_wmb(); | |
6cb88bc0 PMF |
838 | //ust// call_rcu_sched(&entry->rcu, free_old_closure); |
839 | synchronize_rcu(); free_old_closure(&entry->rcu); | |
68c1021b PMF |
840 | goto end; |
841 | ||
842 | error_unregister_channel: | |
843 | ret_err = ltt_channels_unregister(channel); | |
844 | WARN_ON(ret_err); | |
845 | error_remove_marker: | |
846 | ret_err = remove_marker(channel, name); | |
847 | WARN_ON(ret_err); | |
848 | end: | |
849 | mutex_unlock(&markers_mutex); | |
850 | return ret; | |
851 | } | |
59b161cd | 852 | //ust// EXPORT_SYMBOL_GPL(marker_probe_register); |
68c1021b PMF |
853 | |
854 | /** | |
855 | * marker_probe_unregister - Disconnect a probe from a marker | |
856 | * @channel: marker channel | |
857 | * @name: marker name | |
858 | * @probe: probe function pointer | |
859 | * @probe_private: probe private data | |
860 | * | |
861 | * Returns the private data given to marker_probe_register, or an ERR_PTR(). | |
862 | * We do not need to call a synchronize_sched to make sure the probes have | |
863 | * finished running before doing a module unload, because the module unload | |
864 | * itself uses stop_machine(), which insures that every preempt disabled section | |
865 | * have finished. | |
866 | */ | |
867 | int marker_probe_unregister(const char *channel, const char *name, | |
868 | marker_probe_func *probe, void *probe_private) | |
869 | { | |
870 | struct marker_entry *entry; | |
871 | struct marker_probe_closure *old; | |
872 | int ret = -ENOENT; | |
873 | ||
874 | mutex_lock(&markers_mutex); | |
875 | entry = get_marker(channel, name); | |
876 | if (!entry) | |
877 | goto end; | |
6cb88bc0 PMF |
878 | //ust// if (entry->rcu_pending) |
879 | //ust// rcu_barrier_sched(); | |
68c1021b PMF |
880 | old = marker_entry_remove_probe(entry, probe, probe_private); |
881 | mutex_unlock(&markers_mutex); | |
882 | ||
883 | marker_update_probes(); | |
884 | ||
885 | mutex_lock(&markers_mutex); | |
886 | entry = get_marker(channel, name); | |
887 | if (!entry) | |
888 | goto end; | |
6cb88bc0 PMF |
889 | //ust// if (entry->rcu_pending) |
890 | //ust// rcu_barrier_sched(); | |
68c1021b PMF |
891 | entry->oldptr = old; |
892 | entry->rcu_pending = 1; | |
893 | /* write rcu_pending before calling the RCU callback */ | |
894 | smp_wmb(); | |
6cb88bc0 PMF |
895 | //ust// call_rcu_sched(&entry->rcu, free_old_closure); |
896 | synchronize_rcu(); free_old_closure(&entry->rcu); | |
68c1021b PMF |
897 | remove_marker(channel, name); /* Ignore busy error message */ |
898 | ret = 0; | |
899 | end: | |
900 | mutex_unlock(&markers_mutex); | |
901 | return ret; | |
902 | } | |
59b161cd | 903 | //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister); |
68c1021b PMF |
904 | |
905 | static struct marker_entry * | |
906 | get_marker_from_private_data(marker_probe_func *probe, void *probe_private) | |
907 | { | |
908 | struct marker_entry *entry; | |
909 | unsigned int i; | |
910 | struct hlist_head *head; | |
911 | struct hlist_node *node; | |
912 | ||
913 | for (i = 0; i < MARKER_TABLE_SIZE; i++) { | |
914 | head = &marker_table[i]; | |
915 | hlist_for_each_entry(entry, node, head, hlist) { | |
916 | if (!entry->ptype) { | |
917 | if (entry->single.func == probe | |
918 | && entry->single.probe_private | |
919 | == probe_private) | |
920 | return entry; | |
921 | } else { | |
922 | struct marker_probe_closure *closure; | |
923 | closure = entry->multi; | |
924 | for (i = 0; closure[i].func; i++) { | |
925 | if (closure[i].func == probe && | |
926 | closure[i].probe_private | |
927 | == probe_private) | |
928 | return entry; | |
929 | } | |
930 | } | |
931 | } | |
932 | } | |
933 | return NULL; | |
934 | } | |
935 | ||
936 | /** | |
937 | * marker_probe_unregister_private_data - Disconnect a probe from a marker | |
938 | * @probe: probe function | |
939 | * @probe_private: probe private data | |
940 | * | |
941 | * Unregister a probe by providing the registered private data. | |
942 | * Only removes the first marker found in hash table. | |
943 | * Return 0 on success or error value. | |
944 | * We do not need to call a synchronize_sched to make sure the probes have | |
945 | * finished running before doing a module unload, because the module unload | |
946 | * itself uses stop_machine(), which insures that every preempt disabled section | |
947 | * have finished. | |
948 | */ | |
949 | int marker_probe_unregister_private_data(marker_probe_func *probe, | |
950 | void *probe_private) | |
951 | { | |
952 | struct marker_entry *entry; | |
953 | int ret = 0; | |
954 | struct marker_probe_closure *old; | |
955 | const char *channel = NULL, *name = NULL; | |
956 | ||
957 | mutex_lock(&markers_mutex); | |
958 | entry = get_marker_from_private_data(probe, probe_private); | |
959 | if (!entry) { | |
960 | ret = -ENOENT; | |
961 | goto end; | |
962 | } | |
6cb88bc0 PMF |
963 | //ust// if (entry->rcu_pending) |
964 | //ust// rcu_barrier_sched(); | |
68c1021b PMF |
965 | old = marker_entry_remove_probe(entry, NULL, probe_private); |
966 | channel = kstrdup(entry->channel, GFP_KERNEL); | |
967 | name = kstrdup(entry->name, GFP_KERNEL); | |
968 | mutex_unlock(&markers_mutex); | |
969 | ||
970 | marker_update_probes(); | |
971 | ||
972 | mutex_lock(&markers_mutex); | |
973 | entry = get_marker(channel, name); | |
974 | if (!entry) | |
975 | goto end; | |
6cb88bc0 PMF |
976 | //ust// if (entry->rcu_pending) |
977 | //ust// rcu_barrier_sched(); | |
68c1021b PMF |
978 | entry->oldptr = old; |
979 | entry->rcu_pending = 1; | |
980 | /* write rcu_pending before calling the RCU callback */ | |
981 | smp_wmb(); | |
6cb88bc0 PMF |
982 | //ust// call_rcu_sched(&entry->rcu, free_old_closure); |
983 | synchronize_rcu(); free_old_closure(&entry->rcu); | |
68c1021b PMF |
984 | /* Ignore busy error message */ |
985 | remove_marker(channel, name); | |
986 | end: | |
987 | mutex_unlock(&markers_mutex); | |
988 | kfree(channel); | |
989 | kfree(name); | |
990 | return ret; | |
991 | } | |
59b161cd | 992 | //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data); |
68c1021b PMF |
993 | |
994 | /** | |
995 | * marker_get_private_data - Get a marker's probe private data | |
996 | * @channel: marker channel | |
997 | * @name: marker name | |
998 | * @probe: probe to match | |
999 | * @num: get the nth matching probe's private data | |
1000 | * | |
1001 | * Returns the nth private data pointer (starting from 0) matching, or an | |
1002 | * ERR_PTR. | |
1003 | * Returns the private data pointer, or an ERR_PTR. | |
1004 | * The private data pointer should _only_ be dereferenced if the caller is the | |
1005 | * owner of the data, or its content could vanish. This is mostly used to | |
1006 | * confirm that a caller is the owner of a registered probe. | |
1007 | */ | |
1008 | void *marker_get_private_data(const char *channel, const char *name, | |
1009 | marker_probe_func *probe, int num) | |
1010 | { | |
1011 | struct hlist_head *head; | |
1012 | struct hlist_node *node; | |
1013 | struct marker_entry *e; | |
1014 | size_t channel_len = strlen(channel) + 1; | |
1015 | size_t name_len = strlen(name) + 1; | |
1016 | int i; | |
1017 | u32 hash; | |
1018 | ||
1019 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
1020 | head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)]; | |
1021 | hlist_for_each_entry(e, node, head, hlist) { | |
1022 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) { | |
1023 | if (!e->ptype) { | |
1024 | if (num == 0 && e->single.func == probe) | |
1025 | return e->single.probe_private; | |
1026 | } else { | |
1027 | struct marker_probe_closure *closure; | |
1028 | int match = 0; | |
1029 | closure = e->multi; | |
1030 | for (i = 0; closure[i].func; i++) { | |
1031 | if (closure[i].func != probe) | |
1032 | continue; | |
1033 | if (match++ == num) | |
1034 | return closure[i].probe_private; | |
1035 | } | |
1036 | } | |
1037 | break; | |
1038 | } | |
1039 | } | |
1040 | return ERR_PTR(-ENOENT); | |
1041 | } | |
59b161cd | 1042 | //ust// EXPORT_SYMBOL_GPL(marker_get_private_data); |
68c1021b PMF |
1043 | |
1044 | /** | |
1045 | * markers_compact_event_ids - Compact markers event IDs and reassign channels | |
1046 | * | |
1047 | * Called when no channel users are active by the channel infrastructure. | |
1048 | * Called with lock_markers() and channel mutex held. | |
1049 | */ | |
59b161cd PMF |
1050 | //ust// void markers_compact_event_ids(void) |
1051 | //ust// { | |
1052 | //ust// struct marker_entry *entry; | |
1053 | //ust// unsigned int i; | |
1054 | //ust// struct hlist_head *head; | |
1055 | //ust// struct hlist_node *node; | |
1056 | //ust// int ret; | |
1057 | //ust// | |
1058 | //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) { | |
1059 | //ust// head = &marker_table[i]; | |
1060 | //ust// hlist_for_each_entry(entry, node, head, hlist) { | |
1061 | //ust// ret = ltt_channels_get_index_from_name(entry->channel); | |
1062 | //ust// WARN_ON(ret < 0); | |
1063 | //ust// entry->channel_id = ret; | |
1064 | //ust// ret = _ltt_channels_get_event_id(entry->channel, | |
1065 | //ust// entry->name); | |
1066 | //ust// WARN_ON(ret < 0); | |
1067 | //ust// entry->event_id = ret; | |
1068 | //ust// } | |
1069 | //ust// } | |
1070 | //ust// } | |
68c1021b | 1071 | |
9c67dc50 | 1072 | //ust//#ifdef CONFIG_MODULES |
68c1021b | 1073 | |
772030fe PMF |
1074 | /* |
1075 | * Returns 0 if current not found. | |
1076 | * Returns 1 if current found. | |
1077 | */ | |
1078 | int lib_get_iter_markers(struct marker_iter *iter) | |
1079 | { | |
1080 | struct lib *iter_lib; | |
1081 | int found = 0; | |
1082 | ||
1083 | //ust// mutex_lock(&module_mutex); | |
1084 | list_for_each_entry(iter_lib, &libs, list) { | |
1085 | if (iter_lib < iter->lib) | |
1086 | continue; | |
1087 | else if (iter_lib > iter->lib) | |
1088 | iter->marker = NULL; | |
1089 | found = marker_get_iter_range(&iter->marker, | |
1090 | iter_lib->markers_start, | |
1091 | iter_lib->markers_start + iter_lib->markers_count); | |
1092 | if (found) { | |
1093 | iter->lib = iter_lib; | |
1094 | break; | |
1095 | } | |
1096 | } | |
1097 | //ust// mutex_unlock(&module_mutex); | |
1098 | return found; | |
1099 | } | |
1100 | ||
68c1021b PMF |
1101 | /** |
1102 | * marker_get_iter_range - Get a next marker iterator given a range. | |
1103 | * @marker: current markers (in), next marker (out) | |
1104 | * @begin: beginning of the range | |
1105 | * @end: end of the range | |
1106 | * | |
1107 | * Returns whether a next marker has been found (1) or not (0). | |
1108 | * Will return the first marker in the range if the input marker is NULL. | |
1109 | */ | |
1110 | int marker_get_iter_range(struct marker **marker, struct marker *begin, | |
1111 | struct marker *end) | |
1112 | { | |
1113 | if (!*marker && begin != end) { | |
1114 | *marker = begin; | |
1115 | return 1; | |
1116 | } | |
1117 | if (*marker >= begin && *marker < end) | |
1118 | return 1; | |
1119 | return 0; | |
1120 | } | |
59b161cd | 1121 | //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range); |
68c1021b PMF |
1122 | |
1123 | static void marker_get_iter(struct marker_iter *iter) | |
1124 | { | |
1125 | int found = 0; | |
1126 | ||
1127 | /* Core kernel markers */ | |
98963de4 | 1128 | if (!iter->lib) { |
c463904d | 1129 | /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */ |
68c1021b PMF |
1130 | found = marker_get_iter_range(&iter->marker, |
1131 | __start___markers, __stop___markers); | |
1132 | if (found) | |
1133 | goto end; | |
1134 | } | |
1135 | /* Markers in modules. */ | |
98963de4 | 1136 | found = lib_get_iter_markers(iter); |
68c1021b PMF |
1137 | end: |
1138 | if (!found) | |
1139 | marker_iter_reset(iter); | |
1140 | } | |
1141 | ||
1142 | void marker_iter_start(struct marker_iter *iter) | |
1143 | { | |
1144 | marker_get_iter(iter); | |
1145 | } | |
59b161cd | 1146 | //ust// EXPORT_SYMBOL_GPL(marker_iter_start); |
68c1021b PMF |
1147 | |
1148 | void marker_iter_next(struct marker_iter *iter) | |
1149 | { | |
1150 | iter->marker++; | |
1151 | /* | |
1152 | * iter->marker may be invalid because we blindly incremented it. | |
1153 | * Make sure it is valid by marshalling on the markers, getting the | |
1154 | * markers from following modules if necessary. | |
1155 | */ | |
1156 | marker_get_iter(iter); | |
1157 | } | |
59b161cd | 1158 | //ust// EXPORT_SYMBOL_GPL(marker_iter_next); |
68c1021b PMF |
1159 | |
1160 | void marker_iter_stop(struct marker_iter *iter) | |
1161 | { | |
1162 | } | |
59b161cd | 1163 | //ust// EXPORT_SYMBOL_GPL(marker_iter_stop); |
68c1021b PMF |
1164 | |
1165 | void marker_iter_reset(struct marker_iter *iter) | |
1166 | { | |
98963de4 | 1167 | iter->lib = NULL; |
68c1021b PMF |
1168 | iter->marker = NULL; |
1169 | } | |
59b161cd | 1170 | //ust// EXPORT_SYMBOL_GPL(marker_iter_reset); |
68c1021b PMF |
1171 | |
1172 | #ifdef CONFIG_MARKERS_USERSPACE | |
1173 | /* | |
1174 | * must be called with current->user_markers_mutex held | |
1175 | */ | |
1176 | static void free_user_marker(char __user *state, struct hlist_head *head) | |
1177 | { | |
1178 | struct user_marker *umark; | |
1179 | struct hlist_node *pos, *n; | |
1180 | ||
1181 | hlist_for_each_entry_safe(umark, pos, n, head, hlist) { | |
1182 | if (umark->state == state) { | |
1183 | hlist_del(&umark->hlist); | |
1184 | kfree(umark); | |
1185 | } | |
1186 | } | |
1187 | } | |
1188 | ||
59b161cd PMF |
1189 | //ust// asmlinkage long sys_marker(char __user *name, char __user *format, |
1190 | //ust// char __user *state, int reg) | |
1191 | //ust// { | |
1192 | //ust// struct user_marker *umark; | |
1193 | //ust// long len; | |
1194 | //ust// struct marker_entry *entry; | |
1195 | //ust// int ret = 0; | |
1196 | //ust// | |
1197 | //ust// printk(KERN_DEBUG "Program %s %s marker [%p, %p]\n", | |
1198 | //ust// current->comm, reg ? "registers" : "unregisters", | |
1199 | //ust// name, state); | |
1200 | //ust// if (reg) { | |
1201 | //ust// umark = kmalloc(sizeof(struct user_marker), GFP_KERNEL); | |
1202 | //ust// umark->name[MAX_USER_MARKER_NAME_LEN - 1] = '\0'; | |
1203 | //ust// umark->format[MAX_USER_MARKER_FORMAT_LEN - 1] = '\0'; | |
1204 | //ust// umark->state = state; | |
1205 | //ust// len = strncpy_from_user(umark->name, name, | |
1206 | //ust// MAX_USER_MARKER_NAME_LEN - 1); | |
1207 | //ust// if (len < 0) { | |
1208 | //ust// ret = -EFAULT; | |
1209 | //ust// goto error; | |
1210 | //ust// } | |
1211 | //ust// len = strncpy_from_user(umark->format, format, | |
1212 | //ust// MAX_USER_MARKER_FORMAT_LEN - 1); | |
1213 | //ust// if (len < 0) { | |
1214 | //ust// ret = -EFAULT; | |
1215 | //ust// goto error; | |
1216 | //ust// } | |
1217 | //ust// printk(KERN_DEBUG "Marker name : %s, format : %s", umark->name, | |
1218 | //ust// umark->format); | |
1219 | //ust// mutex_lock(&markers_mutex); | |
1220 | //ust// entry = get_marker("userspace", umark->name); | |
1221 | //ust// if (entry) { | |
1222 | //ust// if (entry->format && | |
1223 | //ust// strcmp(entry->format, umark->format) != 0) { | |
1224 | //ust// printk(" error, wrong format in process %s", | |
1225 | //ust// current->comm); | |
1226 | //ust// ret = -EPERM; | |
1227 | //ust// goto error_unlock; | |
1228 | //ust// } | |
1229 | //ust// printk(" %s", !!entry->refcount | |
1230 | //ust// ? "enabled" : "disabled"); | |
1231 | //ust// if (put_user(!!entry->refcount, state)) { | |
1232 | //ust// ret = -EFAULT; | |
1233 | //ust// goto error_unlock; | |
1234 | //ust// } | |
1235 | //ust// printk("\n"); | |
1236 | //ust// } else { | |
1237 | //ust// printk(" disabled\n"); | |
1238 | //ust// if (put_user(0, umark->state)) { | |
1239 | //ust// printk(KERN_WARNING | |
1240 | //ust// "Marker in %s caused a fault\n", | |
1241 | //ust// current->comm); | |
1242 | //ust// goto error_unlock; | |
1243 | //ust// } | |
1244 | //ust// } | |
1245 | //ust// mutex_lock(¤t->group_leader->user_markers_mutex); | |
1246 | //ust// hlist_add_head(&umark->hlist, | |
1247 | //ust// ¤t->group_leader->user_markers); | |
1248 | //ust// current->group_leader->user_markers_sequence++; | |
1249 | //ust// mutex_unlock(¤t->group_leader->user_markers_mutex); | |
1250 | //ust// mutex_unlock(&markers_mutex); | |
1251 | //ust// } else { | |
1252 | //ust// mutex_lock(¤t->group_leader->user_markers_mutex); | |
1253 | //ust// free_user_marker(state, | |
1254 | //ust// ¤t->group_leader->user_markers); | |
1255 | //ust// current->group_leader->user_markers_sequence++; | |
1256 | //ust// mutex_unlock(¤t->group_leader->user_markers_mutex); | |
1257 | //ust// } | |
1258 | //ust// goto end; | |
1259 | //ust// error_unlock: | |
1260 | //ust// mutex_unlock(&markers_mutex); | |
1261 | //ust// error: | |
1262 | //ust// kfree(umark); | |
1263 | //ust// end: | |
1264 | //ust// return ret; | |
1265 | //ust// } | |
1266 | //ust// | |
1267 | //ust// /* | |
1268 | //ust// * Types : | |
1269 | //ust// * string : 0 | |
1270 | //ust// */ | |
1271 | //ust// asmlinkage long sys_trace(int type, uint16_t id, | |
1272 | //ust// char __user *ubuf) | |
1273 | //ust// { | |
1274 | //ust// long ret = -EPERM; | |
1275 | //ust// char *page; | |
1276 | //ust// int len; | |
1277 | //ust// | |
1278 | //ust// switch (type) { | |
1279 | //ust// case 0: /* String */ | |
1280 | //ust// ret = -ENOMEM; | |
1281 | //ust// page = (char *)__get_free_page(GFP_TEMPORARY); | |
1282 | //ust// if (!page) | |
1283 | //ust// goto string_out; | |
1284 | //ust// len = strncpy_from_user(page, ubuf, PAGE_SIZE); | |
1285 | //ust// if (len < 0) { | |
1286 | //ust// ret = -EFAULT; | |
1287 | //ust// goto string_err; | |
1288 | //ust// } | |
1289 | //ust// trace_mark(userspace, string, "string %s", page); | |
1290 | //ust// string_err: | |
1291 | //ust// free_page((unsigned long) page); | |
1292 | //ust// string_out: | |
1293 | //ust// break; | |
1294 | //ust// default: | |
1295 | //ust// break; | |
1296 | //ust// } | |
1297 | //ust// return ret; | |
1298 | //ust// } | |
1299 | ||
1300 | //ust// static void marker_update_processes(void) | |
1301 | //ust// { | |
1302 | //ust// struct task_struct *g, *t; | |
1303 | //ust// | |
1304 | //ust// /* | |
1305 | //ust// * markers_mutex is taken to protect the p->user_markers read. | |
1306 | //ust// */ | |
1307 | //ust// mutex_lock(&markers_mutex); | |
1308 | //ust// read_lock(&tasklist_lock); | |
1309 | //ust// for_each_process(g) { | |
1310 | //ust// WARN_ON(!thread_group_leader(g)); | |
1311 | //ust// if (hlist_empty(&g->user_markers)) | |
1312 | //ust// continue; | |
1313 | //ust// if (strcmp(g->comm, "testprog") == 0) | |
1314 | //ust// printk(KERN_DEBUG "set update pending for testprog\n"); | |
1315 | //ust// t = g; | |
1316 | //ust// do { | |
1317 | //ust// /* TODO : implement this thread flag in each arch. */ | |
1318 | //ust// set_tsk_thread_flag(t, TIF_MARKER_PENDING); | |
1319 | //ust// } while ((t = next_thread(t)) != g); | |
1320 | //ust// } | |
1321 | //ust// read_unlock(&tasklist_lock); | |
1322 | //ust// mutex_unlock(&markers_mutex); | |
1323 | //ust// } | |
68c1021b PMF |
1324 | |
1325 | /* | |
1326 | * Update current process. | |
1327 | * Note that we have to wait a whole scheduler period before we are sure that | |
1328 | * every running userspace threads have their markers updated. | |
1329 | * (synchronize_sched() can be used to insure this). | |
1330 | */ | |
1331 | void marker_update_process(void) | |
1332 | { | |
1333 | struct user_marker *umark; | |
1334 | struct hlist_node *pos; | |
1335 | struct marker_entry *entry; | |
1336 | ||
1337 | mutex_lock(&markers_mutex); | |
1338 | mutex_lock(¤t->group_leader->user_markers_mutex); | |
1339 | if (strcmp(current->comm, "testprog") == 0) | |
1340 | printk(KERN_DEBUG "do update pending for testprog\n"); | |
1341 | hlist_for_each_entry(umark, pos, | |
1342 | ¤t->group_leader->user_markers, hlist) { | |
1343 | printk(KERN_DEBUG "Updating marker %s in %s\n", | |
1344 | umark->name, current->comm); | |
1345 | entry = get_marker("userspace", umark->name); | |
1346 | if (entry) { | |
1347 | if (entry->format && | |
1348 | strcmp(entry->format, umark->format) != 0) { | |
1349 | printk(KERN_WARNING | |
1350 | " error, wrong format in process %s\n", | |
1351 | current->comm); | |
1352 | break; | |
1353 | } | |
1354 | if (put_user(!!entry->refcount, umark->state)) { | |
1355 | printk(KERN_WARNING | |
1356 | "Marker in %s caused a fault\n", | |
1357 | current->comm); | |
1358 | break; | |
1359 | } | |
1360 | } else { | |
1361 | if (put_user(0, umark->state)) { | |
1362 | printk(KERN_WARNING | |
1363 | "Marker in %s caused a fault\n", | |
1364 | current->comm); | |
1365 | break; | |
1366 | } | |
1367 | } | |
1368 | } | |
1369 | clear_thread_flag(TIF_MARKER_PENDING); | |
1370 | mutex_unlock(¤t->group_leader->user_markers_mutex); | |
1371 | mutex_unlock(&markers_mutex); | |
1372 | } | |
1373 | ||
1374 | /* | |
1375 | * Called at process exit and upon do_execve(). | |
1376 | * We assume that when the leader exits, no more references can be done to the | |
1377 | * leader structure by the other threads. | |
1378 | */ | |
1379 | void exit_user_markers(struct task_struct *p) | |
1380 | { | |
1381 | struct user_marker *umark; | |
1382 | struct hlist_node *pos, *n; | |
1383 | ||
1384 | if (thread_group_leader(p)) { | |
1385 | mutex_lock(&markers_mutex); | |
1386 | mutex_lock(&p->user_markers_mutex); | |
1387 | hlist_for_each_entry_safe(umark, pos, n, &p->user_markers, | |
1388 | hlist) | |
1389 | kfree(umark); | |
1390 | INIT_HLIST_HEAD(&p->user_markers); | |
1391 | p->user_markers_sequence++; | |
1392 | mutex_unlock(&p->user_markers_mutex); | |
1393 | mutex_unlock(&markers_mutex); | |
1394 | } | |
1395 | } | |
1396 | ||
1397 | int is_marker_enabled(const char *channel, const char *name) | |
1398 | { | |
1399 | struct marker_entry *entry; | |
1400 | ||
1401 | mutex_lock(&markers_mutex); | |
1402 | entry = get_marker(channel, name); | |
1403 | mutex_unlock(&markers_mutex); | |
1404 | ||
1405 | return entry && !!entry->refcount; | |
1406 | } | |
9c67dc50 | 1407 | //ust// #endif |
68c1021b PMF |
1408 | |
1409 | int marker_module_notify(struct notifier_block *self, | |
1410 | unsigned long val, void *data) | |
1411 | { | |
1412 | struct module *mod = data; | |
1413 | ||
1414 | switch (val) { | |
1415 | case MODULE_STATE_COMING: | |
1416 | marker_update_probe_range(mod->markers, | |
1417 | mod->markers + mod->num_markers); | |
1418 | break; | |
1419 | case MODULE_STATE_GOING: | |
1420 | marker_update_probe_range(mod->markers, | |
1421 | mod->markers + mod->num_markers); | |
1422 | break; | |
1423 | } | |
1424 | return 0; | |
1425 | } | |
1426 | ||
1427 | struct notifier_block marker_module_nb = { | |
1428 | .notifier_call = marker_module_notify, | |
1429 | .priority = 0, | |
1430 | }; | |
1431 | ||
59b161cd PMF |
1432 | //ust// static int init_markers(void) |
1433 | //ust// { | |
1434 | //ust// return register_module_notifier(&marker_module_nb); | |
1435 | //ust// } | |
1436 | //ust// __initcall(init_markers); | |
1437 | /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */ | |
68c1021b PMF |
1438 | |
1439 | #endif /* CONFIG_MODULES */ | |
1440 | ||
9c67dc50 PMF |
1441 | void ltt_dump_marker_state(struct ltt_trace_struct *trace) |
1442 | { | |
48454c95 | 1443 | struct marker_entry *entry; |
9c67dc50 | 1444 | struct ltt_probe_private_data call_data; |
48454c95 PMF |
1445 | struct hlist_head *head; |
1446 | struct hlist_node *node; | |
1447 | unsigned int i; | |
9c67dc50 | 1448 | |
48454c95 | 1449 | mutex_lock(&markers_mutex); |
9c67dc50 PMF |
1450 | call_data.trace = trace; |
1451 | call_data.serializer = NULL; | |
1452 | ||
48454c95 PMF |
1453 | for (i = 0; i < MARKER_TABLE_SIZE; i++) { |
1454 | head = &marker_table[i]; | |
1455 | hlist_for_each_entry(entry, node, head, hlist) { | |
1456 | __trace_mark(0, metadata, core_marker_id, | |
9c67dc50 | 1457 | &call_data, |
48454c95 PMF |
1458 | "channel %s name %s event_id %hu " |
1459 | "int #1u%zu long #1u%zu pointer #1u%zu " | |
1460 | "size_t #1u%zu alignment #1u%u", | |
1461 | entry->channel, | |
1462 | entry->name, | |
1463 | entry->event_id, | |
1464 | sizeof(int), sizeof(long), | |
1465 | sizeof(void *), sizeof(size_t), | |
1466 | ltt_get_alignment()); | |
1467 | if (entry->format) | |
1468 | __trace_mark(0, metadata, | |
1469 | core_marker_format, | |
1470 | &call_data, | |
1471 | "channel %s name %s format %s", | |
1472 | entry->channel, | |
1473 | entry->name, | |
1474 | entry->format); | |
1475 | } | |
9c67dc50 | 1476 | } |
48454c95 | 1477 | mutex_unlock(&markers_mutex); |
9c67dc50 | 1478 | } |
59b161cd | 1479 | //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state); |
98963de4 | 1480 | |
20b37a31 PMF |
1481 | static void (*new_marker_cb)(struct marker *) = NULL; |
1482 | ||
1483 | void marker_set_new_marker_cb(void (*cb)(struct marker *)) | |
1484 | { | |
1485 | new_marker_cb = cb; | |
1486 | } | |
1487 | ||
1488 | static void new_markers(struct marker *start, struct marker *end) | |
1489 | { | |
1490 | if(new_marker_cb) { | |
1491 | struct marker *m; | |
1492 | for(m=start; m < end; m++) { | |
1493 | new_marker_cb(m); | |
1494 | } | |
1495 | } | |
1496 | } | |
1497 | ||
98963de4 PMF |
1498 | int marker_register_lib(struct marker *markers_start, int markers_count) |
1499 | { | |
1500 | struct lib *pl; | |
1501 | ||
1502 | pl = (struct lib *) malloc(sizeof(struct lib)); | |
1503 | ||
1504 | pl->markers_start = markers_start; | |
1505 | pl->markers_count = markers_count; | |
1506 | ||
0b5207fa PMF |
1507 | /* FIXME: maybe protect this with its own mutex? */ |
1508 | lock_markers(); | |
98963de4 | 1509 | list_add(&pl->list, &libs); |
0b5207fa | 1510 | unlock_markers(); |
98963de4 | 1511 | |
20b37a31 PMF |
1512 | new_markers(markers_start, markers_start + markers_count); |
1513 | ||
4db647c5 PMF |
1514 | /* FIXME: update just the loaded lib */ |
1515 | lib_update_markers(); | |
1516 | ||
1c184644 | 1517 | DBG("just registered a markers section from %p and having %d markers", markers_start, markers_count); |
98963de4 PMF |
1518 | |
1519 | return 0; | |
1520 | } | |
c463904d | 1521 | |
0b5207fa PMF |
1522 | int marker_unregister_lib(struct marker *markers_start, int markers_count) |
1523 | { | |
1524 | /*FIXME: implement; but before implementing, marker_register_lib must | |
1525 | have appropriate locking. */ | |
1526 | ||
1527 | return 0; | |
1528 | } | |
1529 | ||
4db647c5 PMF |
1530 | static int initialized = 0; |
1531 | ||
1532 | void __attribute__((constructor)) init_markers(void) | |
c463904d | 1533 | { |
4db647c5 PMF |
1534 | if(!initialized) { |
1535 | marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker)); | |
4d65ee62 | 1536 | DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers); |
4db647c5 PMF |
1537 | initialized = 1; |
1538 | } | |
c463904d | 1539 | } |