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