Move away headers that will be deprecated
[lttng-ust.git] / libust / marker.c
1 /*
2 * Copyright (C) 2007-2011 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;
7 * version 2.1 of the License.
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 #define _LGPL_SOURCE
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <pthread.h>
23 #include <urcu-call-rcu.h>
24 #include <urcu-bp.h>
25 #include <urcu/rculist.h>
26 #include <urcu/hlist.h>
27
28 #include <ust/core.h>
29 #include <ust/marker.h>
30 #include <ust/marker-internal.h>
31 #include <ust/tracepoint.h>
32 #include <ust/tracepoint-internal.h>
33
34 #include "usterr_signal_safe.h"
35 #include "channels.h"
36 #include "tracercore.h"
37 #include "tracer.h"
38
39 extern struct ust_marker * const __start___ust_marker_ptrs[] __attribute__((visibility("hidden")));
40 extern struct ust_marker * const __stop___ust_marker_ptrs[] __attribute__((visibility("hidden")));
41
42 /* Set to 1 to enable ust_marker debug output */
43 static const int ust_marker_debug;
44 static int initialized;
45 static void (*new_ust_marker_cb)(struct ust_marker *);
46
47 /*
48 * ust_marker mutex protects the builtin and module ust_marker and the
49 * hash table, as well as the ust_marker_libs list.
50 */
51 static DEFINE_MUTEX(ust_marker_mutex);
52 static CDS_LIST_HEAD(ust_marker_libs);
53
54 /*
55 * Allow nested mutex for mutex listing and nested enable.
56 */
57 static __thread int nested_mutex;
58
59 void lock_ust_marker(void)
60 {
61 if (!(nested_mutex++))
62 pthread_mutex_lock(&ust_marker_mutex);
63 }
64
65 void unlock_ust_marker(void)
66 {
67 if (!(--nested_mutex))
68 pthread_mutex_unlock(&ust_marker_mutex);
69 }
70
71 /*
72 * ust_marker hash table, containing the active ust_marker.
73 * Protected by ust_marker mutex.
74 */
75 #define UST_MARKER_HASH_BITS 6
76 #define UST_MARKER_TABLE_SIZE (1 << UST_MARKER_HASH_BITS)
77 static struct cds_hlist_head ust_marker_table[UST_MARKER_TABLE_SIZE];
78
79 struct ust_marker_probe_array {
80 struct rcu_head rcu;
81 struct ust_marker_probe_closure c[0];
82 };
83
84 /*
85 * Note about RCU :
86 * It is used to make sure every handler has finished using its private
87 * data between two consecutive operation (add or remove) on a given
88 * ust_marker. It is also used to delay the free of multiple probes
89 * array until a quiescent state is reached. ust_marker entries
90 * modifications are protected by the ust_marker_mutex.
91 */
92 struct ust_marker_entry {
93 struct cds_hlist_node hlist;
94 char *format;
95 char *name;
96 /* Probe wrapper */
97 void (*call)(const struct ust_marker *mdata, void *call_private, ...);
98 struct ust_marker_probe_closure single;
99 struct ust_marker_probe_array *multi;
100 int refcount; /* Number of times armed. 0 if disarmed. */
101 u16 channel_id;
102 u16 event_id;
103 unsigned char ptype:1;
104 unsigned char format_allocated:1;
105 char channel[0]; /* Contains channel'\0'name'\0'format'\0' */
106 };
107
108 /**
109 * __ust_marker_empty_function - Empty probe callback
110 * @mdata: ust_marker data
111 * @probe_private: probe private data
112 * @call_private: call site private data
113 * @fmt: format string
114 * @...: variable argument list
115 *
116 * Empty callback provided as a probe to the ust_marker. By providing
117 * this to a disabled ust_marker, we make sure the execution flow is
118 * always valid even though the function pointer change and the
119 * ust_marker enabling are two distinct operations that modifies the
120 * execution flow of preemptible code.
121 */
122 notrace void __ust_marker_empty_function(const struct ust_marker *mdata,
123 void *probe_private, void *call_private, const char *fmt, va_list *args)
124 {
125 }
126
127 /*
128 * ust_marker_probe_cb Callback that prepares the variable argument list for probes.
129 * @mdata: pointer of type struct ust_marker
130 * @call_private: caller site private data
131 * @...: Variable argument list.
132 *
133 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
134 * need to put a full cmm_smp_rmb() in this branch. This is why we do not use
135 * rcu_dereference() for the pointer read.
136 */
137 notrace void ust_marker_probe_cb(const struct ust_marker *mdata,
138 void *call_private, ...)
139 {
140 va_list args;
141 char ptype;
142
143 /*
144 * rcu_read_lock_sched does two things : disabling preemption to make
145 * sure the teardown of the callbacks can be done correctly when they
146 * are in modules and they insure RCU read coherency.
147 */
148 rcu_read_lock();
149 ptype = mdata->ptype;
150 if (likely(!ptype)) {
151 ust_marker_probe_func *func;
152 /* Must read the ptype before ptr. They are not data dependant,
153 * so we put an explicit cmm_smp_rmb() here. */
154 cmm_smp_rmb();
155 func = mdata->single.func;
156 /* Must read the ptr before private data. They are not data
157 * dependant, so we put an explicit cmm_smp_rmb() here. */
158 cmm_smp_rmb();
159 va_start(args, call_private);
160 func(mdata, mdata->single.probe_private, call_private,
161 mdata->format, &args);
162 va_end(args);
163 } else {
164 struct ust_marker_probe_array *multi;
165 int i;
166 /*
167 * Read mdata->ptype before mdata->multi.
168 */
169 cmm_smp_rmb();
170 multi = mdata->multi;
171 /*
172 * multi points to an array, therefore accessing the array
173 * depends on reading multi. However, even in this case,
174 * we must insure that the pointer is read _before_ the array
175 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
176 * in the fast path, so put the explicit cmm_barrier here.
177 */
178 cmm_smp_read_barrier_depends();
179 for (i = 0; multi->c[i].func; i++) {
180 va_start(args, call_private);
181 multi->c[i].func(mdata, multi->c[i].probe_private,
182 call_private, mdata->format, &args);
183 va_end(args);
184 }
185 }
186 rcu_read_unlock();
187 }
188
189 /*
190 * ust_marker_probe_cb Callback that does not prepare the variable argument list.
191 * @mdata: pointer of type struct ust_marker
192 * @call_private: caller site private data
193 * @...: Variable argument list.
194 *
195 * Should be connected to ust_marker "UST_MARKER_NOARGS".
196 */
197 static notrace void ust_marker_probe_cb_noarg(const struct ust_marker *mdata,
198 void *call_private, ...)
199 {
200 va_list args; /* not initialized */
201 char ptype;
202
203 rcu_read_lock();
204 ptype = mdata->ptype;
205 if (likely(!ptype)) {
206 ust_marker_probe_func *func;
207 /* Must read the ptype before ptr. They are not data dependant,
208 * so we put an explicit cmm_smp_rmb() here. */
209 cmm_smp_rmb();
210 func = mdata->single.func;
211 /* Must read the ptr before private data. They are not data
212 * dependant, so we put an explicit cmm_smp_rmb() here. */
213 cmm_smp_rmb();
214 func(mdata, mdata->single.probe_private, call_private,
215 mdata->format, &args);
216 } else {
217 struct ust_marker_probe_array *multi;
218 int i;
219 /*
220 * Read mdata->ptype before mdata->multi.
221 */
222 cmm_smp_rmb();
223 multi = mdata->multi;
224 /*
225 * multi points to an array, therefore accessing the array
226 * depends on reading multi. However, even in this case,
227 * we must insure that the pointer is read _before_ the array
228 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
229 * in the fast path, so put the explicit cmm_barrier here.
230 */
231 cmm_smp_read_barrier_depends();
232 for (i = 0; multi->c[i].func; i++)
233 multi->c[i].func(mdata, multi->c[i].probe_private,
234 call_private, mdata->format, &args);
235 }
236 rcu_read_unlock();
237 }
238
239 static void free_old_closure(struct rcu_head *head)
240 {
241 struct ust_marker_probe_array *multi =
242 _ust_container_of(head, struct ust_marker_probe_array, rcu);
243 free(multi);
244 }
245
246 static void debug_print_probes(struct ust_marker_entry *entry)
247 {
248 int i;
249
250 if (!ust_marker_debug)
251 return;
252
253 if (!entry->ptype) {
254 DBG("Single probe : %p %p",
255 entry->single.func,
256 entry->single.probe_private);
257 } else {
258 for (i = 0; entry->multi->c[i].func; i++)
259 DBG("Multi probe %d : %p %p", i,
260 entry->multi->c[i].func,
261 entry->multi->c[i].probe_private);
262 }
263 }
264
265 static struct ust_marker_probe_array *
266 ust_marker_entry_add_probe(struct ust_marker_entry *entry,
267 ust_marker_probe_func *probe, void *probe_private)
268 {
269 int nr_probes = 0;
270 struct ust_marker_probe_array *old, *new;
271
272 WARN_ON(!probe);
273
274 debug_print_probes(entry);
275 old = entry->multi;
276 if (!entry->ptype) {
277 if (entry->single.func == probe &&
278 entry->single.probe_private == probe_private)
279 return ERR_PTR(-EBUSY);
280 if (entry->single.func == __ust_marker_empty_function) {
281 /* 0 -> 1 probes */
282 entry->single.func = probe;
283 entry->single.probe_private = probe_private;
284 entry->refcount = 1;
285 entry->ptype = 0;
286 debug_print_probes(entry);
287 return NULL;
288 } else {
289 /* 1 -> 2 probes */
290 nr_probes = 1;
291 old = NULL;
292 }
293 } else {
294 /* (N -> N+1), (N != 0, 1) probes */
295 for (nr_probes = 0; old->c[nr_probes].func; nr_probes++)
296 if (old->c[nr_probes].func == probe
297 && old->c[nr_probes].probe_private
298 == probe_private)
299 return ERR_PTR(-EBUSY);
300 }
301 /* + 2 : one for new probe, one for NULL func */
302 new = zmalloc(sizeof(struct ust_marker_probe_array)
303 + ((nr_probes + 2) * sizeof(struct ust_marker_probe_closure)));
304 if (new == NULL)
305 return ERR_PTR(-ENOMEM);
306 if (!old)
307 new->c[0] = entry->single;
308 else
309 memcpy(&new->c[0], &old->c[0],
310 nr_probes * sizeof(struct ust_marker_probe_closure));
311 new->c[nr_probes].func = probe;
312 new->c[nr_probes].probe_private = probe_private;
313 entry->refcount = nr_probes + 1;
314 entry->multi = new;
315 entry->ptype = 1;
316 debug_print_probes(entry);
317 return old;
318 }
319
320 static struct ust_marker_probe_array *
321 ust_marker_entry_remove_probe(struct ust_marker_entry *entry,
322 ust_marker_probe_func *probe, void *probe_private)
323 {
324 int nr_probes = 0, nr_del = 0, i;
325 struct ust_marker_probe_array *old, *new;
326
327 old = entry->multi;
328
329 debug_print_probes(entry);
330 if (!entry->ptype) {
331 /* 0 -> N is an error */
332 WARN_ON(entry->single.func == __ust_marker_empty_function);
333 /* 1 -> 0 probes */
334 WARN_ON(probe && entry->single.func != probe);
335 WARN_ON(entry->single.probe_private != probe_private);
336 entry->single.func = __ust_marker_empty_function;
337 entry->refcount = 0;
338 entry->ptype = 0;
339 debug_print_probes(entry);
340 return NULL;
341 } else {
342 /* (N -> M), (N > 1, M >= 0) probes */
343 for (nr_probes = 0; old->c[nr_probes].func; nr_probes++) {
344 if ((!probe || old->c[nr_probes].func == probe)
345 && old->c[nr_probes].probe_private
346 == probe_private)
347 nr_del++;
348 }
349 }
350
351 if (nr_probes - nr_del == 0) {
352 /* N -> 0, (N > 1) */
353 entry->single.func = __ust_marker_empty_function;
354 entry->refcount = 0;
355 entry->ptype = 0;
356 } else if (nr_probes - nr_del == 1) {
357 /* N -> 1, (N > 1) */
358 for (i = 0; old->c[i].func; i++)
359 if ((probe && old->c[i].func != probe) ||
360 old->c[i].probe_private != probe_private)
361 entry->single = old->c[i];
362 entry->refcount = 1;
363 entry->ptype = 0;
364 } else {
365 int j = 0;
366 /* N -> M, (N > 1, M > 1) */
367 /* + 1 for NULL */
368 new = zmalloc(sizeof(struct ust_marker_probe_array)
369 + ((nr_probes - nr_del + 1) * sizeof(struct ust_marker_probe_closure)));
370 if (new == NULL)
371 return ERR_PTR(-ENOMEM);
372 for (i = 0; old->c[i].func; i++)
373 if ((probe && old->c[i].func != probe) ||
374 old->c[i].probe_private != probe_private)
375 new->c[j++] = old->c[i];
376 entry->refcount = nr_probes - nr_del;
377 entry->ptype = 1;
378 entry->multi = new;
379 }
380 debug_print_probes(entry);
381 return old;
382 }
383
384 /*
385 * Get ust_marker if the ust_marker is present in the ust_marker hash table.
386 * Must be called with ust_marker_mutex held.
387 * Returns NULL if not present.
388 */
389 static struct ust_marker_entry *get_ust_marker(const char *channel, const char *name)
390 {
391 struct cds_hlist_head *head;
392 struct cds_hlist_node *node;
393 struct ust_marker_entry *e;
394 size_t channel_len = strlen(channel) + 1;
395 size_t name_len = strlen(name) + 1;
396 u32 hash;
397
398 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
399 head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)];
400 cds_hlist_for_each_entry(e, node, head, hlist) {
401 if (!strcmp(channel, e->channel) && !strcmp(name, e->name))
402 return e;
403 }
404 return NULL;
405 }
406
407 /*
408 * Add the ust_marker to the ust_marker hash table. Must be called with
409 * ust_marker_mutex held.
410 */
411 static struct ust_marker_entry *add_ust_marker(const char *channel, const char *name,
412 const char *format)
413 {
414 struct cds_hlist_head *head;
415 struct cds_hlist_node *node;
416 struct ust_marker_entry *e;
417 size_t channel_len = strlen(channel) + 1;
418 size_t name_len = strlen(name) + 1;
419 size_t format_len = 0;
420 u32 hash;
421
422 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
423 if (format)
424 format_len = strlen(format) + 1;
425 head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)];
426 cds_hlist_for_each_entry(e, node, head, hlist) {
427 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
428 DBG("ust_marker %s.%s busy", channel, name);
429 return ERR_PTR(-EBUSY); /* Already there */
430 }
431 }
432 /*
433 * Using zmalloc here to allocate a variable length element. Could
434 * cause some memory fragmentation if overused.
435 */
436 e = zmalloc(sizeof(struct ust_marker_entry)
437 + channel_len + name_len + format_len);
438 if (!e)
439 return ERR_PTR(-ENOMEM);
440 memcpy(e->channel, channel, channel_len);
441 e->name = &e->channel[channel_len];
442 memcpy(e->name, name, name_len);
443 if (format) {
444 e->format = &e->name[name_len];
445 memcpy(e->format, format, format_len);
446 if (strcmp(e->format, UST_MARKER_NOARGS) == 0)
447 e->call = ust_marker_probe_cb_noarg;
448 else
449 e->call = ust_marker_probe_cb;
450 __ust_marker(metadata, core_marker_format, NULL,
451 "channel %s name %s format %s",
452 e->channel, e->name, e->format);
453 } else {
454 e->format = NULL;
455 e->call = ust_marker_probe_cb;
456 }
457 e->single.func = __ust_marker_empty_function;
458 e->single.probe_private = NULL;
459 e->multi = NULL;
460 e->ptype = 0;
461 e->format_allocated = 0;
462 e->refcount = 0;
463 cds_hlist_add_head(&e->hlist, head);
464 return e;
465 }
466
467 /*
468 * Remove the ust_marker from the ust_marker hash table. Must be called with mutex_lock
469 * held.
470 */
471 static int remove_ust_marker(const char *channel, const char *name)
472 {
473 struct cds_hlist_head *head;
474 struct cds_hlist_node *node;
475 struct ust_marker_entry *e;
476 int found = 0;
477 size_t channel_len = strlen(channel) + 1;
478 size_t name_len = strlen(name) + 1;
479 u32 hash;
480 int ret;
481
482 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
483 head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)];
484 cds_hlist_for_each_entry(e, node, head, hlist) {
485 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
486 found = 1;
487 break;
488 }
489 }
490 if (!found)
491 return -ENOENT;
492 if (e->single.func != __ust_marker_empty_function)
493 return -EBUSY;
494 cds_hlist_del(&e->hlist);
495 if (e->format_allocated)
496 free(e->format);
497 ret = ltt_channels_unregister(e->channel);
498 WARN_ON(ret);
499 free(e);
500 return 0;
501 }
502
503 /*
504 * Set the mark_entry format to the format found in the element.
505 */
506 static int ust_marker_set_format(struct ust_marker_entry *entry, const char *format)
507 {
508 entry->format = strdup(format);
509 if (!entry->format)
510 return -ENOMEM;
511 entry->format_allocated = 1;
512
513 __ust_marker(metadata, core_marker_format, NULL,
514 "channel %s name %s format %s",
515 entry->channel, entry->name, entry->format);
516 return 0;
517 }
518
519 /*
520 * Sets the probe callback corresponding to one ust_marker.
521 */
522 static int set_ust_marker(struct ust_marker_entry *entry, struct ust_marker *elem,
523 int active)
524 {
525 int ret = 0;
526 WARN_ON(strcmp(entry->name, elem->name) != 0);
527
528 if (entry->format) {
529 if (strcmp(entry->format, elem->format) != 0) {
530 ERR("Format mismatch for probe %s (%s), ust_marker (%s)",
531 entry->name,
532 entry->format,
533 elem->format);
534 return -EPERM;
535 }
536 } else {
537 ret = ust_marker_set_format(entry, elem->format);
538 if (ret)
539 return ret;
540 }
541
542 /*
543 * probe_cb setup (statically known) is done here. It is
544 * asynchronous with the rest of execution, therefore we only
545 * pass from a "safe" callback (with argument) to an "unsafe"
546 * callback (does not set arguments).
547 */
548 elem->call = entry->call;
549 elem->channel_id = entry->channel_id;
550 elem->event_id = entry->event_id;
551 /*
552 * Sanity check :
553 * We only update the single probe private data when the ptr is
554 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
555 */
556 WARN_ON(elem->single.func != __ust_marker_empty_function
557 && elem->single.probe_private != entry->single.probe_private
558 && !elem->ptype);
559 elem->single.probe_private = entry->single.probe_private;
560 /*
561 * Make sure the private data is valid when we update the
562 * single probe ptr.
563 */
564 cmm_smp_wmb();
565 elem->single.func = entry->single.func;
566 /*
567 * We also make sure that the new probe callbacks array is consistent
568 * before setting a pointer to it.
569 */
570 rcu_assign_pointer(elem->multi, entry->multi);
571 /*
572 * Update the function or multi probe array pointer before setting the
573 * ptype.
574 */
575 cmm_smp_wmb();
576 elem->ptype = entry->ptype;
577
578 if (elem->tp_name && (active ^ elem->state)) {
579 WARN_ON(!elem->tp_cb);
580 /*
581 * It is ok to directly call the probe registration because type
582 * checking has been done in the __ust_marker_tp() macro.
583 */
584
585 if (active) {
586 ret = tracepoint_probe_register_noupdate(
587 elem->tp_name,
588 elem->tp_cb, NULL);
589 } else {
590 /*
591 * tracepoint_probe_update_all() must be called
592 * before the library containing tp_cb is unloaded.
593 */
594 ret = tracepoint_probe_unregister_noupdate(
595 elem->tp_name,
596 elem->tp_cb, NULL);
597 }
598 }
599 elem->state = active;
600
601 return ret;
602 }
603
604 /*
605 * Disable a ust_marker and its probe callback.
606 * Note: only waiting an RCU period after setting elem->call to the empty
607 * function insures that the original callback is not used anymore. This insured
608 * by rcu_read_lock around the call site.
609 */
610 static void disable_ust_marker(struct ust_marker *elem)
611 {
612 int ret;
613
614 /* leave "call" as is. It is known statically. */
615 if (elem->tp_name && elem->state) {
616 WARN_ON(!elem->tp_cb);
617 /*
618 * It is ok to directly call the probe registration because type
619 * checking has been done in the __ust_marker_tp() macro.
620 */
621 /*
622 * tracepoint_probe_update_all() must be called
623 * before the module containing tp_cb is unloaded.
624 */
625 ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
626 elem->tp_cb, NULL);
627 WARN_ON(ret);
628 }
629 elem->state = 0;
630 elem->single.func = __ust_marker_empty_function;
631 /* Update the function before setting the ptype */
632 cmm_smp_wmb();
633 elem->ptype = 0; /* single probe */
634 /*
635 * Leave the private data and channel_id/event_id there, because removal
636 * is racy and should be done only after an RCU period. These are never
637 * used until the next initialization anyway.
638 */
639 }
640
641 /*
642 * is_ust_marker_enabled - Check if a ust_marker is enabled
643 * @channel: channel name
644 * @name: ust_marker name
645 *
646 * Returns 1 if the ust_marker is enabled, 0 if disabled.
647 */
648 int is_ust_marker_enabled(const char *channel, const char *name)
649 {
650 struct ust_marker_entry *entry;
651
652 lock_ust_marker();
653 entry = get_ust_marker(channel, name);
654 unlock_ust_marker();
655
656 return entry && !!entry->refcount;
657 }
658
659 /**
660 * ust_marker_update_probe_range - Update a probe range
661 * @begin: beginning of the range
662 * @end: end of the range
663 *
664 * Updates the probe callback corresponding to a range of ust_marker.
665 */
666 static
667 void ust_marker_update_probe_range(struct ust_marker * const *begin,
668 struct ust_marker * const *end)
669 {
670 struct ust_marker * const *iter;
671 struct ust_marker_entry *mark_entry;
672
673 for (iter = begin; iter < end; iter++) {
674 if (!*iter)
675 continue; /* skip dummy */
676 mark_entry = get_ust_marker((*iter)->channel, (*iter)->name);
677 if (mark_entry) {
678 set_ust_marker(mark_entry, *iter, !!mark_entry->refcount);
679 /*
680 * ignore error, continue
681 */
682 } else {
683 disable_ust_marker(*iter);
684 }
685 }
686 }
687
688 static void lib_update_ust_marker(void)
689 {
690 struct ust_marker_lib *lib;
691
692 lock_ust_marker();
693 cds_list_for_each_entry(lib, &ust_marker_libs, list)
694 ust_marker_update_probe_range(lib->ust_marker_start,
695 lib->ust_marker_start + lib->ust_marker_count);
696 unlock_ust_marker();
697 }
698
699 /*
700 * Update probes, removing the faulty probes.
701 *
702 * Internal callback only changed before the first probe is connected to it.
703 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
704 * transitions. All other transitions will leave the old private data valid.
705 * This makes the non-atomicity of the callback/private data updates valid.
706 *
707 * "special case" updates :
708 * 0 -> 1 callback
709 * 1 -> 0 callback
710 * 1 -> 2 callbacks
711 * 2 -> 1 callbacks
712 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
713 * Site effect : ust_marker_set_format may delete the ust_marker entry (creating a
714 * replacement).
715 */
716 static void ust_marker_update_probes(void)
717 {
718 lib_update_ust_marker();
719 tracepoint_probe_update_all();
720 }
721
722 /**
723 * ust_marker_probe_register - Connect a probe to a ust_marker
724 * @channel: ust_marker channel
725 * @name: ust_marker name
726 * @format: format string
727 * @probe: probe handler
728 * @probe_private: probe private data
729 *
730 * private data must be a valid allocated memory address, or NULL.
731 * Returns 0 if ok, error value on error.
732 * The probe address must at least be aligned on the architecture pointer size.
733 */
734 int ust_marker_probe_register(const char *channel, const char *name,
735 const char *format, ust_marker_probe_func *probe,
736 void *probe_private)
737 {
738 struct ust_marker_entry *entry;
739 int ret = 0, ret_err;
740 struct ust_marker_probe_array *old;
741 int first_probe = 0;
742
743 lock_ust_marker();
744 entry = get_ust_marker(channel, name);
745 if (!entry) {
746 first_probe = 1;
747 entry = add_ust_marker(channel, name, format);
748 if (IS_ERR(entry))
749 ret = PTR_ERR(entry);
750 if (ret)
751 goto end;
752 ret = ltt_channels_register(channel);
753 if (ret)
754 goto error_remove_ust_marker;
755 ret = ltt_channels_get_index_from_name(channel);
756 if (ret < 0)
757 goto error_unregister_channel;
758 entry->channel_id = ret;
759 ret = ltt_channels_get_event_id(channel, name);
760 if (ret < 0)
761 goto error_unregister_channel;
762 entry->event_id = ret;
763 ret = 0;
764 __ust_marker(metadata, core_marker_id, NULL,
765 "channel %s name %s event_id %hu "
766 "int #1u%zu long #1u%zu pointer #1u%zu "
767 "size_t #1u%zu alignment #1u%u",
768 channel, name, entry->event_id,
769 sizeof(int), sizeof(long), sizeof(void *),
770 sizeof(size_t), ltt_get_alignment());
771 } else if (format) {
772 if (!entry->format)
773 ret = ust_marker_set_format(entry, format);
774 else if (strcmp(entry->format, format))
775 ret = -EPERM;
776 if (ret)
777 goto end;
778 }
779
780 old = ust_marker_entry_add_probe(entry, probe, probe_private);
781 if (IS_ERR(old)) {
782 ret = PTR_ERR(old);
783 if (first_probe)
784 goto error_unregister_channel;
785 else
786 goto end;
787 }
788 unlock_ust_marker();
789
790 /* Activate ust_marker if necessary */
791 ust_marker_update_probes();
792
793 if (old) {
794 synchronize_rcu();
795 free_old_closure(&old->rcu);
796 }
797 return ret;
798
799 error_unregister_channel:
800 ret_err = ltt_channels_unregister(channel);
801 WARN_ON(ret_err);
802 error_remove_ust_marker:
803 ret_err = remove_ust_marker(channel, name);
804 WARN_ON(ret_err);
805 end:
806 unlock_ust_marker();
807 return ret;
808 }
809
810 /**
811 * ust_marker_probe_unregister - Disconnect a probe from a ust_marker
812 * @channel: ust_marker channel
813 * @name: ust_marker name
814 * @probe: probe function pointer
815 * @probe_private: probe private data
816 *
817 * Returns the private data given to ust_marker_probe_register, or an ERR_PTR().
818 * We do not need to call a synchronize_sched to make sure the probes have
819 * finished running before doing a module unload, because the module unload
820 * itself uses stop_machine(), which insures that every preempt disabled section
821 * have finished.
822 */
823 int ust_marker_probe_unregister(const char *channel, const char *name,
824 ust_marker_probe_func *probe, void *probe_private)
825 {
826 struct ust_marker_entry *entry;
827 struct ust_marker_probe_array *old;
828 int ret = 0;
829
830 lock_ust_marker();
831 entry = get_ust_marker(channel, name);
832 if (!entry) {
833 ret = -ENOENT;
834 goto end;
835 }
836 old = ust_marker_entry_remove_probe(entry, probe, probe_private);
837 unlock_ust_marker();
838
839 ust_marker_update_probes();
840
841 if (old) {
842 synchronize_rcu();
843 free_old_closure(&old->rcu);
844 }
845 return ret;
846
847 end:
848 unlock_ust_marker();
849 return ret;
850 }
851
852 static struct ust_marker_entry *
853 get_ust_marker_from_private_data(ust_marker_probe_func *probe,
854 void *probe_private)
855 {
856 struct ust_marker_entry *entry;
857 unsigned int i;
858 struct cds_hlist_head *head;
859 struct cds_hlist_node *node;
860
861 for (i = 0; i < UST_MARKER_TABLE_SIZE; i++) {
862 head = &ust_marker_table[i];
863 cds_hlist_for_each_entry(entry, node, head, hlist) {
864 if (!entry->ptype) {
865 if (entry->single.func == probe
866 && entry->single.probe_private
867 == probe_private)
868 return entry;
869 } else {
870 struct ust_marker_probe_array *closure;
871 closure = entry->multi;
872 for (i = 0; closure->c[i].func; i++) {
873 if (closure->c[i].func == probe &&
874 closure->c[i].probe_private
875 == probe_private)
876 return entry;
877 }
878 }
879 }
880 }
881 return NULL;
882 }
883
884 /**
885 * ust_marker_probe_unregister_private_data - Disconnect a probe from a ust_marker
886 * @probe: probe function
887 * @probe_private: probe private data
888 *
889 * Unregister a probe by providing the registered private data.
890 * Only removes the first ust_marker found in hash table.
891 * Return 0 on success or error value.
892 * We do not need to call a synchronize_sched to make sure the probes have
893 * finished running before doing a module unload, because the module unload
894 * itself uses stop_machine(), which insures that every preempt disabled section
895 * have finished.
896 */
897 int ust_marker_probe_unregister_private_data(ust_marker_probe_func *probe,
898 void *probe_private)
899 {
900 struct ust_marker_entry *entry;
901 int ret = 0;
902 struct ust_marker_probe_array *old;
903 char *channel = NULL, *name = NULL;
904
905 lock_ust_marker();
906 entry = get_ust_marker_from_private_data(probe, probe_private);
907 if (!entry) {
908 ret = -ENOENT;
909 goto unlock;
910 }
911 old = ust_marker_entry_remove_probe(entry, NULL, probe_private);
912 channel = strdup(entry->channel);
913 name = strdup(entry->name);
914 /* Ignore busy error message */
915 remove_ust_marker(channel, name);
916 unlock_ust_marker();
917
918 ust_marker_update_probes();
919
920 if (old) {
921 synchronize_rcu();
922 free_old_closure(&old->rcu);
923 }
924 goto end;
925
926 unlock:
927 unlock_ust_marker();
928 end:
929 free(channel);
930 free(name);
931 return ret;
932 }
933
934 /**
935 * ust_marker_get_private_data - Get a ust_marker's probe private data
936 * @channel: ust_marker channel
937 * @name: ust_marker name
938 * @probe: probe to match
939 * @num: get the nth matching probe's private data
940 *
941 * Returns the nth private data pointer (starting from 0) matching, or an
942 * ERR_PTR.
943 * Returns the private data pointer, or an ERR_PTR.
944 * The private data pointer should _only_ be dereferenced if the caller is the
945 * owner of the data, or its content could vanish. This is mostly used to
946 * confirm that a caller is the owner of a registered probe.
947 */
948 void *ust_marker_get_private_data(const char *channel, const char *name,
949 ust_marker_probe_func *probe, int num)
950 {
951 struct cds_hlist_head *head;
952 struct cds_hlist_node *node;
953 struct ust_marker_entry *e;
954 size_t channel_len = strlen(channel) + 1;
955 size_t name_len = strlen(name) + 1;
956 int i;
957 u32 hash;
958
959 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
960 head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)];
961 cds_hlist_for_each_entry(e, node, head, hlist) {
962 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
963 if (!e->ptype) {
964 if (num == 0 && e->single.func == probe)
965 return e->single.probe_private;
966 } else {
967 struct ust_marker_probe_array *closure;
968 int match = 0;
969 closure = e->multi;
970 for (i = 0; closure->c[i].func; i++) {
971 if (closure->c[i].func != probe)
972 continue;
973 if (match++ == num)
974 return closure->c[i].probe_private;
975 }
976 }
977 break;
978 }
979 }
980 return ERR_PTR(-ENOENT);
981 }
982
983 /**
984 * ust_marker_get_iter_range - Get a next ust_marker iterator given a range.
985 * @ust_marker: current ust_marker (in), next ust_marker (out)
986 * @begin: beginning of the range
987 * @end: end of the range
988 *
989 * Returns whether a next ust_marker has been found (1) or not (0).
990 * Will return the first ust_marker in the range if the input ust_marker is NULL.
991 * Called with markers mutex held.
992 */
993 static
994 int ust_marker_get_iter_range(struct ust_marker * const **ust_marker,
995 struct ust_marker * const *begin,
996 struct ust_marker * const *end)
997 {
998 if (!*ust_marker && begin != end)
999 *ust_marker = begin;
1000 while (*ust_marker >= begin && *ust_marker < end) {
1001 if (!**ust_marker)
1002 (*ust_marker)++; /* skip dummy */
1003 else
1004 return 1;
1005 }
1006 return 0;
1007 }
1008
1009 /*
1010 * Returns 0 if current not found.
1011 * Returns 1 if current found.
1012 * Called with markers mutex held.
1013 */
1014 static
1015 int lib_get_iter_ust_marker(struct ust_marker_iter *iter)
1016 {
1017 struct ust_marker_lib *iter_lib;
1018 int found = 0;
1019
1020 cds_list_for_each_entry(iter_lib, &ust_marker_libs, list) {
1021 if (iter_lib < iter->lib)
1022 continue;
1023 else if (iter_lib > iter->lib)
1024 iter->ust_marker = NULL;
1025 found = ust_marker_get_iter_range(&iter->ust_marker,
1026 iter_lib->ust_marker_start,
1027 iter_lib->ust_marker_start + iter_lib->ust_marker_count);
1028 if (found) {
1029 iter->lib = iter_lib;
1030 break;
1031 }
1032 }
1033 return found;
1034 }
1035
1036 /* Called with markers mutex held. */
1037 static void ust_marker_get_iter(struct ust_marker_iter *iter)
1038 {
1039 int found = 0;
1040
1041 found = lib_get_iter_ust_marker(iter);
1042 if (!found)
1043 ust_marker_iter_reset(iter);
1044 }
1045
1046 void ust_marker_iter_start(struct ust_marker_iter *iter)
1047 {
1048 lock_ust_marker();
1049 ust_marker_get_iter(iter);
1050 }
1051
1052 /* Called with markers mutex held. */
1053 void ust_marker_iter_next(struct ust_marker_iter *iter)
1054 {
1055 iter->ust_marker++;
1056 /*
1057 * iter->ust_marker may be invalid because we blindly incremented it.
1058 * Make sure it is valid by marshalling on the ust_marker, getting the
1059 * ust_marker from following modules if necessary.
1060 */
1061 ust_marker_get_iter(iter);
1062 }
1063
1064 void ust_marker_iter_stop(struct ust_marker_iter *iter)
1065 {
1066 unlock_ust_marker();
1067 }
1068
1069 void ust_marker_iter_reset(struct ust_marker_iter *iter)
1070 {
1071 iter->lib = NULL;
1072 iter->ust_marker = NULL;
1073 }
1074
1075 void ltt_dump_ust_marker_state(struct ust_trace *trace)
1076 {
1077 struct ust_marker_entry *entry;
1078 struct ltt_probe_private_data call_data;
1079 struct cds_hlist_head *head;
1080 struct cds_hlist_node *node;
1081 unsigned int i;
1082
1083 lock_ust_marker();
1084 call_data.trace = trace;
1085 call_data.serializer = NULL;
1086
1087 for (i = 0; i < UST_MARKER_TABLE_SIZE; i++) {
1088 head = &ust_marker_table[i];
1089 cds_hlist_for_each_entry(entry, node, head, hlist) {
1090 __ust_marker(metadata, core_marker_id,
1091 &call_data,
1092 "channel %s name %s event_id %hu "
1093 "int #1u%zu long #1u%zu pointer #1u%zu "
1094 "size_t #1u%zu alignment #1u%u",
1095 entry->channel,
1096 entry->name,
1097 entry->event_id,
1098 sizeof(int), sizeof(long),
1099 sizeof(void *), sizeof(size_t),
1100 ltt_get_alignment());
1101 if (entry->format)
1102 __ust_marker(metadata,
1103 core_marker_format,
1104 &call_data,
1105 "channel %s name %s format %s",
1106 entry->channel,
1107 entry->name,
1108 entry->format);
1109 }
1110 }
1111 unlock_ust_marker();
1112 }
1113
1114 void ust_marker_set_new_ust_marker_cb(void (*cb)(struct ust_marker *))
1115 {
1116 new_ust_marker_cb = cb;
1117 }
1118
1119 static void new_ust_marker(struct ust_marker * const *start,
1120 struct ust_marker * const *end)
1121 {
1122 if (new_ust_marker_cb) {
1123 struct ust_marker * const *m;
1124
1125 for (m = start; m < end; m++) {
1126 if (*m)
1127 new_ust_marker_cb(*m);
1128 }
1129 }
1130 }
1131
1132 int ust_marker_register_lib(struct ust_marker * const *ust_marker_start,
1133 int ust_marker_count)
1134 {
1135 struct ust_marker_lib *pl, *iter;
1136
1137 pl = (struct ust_marker_lib *) zmalloc(sizeof(struct ust_marker_lib));
1138
1139 pl->ust_marker_start = ust_marker_start;
1140 pl->ust_marker_count = ust_marker_count;
1141
1142 lock_ust_marker();
1143
1144 /*
1145 * We sort the libs by struct lib pointer address.
1146 */
1147 cds_list_for_each_entry_reverse(iter, &ust_marker_libs, list) {
1148 BUG_ON(iter == pl); /* Should never be in the list twice */
1149 if (iter < pl) {
1150 /* We belong to the location right after iter. */
1151 cds_list_add(&pl->list, &iter->list);
1152 goto lib_added;
1153 }
1154 }
1155 /* We should be added at the head of the list */
1156 cds_list_add(&pl->list, &ust_marker_libs);
1157 lib_added:
1158 unlock_ust_marker();
1159
1160 new_ust_marker(ust_marker_start, ust_marker_start + ust_marker_count);
1161
1162 /* TODO: update just the loaded lib */
1163 lib_update_ust_marker();
1164
1165 DBG("just registered a ust_marker section from %p and having %d ust_marker (minus dummy ust_marker)", ust_marker_start, ust_marker_count);
1166
1167 return 0;
1168 }
1169
1170 int ust_marker_unregister_lib(struct ust_marker * const *ust_marker_start)
1171 {
1172 struct ust_marker_lib *lib;
1173
1174 lock_ust_marker();
1175 cds_list_for_each_entry(lib, &ust_marker_libs, list) {
1176 if(lib->ust_marker_start == ust_marker_start) {
1177 struct ust_marker_lib *lib2free = lib;
1178 cds_list_del(&lib->list);
1179 free(lib2free);
1180 break;
1181 }
1182 }
1183 unlock_ust_marker();
1184
1185 return 0;
1186 }
1187
1188 void __attribute__((constructor)) init_ust_marker(void)
1189 {
1190 if (!initialized) {
1191 init_tracepoint();
1192 ust_marker_register_lib(__start___ust_marker_ptrs,
1193 __stop___ust_marker_ptrs
1194 - __start___ust_marker_ptrs);
1195 initialized = 1;
1196 }
1197 }
1198
1199 void __attribute__((destructor)) destroy_ust_marker(void)
1200 {
1201 ust_marker_unregister_lib(__start___ust_marker_ptrs);
1202 }
This page took 0.054809 seconds and 4 git commands to generate.