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