Fix: use after free on metadata cache reallocation
[lttng-modules.git] / lttng-events.c
1 /*
2 * lttng-events.c
3 *
4 * Holds LTTng per-session event registry.
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/utsname.h>
30 #include <linux/err.h>
31 #include "wrapper/uuid.h"
32 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
33 #include "wrapper/random.h"
34 #include "wrapper/tracepoint.h"
35 #include "lttng-kernel-version.h"
36 #include "lttng-events.h"
37 #include "lttng-tracer.h"
38 #include "lttng-abi-old.h"
39
40 #define METADATA_CACHE_DEFAULT_SIZE 4096
41
42 static LIST_HEAD(sessions);
43 static LIST_HEAD(lttng_transport_list);
44 /*
45 * Protect the sessions and metadata caches.
46 */
47 static DEFINE_MUTEX(sessions_mutex);
48 static struct kmem_cache *event_cache;
49
50 static void _lttng_event_destroy(struct lttng_event *event);
51 static void _lttng_channel_destroy(struct lttng_channel *chan);
52 static int _lttng_event_unregister(struct lttng_event *event);
53 static
54 int _lttng_event_metadata_statedump(struct lttng_session *session,
55 struct lttng_channel *chan,
56 struct lttng_event *event);
57 static
58 int _lttng_session_metadata_statedump(struct lttng_session *session);
59 static
60 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
61
62 void synchronize_trace(void)
63 {
64 synchronize_sched();
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
66 #ifdef CONFIG_PREEMPT_RT_FULL
67 synchronize_rcu();
68 #endif
69 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
70 #ifdef CONFIG_PREEMPT_RT
71 synchronize_rcu();
72 #endif
73 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
74 }
75
76 struct lttng_session *lttng_session_create(void)
77 {
78 struct lttng_session *session;
79 struct lttng_metadata_cache *metadata_cache;
80
81 mutex_lock(&sessions_mutex);
82 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
83 if (!session)
84 goto err;
85 INIT_LIST_HEAD(&session->chan);
86 INIT_LIST_HEAD(&session->events);
87 uuid_le_gen(&session->uuid);
88
89 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
90 GFP_KERNEL);
91 if (!metadata_cache)
92 goto err_free_session;
93 metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE,
94 GFP_KERNEL);
95 if (!metadata_cache->data)
96 goto err_free_cache;
97 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
98 kref_init(&metadata_cache->refcount);
99 mutex_init(&metadata_cache->lock);
100 session->metadata_cache = metadata_cache;
101 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
102 memcpy(&metadata_cache->uuid, &session->uuid,
103 sizeof(metadata_cache->uuid));
104 list_add(&session->list, &sessions);
105 mutex_unlock(&sessions_mutex);
106 return session;
107
108 err_free_cache:
109 kfree(metadata_cache);
110 err_free_session:
111 kfree(session);
112 err:
113 mutex_unlock(&sessions_mutex);
114 return NULL;
115 }
116
117 void metadata_cache_destroy(struct kref *kref)
118 {
119 struct lttng_metadata_cache *cache =
120 container_of(kref, struct lttng_metadata_cache, refcount);
121 kfree(cache->data);
122 kfree(cache);
123 }
124
125 void lttng_session_destroy(struct lttng_session *session)
126 {
127 struct lttng_channel *chan, *tmpchan;
128 struct lttng_event *event, *tmpevent;
129 struct lttng_metadata_stream *metadata_stream;
130 int ret;
131
132 mutex_lock(&sessions_mutex);
133 ACCESS_ONCE(session->active) = 0;
134 list_for_each_entry(chan, &session->chan, list) {
135 ret = lttng_syscalls_unregister(chan);
136 WARN_ON(ret);
137 }
138 list_for_each_entry(event, &session->events, list) {
139 ret = _lttng_event_unregister(event);
140 WARN_ON(ret);
141 }
142 synchronize_trace(); /* Wait for in-flight events to complete */
143 list_for_each_entry_safe(event, tmpevent, &session->events, list)
144 _lttng_event_destroy(event);
145 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
146 BUG_ON(chan->channel_type == METADATA_CHANNEL);
147 _lttng_channel_destroy(chan);
148 }
149 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
150 _lttng_metadata_channel_hangup(metadata_stream);
151 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
152 list_del(&session->list);
153 mutex_unlock(&sessions_mutex);
154 kfree(session);
155 }
156
157 int lttng_session_enable(struct lttng_session *session)
158 {
159 int ret = 0;
160 struct lttng_channel *chan;
161
162 mutex_lock(&sessions_mutex);
163 if (session->active) {
164 ret = -EBUSY;
165 goto end;
166 }
167
168 /*
169 * Snapshot the number of events per channel to know the type of header
170 * we need to use.
171 */
172 list_for_each_entry(chan, &session->chan, list) {
173 if (chan->header_type)
174 continue; /* don't change it if session stop/restart */
175 if (chan->free_event_id < 31)
176 chan->header_type = 1; /* compact */
177 else
178 chan->header_type = 2; /* large */
179 }
180
181 ACCESS_ONCE(session->active) = 1;
182 ACCESS_ONCE(session->been_active) = 1;
183 ret = _lttng_session_metadata_statedump(session);
184 if (ret) {
185 ACCESS_ONCE(session->active) = 0;
186 goto end;
187 }
188 ret = lttng_statedump_start(session);
189 if (ret)
190 ACCESS_ONCE(session->active) = 0;
191 end:
192 mutex_unlock(&sessions_mutex);
193 return ret;
194 }
195
196 int lttng_session_disable(struct lttng_session *session)
197 {
198 int ret = 0;
199
200 mutex_lock(&sessions_mutex);
201 if (!session->active) {
202 ret = -EBUSY;
203 goto end;
204 }
205 ACCESS_ONCE(session->active) = 0;
206 end:
207 mutex_unlock(&sessions_mutex);
208 return ret;
209 }
210
211 int lttng_channel_enable(struct lttng_channel *channel)
212 {
213 int old;
214
215 if (channel->channel_type == METADATA_CHANNEL)
216 return -EPERM;
217 old = xchg(&channel->enabled, 1);
218 if (old)
219 return -EEXIST;
220 return 0;
221 }
222
223 int lttng_channel_disable(struct lttng_channel *channel)
224 {
225 int old;
226
227 if (channel->channel_type == METADATA_CHANNEL)
228 return -EPERM;
229 old = xchg(&channel->enabled, 0);
230 if (!old)
231 return -EEXIST;
232 return 0;
233 }
234
235 int lttng_event_enable(struct lttng_event *event)
236 {
237 int old;
238
239 if (event->chan->channel_type == METADATA_CHANNEL)
240 return -EPERM;
241 old = xchg(&event->enabled, 1);
242 if (old)
243 return -EEXIST;
244 return 0;
245 }
246
247 int lttng_event_disable(struct lttng_event *event)
248 {
249 int old;
250
251 if (event->chan->channel_type == METADATA_CHANNEL)
252 return -EPERM;
253 old = xchg(&event->enabled, 0);
254 if (!old)
255 return -EEXIST;
256 return 0;
257 }
258
259 static struct lttng_transport *lttng_transport_find(const char *name)
260 {
261 struct lttng_transport *transport;
262
263 list_for_each_entry(transport, &lttng_transport_list, node) {
264 if (!strcmp(transport->name, name))
265 return transport;
266 }
267 return NULL;
268 }
269
270 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
271 const char *transport_name,
272 void *buf_addr,
273 size_t subbuf_size, size_t num_subbuf,
274 unsigned int switch_timer_interval,
275 unsigned int read_timer_interval,
276 enum channel_type channel_type)
277 {
278 struct lttng_channel *chan;
279 struct lttng_transport *transport = NULL;
280
281 mutex_lock(&sessions_mutex);
282 if (session->been_active && channel_type != METADATA_CHANNEL)
283 goto active; /* Refuse to add channel to active session */
284 transport = lttng_transport_find(transport_name);
285 if (!transport) {
286 printk(KERN_WARNING "LTTng transport %s not found\n",
287 transport_name);
288 goto notransport;
289 }
290 if (!try_module_get(transport->owner)) {
291 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
292 goto notransport;
293 }
294 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
295 if (!chan)
296 goto nomem;
297 chan->session = session;
298 chan->id = session->free_chan_id++;
299 chan->ops = &transport->ops;
300 /*
301 * Note: the channel creation op already writes into the packet
302 * headers. Therefore the "chan" information used as input
303 * should be already accessible.
304 */
305 chan->chan = transport->ops.channel_create(transport_name,
306 chan, buf_addr, subbuf_size, num_subbuf,
307 switch_timer_interval, read_timer_interval);
308 if (!chan->chan)
309 goto create_error;
310 chan->enabled = 1;
311 chan->transport = transport;
312 chan->channel_type = channel_type;
313 list_add(&chan->list, &session->chan);
314 mutex_unlock(&sessions_mutex);
315 return chan;
316
317 create_error:
318 kfree(chan);
319 nomem:
320 if (transport)
321 module_put(transport->owner);
322 notransport:
323 active:
324 mutex_unlock(&sessions_mutex);
325 return NULL;
326 }
327
328 /*
329 * Only used internally at session destruction for per-cpu channels, and
330 * when metadata channel is released.
331 * Needs to be called with sessions mutex held.
332 */
333 static
334 void _lttng_channel_destroy(struct lttng_channel *chan)
335 {
336 chan->ops->channel_destroy(chan->chan);
337 module_put(chan->transport->owner);
338 list_del(&chan->list);
339 lttng_destroy_context(chan->ctx);
340 kfree(chan);
341 }
342
343 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
344 {
345 BUG_ON(chan->channel_type != METADATA_CHANNEL);
346
347 /* Protect the metadata cache with the sessions_mutex. */
348 mutex_lock(&sessions_mutex);
349 _lttng_channel_destroy(chan);
350 mutex_unlock(&sessions_mutex);
351 }
352 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
353
354 static
355 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
356 {
357 stream->finalized = 1;
358 wake_up_interruptible(&stream->read_wait);
359 }
360
361 /*
362 * Supports event creation while tracing session is active.
363 */
364 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
365 struct lttng_kernel_event *event_param,
366 void *filter,
367 const struct lttng_event_desc *internal_desc)
368 {
369 struct lttng_event *event;
370 int ret;
371
372 mutex_lock(&sessions_mutex);
373 if (chan->free_event_id == -1U) {
374 ret = -EMFILE;
375 goto full;
376 }
377 /*
378 * This is O(n^2) (for each event, the loop is called at event
379 * creation). Might require a hash if we have lots of events.
380 */
381 list_for_each_entry(event, &chan->session->events, list) {
382 if (!strcmp(event->desc->name, event_param->name)) {
383 ret = -EEXIST;
384 goto exist;
385 }
386 }
387 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
388 if (!event) {
389 ret = -ENOMEM;
390 goto cache_error;
391 }
392 event->chan = chan;
393 event->filter = filter;
394 event->id = chan->free_event_id++;
395 event->enabled = 1;
396 event->instrumentation = event_param->instrumentation;
397 /* Populate lttng_event structure before tracepoint registration. */
398 smp_wmb();
399 switch (event_param->instrumentation) {
400 case LTTNG_KERNEL_TRACEPOINT:
401 event->desc = lttng_event_get(event_param->name);
402 if (!event->desc) {
403 ret = -ENOENT;
404 goto register_error;
405 }
406 ret = lttng_wrapper_tracepoint_probe_register(event->desc->kname,
407 event->desc->probe_callback,
408 event);
409 if (ret) {
410 ret = -EINVAL;
411 goto register_error;
412 }
413 break;
414 case LTTNG_KERNEL_KPROBE:
415 ret = lttng_kprobes_register(event_param->name,
416 event_param->u.kprobe.symbol_name,
417 event_param->u.kprobe.offset,
418 event_param->u.kprobe.addr,
419 event);
420 if (ret) {
421 ret = -EINVAL;
422 goto register_error;
423 }
424 ret = try_module_get(event->desc->owner);
425 WARN_ON_ONCE(!ret);
426 break;
427 case LTTNG_KERNEL_KRETPROBE:
428 {
429 struct lttng_event *event_return;
430
431 /* kretprobe defines 2 events */
432 event_return =
433 kmem_cache_zalloc(event_cache, GFP_KERNEL);
434 if (!event_return) {
435 ret = -ENOMEM;
436 goto register_error;
437 }
438 event_return->chan = chan;
439 event_return->filter = filter;
440 event_return->id = chan->free_event_id++;
441 event_return->enabled = 1;
442 event_return->instrumentation = event_param->instrumentation;
443 /*
444 * Populate lttng_event structure before kretprobe registration.
445 */
446 smp_wmb();
447 ret = lttng_kretprobes_register(event_param->name,
448 event_param->u.kretprobe.symbol_name,
449 event_param->u.kretprobe.offset,
450 event_param->u.kretprobe.addr,
451 event, event_return);
452 if (ret) {
453 kmem_cache_free(event_cache, event_return);
454 ret = -EINVAL;
455 goto register_error;
456 }
457 /* Take 2 refs on the module: one per event. */
458 ret = try_module_get(event->desc->owner);
459 WARN_ON_ONCE(!ret);
460 ret = try_module_get(event->desc->owner);
461 WARN_ON_ONCE(!ret);
462 ret = _lttng_event_metadata_statedump(chan->session, chan,
463 event_return);
464 WARN_ON_ONCE(ret > 0);
465 if (ret) {
466 kmem_cache_free(event_cache, event_return);
467 module_put(event->desc->owner);
468 module_put(event->desc->owner);
469 goto statedump_error;
470 }
471 list_add(&event_return->list, &chan->session->events);
472 break;
473 }
474 case LTTNG_KERNEL_FUNCTION:
475 ret = lttng_ftrace_register(event_param->name,
476 event_param->u.ftrace.symbol_name,
477 event);
478 if (ret) {
479 goto register_error;
480 }
481 ret = try_module_get(event->desc->owner);
482 WARN_ON_ONCE(!ret);
483 break;
484 case LTTNG_KERNEL_NOOP:
485 event->desc = internal_desc;
486 if (!event->desc) {
487 ret = -EINVAL;
488 goto register_error;
489 }
490 break;
491 default:
492 WARN_ON_ONCE(1);
493 ret = -EINVAL;
494 goto register_error;
495 }
496 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
497 WARN_ON_ONCE(ret > 0);
498 if (ret) {
499 goto statedump_error;
500 }
501 list_add(&event->list, &chan->session->events);
502 mutex_unlock(&sessions_mutex);
503 return event;
504
505 statedump_error:
506 /* If a statedump error occurs, events will not be readable. */
507 register_error:
508 kmem_cache_free(event_cache, event);
509 cache_error:
510 exist:
511 full:
512 mutex_unlock(&sessions_mutex);
513 return ERR_PTR(ret);
514 }
515
516 /*
517 * Only used internally at session destruction.
518 */
519 int _lttng_event_unregister(struct lttng_event *event)
520 {
521 int ret = -EINVAL;
522
523 switch (event->instrumentation) {
524 case LTTNG_KERNEL_TRACEPOINT:
525 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
526 event->desc->probe_callback,
527 event);
528 if (ret)
529 return ret;
530 break;
531 case LTTNG_KERNEL_KPROBE:
532 lttng_kprobes_unregister(event);
533 ret = 0;
534 break;
535 case LTTNG_KERNEL_KRETPROBE:
536 lttng_kretprobes_unregister(event);
537 ret = 0;
538 break;
539 case LTTNG_KERNEL_FUNCTION:
540 lttng_ftrace_unregister(event);
541 ret = 0;
542 break;
543 case LTTNG_KERNEL_NOOP:
544 ret = 0;
545 break;
546 default:
547 WARN_ON_ONCE(1);
548 }
549 return ret;
550 }
551
552 /*
553 * Only used internally at session destruction.
554 */
555 static
556 void _lttng_event_destroy(struct lttng_event *event)
557 {
558 switch (event->instrumentation) {
559 case LTTNG_KERNEL_TRACEPOINT:
560 lttng_event_put(event->desc);
561 break;
562 case LTTNG_KERNEL_KPROBE:
563 module_put(event->desc->owner);
564 lttng_kprobes_destroy_private(event);
565 break;
566 case LTTNG_KERNEL_KRETPROBE:
567 module_put(event->desc->owner);
568 lttng_kretprobes_destroy_private(event);
569 break;
570 case LTTNG_KERNEL_FUNCTION:
571 module_put(event->desc->owner);
572 lttng_ftrace_destroy_private(event);
573 break;
574 case LTTNG_KERNEL_NOOP:
575 break;
576 default:
577 WARN_ON_ONCE(1);
578 }
579 list_del(&event->list);
580 lttng_destroy_context(event->ctx);
581 kmem_cache_free(event_cache, event);
582 }
583
584 /*
585 * Serialize at most one packet worth of metadata into a metadata
586 * channel.
587 * We grab the metadata cache mutex to get exclusive access to our metadata
588 * buffer and to the metadata cache. Exclusive access to the metadata buffer
589 * allows us to do racy operations such as looking for remaining space left in
590 * packet and write, since mutual exclusion protects us from concurrent writes.
591 * Mutual exclusion on the metadata cache allow us to read the cache content
592 * without racing against reallocation of the cache by updates.
593 * Returns the number of bytes written in the channel, 0 if no data
594 * was written and a negative value on error.
595 */
596 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
597 struct channel *chan)
598 {
599 struct lib_ring_buffer_ctx ctx;
600 int ret = 0;
601 size_t len, reserve_len;
602
603 /*
604 * Ensure we support mutiple get_next / put sequences followed by
605 * put_next. The metadata cache lock protects reading the metadata
606 * cache. It can indeed be read concurrently by "get_next_subbuf" and
607 * "flush" operations on the buffer invoked by different processes.
608 * Moreover, since the metadata cache memory can be reallocated, we
609 * need to have exclusive access against updates even though we only
610 * read it.
611 */
612 mutex_lock(&stream->metadata_cache->lock);
613 WARN_ON(stream->metadata_in < stream->metadata_out);
614 if (stream->metadata_in != stream->metadata_out)
615 goto end;
616
617 len = stream->metadata_cache->metadata_written -
618 stream->metadata_in;
619 if (!len)
620 goto end;
621 reserve_len = min_t(size_t,
622 stream->transport->ops.packet_avail_size(chan),
623 len);
624 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
625 sizeof(char), -1);
626 /*
627 * If reservation failed, return an error to the caller.
628 */
629 ret = stream->transport->ops.event_reserve(&ctx, 0);
630 if (ret != 0) {
631 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
632 goto end;
633 }
634 stream->transport->ops.event_write(&ctx,
635 stream->metadata_cache->data + stream->metadata_in,
636 reserve_len);
637 stream->transport->ops.event_commit(&ctx);
638 stream->metadata_in += reserve_len;
639 ret = reserve_len;
640
641 end:
642 mutex_unlock(&stream->metadata_cache->lock);
643 return ret;
644 }
645
646 /*
647 * Write the metadata to the metadata cache.
648 * Must be called with sessions_mutex held.
649 * The metadata cache lock protects us from concurrent read access from
650 * thread outputting metadata content to ring buffer.
651 */
652 int lttng_metadata_printf(struct lttng_session *session,
653 const char *fmt, ...)
654 {
655 char *str;
656 size_t len;
657 va_list ap;
658 struct lttng_metadata_stream *stream;
659
660 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
661
662 va_start(ap, fmt);
663 str = kvasprintf(GFP_KERNEL, fmt, ap);
664 va_end(ap);
665 if (!str)
666 return -ENOMEM;
667
668 len = strlen(str);
669 mutex_lock(&session->metadata_cache->lock);
670 if (session->metadata_cache->metadata_written + len >
671 session->metadata_cache->cache_alloc) {
672 char *tmp_cache_realloc;
673 unsigned int tmp_cache_alloc_size;
674
675 tmp_cache_alloc_size = max_t(unsigned int,
676 session->metadata_cache->cache_alloc + len,
677 session->metadata_cache->cache_alloc << 1);
678 tmp_cache_realloc = krealloc(session->metadata_cache->data,
679 tmp_cache_alloc_size, GFP_KERNEL);
680 if (!tmp_cache_realloc)
681 goto err;
682 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
683 session->metadata_cache->data = tmp_cache_realloc;
684 }
685 memcpy(session->metadata_cache->data +
686 session->metadata_cache->metadata_written,
687 str, len);
688 session->metadata_cache->metadata_written += len;
689 mutex_unlock(&session->metadata_cache->lock);
690 kfree(str);
691
692 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
693 wake_up_interruptible(&stream->read_wait);
694
695 return 0;
696
697 err:
698 mutex_unlock(&session->metadata_cache->lock);
699 kfree(str);
700 return -ENOMEM;
701 }
702
703 /*
704 * Must be called with sessions_mutex held.
705 */
706 static
707 int _lttng_field_statedump(struct lttng_session *session,
708 const struct lttng_event_field *field)
709 {
710 int ret = 0;
711
712 switch (field->type.atype) {
713 case atype_integer:
714 ret = lttng_metadata_printf(session,
715 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
716 field->type.u.basic.integer.size,
717 field->type.u.basic.integer.alignment,
718 field->type.u.basic.integer.signedness,
719 (field->type.u.basic.integer.encoding == lttng_encode_none)
720 ? "none"
721 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
722 ? "UTF8"
723 : "ASCII",
724 field->type.u.basic.integer.base,
725 #ifdef __BIG_ENDIAN
726 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
727 #else
728 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
729 #endif
730 field->name);
731 break;
732 case atype_enum:
733 ret = lttng_metadata_printf(session,
734 " %s _%s;\n",
735 field->type.u.basic.enumeration.name,
736 field->name);
737 break;
738 case atype_array:
739 {
740 const struct lttng_basic_type *elem_type;
741
742 elem_type = &field->type.u.array.elem_type;
743 ret = lttng_metadata_printf(session,
744 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
745 elem_type->u.basic.integer.size,
746 elem_type->u.basic.integer.alignment,
747 elem_type->u.basic.integer.signedness,
748 (elem_type->u.basic.integer.encoding == lttng_encode_none)
749 ? "none"
750 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
751 ? "UTF8"
752 : "ASCII",
753 elem_type->u.basic.integer.base,
754 #ifdef __BIG_ENDIAN
755 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
756 #else
757 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
758 #endif
759 field->name, field->type.u.array.length);
760 break;
761 }
762 case atype_sequence:
763 {
764 const struct lttng_basic_type *elem_type;
765 const struct lttng_basic_type *length_type;
766
767 elem_type = &field->type.u.sequence.elem_type;
768 length_type = &field->type.u.sequence.length_type;
769 ret = lttng_metadata_printf(session,
770 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
771 length_type->u.basic.integer.size,
772 (unsigned int) length_type->u.basic.integer.alignment,
773 length_type->u.basic.integer.signedness,
774 (length_type->u.basic.integer.encoding == lttng_encode_none)
775 ? "none"
776 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
777 ? "UTF8"
778 : "ASCII"),
779 length_type->u.basic.integer.base,
780 #ifdef __BIG_ENDIAN
781 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
782 #else
783 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
784 #endif
785 field->name);
786 if (ret)
787 return ret;
788
789 ret = lttng_metadata_printf(session,
790 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
791 elem_type->u.basic.integer.size,
792 (unsigned int) elem_type->u.basic.integer.alignment,
793 elem_type->u.basic.integer.signedness,
794 (elem_type->u.basic.integer.encoding == lttng_encode_none)
795 ? "none"
796 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
797 ? "UTF8"
798 : "ASCII"),
799 elem_type->u.basic.integer.base,
800 #ifdef __BIG_ENDIAN
801 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
802 #else
803 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
804 #endif
805 field->name,
806 field->name);
807 break;
808 }
809
810 case atype_string:
811 /* Default encoding is UTF8 */
812 ret = lttng_metadata_printf(session,
813 " string%s _%s;\n",
814 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
815 " { encoding = ASCII; }" : "",
816 field->name);
817 break;
818 default:
819 WARN_ON_ONCE(1);
820 return -EINVAL;
821 }
822 return ret;
823 }
824
825 static
826 int _lttng_context_metadata_statedump(struct lttng_session *session,
827 struct lttng_ctx *ctx)
828 {
829 int ret = 0;
830 int i;
831
832 if (!ctx)
833 return 0;
834 for (i = 0; i < ctx->nr_fields; i++) {
835 const struct lttng_ctx_field *field = &ctx->fields[i];
836
837 ret = _lttng_field_statedump(session, &field->event_field);
838 if (ret)
839 return ret;
840 }
841 return ret;
842 }
843
844 static
845 int _lttng_fields_metadata_statedump(struct lttng_session *session,
846 struct lttng_event *event)
847 {
848 const struct lttng_event_desc *desc = event->desc;
849 int ret = 0;
850 int i;
851
852 for (i = 0; i < desc->nr_fields; i++) {
853 const struct lttng_event_field *field = &desc->fields[i];
854
855 ret = _lttng_field_statedump(session, field);
856 if (ret)
857 return ret;
858 }
859 return ret;
860 }
861
862 /*
863 * Must be called with sessions_mutex held.
864 */
865 static
866 int _lttng_event_metadata_statedump(struct lttng_session *session,
867 struct lttng_channel *chan,
868 struct lttng_event *event)
869 {
870 int ret = 0;
871
872 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
873 return 0;
874 if (chan->channel_type == METADATA_CHANNEL)
875 return 0;
876
877 ret = lttng_metadata_printf(session,
878 "event {\n"
879 " name = \"%s\";\n"
880 " id = %u;\n"
881 " stream_id = %u;\n",
882 event->desc->name,
883 event->id,
884 event->chan->id);
885 if (ret)
886 goto end;
887
888 if (event->ctx) {
889 ret = lttng_metadata_printf(session,
890 " context := struct {\n");
891 if (ret)
892 goto end;
893 }
894 ret = _lttng_context_metadata_statedump(session, event->ctx);
895 if (ret)
896 goto end;
897 if (event->ctx) {
898 ret = lttng_metadata_printf(session,
899 " };\n");
900 if (ret)
901 goto end;
902 }
903
904 ret = lttng_metadata_printf(session,
905 " fields := struct {\n"
906 );
907 if (ret)
908 goto end;
909
910 ret = _lttng_fields_metadata_statedump(session, event);
911 if (ret)
912 goto end;
913
914 /*
915 * LTTng space reservation can only reserve multiples of the
916 * byte size.
917 */
918 ret = lttng_metadata_printf(session,
919 " };\n"
920 "};\n\n");
921 if (ret)
922 goto end;
923
924 event->metadata_dumped = 1;
925 end:
926 return ret;
927
928 }
929
930 /*
931 * Must be called with sessions_mutex held.
932 */
933 static
934 int _lttng_channel_metadata_statedump(struct lttng_session *session,
935 struct lttng_channel *chan)
936 {
937 int ret = 0;
938
939 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
940 return 0;
941
942 if (chan->channel_type == METADATA_CHANNEL)
943 return 0;
944
945 WARN_ON_ONCE(!chan->header_type);
946 ret = lttng_metadata_printf(session,
947 "stream {\n"
948 " id = %u;\n"
949 " event.header := %s;\n"
950 " packet.context := struct packet_context;\n",
951 chan->id,
952 chan->header_type == 1 ? "struct event_header_compact" :
953 "struct event_header_large");
954 if (ret)
955 goto end;
956
957 if (chan->ctx) {
958 ret = lttng_metadata_printf(session,
959 " event.context := struct {\n");
960 if (ret)
961 goto end;
962 }
963 ret = _lttng_context_metadata_statedump(session, chan->ctx);
964 if (ret)
965 goto end;
966 if (chan->ctx) {
967 ret = lttng_metadata_printf(session,
968 " };\n");
969 if (ret)
970 goto end;
971 }
972
973 ret = lttng_metadata_printf(session,
974 "};\n\n");
975
976 chan->metadata_dumped = 1;
977 end:
978 return ret;
979 }
980
981 /*
982 * Must be called with sessions_mutex held.
983 */
984 static
985 int _lttng_stream_packet_context_declare(struct lttng_session *session)
986 {
987 return lttng_metadata_printf(session,
988 "struct packet_context {\n"
989 " uint64_clock_monotonic_t timestamp_begin;\n"
990 " uint64_clock_monotonic_t timestamp_end;\n"
991 " uint64_t content_size;\n"
992 " uint64_t packet_size;\n"
993 " unsigned long events_discarded;\n"
994 " uint32_t cpu_id;\n"
995 "};\n\n"
996 );
997 }
998
999 /*
1000 * Compact header:
1001 * id: range: 0 - 30.
1002 * id 31 is reserved to indicate an extended header.
1003 *
1004 * Large header:
1005 * id: range: 0 - 65534.
1006 * id 65535 is reserved to indicate an extended header.
1007 *
1008 * Must be called with sessions_mutex held.
1009 */
1010 static
1011 int _lttng_event_header_declare(struct lttng_session *session)
1012 {
1013 return lttng_metadata_printf(session,
1014 "struct event_header_compact {\n"
1015 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1016 " variant <id> {\n"
1017 " struct {\n"
1018 " uint27_clock_monotonic_t timestamp;\n"
1019 " } compact;\n"
1020 " struct {\n"
1021 " uint32_t id;\n"
1022 " uint64_clock_monotonic_t timestamp;\n"
1023 " } extended;\n"
1024 " } v;\n"
1025 "} align(%u);\n"
1026 "\n"
1027 "struct event_header_large {\n"
1028 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1029 " variant <id> {\n"
1030 " struct {\n"
1031 " uint32_clock_monotonic_t timestamp;\n"
1032 " } compact;\n"
1033 " struct {\n"
1034 " uint32_t id;\n"
1035 " uint64_clock_monotonic_t timestamp;\n"
1036 " } extended;\n"
1037 " } v;\n"
1038 "} align(%u);\n\n",
1039 lttng_alignof(uint32_t) * CHAR_BIT,
1040 lttng_alignof(uint16_t) * CHAR_BIT
1041 );
1042 }
1043
1044 /*
1045 * Approximation of NTP time of day to clock monotonic correlation,
1046 * taken at start of trace.
1047 * Yes, this is only an approximation. Yes, we can (and will) do better
1048 * in future versions.
1049 */
1050 static
1051 uint64_t measure_clock_offset(void)
1052 {
1053 uint64_t offset, monotonic[2], realtime;
1054 struct timespec rts = { 0, 0 };
1055 unsigned long flags;
1056
1057 /* Disable interrupts to increase correlation precision. */
1058 local_irq_save(flags);
1059 monotonic[0] = trace_clock_read64();
1060 getnstimeofday(&rts);
1061 monotonic[1] = trace_clock_read64();
1062 local_irq_restore(flags);
1063
1064 offset = (monotonic[0] + monotonic[1]) >> 1;
1065 realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
1066 realtime += rts.tv_nsec;
1067 offset = realtime - offset;
1068 return offset;
1069 }
1070
1071 /*
1072 * Output metadata into this session's metadata buffers.
1073 * Must be called with sessions_mutex held.
1074 */
1075 static
1076 int _lttng_session_metadata_statedump(struct lttng_session *session)
1077 {
1078 unsigned char *uuid_c = session->uuid.b;
1079 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
1080 struct lttng_channel *chan;
1081 struct lttng_event *event;
1082 int ret = 0;
1083
1084 if (!ACCESS_ONCE(session->active))
1085 return 0;
1086 if (session->metadata_dumped)
1087 goto skip_session;
1088
1089 snprintf(uuid_s, sizeof(uuid_s),
1090 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1091 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1092 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1093 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1094 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1095
1096 ret = lttng_metadata_printf(session,
1097 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1098 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1099 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1100 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1101 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1102 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1103 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1104 "\n"
1105 "trace {\n"
1106 " major = %u;\n"
1107 " minor = %u;\n"
1108 " uuid = \"%s\";\n"
1109 " byte_order = %s;\n"
1110 " packet.header := struct {\n"
1111 " uint32_t magic;\n"
1112 " uint8_t uuid[16];\n"
1113 " uint32_t stream_id;\n"
1114 " };\n"
1115 "};\n\n",
1116 lttng_alignof(uint8_t) * CHAR_BIT,
1117 lttng_alignof(uint16_t) * CHAR_BIT,
1118 lttng_alignof(uint32_t) * CHAR_BIT,
1119 lttng_alignof(uint64_t) * CHAR_BIT,
1120 sizeof(unsigned long) * CHAR_BIT,
1121 lttng_alignof(unsigned long) * CHAR_BIT,
1122 CTF_SPEC_MAJOR,
1123 CTF_SPEC_MINOR,
1124 uuid_s,
1125 #ifdef __BIG_ENDIAN
1126 "be"
1127 #else
1128 "le"
1129 #endif
1130 );
1131 if (ret)
1132 goto end;
1133
1134 ret = lttng_metadata_printf(session,
1135 "env {\n"
1136 " hostname = \"%s\";\n"
1137 " domain = \"kernel\";\n"
1138 " sysname = \"%s\";\n"
1139 " kernel_release = \"%s\";\n"
1140 " kernel_version = \"%s\";\n"
1141 " tracer_name = \"lttng-modules\";\n"
1142 " tracer_major = %d;\n"
1143 " tracer_minor = %d;\n"
1144 " tracer_patchlevel = %d;\n"
1145 "};\n\n",
1146 current->nsproxy->uts_ns->name.nodename,
1147 utsname()->sysname,
1148 utsname()->release,
1149 utsname()->version,
1150 LTTNG_MODULES_MAJOR_VERSION,
1151 LTTNG_MODULES_MINOR_VERSION,
1152 LTTNG_MODULES_PATCHLEVEL_VERSION
1153 );
1154 if (ret)
1155 goto end;
1156
1157 ret = lttng_metadata_printf(session,
1158 "clock {\n"
1159 " name = %s;\n",
1160 "monotonic"
1161 );
1162 if (ret)
1163 goto end;
1164
1165 if (!trace_clock_uuid(clock_uuid_s)) {
1166 ret = lttng_metadata_printf(session,
1167 " uuid = \"%s\";\n",
1168 clock_uuid_s
1169 );
1170 if (ret)
1171 goto end;
1172 }
1173
1174 ret = lttng_metadata_printf(session,
1175 " description = \"Monotonic Clock\";\n"
1176 " freq = %llu; /* Frequency, in Hz */\n"
1177 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1178 " offset = %llu;\n"
1179 "};\n\n",
1180 (unsigned long long) trace_clock_freq(),
1181 (unsigned long long) measure_clock_offset()
1182 );
1183 if (ret)
1184 goto end;
1185
1186 ret = lttng_metadata_printf(session,
1187 "typealias integer {\n"
1188 " size = 27; align = 1; signed = false;\n"
1189 " map = clock.monotonic.value;\n"
1190 "} := uint27_clock_monotonic_t;\n"
1191 "\n"
1192 "typealias integer {\n"
1193 " size = 32; align = %u; signed = false;\n"
1194 " map = clock.monotonic.value;\n"
1195 "} := uint32_clock_monotonic_t;\n"
1196 "\n"
1197 "typealias integer {\n"
1198 " size = 64; align = %u; signed = false;\n"
1199 " map = clock.monotonic.value;\n"
1200 "} := uint64_clock_monotonic_t;\n\n",
1201 lttng_alignof(uint32_t) * CHAR_BIT,
1202 lttng_alignof(uint64_t) * CHAR_BIT
1203 );
1204 if (ret)
1205 goto end;
1206
1207 ret = _lttng_stream_packet_context_declare(session);
1208 if (ret)
1209 goto end;
1210
1211 ret = _lttng_event_header_declare(session);
1212 if (ret)
1213 goto end;
1214
1215 skip_session:
1216 list_for_each_entry(chan, &session->chan, list) {
1217 ret = _lttng_channel_metadata_statedump(session, chan);
1218 if (ret)
1219 goto end;
1220 }
1221
1222 list_for_each_entry(event, &session->events, list) {
1223 ret = _lttng_event_metadata_statedump(session, event->chan, event);
1224 if (ret)
1225 goto end;
1226 }
1227 session->metadata_dumped = 1;
1228 end:
1229 return ret;
1230 }
1231
1232 /**
1233 * lttng_transport_register - LTT transport registration
1234 * @transport: transport structure
1235 *
1236 * Registers a transport which can be used as output to extract the data out of
1237 * LTTng. The module calling this registration function must ensure that no
1238 * trap-inducing code will be executed by the transport functions. E.g.
1239 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1240 * is made visible to the transport function. This registration acts as a
1241 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1242 * after its registration must it synchronize the TLBs.
1243 */
1244 void lttng_transport_register(struct lttng_transport *transport)
1245 {
1246 /*
1247 * Make sure no page fault can be triggered by the module about to be
1248 * registered. We deal with this here so we don't have to call
1249 * vmalloc_sync_all() in each module's init.
1250 */
1251 wrapper_vmalloc_sync_all();
1252
1253 mutex_lock(&sessions_mutex);
1254 list_add_tail(&transport->node, &lttng_transport_list);
1255 mutex_unlock(&sessions_mutex);
1256 }
1257 EXPORT_SYMBOL_GPL(lttng_transport_register);
1258
1259 /**
1260 * lttng_transport_unregister - LTT transport unregistration
1261 * @transport: transport structure
1262 */
1263 void lttng_transport_unregister(struct lttng_transport *transport)
1264 {
1265 mutex_lock(&sessions_mutex);
1266 list_del(&transport->node);
1267 mutex_unlock(&sessions_mutex);
1268 }
1269 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
1270
1271 static int __init lttng_events_init(void)
1272 {
1273 int ret;
1274
1275 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
1276 if (ret)
1277 return ret;
1278
1279 ret = lttng_tracepoint_init();
1280 if (ret)
1281 return ret;
1282 event_cache = KMEM_CACHE(lttng_event, 0);
1283 if (!event_cache) {
1284 ret = -ENOMEM;
1285 goto error_kmem;
1286 }
1287 ret = lttng_abi_init();
1288 if (ret)
1289 goto error_abi;
1290 ret = lttng_logger_init();
1291 if (ret)
1292 goto error_logger;
1293 return 0;
1294
1295 error_logger:
1296 lttng_abi_exit();
1297 error_abi:
1298 kmem_cache_destroy(event_cache);
1299 error_kmem:
1300 lttng_tracepoint_exit();
1301 return ret;
1302 }
1303
1304 module_init(lttng_events_init);
1305
1306 static void __exit lttng_events_exit(void)
1307 {
1308 struct lttng_session *session, *tmpsession;
1309
1310 lttng_logger_exit();
1311 lttng_abi_exit();
1312 list_for_each_entry_safe(session, tmpsession, &sessions, list)
1313 lttng_session_destroy(session);
1314 kmem_cache_destroy(event_cache);
1315 lttng_tracepoint_exit();
1316 }
1317
1318 module_exit(lttng_events_exit);
1319
1320 MODULE_LICENSE("GPL and additional rights");
1321 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1322 MODULE_DESCRIPTION("LTTng Events");
1323 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
1324 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
1325 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
1326 LTTNG_MODULES_EXTRAVERSION);
This page took 0.073618 seconds and 5 git commands to generate.