fix: vmalloc on v5.8 without KALLSYMS
[lttng-modules.git] / lttng-events.c
1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
2 *
3 * lttng-events.c
4 *
5 * Holds LTTng per-session event registry.
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 /*
11 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
12 * overrides a function with a define.
13 */
14 #include "wrapper/page_alloc.h"
15
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <linux/utsname.h>
22 #include <linux/err.h>
23 #include <linux/seq_file.h>
24 #include <linux/file.h>
25 #include <linux/anon_inodes.h>
26 #include <wrapper/file.h>
27 #include <linux/jhash.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/dmi.h>
31
32 #include <wrapper/uuid.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <wrapper/random.h>
35 #include <wrapper/tracepoint.h>
36 #include <wrapper/list.h>
37 #include <wrapper/types.h>
38 #include <lttng-kernel-version.h>
39 #include <lttng-events.h>
40 #include <lttng-tracer.h>
41 #include <lttng-abi-old.h>
42 #include <lttng-endian.h>
43 #include <lttng-string-utils.h>
44 #include <wrapper/ringbuffer/backend.h>
45 #include <wrapper/ringbuffer/frontend.h>
46 #include <wrapper/time.h>
47
48 #define METADATA_CACHE_DEFAULT_SIZE 4096
49
50 static LIST_HEAD(sessions);
51 static LIST_HEAD(lttng_transport_list);
52 /*
53 * Protect the sessions and metadata caches.
54 */
55 static DEFINE_MUTEX(sessions_mutex);
56 static struct kmem_cache *event_cache;
57
58 static void lttng_session_lazy_sync_enablers(struct lttng_session *session);
59 static void lttng_session_sync_enablers(struct lttng_session *session);
60 static void lttng_enabler_destroy(struct lttng_enabler *enabler);
61
62 static void _lttng_event_destroy(struct lttng_event *event);
63 static void _lttng_channel_destroy(struct lttng_channel *chan);
64 static int _lttng_event_unregister(struct lttng_event *event);
65 static
66 int _lttng_event_metadata_statedump(struct lttng_session *session,
67 struct lttng_channel *chan,
68 struct lttng_event *event);
69 static
70 int _lttng_session_metadata_statedump(struct lttng_session *session);
71 static
72 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
73 static
74 int _lttng_field_statedump(struct lttng_session *session,
75 const struct lttng_event_field *field,
76 size_t nesting);
77
78 void synchronize_trace(void)
79 {
80 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
81 synchronize_rcu();
82 #else
83 synchronize_sched();
84 #endif
85
86 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
87 #ifdef CONFIG_PREEMPT_RT_FULL
88 synchronize_rcu();
89 #endif
90 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
91 #ifdef CONFIG_PREEMPT_RT
92 synchronize_rcu();
93 #endif
94 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
95 }
96
97 void lttng_lock_sessions(void)
98 {
99 mutex_lock(&sessions_mutex);
100 }
101
102 void lttng_unlock_sessions(void)
103 {
104 mutex_unlock(&sessions_mutex);
105 }
106
107 /*
108 * Called with sessions lock held.
109 */
110 int lttng_session_active(void)
111 {
112 struct lttng_session *iter;
113
114 list_for_each_entry(iter, &sessions, list) {
115 if (iter->active)
116 return 1;
117 }
118 return 0;
119 }
120
121 struct lttng_session *lttng_session_create(void)
122 {
123 struct lttng_session *session;
124 struct lttng_metadata_cache *metadata_cache;
125 int i;
126
127 mutex_lock(&sessions_mutex);
128 session = lttng_kvzalloc(sizeof(struct lttng_session), GFP_KERNEL);
129 if (!session)
130 goto err;
131 INIT_LIST_HEAD(&session->chan);
132 INIT_LIST_HEAD(&session->events);
133 lttng_guid_gen(&session->uuid);
134
135 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
136 GFP_KERNEL);
137 if (!metadata_cache)
138 goto err_free_session;
139 metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE);
140 if (!metadata_cache->data)
141 goto err_free_cache;
142 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
143 kref_init(&metadata_cache->refcount);
144 mutex_init(&metadata_cache->lock);
145 session->metadata_cache = metadata_cache;
146 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
147 memcpy(&metadata_cache->uuid, &session->uuid,
148 sizeof(metadata_cache->uuid));
149 INIT_LIST_HEAD(&session->enablers_head);
150 for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++)
151 INIT_HLIST_HEAD(&session->events_ht.table[i]);
152 list_add(&session->list, &sessions);
153 session->pid_tracker.session = session;
154 session->pid_tracker.tracker_type = TRACKER_PID;
155 session->vpid_tracker.session = session;
156 session->vpid_tracker.tracker_type = TRACKER_VPID;
157 session->uid_tracker.session = session;
158 session->uid_tracker.tracker_type = TRACKER_UID;
159 session->vuid_tracker.session = session;
160 session->vuid_tracker.tracker_type = TRACKER_VUID;
161 session->gid_tracker.session = session;
162 session->gid_tracker.tracker_type = TRACKER_GID;
163 session->vgid_tracker.session = session;
164 session->vgid_tracker.tracker_type = TRACKER_VGID;
165 mutex_unlock(&sessions_mutex);
166 return session;
167
168 err_free_cache:
169 kfree(metadata_cache);
170 err_free_session:
171 lttng_kvfree(session);
172 err:
173 mutex_unlock(&sessions_mutex);
174 return NULL;
175 }
176
177 void metadata_cache_destroy(struct kref *kref)
178 {
179 struct lttng_metadata_cache *cache =
180 container_of(kref, struct lttng_metadata_cache, refcount);
181 vfree(cache->data);
182 kfree(cache);
183 }
184
185 void lttng_session_destroy(struct lttng_session *session)
186 {
187 struct lttng_channel *chan, *tmpchan;
188 struct lttng_event *event, *tmpevent;
189 struct lttng_metadata_stream *metadata_stream;
190 struct lttng_enabler *enabler, *tmpenabler;
191 int ret;
192
193 mutex_lock(&sessions_mutex);
194 WRITE_ONCE(session->active, 0);
195 list_for_each_entry(chan, &session->chan, list) {
196 ret = lttng_syscalls_unregister(chan);
197 WARN_ON(ret);
198 }
199 list_for_each_entry(event, &session->events, list) {
200 ret = _lttng_event_unregister(event);
201 WARN_ON(ret);
202 }
203 synchronize_trace(); /* Wait for in-flight events to complete */
204 list_for_each_entry_safe(enabler, tmpenabler,
205 &session->enablers_head, node)
206 lttng_enabler_destroy(enabler);
207 list_for_each_entry_safe(event, tmpevent, &session->events, list)
208 _lttng_event_destroy(event);
209 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
210 BUG_ON(chan->channel_type == METADATA_CHANNEL);
211 _lttng_channel_destroy(chan);
212 }
213 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
214 _lttng_metadata_channel_hangup(metadata_stream);
215 lttng_id_tracker_destroy(&session->pid_tracker, false);
216 lttng_id_tracker_destroy(&session->vpid_tracker, false);
217 lttng_id_tracker_destroy(&session->uid_tracker, false);
218 lttng_id_tracker_destroy(&session->vuid_tracker, false);
219 lttng_id_tracker_destroy(&session->gid_tracker, false);
220 lttng_id_tracker_destroy(&session->vgid_tracker, false);
221 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
222 list_del(&session->list);
223 mutex_unlock(&sessions_mutex);
224 lttng_kvfree(session);
225 }
226
227 int lttng_session_statedump(struct lttng_session *session)
228 {
229 int ret;
230
231 mutex_lock(&sessions_mutex);
232 ret = lttng_statedump_start(session);
233 mutex_unlock(&sessions_mutex);
234 return ret;
235 }
236
237 int lttng_session_enable(struct lttng_session *session)
238 {
239 int ret = 0;
240 struct lttng_channel *chan;
241
242 mutex_lock(&sessions_mutex);
243 if (session->active) {
244 ret = -EBUSY;
245 goto end;
246 }
247
248 /* Set transient enabler state to "enabled" */
249 session->tstate = 1;
250
251 /* We need to sync enablers with session before activation. */
252 lttng_session_sync_enablers(session);
253
254 /*
255 * Snapshot the number of events per channel to know the type of header
256 * we need to use.
257 */
258 list_for_each_entry(chan, &session->chan, list) {
259 if (chan->header_type)
260 continue; /* don't change it if session stop/restart */
261 if (chan->free_event_id < 31)
262 chan->header_type = 1; /* compact */
263 else
264 chan->header_type = 2; /* large */
265 }
266
267 /* Clear each stream's quiescent state. */
268 list_for_each_entry(chan, &session->chan, list) {
269 if (chan->channel_type != METADATA_CHANNEL)
270 lib_ring_buffer_clear_quiescent_channel(chan->chan);
271 }
272
273 WRITE_ONCE(session->active, 1);
274 WRITE_ONCE(session->been_active, 1);
275 ret = _lttng_session_metadata_statedump(session);
276 if (ret) {
277 WRITE_ONCE(session->active, 0);
278 goto end;
279 }
280 ret = lttng_statedump_start(session);
281 if (ret)
282 WRITE_ONCE(session->active, 0);
283 end:
284 mutex_unlock(&sessions_mutex);
285 return ret;
286 }
287
288 int lttng_session_disable(struct lttng_session *session)
289 {
290 int ret = 0;
291 struct lttng_channel *chan;
292
293 mutex_lock(&sessions_mutex);
294 if (!session->active) {
295 ret = -EBUSY;
296 goto end;
297 }
298 WRITE_ONCE(session->active, 0);
299
300 /* Set transient enabler state to "disabled" */
301 session->tstate = 0;
302 lttng_session_sync_enablers(session);
303
304 /* Set each stream's quiescent state. */
305 list_for_each_entry(chan, &session->chan, list) {
306 if (chan->channel_type != METADATA_CHANNEL)
307 lib_ring_buffer_set_quiescent_channel(chan->chan);
308 }
309 end:
310 mutex_unlock(&sessions_mutex);
311 return ret;
312 }
313
314 int lttng_session_metadata_regenerate(struct lttng_session *session)
315 {
316 int ret = 0;
317 struct lttng_channel *chan;
318 struct lttng_event *event;
319 struct lttng_metadata_cache *cache = session->metadata_cache;
320 struct lttng_metadata_stream *stream;
321
322 mutex_lock(&sessions_mutex);
323 if (!session->active) {
324 ret = -EBUSY;
325 goto end;
326 }
327
328 mutex_lock(&cache->lock);
329 memset(cache->data, 0, cache->cache_alloc);
330 cache->metadata_written = 0;
331 cache->version++;
332 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) {
333 stream->metadata_out = 0;
334 stream->metadata_in = 0;
335 }
336 mutex_unlock(&cache->lock);
337
338 session->metadata_dumped = 0;
339 list_for_each_entry(chan, &session->chan, list) {
340 chan->metadata_dumped = 0;
341 }
342
343 list_for_each_entry(event, &session->events, list) {
344 event->metadata_dumped = 0;
345 }
346
347 ret = _lttng_session_metadata_statedump(session);
348
349 end:
350 mutex_unlock(&sessions_mutex);
351 return ret;
352 }
353
354 int lttng_channel_enable(struct lttng_channel *channel)
355 {
356 int ret = 0;
357
358 mutex_lock(&sessions_mutex);
359 if (channel->channel_type == METADATA_CHANNEL) {
360 ret = -EPERM;
361 goto end;
362 }
363 if (channel->enabled) {
364 ret = -EEXIST;
365 goto end;
366 }
367 /* Set transient enabler state to "enabled" */
368 channel->tstate = 1;
369 lttng_session_sync_enablers(channel->session);
370 /* Set atomically the state to "enabled" */
371 WRITE_ONCE(channel->enabled, 1);
372 end:
373 mutex_unlock(&sessions_mutex);
374 return ret;
375 }
376
377 int lttng_channel_disable(struct lttng_channel *channel)
378 {
379 int ret = 0;
380
381 mutex_lock(&sessions_mutex);
382 if (channel->channel_type == METADATA_CHANNEL) {
383 ret = -EPERM;
384 goto end;
385 }
386 if (!channel->enabled) {
387 ret = -EEXIST;
388 goto end;
389 }
390 /* Set atomically the state to "disabled" */
391 WRITE_ONCE(channel->enabled, 0);
392 /* Set transient enabler state to "enabled" */
393 channel->tstate = 0;
394 lttng_session_sync_enablers(channel->session);
395 end:
396 mutex_unlock(&sessions_mutex);
397 return ret;
398 }
399
400 int lttng_event_enable(struct lttng_event *event)
401 {
402 int ret = 0;
403
404 mutex_lock(&sessions_mutex);
405 if (event->chan->channel_type == METADATA_CHANNEL) {
406 ret = -EPERM;
407 goto end;
408 }
409 if (event->enabled) {
410 ret = -EEXIST;
411 goto end;
412 }
413 switch (event->instrumentation) {
414 case LTTNG_KERNEL_TRACEPOINT:
415 case LTTNG_KERNEL_SYSCALL:
416 ret = -EINVAL;
417 break;
418 case LTTNG_KERNEL_KPROBE:
419 case LTTNG_KERNEL_UPROBE:
420 case LTTNG_KERNEL_NOOP:
421 WRITE_ONCE(event->enabled, 1);
422 break;
423 case LTTNG_KERNEL_KRETPROBE:
424 ret = lttng_kretprobes_event_enable_state(event, 1);
425 break;
426 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
427 default:
428 WARN_ON_ONCE(1);
429 ret = -EINVAL;
430 }
431 end:
432 mutex_unlock(&sessions_mutex);
433 return ret;
434 }
435
436 int lttng_event_disable(struct lttng_event *event)
437 {
438 int ret = 0;
439
440 mutex_lock(&sessions_mutex);
441 if (event->chan->channel_type == METADATA_CHANNEL) {
442 ret = -EPERM;
443 goto end;
444 }
445 if (!event->enabled) {
446 ret = -EEXIST;
447 goto end;
448 }
449 switch (event->instrumentation) {
450 case LTTNG_KERNEL_TRACEPOINT:
451 case LTTNG_KERNEL_SYSCALL:
452 ret = -EINVAL;
453 break;
454 case LTTNG_KERNEL_KPROBE:
455 case LTTNG_KERNEL_UPROBE:
456 case LTTNG_KERNEL_NOOP:
457 WRITE_ONCE(event->enabled, 0);
458 break;
459 case LTTNG_KERNEL_KRETPROBE:
460 ret = lttng_kretprobes_event_enable_state(event, 0);
461 break;
462 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
463 default:
464 WARN_ON_ONCE(1);
465 ret = -EINVAL;
466 }
467 end:
468 mutex_unlock(&sessions_mutex);
469 return ret;
470 }
471
472 static struct lttng_transport *lttng_transport_find(const char *name)
473 {
474 struct lttng_transport *transport;
475
476 list_for_each_entry(transport, &lttng_transport_list, node) {
477 if (!strcmp(transport->name, name))
478 return transport;
479 }
480 return NULL;
481 }
482
483 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
484 const char *transport_name,
485 void *buf_addr,
486 size_t subbuf_size, size_t num_subbuf,
487 unsigned int switch_timer_interval,
488 unsigned int read_timer_interval,
489 enum channel_type channel_type)
490 {
491 struct lttng_channel *chan;
492 struct lttng_transport *transport = NULL;
493
494 mutex_lock(&sessions_mutex);
495 if (session->been_active && channel_type != METADATA_CHANNEL)
496 goto active; /* Refuse to add channel to active session */
497 transport = lttng_transport_find(transport_name);
498 if (!transport) {
499 printk(KERN_WARNING "LTTng transport %s not found\n",
500 transport_name);
501 goto notransport;
502 }
503 if (!try_module_get(transport->owner)) {
504 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
505 goto notransport;
506 }
507 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
508 if (!chan)
509 goto nomem;
510 chan->session = session;
511 chan->id = session->free_chan_id++;
512 chan->ops = &transport->ops;
513 /*
514 * Note: the channel creation op already writes into the packet
515 * headers. Therefore the "chan" information used as input
516 * should be already accessible.
517 */
518 chan->chan = transport->ops.channel_create(transport_name,
519 chan, buf_addr, subbuf_size, num_subbuf,
520 switch_timer_interval, read_timer_interval);
521 if (!chan->chan)
522 goto create_error;
523 chan->tstate = 1;
524 chan->enabled = 1;
525 chan->transport = transport;
526 chan->channel_type = channel_type;
527 list_add(&chan->list, &session->chan);
528 mutex_unlock(&sessions_mutex);
529 return chan;
530
531 create_error:
532 kfree(chan);
533 nomem:
534 if (transport)
535 module_put(transport->owner);
536 notransport:
537 active:
538 mutex_unlock(&sessions_mutex);
539 return NULL;
540 }
541
542 /*
543 * Only used internally at session destruction for per-cpu channels, and
544 * when metadata channel is released.
545 * Needs to be called with sessions mutex held.
546 */
547 static
548 void _lttng_channel_destroy(struct lttng_channel *chan)
549 {
550 chan->ops->channel_destroy(chan->chan);
551 module_put(chan->transport->owner);
552 list_del(&chan->list);
553 lttng_destroy_context(chan->ctx);
554 kfree(chan);
555 }
556
557 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
558 {
559 BUG_ON(chan->channel_type != METADATA_CHANNEL);
560
561 /* Protect the metadata cache with the sessions_mutex. */
562 mutex_lock(&sessions_mutex);
563 _lttng_channel_destroy(chan);
564 mutex_unlock(&sessions_mutex);
565 }
566 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
567
568 static
569 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
570 {
571 stream->finalized = 1;
572 wake_up_interruptible(&stream->read_wait);
573 }
574
575 /*
576 * Supports event creation while tracing session is active.
577 * Needs to be called with sessions mutex held.
578 */
579 struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
580 struct lttng_kernel_event *event_param,
581 void *filter,
582 const struct lttng_event_desc *event_desc,
583 enum lttng_kernel_instrumentation itype)
584 {
585 struct lttng_session *session = chan->session;
586 struct lttng_event *event;
587 const char *event_name;
588 struct hlist_head *head;
589 size_t name_len;
590 uint32_t hash;
591 int ret;
592
593 if (chan->free_event_id == -1U) {
594 ret = -EMFILE;
595 goto full;
596 }
597
598 switch (itype) {
599 case LTTNG_KERNEL_TRACEPOINT:
600 event_name = event_desc->name;
601 break;
602 case LTTNG_KERNEL_KPROBE:
603 case LTTNG_KERNEL_UPROBE:
604 case LTTNG_KERNEL_KRETPROBE:
605 case LTTNG_KERNEL_NOOP:
606 case LTTNG_KERNEL_SYSCALL:
607 event_name = event_param->name;
608 break;
609 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
610 default:
611 WARN_ON_ONCE(1);
612 ret = -EINVAL;
613 goto type_error;
614 }
615 name_len = strlen(event_name);
616 hash = jhash(event_name, name_len, 0);
617 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
618 lttng_hlist_for_each_entry(event, head, hlist) {
619 WARN_ON_ONCE(!event->desc);
620 if (!strncmp(event->desc->name, event_name,
621 LTTNG_KERNEL_SYM_NAME_LEN - 1)
622 && chan == event->chan) {
623 ret = -EEXIST;
624 goto exist;
625 }
626 }
627
628 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
629 if (!event) {
630 ret = -ENOMEM;
631 goto cache_error;
632 }
633 event->chan = chan;
634 event->filter = filter;
635 event->id = chan->free_event_id++;
636 event->instrumentation = itype;
637 event->evtype = LTTNG_TYPE_EVENT;
638 INIT_LIST_HEAD(&event->bytecode_runtime_head);
639 INIT_LIST_HEAD(&event->enablers_ref_head);
640
641 switch (itype) {
642 case LTTNG_KERNEL_TRACEPOINT:
643 /* Event will be enabled by enabler sync. */
644 event->enabled = 0;
645 event->registered = 0;
646 event->desc = lttng_event_get(event_name);
647 if (!event->desc) {
648 ret = -ENOENT;
649 goto register_error;
650 }
651 /* Populate lttng_event structure before event registration. */
652 smp_wmb();
653 break;
654 case LTTNG_KERNEL_KPROBE:
655 /*
656 * Needs to be explicitly enabled after creation, since
657 * we may want to apply filters.
658 */
659 event->enabled = 0;
660 event->registered = 1;
661 /*
662 * Populate lttng_event structure before event
663 * registration.
664 */
665 smp_wmb();
666 ret = lttng_kprobes_register(event_name,
667 event_param->u.kprobe.symbol_name,
668 event_param->u.kprobe.offset,
669 event_param->u.kprobe.addr,
670 event);
671 if (ret) {
672 ret = -EINVAL;
673 goto register_error;
674 }
675 ret = try_module_get(event->desc->owner);
676 WARN_ON_ONCE(!ret);
677 break;
678 case LTTNG_KERNEL_KRETPROBE:
679 {
680 struct lttng_event *event_return;
681
682 /* kretprobe defines 2 events */
683 /*
684 * Needs to be explicitly enabled after creation, since
685 * we may want to apply filters.
686 */
687 event->enabled = 0;
688 event->registered = 1;
689 event_return =
690 kmem_cache_zalloc(event_cache, GFP_KERNEL);
691 if (!event_return) {
692 ret = -ENOMEM;
693 goto register_error;
694 }
695 event_return->chan = chan;
696 event_return->filter = filter;
697 event_return->id = chan->free_event_id++;
698 event_return->enabled = 0;
699 event_return->registered = 1;
700 event_return->instrumentation = itype;
701 /*
702 * Populate lttng_event structure before kretprobe registration.
703 */
704 smp_wmb();
705 ret = lttng_kretprobes_register(event_name,
706 event_param->u.kretprobe.symbol_name,
707 event_param->u.kretprobe.offset,
708 event_param->u.kretprobe.addr,
709 event, event_return);
710 if (ret) {
711 kmem_cache_free(event_cache, event_return);
712 ret = -EINVAL;
713 goto register_error;
714 }
715 /* Take 2 refs on the module: one per event. */
716 ret = try_module_get(event->desc->owner);
717 WARN_ON_ONCE(!ret);
718 ret = try_module_get(event->desc->owner);
719 WARN_ON_ONCE(!ret);
720 ret = _lttng_event_metadata_statedump(chan->session, chan,
721 event_return);
722 WARN_ON_ONCE(ret > 0);
723 if (ret) {
724 kmem_cache_free(event_cache, event_return);
725 module_put(event->desc->owner);
726 module_put(event->desc->owner);
727 goto statedump_error;
728 }
729 list_add(&event_return->list, &chan->session->events);
730 break;
731 }
732 case LTTNG_KERNEL_NOOP:
733 case LTTNG_KERNEL_SYSCALL:
734 /*
735 * Needs to be explicitly enabled after creation, since
736 * we may want to apply filters.
737 */
738 event->enabled = 0;
739 event->registered = 0;
740 event->desc = event_desc;
741 if (!event->desc) {
742 ret = -EINVAL;
743 goto register_error;
744 }
745 break;
746 case LTTNG_KERNEL_UPROBE:
747 /*
748 * Needs to be explicitly enabled after creation, since
749 * we may want to apply filters.
750 */
751 event->enabled = 0;
752 event->registered = 1;
753
754 /*
755 * Populate lttng_event structure before event
756 * registration.
757 */
758 smp_wmb();
759
760 ret = lttng_uprobes_register(event_param->name,
761 event_param->u.uprobe.fd,
762 event);
763 if (ret)
764 goto register_error;
765 ret = try_module_get(event->desc->owner);
766 WARN_ON_ONCE(!ret);
767 break;
768 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
769 default:
770 WARN_ON_ONCE(1);
771 ret = -EINVAL;
772 goto register_error;
773 }
774 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
775 WARN_ON_ONCE(ret > 0);
776 if (ret) {
777 goto statedump_error;
778 }
779 hlist_add_head(&event->hlist, head);
780 list_add(&event->list, &chan->session->events);
781 return event;
782
783 statedump_error:
784 /* If a statedump error occurs, events will not be readable. */
785 register_error:
786 kmem_cache_free(event_cache, event);
787 cache_error:
788 exist:
789 type_error:
790 full:
791 return ERR_PTR(ret);
792 }
793
794 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
795 struct lttng_kernel_event *event_param,
796 void *filter,
797 const struct lttng_event_desc *event_desc,
798 enum lttng_kernel_instrumentation itype)
799 {
800 struct lttng_event *event;
801
802 mutex_lock(&sessions_mutex);
803 event = _lttng_event_create(chan, event_param, filter, event_desc,
804 itype);
805 mutex_unlock(&sessions_mutex);
806 return event;
807 }
808
809 /* Only used for tracepoints for now. */
810 static
811 void register_event(struct lttng_event *event)
812 {
813 const struct lttng_event_desc *desc;
814 int ret = -EINVAL;
815
816 if (event->registered)
817 return;
818
819 desc = event->desc;
820 switch (event->instrumentation) {
821 case LTTNG_KERNEL_TRACEPOINT:
822 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
823 desc->probe_callback,
824 event);
825 break;
826 case LTTNG_KERNEL_SYSCALL:
827 ret = lttng_syscall_filter_enable(event->chan,
828 desc->name);
829 break;
830 case LTTNG_KERNEL_KPROBE:
831 case LTTNG_KERNEL_UPROBE:
832 case LTTNG_KERNEL_KRETPROBE:
833 case LTTNG_KERNEL_NOOP:
834 ret = 0;
835 break;
836 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
837 default:
838 WARN_ON_ONCE(1);
839 }
840 if (!ret)
841 event->registered = 1;
842 }
843
844 /*
845 * Only used internally at session destruction.
846 */
847 int _lttng_event_unregister(struct lttng_event *event)
848 {
849 const struct lttng_event_desc *desc;
850 int ret = -EINVAL;
851
852 if (!event->registered)
853 return 0;
854
855 desc = event->desc;
856 switch (event->instrumentation) {
857 case LTTNG_KERNEL_TRACEPOINT:
858 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
859 event->desc->probe_callback,
860 event);
861 break;
862 case LTTNG_KERNEL_KPROBE:
863 lttng_kprobes_unregister(event);
864 ret = 0;
865 break;
866 case LTTNG_KERNEL_KRETPROBE:
867 lttng_kretprobes_unregister(event);
868 ret = 0;
869 break;
870 case LTTNG_KERNEL_SYSCALL:
871 ret = lttng_syscall_filter_disable(event->chan,
872 desc->name);
873 break;
874 case LTTNG_KERNEL_NOOP:
875 ret = 0;
876 break;
877 case LTTNG_KERNEL_UPROBE:
878 lttng_uprobes_unregister(event);
879 ret = 0;
880 break;
881 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
882 default:
883 WARN_ON_ONCE(1);
884 }
885 if (!ret)
886 event->registered = 0;
887 return ret;
888 }
889
890 /*
891 * Only used internally at session destruction.
892 */
893 static
894 void _lttng_event_destroy(struct lttng_event *event)
895 {
896 switch (event->instrumentation) {
897 case LTTNG_KERNEL_TRACEPOINT:
898 lttng_event_put(event->desc);
899 break;
900 case LTTNG_KERNEL_KPROBE:
901 module_put(event->desc->owner);
902 lttng_kprobes_destroy_private(event);
903 break;
904 case LTTNG_KERNEL_KRETPROBE:
905 module_put(event->desc->owner);
906 lttng_kretprobes_destroy_private(event);
907 break;
908 case LTTNG_KERNEL_NOOP:
909 case LTTNG_KERNEL_SYSCALL:
910 break;
911 case LTTNG_KERNEL_UPROBE:
912 module_put(event->desc->owner);
913 lttng_uprobes_destroy_private(event);
914 break;
915 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
916 default:
917 WARN_ON_ONCE(1);
918 }
919 list_del(&event->list);
920 lttng_destroy_context(event->ctx);
921 kmem_cache_free(event_cache, event);
922 }
923
924 struct lttng_id_tracker *get_tracker(struct lttng_session *session,
925 enum tracker_type tracker_type)
926 {
927 switch (tracker_type) {
928 case TRACKER_PID:
929 return &session->pid_tracker;
930 case TRACKER_VPID:
931 return &session->vpid_tracker;
932 case TRACKER_UID:
933 return &session->uid_tracker;
934 case TRACKER_VUID:
935 return &session->vuid_tracker;
936 case TRACKER_GID:
937 return &session->gid_tracker;
938 case TRACKER_VGID:
939 return &session->vgid_tracker;
940 default:
941 WARN_ON_ONCE(1);
942 return NULL;
943 }
944 }
945
946 int lttng_session_track_id(struct lttng_session *session,
947 enum tracker_type tracker_type, int id)
948 {
949 struct lttng_id_tracker *tracker;
950 int ret;
951
952 tracker = get_tracker(session, tracker_type);
953 if (!tracker)
954 return -EINVAL;
955 if (id < -1)
956 return -EINVAL;
957 mutex_lock(&sessions_mutex);
958 if (id == -1) {
959 /* track all ids: destroy tracker. */
960 lttng_id_tracker_destroy(tracker, true);
961 ret = 0;
962 } else {
963 ret = lttng_id_tracker_add(tracker, id);
964 }
965 mutex_unlock(&sessions_mutex);
966 return ret;
967 }
968
969 int lttng_session_untrack_id(struct lttng_session *session,
970 enum tracker_type tracker_type, int id)
971 {
972 struct lttng_id_tracker *tracker;
973 int ret;
974
975 tracker = get_tracker(session, tracker_type);
976 if (!tracker)
977 return -EINVAL;
978 if (id < -1)
979 return -EINVAL;
980 mutex_lock(&sessions_mutex);
981 if (id == -1) {
982 /* untrack all ids: replace by empty tracker. */
983 ret = lttng_id_tracker_empty_set(tracker);
984 } else {
985 ret = lttng_id_tracker_del(tracker, id);
986 }
987 mutex_unlock(&sessions_mutex);
988 return ret;
989 }
990
991 static
992 void *id_list_start(struct seq_file *m, loff_t *pos)
993 {
994 struct lttng_id_tracker *id_tracker = m->private;
995 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
996 struct lttng_id_hash_node *e;
997 int iter = 0, i;
998
999 mutex_lock(&sessions_mutex);
1000 if (id_tracker_p) {
1001 for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) {
1002 struct hlist_head *head = &id_tracker_p->id_hash[i];
1003
1004 lttng_hlist_for_each_entry(e, head, hlist) {
1005 if (iter++ >= *pos)
1006 return e;
1007 }
1008 }
1009 } else {
1010 /* ID tracker disabled. */
1011 if (iter >= *pos && iter == 0) {
1012 return id_tracker_p; /* empty tracker */
1013 }
1014 iter++;
1015 }
1016 /* End of list */
1017 return NULL;
1018 }
1019
1020 /* Called with sessions_mutex held. */
1021 static
1022 void *id_list_next(struct seq_file *m, void *p, loff_t *ppos)
1023 {
1024 struct lttng_id_tracker *id_tracker = m->private;
1025 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1026 struct lttng_id_hash_node *e;
1027 int iter = 0, i;
1028
1029 (*ppos)++;
1030 if (id_tracker_p) {
1031 for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) {
1032 struct hlist_head *head = &id_tracker_p->id_hash[i];
1033
1034 lttng_hlist_for_each_entry(e, head, hlist) {
1035 if (iter++ >= *ppos)
1036 return e;
1037 }
1038 }
1039 } else {
1040 /* ID tracker disabled. */
1041 if (iter >= *ppos && iter == 0)
1042 return p; /* empty tracker */
1043 iter++;
1044 }
1045
1046 /* End of list */
1047 return NULL;
1048 }
1049
1050 static
1051 void id_list_stop(struct seq_file *m, void *p)
1052 {
1053 mutex_unlock(&sessions_mutex);
1054 }
1055
1056 static
1057 int id_list_show(struct seq_file *m, void *p)
1058 {
1059 struct lttng_id_tracker *id_tracker = m->private;
1060 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1061 int id;
1062
1063 if (p == id_tracker_p) {
1064 /* Tracker disabled. */
1065 id = -1;
1066 } else {
1067 const struct lttng_id_hash_node *e = p;
1068
1069 id = lttng_id_tracker_get_node_id(e);
1070 }
1071 switch (id_tracker->tracker_type) {
1072 case TRACKER_PID:
1073 seq_printf(m, "process { pid = %d; };\n", id);
1074 break;
1075 case TRACKER_VPID:
1076 seq_printf(m, "process { vpid = %d; };\n", id);
1077 break;
1078 case TRACKER_UID:
1079 seq_printf(m, "user { uid = %d; };\n", id);
1080 break;
1081 case TRACKER_VUID:
1082 seq_printf(m, "user { vuid = %d; };\n", id);
1083 break;
1084 case TRACKER_GID:
1085 seq_printf(m, "group { gid = %d; };\n", id);
1086 break;
1087 case TRACKER_VGID:
1088 seq_printf(m, "group { vgid = %d; };\n", id);
1089 break;
1090 default:
1091 seq_printf(m, "UNKNOWN { field = %d };\n", id);
1092 }
1093 return 0;
1094 }
1095
1096 static
1097 const struct seq_operations lttng_tracker_ids_list_seq_ops = {
1098 .start = id_list_start,
1099 .next = id_list_next,
1100 .stop = id_list_stop,
1101 .show = id_list_show,
1102 };
1103
1104 static
1105 int lttng_tracker_ids_list_open(struct inode *inode, struct file *file)
1106 {
1107 return seq_open(file, &lttng_tracker_ids_list_seq_ops);
1108 }
1109
1110 static
1111 int lttng_tracker_ids_list_release(struct inode *inode, struct file *file)
1112 {
1113 struct seq_file *m = file->private_data;
1114 struct lttng_id_tracker *id_tracker = m->private;
1115 int ret;
1116
1117 WARN_ON_ONCE(!id_tracker);
1118 ret = seq_release(inode, file);
1119 if (!ret)
1120 fput(id_tracker->session->file);
1121 return ret;
1122 }
1123
1124 const struct file_operations lttng_tracker_ids_list_fops = {
1125 .owner = THIS_MODULE,
1126 .open = lttng_tracker_ids_list_open,
1127 .read = seq_read,
1128 .llseek = seq_lseek,
1129 .release = lttng_tracker_ids_list_release,
1130 };
1131
1132 int lttng_session_list_tracker_ids(struct lttng_session *session,
1133 enum tracker_type tracker_type)
1134 {
1135 struct file *tracker_ids_list_file;
1136 struct seq_file *m;
1137 int file_fd, ret;
1138
1139 file_fd = lttng_get_unused_fd();
1140 if (file_fd < 0) {
1141 ret = file_fd;
1142 goto fd_error;
1143 }
1144
1145 tracker_ids_list_file = anon_inode_getfile("[lttng_tracker_ids_list]",
1146 &lttng_tracker_ids_list_fops,
1147 NULL, O_RDWR);
1148 if (IS_ERR(tracker_ids_list_file)) {
1149 ret = PTR_ERR(tracker_ids_list_file);
1150 goto file_error;
1151 }
1152 if (!atomic_long_add_unless(&session->file->f_count, 1, LONG_MAX)) {
1153 ret = -EOVERFLOW;
1154 goto refcount_error;
1155 }
1156 ret = lttng_tracker_ids_list_fops.open(NULL, tracker_ids_list_file);
1157 if (ret < 0)
1158 goto open_error;
1159 m = tracker_ids_list_file->private_data;
1160
1161 m->private = get_tracker(session, tracker_type);
1162 BUG_ON(!m->private);
1163 fd_install(file_fd, tracker_ids_list_file);
1164
1165 return file_fd;
1166
1167 open_error:
1168 atomic_long_dec(&session->file->f_count);
1169 refcount_error:
1170 fput(tracker_ids_list_file);
1171 file_error:
1172 put_unused_fd(file_fd);
1173 fd_error:
1174 return ret;
1175 }
1176
1177 /*
1178 * Enabler management.
1179 */
1180 static
1181 int lttng_match_enabler_star_glob(const char *desc_name,
1182 const char *pattern)
1183 {
1184 if (!strutils_star_glob_match(pattern, LTTNG_SIZE_MAX,
1185 desc_name, LTTNG_SIZE_MAX))
1186 return 0;
1187 return 1;
1188 }
1189
1190 static
1191 int lttng_match_enabler_name(const char *desc_name,
1192 const char *name)
1193 {
1194 if (strcmp(desc_name, name))
1195 return 0;
1196 return 1;
1197 }
1198
1199 static
1200 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1201 struct lttng_enabler *enabler)
1202 {
1203 const char *desc_name, *enabler_name;
1204
1205 enabler_name = enabler->event_param.name;
1206 switch (enabler->event_param.instrumentation) {
1207 case LTTNG_KERNEL_TRACEPOINT:
1208 desc_name = desc->name;
1209 break;
1210 case LTTNG_KERNEL_SYSCALL:
1211 desc_name = desc->name;
1212 if (!strncmp(desc_name, "compat_", strlen("compat_")))
1213 desc_name += strlen("compat_");
1214 if (!strncmp(desc_name, "syscall_exit_",
1215 strlen("syscall_exit_"))) {
1216 desc_name += strlen("syscall_exit_");
1217 } else if (!strncmp(desc_name, "syscall_entry_",
1218 strlen("syscall_entry_"))) {
1219 desc_name += strlen("syscall_entry_");
1220 } else {
1221 WARN_ON_ONCE(1);
1222 return -EINVAL;
1223 }
1224 break;
1225 default:
1226 WARN_ON_ONCE(1);
1227 return -EINVAL;
1228 }
1229 switch (enabler->type) {
1230 case LTTNG_ENABLER_STAR_GLOB:
1231 return lttng_match_enabler_star_glob(desc_name, enabler_name);
1232 case LTTNG_ENABLER_NAME:
1233 return lttng_match_enabler_name(desc_name, enabler_name);
1234 default:
1235 return -EINVAL;
1236 }
1237 }
1238
1239 static
1240 int lttng_event_match_enabler(struct lttng_event *event,
1241 struct lttng_enabler *enabler)
1242 {
1243 if (enabler->event_param.instrumentation != event->instrumentation)
1244 return 0;
1245 if (lttng_desc_match_enabler(event->desc, enabler)
1246 && event->chan == enabler->chan)
1247 return 1;
1248 else
1249 return 0;
1250 }
1251
1252 static
1253 struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event,
1254 struct lttng_enabler *enabler)
1255 {
1256 struct lttng_enabler_ref *enabler_ref;
1257
1258 list_for_each_entry(enabler_ref,
1259 &event->enablers_ref_head, node) {
1260 if (enabler_ref->ref == enabler)
1261 return enabler_ref;
1262 }
1263 return NULL;
1264 }
1265
1266 static
1267 void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
1268 {
1269 struct lttng_session *session = enabler->chan->session;
1270 struct lttng_probe_desc *probe_desc;
1271 const struct lttng_event_desc *desc;
1272 int i;
1273 struct list_head *probe_list;
1274
1275 probe_list = lttng_get_probe_list_head();
1276 /*
1277 * For each probe event, if we find that a probe event matches
1278 * our enabler, create an associated lttng_event if not
1279 * already present.
1280 */
1281 list_for_each_entry(probe_desc, probe_list, head) {
1282 for (i = 0; i < probe_desc->nr_events; i++) {
1283 int found = 0;
1284 struct hlist_head *head;
1285 const char *event_name;
1286 size_t name_len;
1287 uint32_t hash;
1288 struct lttng_event *event;
1289
1290 desc = probe_desc->event_desc[i];
1291 if (!lttng_desc_match_enabler(desc, enabler))
1292 continue;
1293 event_name = desc->name;
1294 name_len = strlen(event_name);
1295
1296 /*
1297 * Check if already created.
1298 */
1299 hash = jhash(event_name, name_len, 0);
1300 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
1301 lttng_hlist_for_each_entry(event, head, hlist) {
1302 if (event->desc == desc
1303 && event->chan == enabler->chan)
1304 found = 1;
1305 }
1306 if (found)
1307 continue;
1308
1309 /*
1310 * We need to create an event for this
1311 * event probe.
1312 */
1313 event = _lttng_event_create(enabler->chan,
1314 NULL, NULL, desc,
1315 LTTNG_KERNEL_TRACEPOINT);
1316 if (!event) {
1317 printk(KERN_INFO "Unable to create event %s\n",
1318 probe_desc->event_desc[i]->name);
1319 }
1320 }
1321 }
1322 }
1323
1324 static
1325 void lttng_create_syscall_if_missing(struct lttng_enabler *enabler)
1326 {
1327 int ret;
1328
1329 ret = lttng_syscalls_register(enabler->chan, NULL);
1330 WARN_ON_ONCE(ret);
1331 }
1332
1333 /*
1334 * Create struct lttng_event if it is missing and present in the list of
1335 * tracepoint probes.
1336 * Should be called with sessions mutex held.
1337 */
1338 static
1339 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
1340 {
1341 switch (enabler->event_param.instrumentation) {
1342 case LTTNG_KERNEL_TRACEPOINT:
1343 lttng_create_tracepoint_if_missing(enabler);
1344 break;
1345 case LTTNG_KERNEL_SYSCALL:
1346 lttng_create_syscall_if_missing(enabler);
1347 break;
1348 default:
1349 WARN_ON_ONCE(1);
1350 break;
1351 }
1352 }
1353
1354 /*
1355 * Create events associated with an enabler (if not already present),
1356 * and add backward reference from the event to the enabler.
1357 * Should be called with sessions mutex held.
1358 */
1359 static
1360 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
1361 {
1362 struct lttng_session *session = enabler->chan->session;
1363 struct lttng_event *event;
1364
1365 /* First ensure that probe events are created for this enabler. */
1366 lttng_create_event_if_missing(enabler);
1367
1368 /* For each event matching enabler in session event list. */
1369 list_for_each_entry(event, &session->events, list) {
1370 struct lttng_enabler_ref *enabler_ref;
1371
1372 if (!lttng_event_match_enabler(event, enabler))
1373 continue;
1374 enabler_ref = lttng_event_enabler_ref(event, enabler);
1375 if (!enabler_ref) {
1376 /*
1377 * If no backward ref, create it.
1378 * Add backward ref from event to enabler.
1379 */
1380 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1381 if (!enabler_ref)
1382 return -ENOMEM;
1383 enabler_ref->ref = enabler;
1384 list_add(&enabler_ref->node,
1385 &event->enablers_ref_head);
1386 }
1387
1388 /*
1389 * Link filter bytecodes if not linked yet.
1390 */
1391 lttng_enabler_event_link_bytecode(event, enabler);
1392
1393 /* TODO: merge event context. */
1394 }
1395 return 0;
1396 }
1397
1398 /*
1399 * Called at module load: connect the probe on all enablers matching
1400 * this event.
1401 * Called with sessions lock held.
1402 */
1403 int lttng_fix_pending_events(void)
1404 {
1405 struct lttng_session *session;
1406
1407 list_for_each_entry(session, &sessions, list)
1408 lttng_session_lazy_sync_enablers(session);
1409 return 0;
1410 }
1411
1412 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1413 struct lttng_kernel_event *event_param,
1414 struct lttng_channel *chan)
1415 {
1416 struct lttng_enabler *enabler;
1417
1418 enabler = kzalloc(sizeof(*enabler), GFP_KERNEL);
1419 if (!enabler)
1420 return NULL;
1421 enabler->type = type;
1422 INIT_LIST_HEAD(&enabler->filter_bytecode_head);
1423 memcpy(&enabler->event_param, event_param,
1424 sizeof(enabler->event_param));
1425 enabler->chan = chan;
1426 /* ctx left NULL */
1427 enabler->enabled = 0;
1428 enabler->evtype = LTTNG_TYPE_ENABLER;
1429 mutex_lock(&sessions_mutex);
1430 list_add(&enabler->node, &enabler->chan->session->enablers_head);
1431 lttng_session_lazy_sync_enablers(enabler->chan->session);
1432 mutex_unlock(&sessions_mutex);
1433 return enabler;
1434 }
1435
1436 int lttng_enabler_enable(struct lttng_enabler *enabler)
1437 {
1438 mutex_lock(&sessions_mutex);
1439 enabler->enabled = 1;
1440 lttng_session_lazy_sync_enablers(enabler->chan->session);
1441 mutex_unlock(&sessions_mutex);
1442 return 0;
1443 }
1444
1445 int lttng_enabler_disable(struct lttng_enabler *enabler)
1446 {
1447 mutex_lock(&sessions_mutex);
1448 enabler->enabled = 0;
1449 lttng_session_lazy_sync_enablers(enabler->chan->session);
1450 mutex_unlock(&sessions_mutex);
1451 return 0;
1452 }
1453
1454 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1455 struct lttng_kernel_filter_bytecode __user *bytecode)
1456 {
1457 struct lttng_filter_bytecode_node *bytecode_node;
1458 uint32_t bytecode_len;
1459 int ret;
1460
1461 ret = get_user(bytecode_len, &bytecode->len);
1462 if (ret)
1463 return ret;
1464 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1465 GFP_KERNEL);
1466 if (!bytecode_node)
1467 return -ENOMEM;
1468 ret = copy_from_user(&bytecode_node->bc, bytecode,
1469 sizeof(*bytecode) + bytecode_len);
1470 if (ret)
1471 goto error_free;
1472 bytecode_node->enabler = enabler;
1473 /* Enforce length based on allocated size */
1474 bytecode_node->bc.len = bytecode_len;
1475 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1476 lttng_session_lazy_sync_enablers(enabler->chan->session);
1477 return 0;
1478
1479 error_free:
1480 kfree(bytecode_node);
1481 return ret;
1482 }
1483
1484 int lttng_event_add_callsite(struct lttng_event *event,
1485 struct lttng_kernel_event_callsite __user *callsite)
1486 {
1487
1488 switch (event->instrumentation) {
1489 case LTTNG_KERNEL_UPROBE:
1490 return lttng_uprobes_add_callsite(event, callsite);
1491 default:
1492 return -EINVAL;
1493 }
1494 }
1495
1496 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1497 struct lttng_kernel_context *context_param)
1498 {
1499 return -ENOSYS;
1500 }
1501
1502 static
1503 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1504 {
1505 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1506
1507 /* Destroy filter bytecode */
1508 list_for_each_entry_safe(filter_node, tmp_filter_node,
1509 &enabler->filter_bytecode_head, node) {
1510 kfree(filter_node);
1511 }
1512
1513 /* Destroy contexts */
1514 lttng_destroy_context(enabler->ctx);
1515
1516 list_del(&enabler->node);
1517 kfree(enabler);
1518 }
1519
1520 /*
1521 * lttng_session_sync_enablers should be called just before starting a
1522 * session.
1523 * Should be called with sessions mutex held.
1524 */
1525 static
1526 void lttng_session_sync_enablers(struct lttng_session *session)
1527 {
1528 struct lttng_enabler *enabler;
1529 struct lttng_event *event;
1530
1531 list_for_each_entry(enabler, &session->enablers_head, node)
1532 lttng_enabler_ref_events(enabler);
1533 /*
1534 * For each event, if at least one of its enablers is enabled,
1535 * and its channel and session transient states are enabled, we
1536 * enable the event, else we disable it.
1537 */
1538 list_for_each_entry(event, &session->events, list) {
1539 struct lttng_enabler_ref *enabler_ref;
1540 struct lttng_bytecode_runtime *runtime;
1541 int enabled = 0, has_enablers_without_bytecode = 0;
1542
1543 switch (event->instrumentation) {
1544 case LTTNG_KERNEL_TRACEPOINT:
1545 case LTTNG_KERNEL_SYSCALL:
1546 /* Enable events */
1547 list_for_each_entry(enabler_ref,
1548 &event->enablers_ref_head, node) {
1549 if (enabler_ref->ref->enabled) {
1550 enabled = 1;
1551 break;
1552 }
1553 }
1554 break;
1555 default:
1556 /* Not handled with lazy sync. */
1557 continue;
1558 }
1559 /*
1560 * Enabled state is based on union of enablers, with
1561 * intesection of session and channel transient enable
1562 * states.
1563 */
1564 enabled = enabled && session->tstate && event->chan->tstate;
1565
1566 WRITE_ONCE(event->enabled, enabled);
1567 /*
1568 * Sync tracepoint registration with event enabled
1569 * state.
1570 */
1571 if (enabled) {
1572 register_event(event);
1573 } else {
1574 _lttng_event_unregister(event);
1575 }
1576
1577 /* Check if has enablers without bytecode enabled */
1578 list_for_each_entry(enabler_ref,
1579 &event->enablers_ref_head, node) {
1580 if (enabler_ref->ref->enabled
1581 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1582 has_enablers_without_bytecode = 1;
1583 break;
1584 }
1585 }
1586 event->has_enablers_without_bytecode =
1587 has_enablers_without_bytecode;
1588
1589 /* Enable filters */
1590 list_for_each_entry(runtime,
1591 &event->bytecode_runtime_head, node)
1592 lttng_filter_sync_state(runtime);
1593 }
1594 }
1595
1596 /*
1597 * Apply enablers to session events, adding events to session if need
1598 * be. It is required after each modification applied to an active
1599 * session, and right before session "start".
1600 * "lazy" sync means we only sync if required.
1601 * Should be called with sessions mutex held.
1602 */
1603 static
1604 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1605 {
1606 /* We can skip if session is not active */
1607 if (!session->active)
1608 return;
1609 lttng_session_sync_enablers(session);
1610 }
1611
1612 /*
1613 * Serialize at most one packet worth of metadata into a metadata
1614 * channel.
1615 * We grab the metadata cache mutex to get exclusive access to our metadata
1616 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1617 * allows us to do racy operations such as looking for remaining space left in
1618 * packet and write, since mutual exclusion protects us from concurrent writes.
1619 * Mutual exclusion on the metadata cache allow us to read the cache content
1620 * without racing against reallocation of the cache by updates.
1621 * Returns the number of bytes written in the channel, 0 if no data
1622 * was written and a negative value on error.
1623 */
1624 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1625 struct channel *chan, bool *coherent)
1626 {
1627 struct lib_ring_buffer_ctx ctx;
1628 int ret = 0;
1629 size_t len, reserve_len;
1630
1631 /*
1632 * Ensure we support mutiple get_next / put sequences followed by
1633 * put_next. The metadata cache lock protects reading the metadata
1634 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1635 * "flush" operations on the buffer invoked by different processes.
1636 * Moreover, since the metadata cache memory can be reallocated, we
1637 * need to have exclusive access against updates even though we only
1638 * read it.
1639 */
1640 mutex_lock(&stream->metadata_cache->lock);
1641 WARN_ON(stream->metadata_in < stream->metadata_out);
1642 if (stream->metadata_in != stream->metadata_out)
1643 goto end;
1644
1645 /* Metadata regenerated, change the version. */
1646 if (stream->metadata_cache->version != stream->version)
1647 stream->version = stream->metadata_cache->version;
1648
1649 len = stream->metadata_cache->metadata_written -
1650 stream->metadata_in;
1651 if (!len)
1652 goto end;
1653 reserve_len = min_t(size_t,
1654 stream->transport->ops.packet_avail_size(chan),
1655 len);
1656 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
1657 sizeof(char), -1);
1658 /*
1659 * If reservation failed, return an error to the caller.
1660 */
1661 ret = stream->transport->ops.event_reserve(&ctx, 0);
1662 if (ret != 0) {
1663 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1664 stream->coherent = false;
1665 goto end;
1666 }
1667 stream->transport->ops.event_write(&ctx,
1668 stream->metadata_cache->data + stream->metadata_in,
1669 reserve_len);
1670 stream->transport->ops.event_commit(&ctx);
1671 stream->metadata_in += reserve_len;
1672 if (reserve_len < len || stream->metadata_cache->producing != 0)
1673 stream->coherent = false;
1674 else
1675 stream->coherent = true;
1676 ret = reserve_len;
1677
1678 end:
1679 if (coherent)
1680 *coherent = stream->coherent;
1681 mutex_unlock(&stream->metadata_cache->lock);
1682 return ret;
1683 }
1684
1685 static
1686 void lttng_metadata_begin(struct lttng_session *session)
1687 {
1688 mutex_lock(&session->metadata_cache->lock);
1689 session->metadata_cache->producing++;
1690 mutex_unlock(&session->metadata_cache->lock);
1691 }
1692
1693 static
1694 void lttng_metadata_end(struct lttng_session *session)
1695 {
1696 mutex_lock(&session->metadata_cache->lock);
1697 WARN_ON_ONCE(!session->metadata_cache->producing);
1698 session->metadata_cache->producing--;
1699 mutex_unlock(&session->metadata_cache->lock);
1700 }
1701
1702 /*
1703 * Write the metadata to the metadata cache.
1704 * Must be called with sessions_mutex held.
1705 * The metadata cache lock protects us from concurrent read access from
1706 * thread outputting metadata content to ring buffer.
1707 * The content of the printf is printed as a single atomic metadata
1708 * transaction.
1709 */
1710 int lttng_metadata_printf(struct lttng_session *session,
1711 const char *fmt, ...)
1712 {
1713 char *str;
1714 size_t len;
1715 va_list ap;
1716 struct lttng_metadata_stream *stream;
1717
1718 WARN_ON_ONCE(!READ_ONCE(session->active));
1719
1720 va_start(ap, fmt);
1721 str = kvasprintf(GFP_KERNEL, fmt, ap);
1722 va_end(ap);
1723 if (!str)
1724 return -ENOMEM;
1725
1726 len = strlen(str);
1727 mutex_lock(&session->metadata_cache->lock);
1728 session->metadata_cache->producing++;
1729 if (session->metadata_cache->metadata_written + len >
1730 session->metadata_cache->cache_alloc) {
1731 char *tmp_cache_realloc;
1732 unsigned int tmp_cache_alloc_size;
1733
1734 tmp_cache_alloc_size = max_t(unsigned int,
1735 session->metadata_cache->cache_alloc + len,
1736 session->metadata_cache->cache_alloc << 1);
1737 tmp_cache_realloc = vzalloc(tmp_cache_alloc_size);
1738 if (!tmp_cache_realloc)
1739 goto err;
1740 if (session->metadata_cache->data) {
1741 memcpy(tmp_cache_realloc,
1742 session->metadata_cache->data,
1743 session->metadata_cache->cache_alloc);
1744 vfree(session->metadata_cache->data);
1745 }
1746
1747 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1748 session->metadata_cache->data = tmp_cache_realloc;
1749 }
1750 memcpy(session->metadata_cache->data +
1751 session->metadata_cache->metadata_written,
1752 str, len);
1753 session->metadata_cache->metadata_written += len;
1754 session->metadata_cache->producing--;
1755 mutex_unlock(&session->metadata_cache->lock);
1756 kfree(str);
1757
1758 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1759 wake_up_interruptible(&stream->read_wait);
1760
1761 return 0;
1762
1763 err:
1764 session->metadata_cache->producing--;
1765 mutex_unlock(&session->metadata_cache->lock);
1766 kfree(str);
1767 return -ENOMEM;
1768 }
1769
1770 static
1771 int print_tabs(struct lttng_session *session, size_t nesting)
1772 {
1773 size_t i;
1774
1775 for (i = 0; i < nesting; i++) {
1776 int ret;
1777
1778 ret = lttng_metadata_printf(session, " ");
1779 if (ret) {
1780 return ret;
1781 }
1782 }
1783 return 0;
1784 }
1785
1786 /*
1787 * Must be called with sessions_mutex held.
1788 */
1789 static
1790 int _lttng_struct_type_statedump(struct lttng_session *session,
1791 const struct lttng_type *type,
1792 size_t nesting)
1793 {
1794 int ret;
1795 uint32_t i, nr_fields;
1796
1797 ret = print_tabs(session, nesting);
1798 if (ret)
1799 return ret;
1800 ret = lttng_metadata_printf(session,
1801 "struct {\n");
1802 if (ret)
1803 return ret;
1804 nr_fields = type->u._struct.nr_fields;
1805 for (i = 0; i < nr_fields; i++) {
1806 const struct lttng_event_field *iter_field;
1807
1808 iter_field = &type->u._struct.fields[i];
1809 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1810 if (ret)
1811 return ret;
1812 }
1813 ret = print_tabs(session, nesting);
1814 if (ret)
1815 return ret;
1816 ret = lttng_metadata_printf(session,
1817 "}");
1818 return ret;
1819 }
1820
1821 /*
1822 * Must be called with sessions_mutex held.
1823 */
1824 static
1825 int _lttng_struct_statedump(struct lttng_session *session,
1826 const struct lttng_event_field *field,
1827 size_t nesting)
1828 {
1829 int ret;
1830
1831 ret = _lttng_struct_type_statedump(session,
1832 &field->type, nesting);
1833 if (ret)
1834 return ret;
1835 ret = lttng_metadata_printf(session,
1836 "_%s;\n",
1837 field->name);
1838 return ret;
1839 }
1840
1841 /*
1842 * Must be called with sessions_mutex held.
1843 */
1844 static
1845 int _lttng_variant_type_statedump(struct lttng_session *session,
1846 const struct lttng_type *type,
1847 size_t nesting)
1848 {
1849 int ret;
1850 uint32_t i, nr_choices;
1851
1852 ret = print_tabs(session, nesting);
1853 if (ret)
1854 return ret;
1855 ret = lttng_metadata_printf(session,
1856 "variant <_%s> {\n",
1857 type->u.variant.tag_name);
1858 if (ret)
1859 return ret;
1860 nr_choices = type->u.variant.nr_choices;
1861 for (i = 0; i < nr_choices; i++) {
1862 const struct lttng_event_field *iter_field;
1863
1864 iter_field = &type->u.variant.choices[i];
1865 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1866 if (ret)
1867 return ret;
1868 }
1869 ret = print_tabs(session, nesting);
1870 if (ret)
1871 return ret;
1872 ret = lttng_metadata_printf(session,
1873 "}");
1874 return ret;
1875 }
1876
1877 /*
1878 * Must be called with sessions_mutex held.
1879 */
1880 static
1881 int _lttng_variant_statedump(struct lttng_session *session,
1882 const struct lttng_event_field *field,
1883 size_t nesting)
1884 {
1885 int ret;
1886
1887 ret = _lttng_variant_type_statedump(session,
1888 &field->type, nesting);
1889 if (ret)
1890 return ret;
1891 ret = lttng_metadata_printf(session,
1892 "_%s;\n",
1893 field->name);
1894 return ret;
1895 }
1896
1897 /*
1898 * Must be called with sessions_mutex held.
1899 */
1900 static
1901 int _lttng_array_compound_statedump(struct lttng_session *session,
1902 const struct lttng_event_field *field,
1903 size_t nesting)
1904 {
1905 int ret;
1906 const struct lttng_type *elem_type;
1907
1908 /* Only array of structures and variants are currently supported. */
1909 elem_type = field->type.u.array_compound.elem_type;
1910 switch (elem_type->atype) {
1911 case atype_struct:
1912 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1913 if (ret)
1914 return ret;
1915 break;
1916 case atype_variant:
1917 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1918 if (ret)
1919 return ret;
1920 break;
1921 default:
1922 return -EINVAL;
1923 }
1924 ret = lttng_metadata_printf(session,
1925 " _%s[%u];\n",
1926 field->name,
1927 field->type.u.array_compound.length);
1928 return ret;
1929 }
1930
1931 /*
1932 * Must be called with sessions_mutex held.
1933 */
1934 static
1935 int _lttng_sequence_compound_statedump(struct lttng_session *session,
1936 const struct lttng_event_field *field,
1937 size_t nesting)
1938 {
1939 int ret;
1940 const char *length_name;
1941 const struct lttng_type *elem_type;
1942
1943 length_name = field->type.u.sequence_compound.length_name;
1944
1945 /* Only array of structures and variants are currently supported. */
1946 elem_type = field->type.u.sequence_compound.elem_type;
1947 switch (elem_type->atype) {
1948 case atype_struct:
1949 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1950 if (ret)
1951 return ret;
1952 break;
1953 case atype_variant:
1954 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1955 if (ret)
1956 return ret;
1957 break;
1958 default:
1959 return -EINVAL;
1960 }
1961 ret = lttng_metadata_printf(session,
1962 " _%s[ _%s ];\n",
1963 field->name,
1964 length_name);
1965 return ret;
1966 }
1967
1968 /*
1969 * Must be called with sessions_mutex held.
1970 */
1971 static
1972 int _lttng_enum_statedump(struct lttng_session *session,
1973 const struct lttng_event_field *field,
1974 size_t nesting)
1975 {
1976 const struct lttng_enum_desc *enum_desc;
1977 const struct lttng_integer_type *container_type;
1978 int ret;
1979 unsigned int i, nr_entries;
1980
1981 enum_desc = field->type.u.basic.enumeration.desc;
1982 container_type = &field->type.u.basic.enumeration.container_type;
1983 nr_entries = enum_desc->nr_entries;
1984
1985 ret = print_tabs(session, nesting);
1986 if (ret)
1987 goto end;
1988 ret = lttng_metadata_printf(session,
1989 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
1990 container_type->size,
1991 container_type->alignment,
1992 container_type->signedness,
1993 (container_type->encoding == lttng_encode_none)
1994 ? "none"
1995 : (container_type->encoding == lttng_encode_UTF8)
1996 ? "UTF8"
1997 : "ASCII",
1998 container_type->base,
1999 #if __BYTE_ORDER == __BIG_ENDIAN
2000 container_type->reverse_byte_order ? " byte_order = le;" : ""
2001 #else
2002 container_type->reverse_byte_order ? " byte_order = be;" : ""
2003 #endif
2004 );
2005 if (ret)
2006 goto end;
2007 /* Dump all entries */
2008 for (i = 0; i < nr_entries; i++) {
2009 const struct lttng_enum_entry *entry = &enum_desc->entries[i];
2010 int j, len;
2011
2012 ret = print_tabs(session, nesting + 1);
2013 if (ret)
2014 goto end;
2015 ret = lttng_metadata_printf(session,
2016 "\"");
2017 if (ret)
2018 goto end;
2019 len = strlen(entry->string);
2020 /* Escape the character '"' */
2021 for (j = 0; j < len; j++) {
2022 char c = entry->string[j];
2023
2024 switch (c) {
2025 case '"':
2026 ret = lttng_metadata_printf(session,
2027 "\\\"");
2028 break;
2029 case '\\':
2030 ret = lttng_metadata_printf(session,
2031 "\\\\");
2032 break;
2033 default:
2034 ret = lttng_metadata_printf(session,
2035 "%c", c);
2036 break;
2037 }
2038 if (ret)
2039 goto end;
2040 }
2041 ret = lttng_metadata_printf(session, "\"");
2042 if (ret)
2043 goto end;
2044
2045 if (entry->options.is_auto) {
2046 ret = lttng_metadata_printf(session, ",\n");
2047 if (ret)
2048 goto end;
2049 } else {
2050 ret = lttng_metadata_printf(session,
2051 " = ");
2052 if (ret)
2053 goto end;
2054 if (entry->start.signedness)
2055 ret = lttng_metadata_printf(session,
2056 "%lld", (long long) entry->start.value);
2057 else
2058 ret = lttng_metadata_printf(session,
2059 "%llu", entry->start.value);
2060 if (ret)
2061 goto end;
2062 if (entry->start.signedness == entry->end.signedness &&
2063 entry->start.value
2064 == entry->end.value) {
2065 ret = lttng_metadata_printf(session,
2066 ",\n");
2067 } else {
2068 if (entry->end.signedness) {
2069 ret = lttng_metadata_printf(session,
2070 " ... %lld,\n",
2071 (long long) entry->end.value);
2072 } else {
2073 ret = lttng_metadata_printf(session,
2074 " ... %llu,\n",
2075 entry->end.value);
2076 }
2077 }
2078 if (ret)
2079 goto end;
2080 }
2081 }
2082 ret = print_tabs(session, nesting);
2083 if (ret)
2084 goto end;
2085 ret = lttng_metadata_printf(session, "} _%s;\n",
2086 field->name);
2087 end:
2088 return ret;
2089 }
2090
2091 /*
2092 * Must be called with sessions_mutex held.
2093 */
2094 static
2095 int _lttng_field_statedump(struct lttng_session *session,
2096 const struct lttng_event_field *field,
2097 size_t nesting)
2098 {
2099 int ret = 0;
2100
2101 switch (field->type.atype) {
2102 case atype_integer:
2103 ret = print_tabs(session, nesting);
2104 if (ret)
2105 return ret;
2106 ret = lttng_metadata_printf(session,
2107 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
2108 field->type.u.basic.integer.size,
2109 field->type.u.basic.integer.alignment,
2110 field->type.u.basic.integer.signedness,
2111 (field->type.u.basic.integer.encoding == lttng_encode_none)
2112 ? "none"
2113 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
2114 ? "UTF8"
2115 : "ASCII",
2116 field->type.u.basic.integer.base,
2117 #if __BYTE_ORDER == __BIG_ENDIAN
2118 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2119 #else
2120 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2121 #endif
2122 field->name);
2123 break;
2124 case atype_enum:
2125 ret = _lttng_enum_statedump(session, field, nesting);
2126 break;
2127 case atype_array:
2128 case atype_array_bitfield:
2129 {
2130 const struct lttng_basic_type *elem_type;
2131
2132 elem_type = &field->type.u.array.elem_type;
2133 if (field->type.u.array.elem_alignment) {
2134 ret = print_tabs(session, nesting);
2135 if (ret)
2136 return ret;
2137 ret = lttng_metadata_printf(session,
2138 "struct { } align(%u) _%s_padding;\n",
2139 field->type.u.array.elem_alignment * CHAR_BIT,
2140 field->name);
2141 if (ret)
2142 return ret;
2143 }
2144 ret = print_tabs(session, nesting);
2145 if (ret)
2146 return ret;
2147 ret = lttng_metadata_printf(session,
2148 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
2149 elem_type->u.basic.integer.size,
2150 elem_type->u.basic.integer.alignment,
2151 elem_type->u.basic.integer.signedness,
2152 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2153 ? "none"
2154 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
2155 ? "UTF8"
2156 : "ASCII",
2157 elem_type->u.basic.integer.base,
2158 #if __BYTE_ORDER == __BIG_ENDIAN
2159 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2160 #else
2161 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2162 #endif
2163 field->name, field->type.u.array.length);
2164 break;
2165 }
2166 case atype_sequence:
2167 case atype_sequence_bitfield:
2168 {
2169 const struct lttng_basic_type *elem_type;
2170 const struct lttng_basic_type *length_type;
2171
2172 elem_type = &field->type.u.sequence.elem_type;
2173 length_type = &field->type.u.sequence.length_type;
2174 ret = print_tabs(session, nesting);
2175 if (ret)
2176 return ret;
2177 ret = lttng_metadata_printf(session,
2178 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
2179 length_type->u.basic.integer.size,
2180 (unsigned int) length_type->u.basic.integer.alignment,
2181 length_type->u.basic.integer.signedness,
2182 (length_type->u.basic.integer.encoding == lttng_encode_none)
2183 ? "none"
2184 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
2185 ? "UTF8"
2186 : "ASCII"),
2187 length_type->u.basic.integer.base,
2188 #if __BYTE_ORDER == __BIG_ENDIAN
2189 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2190 #else
2191 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2192 #endif
2193 field->name);
2194 if (ret)
2195 return ret;
2196
2197 if (field->type.u.sequence.elem_alignment) {
2198 ret = print_tabs(session, nesting);
2199 if (ret)
2200 return ret;
2201 ret = lttng_metadata_printf(session,
2202 "struct { } align(%u) _%s_padding;\n",
2203 field->type.u.sequence.elem_alignment * CHAR_BIT,
2204 field->name);
2205 if (ret)
2206 return ret;
2207 }
2208 ret = print_tabs(session, nesting);
2209 if (ret)
2210 return ret;
2211 ret = lttng_metadata_printf(session,
2212 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
2213 elem_type->u.basic.integer.size,
2214 (unsigned int) elem_type->u.basic.integer.alignment,
2215 elem_type->u.basic.integer.signedness,
2216 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2217 ? "none"
2218 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
2219 ? "UTF8"
2220 : "ASCII"),
2221 elem_type->u.basic.integer.base,
2222 #if __BYTE_ORDER == __BIG_ENDIAN
2223 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
2224 #else
2225 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
2226 #endif
2227 field->name,
2228 field->name);
2229 break;
2230 }
2231
2232 case atype_string:
2233 /* Default encoding is UTF8 */
2234 ret = print_tabs(session, nesting);
2235 if (ret)
2236 return ret;
2237 ret = lttng_metadata_printf(session,
2238 "string%s _%s;\n",
2239 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
2240 " { encoding = ASCII; }" : "",
2241 field->name);
2242 break;
2243 case atype_struct:
2244 ret = _lttng_struct_statedump(session, field, nesting);
2245 break;
2246 case atype_array_compound:
2247 ret = _lttng_array_compound_statedump(session, field, nesting);
2248 break;
2249 case atype_sequence_compound:
2250 ret = _lttng_sequence_compound_statedump(session, field, nesting);
2251 break;
2252 case atype_variant:
2253 ret = _lttng_variant_statedump(session, field, nesting);
2254 break;
2255
2256 default:
2257 WARN_ON_ONCE(1);
2258 return -EINVAL;
2259 }
2260 return ret;
2261 }
2262
2263 static
2264 int _lttng_context_metadata_statedump(struct lttng_session *session,
2265 struct lttng_ctx *ctx)
2266 {
2267 int ret = 0;
2268 int i;
2269
2270 if (!ctx)
2271 return 0;
2272 for (i = 0; i < ctx->nr_fields; i++) {
2273 const struct lttng_ctx_field *field = &ctx->fields[i];
2274
2275 ret = _lttng_field_statedump(session, &field->event_field, 2);
2276 if (ret)
2277 return ret;
2278 }
2279 return ret;
2280 }
2281
2282 static
2283 int _lttng_fields_metadata_statedump(struct lttng_session *session,
2284 struct lttng_event *event)
2285 {
2286 const struct lttng_event_desc *desc = event->desc;
2287 int ret = 0;
2288 int i;
2289
2290 for (i = 0; i < desc->nr_fields; i++) {
2291 const struct lttng_event_field *field = &desc->fields[i];
2292
2293 ret = _lttng_field_statedump(session, field, 2);
2294 if (ret)
2295 return ret;
2296 }
2297 return ret;
2298 }
2299
2300 /*
2301 * Must be called with sessions_mutex held.
2302 * The entire event metadata is printed as a single atomic metadata
2303 * transaction.
2304 */
2305 static
2306 int _lttng_event_metadata_statedump(struct lttng_session *session,
2307 struct lttng_channel *chan,
2308 struct lttng_event *event)
2309 {
2310 int ret = 0;
2311
2312 if (event->metadata_dumped || !READ_ONCE(session->active))
2313 return 0;
2314 if (chan->channel_type == METADATA_CHANNEL)
2315 return 0;
2316
2317 lttng_metadata_begin(session);
2318
2319 ret = lttng_metadata_printf(session,
2320 "event {\n"
2321 " name = \"%s\";\n"
2322 " id = %u;\n"
2323 " stream_id = %u;\n",
2324 event->desc->name,
2325 event->id,
2326 event->chan->id);
2327 if (ret)
2328 goto end;
2329
2330 if (event->ctx) {
2331 ret = lttng_metadata_printf(session,
2332 " context := struct {\n");
2333 if (ret)
2334 goto end;
2335 }
2336 ret = _lttng_context_metadata_statedump(session, event->ctx);
2337 if (ret)
2338 goto end;
2339 if (event->ctx) {
2340 ret = lttng_metadata_printf(session,
2341 " };\n");
2342 if (ret)
2343 goto end;
2344 }
2345
2346 ret = lttng_metadata_printf(session,
2347 " fields := struct {\n"
2348 );
2349 if (ret)
2350 goto end;
2351
2352 ret = _lttng_fields_metadata_statedump(session, event);
2353 if (ret)
2354 goto end;
2355
2356 /*
2357 * LTTng space reservation can only reserve multiples of the
2358 * byte size.
2359 */
2360 ret = lttng_metadata_printf(session,
2361 " };\n"
2362 "};\n\n");
2363 if (ret)
2364 goto end;
2365
2366 event->metadata_dumped = 1;
2367 end:
2368 lttng_metadata_end(session);
2369 return ret;
2370
2371 }
2372
2373 /*
2374 * Must be called with sessions_mutex held.
2375 * The entire channel metadata is printed as a single atomic metadata
2376 * transaction.
2377 */
2378 static
2379 int _lttng_channel_metadata_statedump(struct lttng_session *session,
2380 struct lttng_channel *chan)
2381 {
2382 int ret = 0;
2383
2384 if (chan->metadata_dumped || !READ_ONCE(session->active))
2385 return 0;
2386
2387 if (chan->channel_type == METADATA_CHANNEL)
2388 return 0;
2389
2390 lttng_metadata_begin(session);
2391
2392 WARN_ON_ONCE(!chan->header_type);
2393 ret = lttng_metadata_printf(session,
2394 "stream {\n"
2395 " id = %u;\n"
2396 " event.header := %s;\n"
2397 " packet.context := struct packet_context;\n",
2398 chan->id,
2399 chan->header_type == 1 ? "struct event_header_compact" :
2400 "struct event_header_large");
2401 if (ret)
2402 goto end;
2403
2404 if (chan->ctx) {
2405 ret = lttng_metadata_printf(session,
2406 " event.context := struct {\n");
2407 if (ret)
2408 goto end;
2409 }
2410 ret = _lttng_context_metadata_statedump(session, chan->ctx);
2411 if (ret)
2412 goto end;
2413 if (chan->ctx) {
2414 ret = lttng_metadata_printf(session,
2415 " };\n");
2416 if (ret)
2417 goto end;
2418 }
2419
2420 ret = lttng_metadata_printf(session,
2421 "};\n\n");
2422
2423 chan->metadata_dumped = 1;
2424 end:
2425 lttng_metadata_end(session);
2426 return ret;
2427 }
2428
2429 /*
2430 * Must be called with sessions_mutex held.
2431 */
2432 static
2433 int _lttng_stream_packet_context_declare(struct lttng_session *session)
2434 {
2435 return lttng_metadata_printf(session,
2436 "struct packet_context {\n"
2437 " uint64_clock_monotonic_t timestamp_begin;\n"
2438 " uint64_clock_monotonic_t timestamp_end;\n"
2439 " uint64_t content_size;\n"
2440 " uint64_t packet_size;\n"
2441 " uint64_t packet_seq_num;\n"
2442 " unsigned long events_discarded;\n"
2443 " uint32_t cpu_id;\n"
2444 "};\n\n"
2445 );
2446 }
2447
2448 /*
2449 * Compact header:
2450 * id: range: 0 - 30.
2451 * id 31 is reserved to indicate an extended header.
2452 *
2453 * Large header:
2454 * id: range: 0 - 65534.
2455 * id 65535 is reserved to indicate an extended header.
2456 *
2457 * Must be called with sessions_mutex held.
2458 */
2459 static
2460 int _lttng_event_header_declare(struct lttng_session *session)
2461 {
2462 return lttng_metadata_printf(session,
2463 "struct event_header_compact {\n"
2464 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2465 " variant <id> {\n"
2466 " struct {\n"
2467 " uint27_clock_monotonic_t timestamp;\n"
2468 " } compact;\n"
2469 " struct {\n"
2470 " uint32_t id;\n"
2471 " uint64_clock_monotonic_t timestamp;\n"
2472 " } extended;\n"
2473 " } v;\n"
2474 "} align(%u);\n"
2475 "\n"
2476 "struct event_header_large {\n"
2477 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2478 " variant <id> {\n"
2479 " struct {\n"
2480 " uint32_clock_monotonic_t timestamp;\n"
2481 " } compact;\n"
2482 " struct {\n"
2483 " uint32_t id;\n"
2484 " uint64_clock_monotonic_t timestamp;\n"
2485 " } extended;\n"
2486 " } v;\n"
2487 "} align(%u);\n\n",
2488 lttng_alignof(uint32_t) * CHAR_BIT,
2489 lttng_alignof(uint16_t) * CHAR_BIT
2490 );
2491 }
2492
2493 /*
2494 * Approximation of NTP time of day to clock monotonic correlation,
2495 * taken at start of trace.
2496 * Yes, this is only an approximation. Yes, we can (and will) do better
2497 * in future versions.
2498 * This function may return a negative offset. It may happen if the
2499 * system sets the REALTIME clock to 0 after boot.
2500 *
2501 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2502 * y2038 compliant.
2503 */
2504 static
2505 int64_t measure_clock_offset(void)
2506 {
2507 uint64_t monotonic_avg, monotonic[2], realtime;
2508 uint64_t tcf = trace_clock_freq();
2509 int64_t offset;
2510 unsigned long flags;
2511 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2512 struct timespec64 rts = { 0, 0 };
2513 #else
2514 struct timespec rts = { 0, 0 };
2515 #endif
2516
2517 /* Disable interrupts to increase correlation precision. */
2518 local_irq_save(flags);
2519 monotonic[0] = trace_clock_read64();
2520 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2521 ktime_get_real_ts64(&rts);
2522 #else
2523 getnstimeofday(&rts);
2524 #endif
2525 monotonic[1] = trace_clock_read64();
2526 local_irq_restore(flags);
2527
2528 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2529 realtime = (uint64_t) rts.tv_sec * tcf;
2530 if (tcf == NSEC_PER_SEC) {
2531 realtime += rts.tv_nsec;
2532 } else {
2533 uint64_t n = rts.tv_nsec * tcf;
2534
2535 do_div(n, NSEC_PER_SEC);
2536 realtime += n;
2537 }
2538 offset = (int64_t) realtime - monotonic_avg;
2539 return offset;
2540 }
2541
2542 static
2543 int print_escaped_ctf_string(struct lttng_session *session, const char *string)
2544 {
2545 int ret = 0;
2546 size_t i;
2547 char cur;
2548
2549 i = 0;
2550 cur = string[i];
2551 while (cur != '\0') {
2552 switch (cur) {
2553 case '\n':
2554 ret = lttng_metadata_printf(session, "%s", "\\n");
2555 break;
2556 case '\\':
2557 case '"':
2558 ret = lttng_metadata_printf(session, "%c", '\\');
2559 if (ret)
2560 goto error;
2561 /* We still print the current char */
2562 /* Fallthrough */
2563 default:
2564 ret = lttng_metadata_printf(session, "%c", cur);
2565 break;
2566 }
2567
2568 if (ret)
2569 goto error;
2570
2571 cur = string[++i];
2572 }
2573 error:
2574 return ret;
2575 }
2576
2577 static
2578 int print_metadata_escaped_field(struct lttng_session *session, const char *field,
2579 const char *field_value)
2580 {
2581 int ret;
2582
2583 ret = lttng_metadata_printf(session, " %s = \"", field);
2584 if (ret)
2585 goto error;
2586
2587 ret = print_escaped_ctf_string(session, field_value);
2588 if (ret)
2589 goto error;
2590
2591 ret = lttng_metadata_printf(session, "\";\n");
2592
2593 error:
2594 return ret;
2595 }
2596
2597 /*
2598 * Output metadata into this session's metadata buffers.
2599 * Must be called with sessions_mutex held.
2600 */
2601 static
2602 int _lttng_session_metadata_statedump(struct lttng_session *session)
2603 {
2604 unsigned char *uuid_c = session->uuid.b;
2605 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
2606 const char *product_uuid;
2607 struct lttng_channel *chan;
2608 struct lttng_event *event;
2609 int ret = 0;
2610
2611 if (!READ_ONCE(session->active))
2612 return 0;
2613
2614 lttng_metadata_begin(session);
2615
2616 if (session->metadata_dumped)
2617 goto skip_session;
2618
2619 snprintf(uuid_s, sizeof(uuid_s),
2620 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2621 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2622 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2623 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2624 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
2625
2626 ret = lttng_metadata_printf(session,
2627 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2628 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2629 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2630 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2631 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2632 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2633 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2634 "\n"
2635 "trace {\n"
2636 " major = %u;\n"
2637 " minor = %u;\n"
2638 " uuid = \"%s\";\n"
2639 " byte_order = %s;\n"
2640 " packet.header := struct {\n"
2641 " uint32_t magic;\n"
2642 " uint8_t uuid[16];\n"
2643 " uint32_t stream_id;\n"
2644 " uint64_t stream_instance_id;\n"
2645 " };\n"
2646 "};\n\n",
2647 lttng_alignof(uint8_t) * CHAR_BIT,
2648 lttng_alignof(uint16_t) * CHAR_BIT,
2649 lttng_alignof(uint32_t) * CHAR_BIT,
2650 lttng_alignof(uint64_t) * CHAR_BIT,
2651 sizeof(unsigned long) * CHAR_BIT,
2652 lttng_alignof(unsigned long) * CHAR_BIT,
2653 CTF_SPEC_MAJOR,
2654 CTF_SPEC_MINOR,
2655 uuid_s,
2656 #if __BYTE_ORDER == __BIG_ENDIAN
2657 "be"
2658 #else
2659 "le"
2660 #endif
2661 );
2662 if (ret)
2663 goto end;
2664
2665 ret = lttng_metadata_printf(session,
2666 "env {\n"
2667 " hostname = \"%s\";\n"
2668 " domain = \"kernel\";\n"
2669 " sysname = \"%s\";\n"
2670 " kernel_release = \"%s\";\n"
2671 " kernel_version = \"%s\";\n"
2672 " tracer_name = \"lttng-modules\";\n"
2673 " tracer_major = %d;\n"
2674 " tracer_minor = %d;\n"
2675 " tracer_patchlevel = %d;\n"
2676 " trace_buffering_scheme = \"global\";\n",
2677 current->nsproxy->uts_ns->name.nodename,
2678 utsname()->sysname,
2679 utsname()->release,
2680 utsname()->version,
2681 LTTNG_MODULES_MAJOR_VERSION,
2682 LTTNG_MODULES_MINOR_VERSION,
2683 LTTNG_MODULES_PATCHLEVEL_VERSION
2684 );
2685 if (ret)
2686 goto end;
2687
2688 ret = print_metadata_escaped_field(session, "trace_name", session->name);
2689 if (ret)
2690 goto end;
2691 ret = print_metadata_escaped_field(session, "trace_creation_datetime",
2692 session->creation_time);
2693 if (ret)
2694 goto end;
2695
2696 /* Add the product UUID to the 'env' section */
2697 product_uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
2698 if (product_uuid) {
2699 ret = lttng_metadata_printf(session,
2700 " product_uuid = \"%s\";\n",
2701 product_uuid
2702 );
2703 if (ret)
2704 goto end;
2705 }
2706
2707 /* Close the 'env' section */
2708 ret = lttng_metadata_printf(session, "};\n\n");
2709 if (ret)
2710 goto end;
2711
2712 ret = lttng_metadata_printf(session,
2713 "clock {\n"
2714 " name = \"%s\";\n",
2715 trace_clock_name()
2716 );
2717 if (ret)
2718 goto end;
2719
2720 if (!trace_clock_uuid(clock_uuid_s)) {
2721 ret = lttng_metadata_printf(session,
2722 " uuid = \"%s\";\n",
2723 clock_uuid_s
2724 );
2725 if (ret)
2726 goto end;
2727 }
2728
2729 ret = lttng_metadata_printf(session,
2730 " description = \"%s\";\n"
2731 " freq = %llu; /* Frequency, in Hz */\n"
2732 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2733 " offset = %lld;\n"
2734 "};\n\n",
2735 trace_clock_description(),
2736 (unsigned long long) trace_clock_freq(),
2737 (long long) measure_clock_offset()
2738 );
2739 if (ret)
2740 goto end;
2741
2742 ret = lttng_metadata_printf(session,
2743 "typealias integer {\n"
2744 " size = 27; align = 1; signed = false;\n"
2745 " map = clock.%s.value;\n"
2746 "} := uint27_clock_monotonic_t;\n"
2747 "\n"
2748 "typealias integer {\n"
2749 " size = 32; align = %u; signed = false;\n"
2750 " map = clock.%s.value;\n"
2751 "} := uint32_clock_monotonic_t;\n"
2752 "\n"
2753 "typealias integer {\n"
2754 " size = 64; align = %u; signed = false;\n"
2755 " map = clock.%s.value;\n"
2756 "} := uint64_clock_monotonic_t;\n\n",
2757 trace_clock_name(),
2758 lttng_alignof(uint32_t) * CHAR_BIT,
2759 trace_clock_name(),
2760 lttng_alignof(uint64_t) * CHAR_BIT,
2761 trace_clock_name()
2762 );
2763 if (ret)
2764 goto end;
2765
2766 ret = _lttng_stream_packet_context_declare(session);
2767 if (ret)
2768 goto end;
2769
2770 ret = _lttng_event_header_declare(session);
2771 if (ret)
2772 goto end;
2773
2774 skip_session:
2775 list_for_each_entry(chan, &session->chan, list) {
2776 ret = _lttng_channel_metadata_statedump(session, chan);
2777 if (ret)
2778 goto end;
2779 }
2780
2781 list_for_each_entry(event, &session->events, list) {
2782 ret = _lttng_event_metadata_statedump(session, event->chan, event);
2783 if (ret)
2784 goto end;
2785 }
2786 session->metadata_dumped = 1;
2787 end:
2788 lttng_metadata_end(session);
2789 return ret;
2790 }
2791
2792 /**
2793 * lttng_transport_register - LTT transport registration
2794 * @transport: transport structure
2795 *
2796 * Registers a transport which can be used as output to extract the data out of
2797 * LTTng. The module calling this registration function must ensure that no
2798 * trap-inducing code will be executed by the transport functions. E.g.
2799 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2800 * is made visible to the transport function. This registration acts as a
2801 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2802 * after its registration must it synchronize the TLBs.
2803 */
2804 void lttng_transport_register(struct lttng_transport *transport)
2805 {
2806 /*
2807 * Make sure no page fault can be triggered by the module about to be
2808 * registered. We deal with this here so we don't have to call
2809 * vmalloc_sync_mappings() in each module's init.
2810 */
2811 wrapper_vmalloc_sync_mappings();
2812
2813 mutex_lock(&sessions_mutex);
2814 list_add_tail(&transport->node, &lttng_transport_list);
2815 mutex_unlock(&sessions_mutex);
2816 }
2817 EXPORT_SYMBOL_GPL(lttng_transport_register);
2818
2819 /**
2820 * lttng_transport_unregister - LTT transport unregistration
2821 * @transport: transport structure
2822 */
2823 void lttng_transport_unregister(struct lttng_transport *transport)
2824 {
2825 mutex_lock(&sessions_mutex);
2826 list_del(&transport->node);
2827 mutex_unlock(&sessions_mutex);
2828 }
2829 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
2830
2831 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2832
2833 enum cpuhp_state lttng_hp_prepare;
2834 enum cpuhp_state lttng_hp_online;
2835
2836 static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node)
2837 {
2838 struct lttng_cpuhp_node *lttng_node;
2839
2840 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2841 switch (lttng_node->component) {
2842 case LTTNG_RING_BUFFER_FRONTEND:
2843 return 0;
2844 case LTTNG_RING_BUFFER_BACKEND:
2845 return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node);
2846 case LTTNG_RING_BUFFER_ITER:
2847 return 0;
2848 case LTTNG_CONTEXT_PERF_COUNTERS:
2849 return 0;
2850 default:
2851 return -EINVAL;
2852 }
2853 }
2854
2855 static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node)
2856 {
2857 struct lttng_cpuhp_node *lttng_node;
2858
2859 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2860 switch (lttng_node->component) {
2861 case LTTNG_RING_BUFFER_FRONTEND:
2862 return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node);
2863 case LTTNG_RING_BUFFER_BACKEND:
2864 return 0;
2865 case LTTNG_RING_BUFFER_ITER:
2866 return 0;
2867 case LTTNG_CONTEXT_PERF_COUNTERS:
2868 return lttng_cpuhp_perf_counter_dead(cpu, lttng_node);
2869 default:
2870 return -EINVAL;
2871 }
2872 }
2873
2874 static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node)
2875 {
2876 struct lttng_cpuhp_node *lttng_node;
2877
2878 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2879 switch (lttng_node->component) {
2880 case LTTNG_RING_BUFFER_FRONTEND:
2881 return lttng_cpuhp_rb_frontend_online(cpu, lttng_node);
2882 case LTTNG_RING_BUFFER_BACKEND:
2883 return 0;
2884 case LTTNG_RING_BUFFER_ITER:
2885 return lttng_cpuhp_rb_iter_online(cpu, lttng_node);
2886 case LTTNG_CONTEXT_PERF_COUNTERS:
2887 return lttng_cpuhp_perf_counter_online(cpu, lttng_node);
2888 default:
2889 return -EINVAL;
2890 }
2891 }
2892
2893 static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node)
2894 {
2895 struct lttng_cpuhp_node *lttng_node;
2896
2897 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2898 switch (lttng_node->component) {
2899 case LTTNG_RING_BUFFER_FRONTEND:
2900 return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node);
2901 case LTTNG_RING_BUFFER_BACKEND:
2902 return 0;
2903 case LTTNG_RING_BUFFER_ITER:
2904 return 0;
2905 case LTTNG_CONTEXT_PERF_COUNTERS:
2906 return 0;
2907 default:
2908 return -EINVAL;
2909 }
2910 }
2911
2912 static int __init lttng_init_cpu_hotplug(void)
2913 {
2914 int ret;
2915
2916 ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare",
2917 lttng_hotplug_prepare,
2918 lttng_hotplug_dead);
2919 if (ret < 0) {
2920 return ret;
2921 }
2922 lttng_hp_prepare = ret;
2923 lttng_rb_set_hp_prepare(ret);
2924
2925 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online",
2926 lttng_hotplug_online,
2927 lttng_hotplug_offline);
2928 if (ret < 0) {
2929 cpuhp_remove_multi_state(lttng_hp_prepare);
2930 lttng_hp_prepare = 0;
2931 return ret;
2932 }
2933 lttng_hp_online = ret;
2934 lttng_rb_set_hp_online(ret);
2935
2936 return 0;
2937 }
2938
2939 static void __exit lttng_exit_cpu_hotplug(void)
2940 {
2941 lttng_rb_set_hp_online(0);
2942 cpuhp_remove_multi_state(lttng_hp_online);
2943 lttng_rb_set_hp_prepare(0);
2944 cpuhp_remove_multi_state(lttng_hp_prepare);
2945 }
2946
2947 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2948 static int lttng_init_cpu_hotplug(void)
2949 {
2950 return 0;
2951 }
2952 static void lttng_exit_cpu_hotplug(void)
2953 {
2954 }
2955 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
2956
2957
2958 static int __init lttng_events_init(void)
2959 {
2960 int ret;
2961
2962 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2963 if (ret)
2964 return ret;
2965 ret = wrapper_get_pfnblock_flags_mask_init();
2966 if (ret)
2967 return ret;
2968 ret = wrapper_get_pageblock_flags_mask_init();
2969 if (ret)
2970 return ret;
2971 ret = lttng_probes_init();
2972 if (ret)
2973 return ret;
2974 ret = lttng_context_init();
2975 if (ret)
2976 return ret;
2977 ret = lttng_tracepoint_init();
2978 if (ret)
2979 goto error_tp;
2980 event_cache = KMEM_CACHE(lttng_event, 0);
2981 if (!event_cache) {
2982 ret = -ENOMEM;
2983 goto error_kmem;
2984 }
2985 ret = lttng_abi_init();
2986 if (ret)
2987 goto error_abi;
2988 ret = lttng_logger_init();
2989 if (ret)
2990 goto error_logger;
2991 ret = lttng_init_cpu_hotplug();
2992 if (ret)
2993 goto error_hotplug;
2994 printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
2995 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2996 __stringify(LTTNG_MODULES_MINOR_VERSION),
2997 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2998 LTTNG_MODULES_EXTRAVERSION,
2999 LTTNG_VERSION_NAME,
3000 #ifdef LTTNG_EXTRA_VERSION_GIT
3001 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3002 #else
3003 "",
3004 #endif
3005 #ifdef LTTNG_EXTRA_VERSION_NAME
3006 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3007 #else
3008 "");
3009 #endif
3010 return 0;
3011
3012 error_hotplug:
3013 lttng_logger_exit();
3014 error_logger:
3015 lttng_abi_exit();
3016 error_abi:
3017 kmem_cache_destroy(event_cache);
3018 error_kmem:
3019 lttng_tracepoint_exit();
3020 error_tp:
3021 lttng_context_exit();
3022 printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3023 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3024 __stringify(LTTNG_MODULES_MINOR_VERSION),
3025 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3026 LTTNG_MODULES_EXTRAVERSION,
3027 LTTNG_VERSION_NAME,
3028 #ifdef LTTNG_EXTRA_VERSION_GIT
3029 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3030 #else
3031 "",
3032 #endif
3033 #ifdef LTTNG_EXTRA_VERSION_NAME
3034 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3035 #else
3036 "");
3037 #endif
3038 return ret;
3039 }
3040
3041 module_init(lttng_events_init);
3042
3043 static void __exit lttng_events_exit(void)
3044 {
3045 struct lttng_session *session, *tmpsession;
3046
3047 lttng_exit_cpu_hotplug();
3048 lttng_logger_exit();
3049 lttng_abi_exit();
3050 list_for_each_entry_safe(session, tmpsession, &sessions, list)
3051 lttng_session_destroy(session);
3052 kmem_cache_destroy(event_cache);
3053 lttng_tracepoint_exit();
3054 lttng_context_exit();
3055 printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3056 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3057 __stringify(LTTNG_MODULES_MINOR_VERSION),
3058 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3059 LTTNG_MODULES_EXTRAVERSION,
3060 LTTNG_VERSION_NAME,
3061 #ifdef LTTNG_EXTRA_VERSION_GIT
3062 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3063 #else
3064 "",
3065 #endif
3066 #ifdef LTTNG_EXTRA_VERSION_NAME
3067 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3068 #else
3069 "");
3070 #endif
3071 }
3072
3073 module_exit(lttng_events_exit);
3074
3075 #include "extra_version/patches.i"
3076 #ifdef LTTNG_EXTRA_VERSION_GIT
3077 MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT);
3078 #endif
3079 #ifdef LTTNG_EXTRA_VERSION_NAME
3080 MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME);
3081 #endif
3082 MODULE_LICENSE("GPL and additional rights");
3083 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3084 MODULE_DESCRIPTION("LTTng tracer");
3085 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
3086 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
3087 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
3088 LTTNG_MODULES_EXTRAVERSION);
This page took 0.092027 seconds and 4 git commands to generate.