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