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