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