Large cleanup, mostly removal of all printk's and printfs in libust
[ust.git] / libust / marker.c
1 /*
2 * Copyright (C) 2007 Mathieu Desnoyers
3 *
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.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
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
17 */
18 //ust// #include <linux/module.h>
19 //ust// #include <linux/mutex.h>
20 //ust// #include <linux/types.h>
21 //#include "jhash.h"
22 //#include "list.h"
23 //#include "rcupdate.h"
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
33 #define _LGPL_SOURCE
34 #include <urcu-bp.h>
35
36 #include <ust/kernelcompat.h>
37
38 #include <ust/marker.h>
39 #include "usterr.h"
40 #include "channels.h"
41 #include "tracercore.h"
42 #include "tracer.h"
43
44 __thread long ust_reg_stack[500];
45 volatile __thread long *ust_reg_stack_ptr = (long *) 0;
46
47 extern struct marker __start___markers[] __attribute__((visibility("hidden")));
48 extern struct marker __stop___markers[] __attribute__((visibility("hidden")));
49
50 /* Set to 1 to enable marker debug output */
51 static const int marker_debug;
52
53 /*
54 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
55 * and module markers and the hash table.
56 */
57 static DEFINE_MUTEX(markers_mutex);
58
59 static LIST_HEAD(libs);
60
61
62 void lock_markers(void)
63 {
64 mutex_lock(&markers_mutex);
65 }
66
67 void unlock_markers(void)
68 {
69 mutex_unlock(&markers_mutex);
70 }
71
72 /*
73 * Marker hash table, containing the active markers.
74 * Protected by module_mutex.
75 */
76 #define MARKER_HASH_BITS 6
77 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
78 static struct hlist_head marker_table[MARKER_TABLE_SIZE];
79
80 /*
81 * Note about RCU :
82 * It is used to make sure every handler has finished using its private data
83 * between two consecutive operation (add or remove) on a given marker. It is
84 * also used to delay the free of multiple probes array until a quiescent state
85 * is reached.
86 * marker entries modifications are protected by the markers_mutex.
87 */
88 struct marker_entry {
89 struct hlist_node hlist;
90 char *format;
91 char *name;
92 /* Probe wrapper */
93 void (*call)(const struct marker *mdata, void *call_private, struct registers *regs, ...);
94 struct marker_probe_closure single;
95 struct marker_probe_closure *multi;
96 int refcount; /* Number of times armed. 0 if disarmed. */
97 struct rcu_head rcu;
98 void *oldptr;
99 int rcu_pending;
100 u16 channel_id;
101 u16 event_id;
102 unsigned char ptype:1;
103 unsigned char format_allocated:1;
104 char channel[0]; /* Contains channel'\0'name'\0'format'\0' */
105 };
106
107 #ifdef CONFIG_MARKERS_USERSPACE
108 static void marker_update_processes(void);
109 #else
110 static void marker_update_processes(void)
111 {
112 }
113 #endif
114
115 /**
116 * __mark_empty_function - Empty probe callback
117 * @mdata: marker data
118 * @probe_private: probe private data
119 * @call_private: call site private data
120 * @fmt: format string
121 * @...: variable argument list
122 *
123 * Empty callback provided as a probe to the markers. By providing this to a
124 * disabled marker, we make sure the execution flow is always valid even
125 * though the function pointer change and the marker enabling are two distinct
126 * operations that modifies the execution flow of preemptible code.
127 */
128 notrace void __mark_empty_function(const struct marker *mdata,
129 void *probe_private, struct registers *regs, void *call_private, const char *fmt, va_list *args)
130 {
131 }
132 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
133
134 /*
135 * marker_probe_cb Callback that prepares the variable argument list for probes.
136 * @mdata: pointer of type struct marker
137 * @call_private: caller site private data
138 * @...: Variable argument list.
139 *
140 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
141 * need to put a full smp_rmb() in this branch. This is why we do not use
142 * rcu_dereference() for the pointer read.
143 */
144 notrace void marker_probe_cb(const struct marker *mdata,
145 void *call_private, struct registers *regs, ...)
146 {
147 va_list args;
148 char ptype;
149
150 /*
151 * rcu_read_lock_sched does two things : disabling preemption to make
152 * sure the teardown of the callbacks can be done correctly when they
153 * are in modules and they insure RCU read coherency.
154 */
155 //ust// rcu_read_lock_sched_notrace();
156 ptype = mdata->ptype;
157 if (likely(!ptype)) {
158 marker_probe_func *func;
159 /* Must read the ptype before ptr. They are not data dependant,
160 * so we put an explicit smp_rmb() here. */
161 smp_rmb();
162 func = mdata->single.func;
163 /* Must read the ptr before private data. They are not data
164 * dependant, so we put an explicit smp_rmb() here. */
165 smp_rmb();
166 va_start(args, regs);
167 func(mdata, mdata->single.probe_private, regs, call_private,
168 mdata->format, &args);
169 va_end(args);
170 } else {
171 struct marker_probe_closure *multi;
172 int i;
173 /*
174 * Read mdata->ptype before mdata->multi.
175 */
176 smp_rmb();
177 multi = mdata->multi;
178 /*
179 * multi points to an array, therefore accessing the array
180 * depends on reading multi. However, even in this case,
181 * we must insure that the pointer is read _before_ the array
182 * data. Same as rcu_dereference, but we need a full smp_rmb()
183 * in the fast path, so put the explicit barrier here.
184 */
185 smp_read_barrier_depends();
186 for (i = 0; multi[i].func; i++) {
187 va_start(args, regs);
188 multi[i].func(mdata, multi[i].probe_private,
189 regs, call_private, mdata->format, &args);
190 va_end(args);
191 }
192 }
193 //ust// rcu_read_unlock_sched_notrace();
194 }
195 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
196
197 /*
198 * marker_probe_cb Callback that does not prepare the variable argument list.
199 * @mdata: pointer of type struct marker
200 * @call_private: caller site private data
201 * @...: Variable argument list.
202 *
203 * Should be connected to markers "MARK_NOARGS".
204 */
205 static notrace void marker_probe_cb_noarg(const struct marker *mdata,
206 void *call_private, struct registers *regs, ...)
207 {
208 va_list args; /* not initialized */
209 char ptype;
210
211 //ust// rcu_read_lock_sched_notrace();
212 ptype = mdata->ptype;
213 if (likely(!ptype)) {
214 marker_probe_func *func;
215 /* Must read the ptype before ptr. They are not data dependant,
216 * so we put an explicit smp_rmb() here. */
217 smp_rmb();
218 func = mdata->single.func;
219 /* Must read the ptr before private data. They are not data
220 * dependant, so we put an explicit smp_rmb() here. */
221 smp_rmb();
222 func(mdata, mdata->single.probe_private, regs, call_private,
223 mdata->format, &args);
224 } else {
225 struct marker_probe_closure *multi;
226 int i;
227 /*
228 * Read mdata->ptype before mdata->multi.
229 */
230 smp_rmb();
231 multi = mdata->multi;
232 /*
233 * multi points to an array, therefore accessing the array
234 * depends on reading multi. However, even in this case,
235 * we must insure that the pointer is read _before_ the array
236 * data. Same as rcu_dereference, but we need a full smp_rmb()
237 * in the fast path, so put the explicit barrier here.
238 */
239 smp_read_barrier_depends();
240 for (i = 0; multi[i].func; i++)
241 multi[i].func(mdata, multi[i].probe_private, regs,
242 call_private, mdata->format, &args);
243 }
244 //ust// rcu_read_unlock_sched_notrace();
245 }
246
247 static void free_old_closure(struct rcu_head *head)
248 {
249 struct marker_entry *entry = container_of(head,
250 struct marker_entry, rcu);
251 kfree(entry->oldptr);
252 /* Make sure we free the data before setting the pending flag to 0 */
253 smp_wmb();
254 entry->rcu_pending = 0;
255 }
256
257 static void debug_print_probes(struct marker_entry *entry)
258 {
259 int i;
260
261 if (!marker_debug)
262 return;
263
264 if (!entry->ptype) {
265 DBG("Single probe : %p %p",
266 entry->single.func,
267 entry->single.probe_private);
268 } else {
269 for (i = 0; entry->multi[i].func; i++)
270 DBG("Multi probe %d : %p %p", i,
271 entry->multi[i].func,
272 entry->multi[i].probe_private);
273 }
274 }
275
276 static struct marker_probe_closure *
277 marker_entry_add_probe(struct marker_entry *entry,
278 marker_probe_func *probe, void *probe_private)
279 {
280 int nr_probes = 0;
281 struct marker_probe_closure *old, *new;
282
283 WARN_ON(!probe);
284
285 debug_print_probes(entry);
286 old = entry->multi;
287 if (!entry->ptype) {
288 if (entry->single.func == probe &&
289 entry->single.probe_private == probe_private)
290 return ERR_PTR(-EBUSY);
291 if (entry->single.func == __mark_empty_function) {
292 /* 0 -> 1 probes */
293 entry->single.func = probe;
294 entry->single.probe_private = probe_private;
295 entry->refcount = 1;
296 entry->ptype = 0;
297 debug_print_probes(entry);
298 return NULL;
299 } else {
300 /* 1 -> 2 probes */
301 nr_probes = 1;
302 old = NULL;
303 }
304 } else {
305 /* (N -> N+1), (N != 0, 1) probes */
306 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
307 if (old[nr_probes].func == probe
308 && old[nr_probes].probe_private
309 == probe_private)
310 return ERR_PTR(-EBUSY);
311 }
312 /* + 2 : one for new probe, one for NULL func */
313 new = kzalloc((nr_probes + 2) * sizeof(struct marker_probe_closure),
314 GFP_KERNEL);
315 if (new == NULL)
316 return ERR_PTR(-ENOMEM);
317 if (!old)
318 new[0] = entry->single;
319 else
320 memcpy(new, old,
321 nr_probes * sizeof(struct marker_probe_closure));
322 new[nr_probes].func = probe;
323 new[nr_probes].probe_private = probe_private;
324 entry->refcount = nr_probes + 1;
325 entry->multi = new;
326 entry->ptype = 1;
327 debug_print_probes(entry);
328 return old;
329 }
330
331 static struct marker_probe_closure *
332 marker_entry_remove_probe(struct marker_entry *entry,
333 marker_probe_func *probe, void *probe_private)
334 {
335 int nr_probes = 0, nr_del = 0, i;
336 struct marker_probe_closure *old, *new;
337
338 old = entry->multi;
339
340 debug_print_probes(entry);
341 if (!entry->ptype) {
342 /* 0 -> N is an error */
343 WARN_ON(entry->single.func == __mark_empty_function);
344 /* 1 -> 0 probes */
345 WARN_ON(probe && entry->single.func != probe);
346 WARN_ON(entry->single.probe_private != probe_private);
347 entry->single.func = __mark_empty_function;
348 entry->refcount = 0;
349 entry->ptype = 0;
350 debug_print_probes(entry);
351 return NULL;
352 } else {
353 /* (N -> M), (N > 1, M >= 0) probes */
354 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
355 if ((!probe || old[nr_probes].func == probe)
356 && old[nr_probes].probe_private
357 == probe_private)
358 nr_del++;
359 }
360 }
361
362 if (nr_probes - nr_del == 0) {
363 /* N -> 0, (N > 1) */
364 entry->single.func = __mark_empty_function;
365 entry->refcount = 0;
366 entry->ptype = 0;
367 } else if (nr_probes - nr_del == 1) {
368 /* N -> 1, (N > 1) */
369 for (i = 0; old[i].func; i++)
370 if ((probe && old[i].func != probe) ||
371 old[i].probe_private != probe_private)
372 entry->single = old[i];
373 entry->refcount = 1;
374 entry->ptype = 0;
375 } else {
376 int j = 0;
377 /* N -> M, (N > 1, M > 1) */
378 /* + 1 for NULL */
379 new = kzalloc((nr_probes - nr_del + 1)
380 * sizeof(struct marker_probe_closure), GFP_KERNEL);
381 if (new == NULL)
382 return ERR_PTR(-ENOMEM);
383 for (i = 0; old[i].func; i++)
384 if ((probe && old[i].func != probe) ||
385 old[i].probe_private != probe_private)
386 new[j++] = old[i];
387 entry->refcount = nr_probes - nr_del;
388 entry->ptype = 1;
389 entry->multi = new;
390 }
391 debug_print_probes(entry);
392 return old;
393 }
394
395 /*
396 * Get marker if the marker is present in the marker hash table.
397 * Must be called with markers_mutex held.
398 * Returns NULL if not present.
399 */
400 static struct marker_entry *get_marker(const char *channel, const char *name)
401 {
402 struct hlist_head *head;
403 struct hlist_node *node;
404 struct marker_entry *e;
405 size_t channel_len = strlen(channel) + 1;
406 size_t name_len = strlen(name) + 1;
407 u32 hash;
408
409 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
410 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
411 hlist_for_each_entry(e, node, head, hlist) {
412 if (!strcmp(channel, e->channel) && !strcmp(name, e->name))
413 return e;
414 }
415 return NULL;
416 }
417
418 /*
419 * Add the marker to the marker hash table. Must be called with markers_mutex
420 * held.
421 */
422 static struct marker_entry *add_marker(const char *channel, const char *name,
423 const char *format)
424 {
425 struct hlist_head *head;
426 struct hlist_node *node;
427 struct marker_entry *e;
428 size_t channel_len = strlen(channel) + 1;
429 size_t name_len = strlen(name) + 1;
430 size_t format_len = 0;
431 u32 hash;
432
433 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
434 if (format)
435 format_len = strlen(format) + 1;
436 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
437 hlist_for_each_entry(e, node, head, hlist) {
438 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
439 DBG("Marker %s.%s busy", 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 */
513 //ust// if (e->rcu_pending)
514 //ust// rcu_barrier_sched();
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 DBG("Format mismatch for probe %s (%s), marker (%s)",
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
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// }
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 {
637 //ust// int ret;
638 //ust//
639 //ust// /* leave "call" as is. It is known statically. */
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// }
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 * is_marker_enabled - Check if a marker is enabled
669 * @channel: channel name
670 * @name: marker name
671 *
672 * Returns 1 if the marker is enabled, 0 if disabled.
673 */
674 int is_marker_enabled(const char *channel, const char *name)
675 {
676 struct marker_entry *entry;
677
678 mutex_lock(&markers_mutex);
679 entry = get_marker(channel, name);
680 mutex_unlock(&markers_mutex);
681
682 return entry && !!entry->refcount;
683 }
684
685 /**
686 * marker_update_probe_range - Update a probe range
687 * @begin: beginning of the range
688 * @end: end of the range
689 *
690 * Updates the probe callback corresponding to a range of markers.
691 */
692 void marker_update_probe_range(struct marker *begin,
693 struct marker *end)
694 {
695 struct marker *iter;
696 struct marker_entry *mark_entry;
697
698 mutex_lock(&markers_mutex);
699 for (iter = begin; iter < end; iter++) {
700 mark_entry = get_marker(iter->channel, iter->name);
701 if (mark_entry) {
702 set_marker(mark_entry, iter, !!mark_entry->refcount);
703 /*
704 * ignore error, continue
705 */
706
707 /* This is added for UST. We emit a core_marker_id event
708 * for markers that are already registered to a probe
709 * upon library load. Otherwise, no core_marker_id will
710 * be generated for these markers. Is this the right thing
711 * to do?
712 */
713 trace_mark(metadata, core_marker_id,
714 "channel %s name %s event_id %hu "
715 "int #1u%zu long #1u%zu pointer #1u%zu "
716 "size_t #1u%zu alignment #1u%u",
717 iter->channel, iter->name, mark_entry->event_id,
718 sizeof(int), sizeof(long), sizeof(void *),
719 sizeof(size_t), ltt_get_alignment());
720 } else {
721 disable_marker(iter);
722 }
723 }
724 mutex_unlock(&markers_mutex);
725 }
726
727 static void lib_update_markers(void)
728 {
729 struct lib *lib;
730
731 /* FIXME: we should probably take a mutex here on libs */
732 //ust// mutex_lock(&module_mutex);
733 list_for_each_entry(lib, &libs, list)
734 marker_update_probe_range(lib->markers_start,
735 lib->markers_start + lib->markers_count);
736 //ust// mutex_unlock(&module_mutex);
737 }
738
739 /*
740 * Update probes, removing the faulty probes.
741 *
742 * Internal callback only changed before the first probe is connected to it.
743 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
744 * transitions. All other transitions will leave the old private data valid.
745 * This makes the non-atomicity of the callback/private data updates valid.
746 *
747 * "special case" updates :
748 * 0 -> 1 callback
749 * 1 -> 0 callback
750 * 1 -> 2 callbacks
751 * 2 -> 1 callbacks
752 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
753 * Site effect : marker_set_format may delete the marker entry (creating a
754 * replacement).
755 */
756 static void marker_update_probes(void)
757 {
758 /* Core kernel markers */
759 //ust// marker_update_probe_range(__start___markers, __stop___markers);
760 /* Markers in modules. */
761 //ust// module_update_markers();
762 lib_update_markers();
763 //ust// tracepoint_probe_update_all();
764 /* Update immediate values */
765 core_imv_update();
766 //ust// module_imv_update(); /* FIXME: need to port for libs? */
767 marker_update_processes();
768 }
769
770 /**
771 * marker_probe_register - Connect a probe to a marker
772 * @channel: marker channel
773 * @name: marker name
774 * @format: format string
775 * @probe: probe handler
776 * @probe_private: probe private data
777 *
778 * private data must be a valid allocated memory address, or NULL.
779 * Returns 0 if ok, error value on error.
780 * The probe address must at least be aligned on the architecture pointer size.
781 */
782 int marker_probe_register(const char *channel, const char *name,
783 const char *format, marker_probe_func *probe,
784 void *probe_private)
785 {
786 struct marker_entry *entry;
787 int ret = 0, ret_err;
788 struct marker_probe_closure *old;
789 int first_probe = 0;
790
791 mutex_lock(&markers_mutex);
792 entry = get_marker(channel, name);
793 if (!entry) {
794 first_probe = 1;
795 entry = add_marker(channel, name, format);
796 if (IS_ERR(entry))
797 ret = PTR_ERR(entry);
798 if (ret)
799 goto end;
800 ret = ltt_channels_register(channel);
801 if (ret)
802 goto error_remove_marker;
803 ret = ltt_channels_get_index_from_name(channel);
804 if (ret < 0)
805 goto error_unregister_channel;
806 entry->channel_id = ret;
807 ret = ltt_channels_get_event_id(channel, name);
808 if (ret < 0)
809 goto error_unregister_channel;
810 entry->event_id = ret;
811 ret = 0;
812 trace_mark(metadata, core_marker_id,
813 "channel %s name %s event_id %hu "
814 "int #1u%zu long #1u%zu pointer #1u%zu "
815 "size_t #1u%zu alignment #1u%u",
816 channel, name, entry->event_id,
817 sizeof(int), sizeof(long), sizeof(void *),
818 sizeof(size_t), ltt_get_alignment());
819 } else if (format) {
820 if (!entry->format)
821 ret = marker_set_format(entry, format);
822 else if (strcmp(entry->format, format))
823 ret = -EPERM;
824 if (ret)
825 goto end;
826 }
827
828 /*
829 * If we detect that a call_rcu is pending for this marker,
830 * make sure it's executed now.
831 */
832 //ust// if (entry->rcu_pending)
833 //ust// rcu_barrier_sched();
834 old = marker_entry_add_probe(entry, probe, probe_private);
835 if (IS_ERR(old)) {
836 ret = PTR_ERR(old);
837 if (first_probe)
838 goto error_unregister_channel;
839 else
840 goto end;
841 }
842 mutex_unlock(&markers_mutex);
843
844 /* Activate marker if necessary */
845 marker_update_probes();
846
847 mutex_lock(&markers_mutex);
848 entry = get_marker(channel, name);
849 if (!entry)
850 goto end;
851 //ust// if (entry->rcu_pending)
852 //ust// rcu_barrier_sched();
853 entry->oldptr = old;
854 entry->rcu_pending = 1;
855 /* write rcu_pending before calling the RCU callback */
856 smp_wmb();
857 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
858 synchronize_rcu(); free_old_closure(&entry->rcu);
859 goto end;
860
861 error_unregister_channel:
862 ret_err = ltt_channels_unregister(channel);
863 WARN_ON(ret_err);
864 error_remove_marker:
865 ret_err = remove_marker(channel, name);
866 WARN_ON(ret_err);
867 end:
868 mutex_unlock(&markers_mutex);
869 return ret;
870 }
871 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
872
873 /**
874 * marker_probe_unregister - Disconnect a probe from a marker
875 * @channel: marker channel
876 * @name: marker name
877 * @probe: probe function pointer
878 * @probe_private: probe private data
879 *
880 * Returns the private data given to marker_probe_register, or an ERR_PTR().
881 * We do not need to call a synchronize_sched to make sure the probes have
882 * finished running before doing a module unload, because the module unload
883 * itself uses stop_machine(), which insures that every preempt disabled section
884 * have finished.
885 */
886 int marker_probe_unregister(const char *channel, const char *name,
887 marker_probe_func *probe, void *probe_private)
888 {
889 struct marker_entry *entry;
890 struct marker_probe_closure *old;
891 int ret = -ENOENT;
892
893 mutex_lock(&markers_mutex);
894 entry = get_marker(channel, name);
895 if (!entry)
896 goto end;
897 //ust// if (entry->rcu_pending)
898 //ust// rcu_barrier_sched();
899 old = marker_entry_remove_probe(entry, probe, probe_private);
900 mutex_unlock(&markers_mutex);
901
902 marker_update_probes();
903
904 mutex_lock(&markers_mutex);
905 entry = get_marker(channel, name);
906 if (!entry)
907 goto end;
908 //ust// if (entry->rcu_pending)
909 //ust// rcu_barrier_sched();
910 entry->oldptr = old;
911 entry->rcu_pending = 1;
912 /* write rcu_pending before calling the RCU callback */
913 smp_wmb();
914 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
915 synchronize_rcu(); free_old_closure(&entry->rcu);
916 remove_marker(channel, name); /* Ignore busy error message */
917 ret = 0;
918 end:
919 mutex_unlock(&markers_mutex);
920 return ret;
921 }
922 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
923
924 static struct marker_entry *
925 get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
926 {
927 struct marker_entry *entry;
928 unsigned int i;
929 struct hlist_head *head;
930 struct hlist_node *node;
931
932 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
933 head = &marker_table[i];
934 hlist_for_each_entry(entry, node, head, hlist) {
935 if (!entry->ptype) {
936 if (entry->single.func == probe
937 && entry->single.probe_private
938 == probe_private)
939 return entry;
940 } else {
941 struct marker_probe_closure *closure;
942 closure = entry->multi;
943 for (i = 0; closure[i].func; i++) {
944 if (closure[i].func == probe &&
945 closure[i].probe_private
946 == probe_private)
947 return entry;
948 }
949 }
950 }
951 }
952 return NULL;
953 }
954
955 /**
956 * marker_probe_unregister_private_data - Disconnect a probe from a marker
957 * @probe: probe function
958 * @probe_private: probe private data
959 *
960 * Unregister a probe by providing the registered private data.
961 * Only removes the first marker found in hash table.
962 * Return 0 on success or error value.
963 * We do not need to call a synchronize_sched to make sure the probes have
964 * finished running before doing a module unload, because the module unload
965 * itself uses stop_machine(), which insures that every preempt disabled section
966 * have finished.
967 */
968 int marker_probe_unregister_private_data(marker_probe_func *probe,
969 void *probe_private)
970 {
971 struct marker_entry *entry;
972 int ret = 0;
973 struct marker_probe_closure *old;
974 const char *channel = NULL, *name = NULL;
975
976 mutex_lock(&markers_mutex);
977 entry = get_marker_from_private_data(probe, probe_private);
978 if (!entry) {
979 ret = -ENOENT;
980 goto end;
981 }
982 //ust// if (entry->rcu_pending)
983 //ust// rcu_barrier_sched();
984 old = marker_entry_remove_probe(entry, NULL, probe_private);
985 channel = kstrdup(entry->channel, GFP_KERNEL);
986 name = kstrdup(entry->name, GFP_KERNEL);
987 mutex_unlock(&markers_mutex);
988
989 marker_update_probes();
990
991 mutex_lock(&markers_mutex);
992 entry = get_marker(channel, name);
993 if (!entry)
994 goto end;
995 //ust// if (entry->rcu_pending)
996 //ust// rcu_barrier_sched();
997 entry->oldptr = old;
998 entry->rcu_pending = 1;
999 /* write rcu_pending before calling the RCU callback */
1000 smp_wmb();
1001 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
1002 synchronize_rcu(); free_old_closure(&entry->rcu);
1003 /* Ignore busy error message */
1004 remove_marker(channel, name);
1005 end:
1006 mutex_unlock(&markers_mutex);
1007 kfree(channel);
1008 kfree(name);
1009 return ret;
1010 }
1011 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
1012
1013 /**
1014 * marker_get_private_data - Get a marker's probe private data
1015 * @channel: marker channel
1016 * @name: marker name
1017 * @probe: probe to match
1018 * @num: get the nth matching probe's private data
1019 *
1020 * Returns the nth private data pointer (starting from 0) matching, or an
1021 * ERR_PTR.
1022 * Returns the private data pointer, or an ERR_PTR.
1023 * The private data pointer should _only_ be dereferenced if the caller is the
1024 * owner of the data, or its content could vanish. This is mostly used to
1025 * confirm that a caller is the owner of a registered probe.
1026 */
1027 void *marker_get_private_data(const char *channel, const char *name,
1028 marker_probe_func *probe, int num)
1029 {
1030 struct hlist_head *head;
1031 struct hlist_node *node;
1032 struct marker_entry *e;
1033 size_t channel_len = strlen(channel) + 1;
1034 size_t name_len = strlen(name) + 1;
1035 int i;
1036 u32 hash;
1037
1038 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
1039 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
1040 hlist_for_each_entry(e, node, head, hlist) {
1041 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
1042 if (!e->ptype) {
1043 if (num == 0 && e->single.func == probe)
1044 return e->single.probe_private;
1045 } else {
1046 struct marker_probe_closure *closure;
1047 int match = 0;
1048 closure = e->multi;
1049 for (i = 0; closure[i].func; i++) {
1050 if (closure[i].func != probe)
1051 continue;
1052 if (match++ == num)
1053 return closure[i].probe_private;
1054 }
1055 }
1056 break;
1057 }
1058 }
1059 return ERR_PTR(-ENOENT);
1060 }
1061 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1062
1063 /**
1064 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1065 *
1066 * Called when no channel users are active by the channel infrastructure.
1067 * Called with lock_markers() and channel mutex held.
1068 */
1069 //ust// void markers_compact_event_ids(void)
1070 //ust// {
1071 //ust// struct marker_entry *entry;
1072 //ust// unsigned int i;
1073 //ust// struct hlist_head *head;
1074 //ust// struct hlist_node *node;
1075 //ust// int ret;
1076 //ust//
1077 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1078 //ust// head = &marker_table[i];
1079 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1080 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1081 //ust// WARN_ON(ret < 0);
1082 //ust// entry->channel_id = ret;
1083 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1084 //ust// entry->name);
1085 //ust// WARN_ON(ret < 0);
1086 //ust// entry->event_id = ret;
1087 //ust// }
1088 //ust// }
1089 //ust// }
1090
1091 //ust//#ifdef CONFIG_MODULES
1092
1093 /*
1094 * Returns 0 if current not found.
1095 * Returns 1 if current found.
1096 */
1097 int lib_get_iter_markers(struct marker_iter *iter)
1098 {
1099 struct lib *iter_lib;
1100 int found = 0;
1101
1102 //ust// mutex_lock(&module_mutex);
1103 list_for_each_entry(iter_lib, &libs, list) {
1104 if (iter_lib < iter->lib)
1105 continue;
1106 else if (iter_lib > iter->lib)
1107 iter->marker = NULL;
1108 found = marker_get_iter_range(&iter->marker,
1109 iter_lib->markers_start,
1110 iter_lib->markers_start + iter_lib->markers_count);
1111 if (found) {
1112 iter->lib = iter_lib;
1113 break;
1114 }
1115 }
1116 //ust// mutex_unlock(&module_mutex);
1117 return found;
1118 }
1119
1120 /**
1121 * marker_get_iter_range - Get a next marker iterator given a range.
1122 * @marker: current markers (in), next marker (out)
1123 * @begin: beginning of the range
1124 * @end: end of the range
1125 *
1126 * Returns whether a next marker has been found (1) or not (0).
1127 * Will return the first marker in the range if the input marker is NULL.
1128 */
1129 int marker_get_iter_range(struct marker **marker, struct marker *begin,
1130 struct marker *end)
1131 {
1132 if (!*marker && begin != end) {
1133 *marker = begin;
1134 return 1;
1135 }
1136 if (*marker >= begin && *marker < end)
1137 return 1;
1138 return 0;
1139 }
1140 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1141
1142 static void marker_get_iter(struct marker_iter *iter)
1143 {
1144 int found = 0;
1145
1146 /* Core kernel markers */
1147 if (!iter->lib) {
1148 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1149 found = marker_get_iter_range(&iter->marker,
1150 __start___markers, __stop___markers);
1151 if (found)
1152 goto end;
1153 }
1154 /* Markers in modules. */
1155 found = lib_get_iter_markers(iter);
1156 end:
1157 if (!found)
1158 marker_iter_reset(iter);
1159 }
1160
1161 void marker_iter_start(struct marker_iter *iter)
1162 {
1163 marker_get_iter(iter);
1164 }
1165 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1166
1167 void marker_iter_next(struct marker_iter *iter)
1168 {
1169 iter->marker++;
1170 /*
1171 * iter->marker may be invalid because we blindly incremented it.
1172 * Make sure it is valid by marshalling on the markers, getting the
1173 * markers from following modules if necessary.
1174 */
1175 marker_get_iter(iter);
1176 }
1177 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1178
1179 void marker_iter_stop(struct marker_iter *iter)
1180 {
1181 }
1182 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1183
1184 void marker_iter_reset(struct marker_iter *iter)
1185 {
1186 iter->lib = NULL;
1187 iter->marker = NULL;
1188 }
1189 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1190
1191 #ifdef CONFIG_MARKERS_USERSPACE
1192 /*
1193 * must be called with current->user_markers_mutex held
1194 */
1195 static void free_user_marker(char __user *state, struct hlist_head *head)
1196 {
1197 struct user_marker *umark;
1198 struct hlist_node *pos, *n;
1199
1200 hlist_for_each_entry_safe(umark, pos, n, head, hlist) {
1201 if (umark->state == state) {
1202 hlist_del(&umark->hlist);
1203 kfree(umark);
1204 }
1205 }
1206 }
1207
1208 /*
1209 * Update current process.
1210 * Note that we have to wait a whole scheduler period before we are sure that
1211 * every running userspace threads have their markers updated.
1212 * (synchronize_sched() can be used to insure this).
1213 */
1214 //ust// void marker_update_process(void)
1215 //ust// {
1216 //ust// struct user_marker *umark;
1217 //ust// struct hlist_node *pos;
1218 //ust// struct marker_entry *entry;
1219 //ust//
1220 //ust// mutex_lock(&markers_mutex);
1221 //ust// mutex_lock(&current->group_leader->user_markers_mutex);
1222 //ust// if (strcmp(current->comm, "testprog") == 0)
1223 //ust// DBG("do update pending for testprog");
1224 //ust// hlist_for_each_entry(umark, pos,
1225 //ust// &current->group_leader->user_markers, hlist) {
1226 //ust// DBG("Updating marker %s in %s", umark->name, current->comm);
1227 //ust// entry = get_marker("userspace", umark->name);
1228 //ust// if (entry) {
1229 //ust// if (entry->format &&
1230 //ust// strcmp(entry->format, umark->format) != 0) {
1231 //ust// WARN("error, wrong format in process %s",
1232 //ust// current->comm);
1233 //ust// break;
1234 //ust// }
1235 //ust// if (put_user(!!entry->refcount, umark->state)) {
1236 //ust// WARN("Marker in %s caused a fault",
1237 //ust// current->comm);
1238 //ust// break;
1239 //ust// }
1240 //ust// } else {
1241 //ust// if (put_user(0, umark->state)) {
1242 //ust// WARN("Marker in %s caused a fault", current->comm);
1243 //ust// break;
1244 //ust// }
1245 //ust// }
1246 //ust// }
1247 //ust// clear_thread_flag(TIF_MARKER_PENDING);
1248 //ust// mutex_unlock(&current->group_leader->user_markers_mutex);
1249 //ust// mutex_unlock(&markers_mutex);
1250 //ust// }
1251
1252 /*
1253 * Called at process exit and upon do_execve().
1254 * We assume that when the leader exits, no more references can be done to the
1255 * leader structure by the other threads.
1256 */
1257 void exit_user_markers(struct task_struct *p)
1258 {
1259 struct user_marker *umark;
1260 struct hlist_node *pos, *n;
1261
1262 if (thread_group_leader(p)) {
1263 mutex_lock(&markers_mutex);
1264 mutex_lock(&p->user_markers_mutex);
1265 hlist_for_each_entry_safe(umark, pos, n, &p->user_markers,
1266 hlist)
1267 kfree(umark);
1268 INIT_HLIST_HEAD(&p->user_markers);
1269 p->user_markers_sequence++;
1270 mutex_unlock(&p->user_markers_mutex);
1271 mutex_unlock(&markers_mutex);
1272 }
1273 }
1274
1275 int is_marker_enabled(const char *channel, const char *name)
1276 {
1277 struct marker_entry *entry;
1278
1279 mutex_lock(&markers_mutex);
1280 entry = get_marker(channel, name);
1281 mutex_unlock(&markers_mutex);
1282
1283 return entry && !!entry->refcount;
1284 }
1285 //ust// #endif
1286
1287 int marker_module_notify(struct notifier_block *self,
1288 unsigned long val, void *data)
1289 {
1290 struct module *mod = data;
1291
1292 switch (val) {
1293 case MODULE_STATE_COMING:
1294 marker_update_probe_range(mod->markers,
1295 mod->markers + mod->num_markers);
1296 break;
1297 case MODULE_STATE_GOING:
1298 marker_update_probe_range(mod->markers,
1299 mod->markers + mod->num_markers);
1300 break;
1301 }
1302 return 0;
1303 }
1304
1305 struct notifier_block marker_module_nb = {
1306 .notifier_call = marker_module_notify,
1307 .priority = 0,
1308 };
1309
1310 //ust// static int init_markers(void)
1311 //ust// {
1312 //ust// return register_module_notifier(&marker_module_nb);
1313 //ust// }
1314 //ust// __initcall(init_markers);
1315 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1316
1317 #endif /* CONFIG_MODULES */
1318
1319 void ltt_dump_marker_state(struct ltt_trace_struct *trace)
1320 {
1321 struct marker_entry *entry;
1322 struct ltt_probe_private_data call_data;
1323 struct hlist_head *head;
1324 struct hlist_node *node;
1325 unsigned int i;
1326
1327 mutex_lock(&markers_mutex);
1328 call_data.trace = trace;
1329 call_data.serializer = NULL;
1330
1331 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1332 head = &marker_table[i];
1333 hlist_for_each_entry(entry, node, head, hlist) {
1334 __trace_mark(0, metadata, core_marker_id,
1335 &call_data,
1336 "channel %s name %s event_id %hu "
1337 "int #1u%zu long #1u%zu pointer #1u%zu "
1338 "size_t #1u%zu alignment #1u%u",
1339 entry->channel,
1340 entry->name,
1341 entry->event_id,
1342 sizeof(int), sizeof(long),
1343 sizeof(void *), sizeof(size_t),
1344 ltt_get_alignment());
1345 if (entry->format)
1346 __trace_mark(0, metadata,
1347 core_marker_format,
1348 &call_data,
1349 "channel %s name %s format %s",
1350 entry->channel,
1351 entry->name,
1352 entry->format);
1353 }
1354 }
1355 mutex_unlock(&markers_mutex);
1356 }
1357 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1358
1359 static void (*new_marker_cb)(struct marker *) = NULL;
1360
1361 void marker_set_new_marker_cb(void (*cb)(struct marker *))
1362 {
1363 new_marker_cb = cb;
1364 }
1365
1366 static void new_markers(struct marker *start, struct marker *end)
1367 {
1368 if(new_marker_cb) {
1369 struct marker *m;
1370 for(m=start; m < end; m++) {
1371 new_marker_cb(m);
1372 }
1373 }
1374 }
1375
1376 int marker_register_lib(struct marker *markers_start, int markers_count)
1377 {
1378 struct lib *pl;
1379 struct marker_addr *addr;
1380
1381 pl = (struct lib *) malloc(sizeof(struct lib));
1382
1383 pl->markers_start = markers_start;
1384 pl->markers_count = markers_count;
1385
1386 /* FIXME: maybe protect this with its own mutex? */
1387 lock_markers();
1388 list_add(&pl->list, &libs);
1389 unlock_markers();
1390
1391 new_markers(markers_start, markers_start + markers_count);
1392
1393 /* FIXME: update just the loaded lib */
1394 lib_update_markers();
1395
1396 DBG("just registered a markers section from %p and having %d markers", markers_start, markers_count);
1397
1398 return 0;
1399 }
1400
1401 int marker_unregister_lib(struct marker *markers_start, int markers_count)
1402 {
1403 /*FIXME: implement; but before implementing, marker_register_lib must
1404 have appropriate locking. */
1405
1406 return 0;
1407 }
1408
1409 static int initialized = 0;
1410
1411 void __attribute__((constructor)) init_markers(void)
1412 {
1413 if(!initialized) {
1414 marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker));
1415 //DBG("markers_start: %p, markers_stop: %p\n", __start___markers, __stop___markers);
1416 initialized = 1;
1417 }
1418 }
This page took 0.059312 seconds and 5 git commands to generate.