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