unregister event when FD is closed
[deliverable/lttng-modules.git] / ltt-events.c
1 /*
2 * ltt-events.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng per-session event registry.
7 */
8
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/uuid.h>
16 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
17 #include "ltt-events.h"
18 #include "ltt-tracer.h"
19
20 static LIST_HEAD(sessions);
21 static LIST_HEAD(ltt_transport_list);
22 static DEFINE_MUTEX(sessions_mutex);
23 static struct kmem_cache *event_cache;
24
25 static void _ltt_event_destroy(struct ltt_event *event);
26 static int _ltt_event_unregister(struct ltt_event *event);
27 static
28 int _ltt_event_metadata_statedump(struct ltt_session *session,
29 struct ltt_channel *chan,
30 struct ltt_event *event);
31 static
32 int _ltt_session_metadata_statedump(struct ltt_session *session);
33
34
35 static
36 void synchronize_trace(void)
37 {
38 synchronize_sched();
39 #ifdef CONFIG_PREEMPT_RT
40 synchronize_rcu();
41 #endif
42 }
43
44 struct ltt_session *ltt_session_create(void)
45 {
46 struct ltt_session *session;
47
48 mutex_lock(&sessions_mutex);
49 session = kzalloc(sizeof(struct ltt_session), GFP_KERNEL);
50 if (!session)
51 return NULL;
52 INIT_LIST_HEAD(&session->chan);
53 INIT_LIST_HEAD(&session->events);
54 uuid_le_gen(&session->uuid);
55 list_add(&session->list, &sessions);
56 mutex_unlock(&sessions_mutex);
57 return session;
58 }
59
60 void ltt_session_destroy(struct ltt_session *session)
61 {
62 struct ltt_channel *chan, *tmpchan;
63 struct ltt_event *event, *tmpevent;
64 int ret;
65
66 mutex_lock(&sessions_mutex);
67 ACCESS_ONCE(session->active) = 0;
68 list_for_each_entry(event, &session->events, list) {
69 ret = _ltt_event_unregister(event);
70 WARN_ON(ret);
71 }
72 synchronize_trace(); /* Wait for in-flight events to complete */
73 list_for_each_entry_safe(event, tmpevent, &session->events, list)
74 _ltt_event_destroy(event);
75 list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
76 _ltt_channel_destroy(chan);
77 list_del(&session->list);
78 mutex_unlock(&sessions_mutex);
79 kfree(session);
80 }
81
82 int ltt_session_start(struct ltt_session *session)
83 {
84 int ret = 0;
85 struct ltt_channel *chan;
86
87 mutex_lock(&sessions_mutex);
88 if (session->active) {
89 ret = -EBUSY;
90 goto end;
91 }
92
93 /*
94 * Snapshot the number of events per channel to know the type of header
95 * we need to use.
96 */
97 list_for_each_entry(chan, &session->chan, list) {
98 if (chan->header_type)
99 continue; /* don't change it if session stop/restart */
100 if (chan->free_event_id < 31)
101 chan->header_type = 1; /* compact */
102 else
103 chan->header_type = 2; /* large */
104 }
105
106 ACCESS_ONCE(session->active) = 1;
107 synchronize_trace(); /* Wait for in-flight events to complete */
108 ret = _ltt_session_metadata_statedump(session);
109 if (ret) {
110 ACCESS_ONCE(session->active) = 0;
111 synchronize_trace(); /* Wait for in-flight events to complete */
112 }
113 end:
114 mutex_unlock(&sessions_mutex);
115 return ret;
116 }
117
118 int ltt_session_stop(struct ltt_session *session)
119 {
120 int ret = 0;
121
122 mutex_lock(&sessions_mutex);
123 if (!session->active) {
124 ret = -EBUSY;
125 goto end;
126 }
127 ACCESS_ONCE(session->active) = 0;
128 synchronize_trace(); /* Wait for in-flight events to complete */
129 end:
130 mutex_unlock(&sessions_mutex);
131 return ret;
132 }
133
134 static struct ltt_transport *ltt_transport_find(const char *name)
135 {
136 struct ltt_transport *transport;
137
138 list_for_each_entry(transport, &ltt_transport_list, node) {
139 if (!strcmp(transport->name, name))
140 return transport;
141 }
142 return NULL;
143 }
144
145 struct ltt_channel *ltt_channel_create(struct ltt_session *session,
146 const char *transport_name,
147 void *buf_addr,
148 size_t subbuf_size, size_t num_subbuf,
149 unsigned int switch_timer_interval,
150 unsigned int read_timer_interval)
151 {
152 struct ltt_channel *chan;
153 struct ltt_transport *transport;
154
155 mutex_lock(&sessions_mutex);
156 if (session->active) {
157 printk(KERN_WARNING "LTTng refusing to add channel to active session\n");
158 goto active; /* Refuse to add channel to active session */
159 }
160 transport = ltt_transport_find(transport_name);
161 if (!transport) {
162 printk(KERN_WARNING "LTTng transport %s not found\n",
163 transport_name);
164 goto notransport;
165 }
166 chan = kzalloc(sizeof(struct ltt_channel), GFP_KERNEL);
167 if (!chan)
168 goto nomem;
169 chan->session = session;
170 init_waitqueue_head(&chan->notify_wait);
171 chan->chan = transport->ops.channel_create("[lttng]", session, buf_addr,
172 subbuf_size, num_subbuf, switch_timer_interval,
173 read_timer_interval);
174 if (!chan->chan)
175 goto create_error;
176 chan->id = session->free_chan_id++;
177 chan->ops = &transport->ops;
178 list_add(&chan->list, &session->chan);
179 mutex_unlock(&sessions_mutex);
180 return chan;
181
182 create_error:
183 kfree(chan);
184 nomem:
185 notransport:
186 active:
187 mutex_unlock(&sessions_mutex);
188 return NULL;
189 }
190
191 /*
192 * Only used internally at session destruction.
193 */
194 void _ltt_channel_destroy(struct ltt_channel *chan)
195 {
196 chan->ops->channel_destroy(chan->chan);
197 list_del(&chan->list);
198 kfree(chan);
199 }
200
201 /*
202 * Supports event creation while tracing session is active.
203 */
204 struct ltt_event *ltt_event_create(struct ltt_channel *chan, char *name,
205 enum instrum_type itype,
206 const struct lttng_event_desc *event_desc,
207 void *filter)
208 {
209 struct ltt_event *event;
210 int ret;
211
212 mutex_lock(&sessions_mutex);
213 if (chan->free_event_id == -1UL)
214 goto full;
215 /*
216 * This is O(n^2) (for each event, the loop is called at event
217 * creation). Might require a hash if we have lots of events.
218 */
219 list_for_each_entry(event, &chan->session->events, list)
220 if (!strcmp(event->desc->name, name))
221 goto exist;
222 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
223 if (!event)
224 goto cache_error;
225 event->chan = chan;
226 event->desc = event_desc;
227 event->filter = filter;
228 event->id = chan->free_event_id++;
229 event->itype = itype;
230 /* Populate ltt_event structure before tracepoint registration. */
231 smp_wmb();
232 switch (itype) {
233 case INSTRUM_TRACEPOINTS:
234 ret = tracepoint_probe_register(name, event_desc->probe_callback,
235 event);
236 if (ret)
237 goto register_error;
238 break;
239 default:
240 WARN_ON_ONCE(1);
241 }
242 ret = _ltt_event_metadata_statedump(chan->session, chan, event);
243 if (ret)
244 goto statedump_error;
245 list_add(&event->list, &chan->session->events);
246 mutex_unlock(&sessions_mutex);
247 return event;
248
249 statedump_error:
250 WARN_ON_ONCE(tracepoint_probe_unregister(name, event_desc->probe_callback,
251 event));
252 register_error:
253 kmem_cache_free(event_cache, event);
254 cache_error:
255 exist:
256 full:
257 mutex_unlock(&sessions_mutex);
258 return NULL;
259 }
260
261 /*
262 * Only used internally at session destruction.
263 */
264 int _ltt_event_unregister(struct ltt_event *event)
265 {
266 int ret = -EINVAL;
267
268 switch (event->itype) {
269 case INSTRUM_TRACEPOINTS:
270 ret = tracepoint_probe_unregister(event->desc->name,
271 event->desc->probe_callback,
272 event);
273 if (ret)
274 return ret;
275 break;
276 default:
277 WARN_ON_ONCE(1);
278 }
279 return ret;
280 }
281
282 /*
283 * Used when an event FD is released.
284 */
285 int ltt_event_unregister(struct ltt_event *event)
286 {
287 int ret;
288
289 mutex_lock(&sessions_mutex);
290 ret = ltt_event_unregister(event);
291 mutex_unlock(&sessions_mutex);
292 return ret;
293 }
294
295 /*
296 * Only used internally at session destruction.
297 */
298 static
299 void _ltt_event_destroy(struct ltt_event *event)
300 {
301 ltt_event_put(event->desc);
302 list_del(&event->list);
303 kmem_cache_free(event_cache, event);
304 }
305
306 /*
307 * We have exclusive access to our metadata buffer (protected by the
308 * sessions_mutex), so we can do racy operations such as looking for
309 * remaining space left in packet and write, since mutual exclusion
310 * protects us from concurrent writes.
311 */
312 int lttng_metadata_printf(struct ltt_session *session,
313 const char *fmt, ...)
314 {
315 struct lib_ring_buffer_ctx ctx;
316 struct ltt_channel *chan = session->metadata;
317 char *str;
318 int ret = 0, waitret;
319 size_t len, reserve_len, pos;
320 va_list ap;
321
322 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
323
324 va_start(ap, fmt);
325 str = kvasprintf(GFP_KERNEL, fmt, ap);
326 va_end(ap);
327 if (!str)
328 return -ENOMEM;
329
330 len = strlen(str);
331 pos = 0;
332
333 for (pos = 0; pos < len; pos += reserve_len) {
334 reserve_len = min_t(size_t,
335 chan->ops->packet_avail_size(chan->chan),
336 len - pos);
337 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
338 sizeof(char), -1);
339 /*
340 * We don't care about metadata buffer's records lost
341 * count, because we always retry here. Report error if
342 * we need to bail out after timeout or being
343 * interrupted.
344 */
345 waitret = wait_event_interruptible_timeout(*chan->ops->get_reader_wait_queue(chan),
346 ({
347 ret = chan->ops->event_reserve(&ctx);
348 ret != -ENOBUFS || !ret;
349 }),
350 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC));
351 if (!waitret || waitret == -ERESTARTSYS || ret) {
352 printk(KERN_WARNING "LTTng: Failure to write metadata to buffers (%s)\n",
353 waitret == -ERESTARTSYS ? "interrupted" :
354 (ret == -ENOBUFS ? "timeout" : "I/O error"));
355 if (waitret == -ERESTARTSYS)
356 ret = waitret;
357 goto end;
358 }
359 chan->ops->event_write(&ctx, &str[pos], len);
360 chan->ops->event_commit(&ctx);
361 }
362 end:
363 kfree(str);
364 return ret;
365 }
366
367 static
368 int _ltt_fields_metadata_statedump(struct ltt_session *session,
369 struct ltt_event *event)
370 {
371 const struct lttng_event_desc *desc = event->desc;
372 int ret = 0;
373 int i;
374
375 for (i = 0; i < desc->nr_fields; i++) {
376 const struct lttng_event_field *field = &desc->fields[i];
377
378 switch (field->type.atype) {
379 case atype_integer:
380 ret = lttng_metadata_printf(session,
381 " integer { size = %u; align = %u; signed = %u;%s } %s;\n",
382 field->type.u.basic.integer.size,
383 field->type.u.basic.integer.alignment,
384 field->type.u.basic.integer.signedness,
385 #ifdef __BIG_ENDIAN
386 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
387 #else
388 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
389 #endif
390 field->name);
391 break;
392 case atype_enum:
393 ret = lttng_metadata_printf(session,
394 " %s %s;\n",
395 field->type.u.basic.enumeration.name,
396 field->name);
397 break;
398 case atype_array:
399 {
400 const struct lttng_basic_type *elem_type;
401
402 elem_type = &field->type.u.array.elem_type;
403 ret = lttng_metadata_printf(session,
404 " integer { size = %u; align = %u; signed = %u;%s } %s[%u];\n",
405 elem_type->u.basic.integer.size,
406 elem_type->u.basic.integer.alignment,
407 elem_type->u.basic.integer.signedness,
408 #ifdef __BIG_ENDIAN
409 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
410 #else
411 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
412 #endif
413 field->name, field->type.u.array.length);
414 break;
415 }
416 case atype_sequence:
417 {
418 const struct lttng_basic_type *elem_type;
419 const struct lttng_basic_type *length_type;
420
421 elem_type = &field->type.u.sequence.elem_type;
422 length_type = &field->type.u.sequence.length_type;
423 ret = lttng_metadata_printf(session,
424 " integer { size = %u; align = %u; signed = %u;%s } %s[ integer { size = %u; align = %u; signed = %u;%s } ];\n",
425 elem_type->u.basic.integer.size,
426 elem_type->u.basic.integer.alignment,
427 elem_type->u.basic.integer.signedness,
428 #ifdef __BIG_ENDIAN
429 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
430 #else
431 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
432 #endif
433 field->name,
434 length_type->u.basic.integer.size,
435 length_type->u.basic.integer.alignment,
436 length_type->u.basic.integer.signedness,
437 #ifdef __BIG_ENDIAN
438 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
439 #else
440 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : ""
441 #endif
442 );
443 break;
444 }
445
446 case atype_string:
447 ret = lttng_metadata_printf(session,
448 " string%s %s;\n",
449 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
450 " { encoding = ASCII; }" : "",
451 field->name);
452 break;
453 default:
454 WARN_ON_ONCE(1);
455 return -EINVAL;
456 }
457 }
458 return ret;
459 }
460
461 static
462 int _ltt_event_metadata_statedump(struct ltt_session *session,
463 struct ltt_channel *chan,
464 struct ltt_event *event)
465 {
466 int ret = 0;
467
468 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
469 return 0;
470 if (chan == session->metadata)
471 return 0;
472
473 ret = lttng_metadata_printf(session,
474 "event {\n"
475 " name = %s;\n"
476 " id = %u;\n"
477 " stream_id = %u;\n"
478 " event.fields := struct {\n",
479 event->desc->name,
480 event->id,
481 event->chan->id);
482 if (ret)
483 goto end;
484
485 ret = _ltt_fields_metadata_statedump(session, event);
486 if (ret)
487 goto end;
488
489 /*
490 * LTTng space reservation can only reserve multiples of the
491 * byte size.
492 */
493 ret = lttng_metadata_printf(session,
494 " } aligned(%u);\n"
495 "};\n\n", ltt_get_header_alignment());
496 if (ret)
497 goto end;
498
499
500
501
502 event->metadata_dumped = 1;
503 end:
504 return ret;
505
506 }
507
508 static
509 int _ltt_channel_metadata_statedump(struct ltt_session *session,
510 struct ltt_channel *chan)
511 {
512 int ret = 0;
513
514 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
515 return 0;
516 if (chan == session->metadata)
517 return 0;
518
519 WARN_ON_ONCE(!chan->header_type);
520 ret = lttng_metadata_printf(session,
521 "stream {\n"
522 " id = %u;\n"
523 " event.header := %s;\n",
524 "};\n\n",
525 chan->id,
526 chan->header_type == 1 ? "struct event_header_compact" :
527 "struct event_header_large");
528 if (ret)
529 goto end;
530
531 chan->metadata_dumped = 1;
532 end:
533 return ret;
534 }
535
536 /*
537 * Output metadata into this session's metadata buffers.
538 */
539 static
540 int _ltt_session_metadata_statedump(struct ltt_session *session)
541 {
542 char uuid_s[37];
543 struct ltt_channel *chan;
544 struct ltt_event *event;
545 int ret = 0;
546
547 if (!ACCESS_ONCE(session->active))
548 return 0;
549 if (session->metadata_dumped)
550 goto skip_session;
551 if (!session->metadata) {
552 printk(KERN_WARNING "LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
553 return -EPERM;
554 }
555
556 snprintf(uuid_s, sizeof(uuid_s),
557 "%x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x",
558 uuid_s[0], uuid_s[1], uuid_s[2], uuid_s[3],
559 uuid_s[4], uuid_s[5], uuid_s[6], uuid_s[7],
560 uuid_s[8], uuid_s[9], uuid_s[10], uuid_s[11],
561 uuid_s[12], uuid_s[13], uuid_s[14], uuid_s[15]);
562
563 ret = lttng_metadata_printf(session,
564 "typealias integer {size = 8; align = %u; signed = false; } := uint8_t;\n"
565 "typealias integer {size = 32; align = %u; signed = false; } := uint32_t;\n"
566 "typealias integer {size = 64; align = %u; signed = false; } := uint64_t;\n"
567 "\n"
568 "trace {\n"
569 " major = %u;\n"
570 " minor = %u;\n"
571 " uuid = %s;\n"
572 " byte_order = %s;\n"
573 " packet.header := struct {\n"
574 " uint32_t magic;\n"
575 " uint8_t uuid[16];\n"
576 " uint32_t stream_id;\n"
577 " uint64_t timestamp_begin;\n"
578 " uint64_t timestamp_end;\n"
579 " uint32_t content_size;\n"
580 " uint32_t packet_size;\n"
581 " uint32_t events_lost;\n"
582 " };\n",
583 "};\n\n",
584 ltt_alignof(uint8_t) * CHAR_BIT,
585 ltt_alignof(uint32_t) * CHAR_BIT,
586 ltt_alignof(uint64_t) * CHAR_BIT,
587 CTF_VERSION_MAJOR,
588 CTF_VERSION_MINOR,
589 uuid_s,
590 #ifdef __BIG_ENDIAN
591 "be"
592 #else
593 "le"
594 #endif
595 );
596 if (ret)
597 goto end;
598
599 skip_session:
600 list_for_each_entry(chan, &session->chan, list) {
601 ret = _ltt_channel_metadata_statedump(session, chan);
602 if (ret)
603 goto end;
604 }
605
606 list_for_each_entry(event, &session->events, list) {
607 ret = _ltt_event_metadata_statedump(session, chan, event);
608 if (ret)
609 goto end;
610 }
611 session->metadata_dumped = 1;
612 end:
613 return ret;
614 }
615
616 /**
617 * ltt_transport_register - LTT transport registration
618 * @transport: transport structure
619 *
620 * Registers a transport which can be used as output to extract the data out of
621 * LTTng. The module calling this registration function must ensure that no
622 * trap-inducing code will be executed by the transport functions. E.g.
623 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
624 * is made visible to the transport function. This registration acts as a
625 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
626 * after its registration must it synchronize the TLBs.
627 */
628 void ltt_transport_register(struct ltt_transport *transport)
629 {
630 /*
631 * Make sure no page fault can be triggered by the module about to be
632 * registered. We deal with this here so we don't have to call
633 * vmalloc_sync_all() in each module's init.
634 */
635 wrapper_vmalloc_sync_all();
636
637 mutex_lock(&sessions_mutex);
638 list_add_tail(&transport->node, &ltt_transport_list);
639 mutex_unlock(&sessions_mutex);
640 }
641 EXPORT_SYMBOL_GPL(ltt_transport_register);
642
643 /**
644 * ltt_transport_unregister - LTT transport unregistration
645 * @transport: transport structure
646 */
647 void ltt_transport_unregister(struct ltt_transport *transport)
648 {
649 mutex_lock(&sessions_mutex);
650 list_del(&transport->node);
651 mutex_unlock(&sessions_mutex);
652 }
653 EXPORT_SYMBOL_GPL(ltt_transport_unregister);
654
655 static int __init ltt_events_init(void)
656 {
657 int ret;
658
659 event_cache = KMEM_CACHE(ltt_event, 0);
660 if (!event_cache)
661 return -ENOMEM;
662 ret = ltt_debugfs_abi_init();
663 if (ret)
664 goto error_abi;
665 return 0;
666 error_abi:
667 kmem_cache_destroy(event_cache);
668 return ret;
669 }
670
671 module_init(ltt_events_init);
672
673 static void __exit ltt_events_exit(void)
674 {
675 struct ltt_session *session, *tmpsession;
676
677 ltt_debugfs_abi_exit();
678 list_for_each_entry_safe(session, tmpsession, &sessions, list)
679 ltt_session_destroy(session);
680 kmem_cache_destroy(event_cache);
681 }
682
683 module_exit(ltt_events_exit);
684
685 MODULE_LICENSE("GPL and additional rights");
686 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
687 MODULE_DESCRIPTION("LTTng Events");
This page took 0.045456 seconds and 6 git commands to generate.