1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
3 * lttng-context-preemptible.c
5 * LTTng preemptible context.
7 * Copyright (C) 2009-2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/sched.h>
13 #include <linux/irqflags.h>
14 #include <lttng-events.h>
15 #include <wrapper/ringbuffer/frontend_types.h>
16 #include <wrapper/vmalloc.h>
17 #include <lttng-tracer.h>
20 * We nest twice in preempt disabling within LTTng: one nesting is done
21 * by the instrumentation (tracepoint, kprobes, kretprobes, syscall
22 * tracepoint), and the second is within the lib ring buffer
23 * lib_ring_buffer_get_cpu().
25 #define LTTNG_PREEMPT_DISABLE_NESTING 2
28 size_t preemptible_get_size(size_t offset
)
32 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint8_t));
33 size
+= sizeof(uint8_t);
38 void preemptible_record(struct lttng_ctx_field
*field
,
39 struct lib_ring_buffer_ctx
*ctx
,
40 struct lttng_channel
*chan
)
42 int pc
= preempt_count();
43 uint8_t preemptible
= 0;
45 WARN_ON_ONCE(pc
< LTTNG_PREEMPT_DISABLE_NESTING
);
46 if (pc
== LTTNG_PREEMPT_DISABLE_NESTING
)
48 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(preemptible
));
49 chan
->ops
->event_write(ctx
, &preemptible
, sizeof(preemptible
));
53 void preemptible_get_value(struct lttng_ctx_field
*field
,
54 struct lttng_probe_ctx
*lttng_probe_ctx
,
55 union lttng_ctx_value
*value
)
57 int pc
= preempt_count();
59 WARN_ON_ONCE(pc
< LTTNG_PREEMPT_DISABLE_NESTING
);
60 if (pc
== LTTNG_PREEMPT_DISABLE_NESTING
)
66 int lttng_add_preemptible_to_ctx(struct lttng_ctx
**ctx
)
68 struct lttng_ctx_field
*field
;
70 field
= lttng_append_context(ctx
);
73 if (lttng_find_context(*ctx
, "preemptible")) {
74 lttng_remove_context_field(ctx
, field
);
77 field
->event_field
.name
= "preemptible";
78 field
->event_field
.type
.atype
= atype_integer
;
79 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(uint8_t) * CHAR_BIT
;
80 field
->event_field
.type
.u
.basic
.integer
.alignment
= lttng_alignof(uint8_t) * CHAR_BIT
;
81 field
->event_field
.type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(uint8_t);
82 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
83 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
84 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
85 field
->get_size
= preemptible_get_size
;
86 field
->record
= preemptible_record
;
87 field
->get_value
= preemptible_get_value
;
88 lttng_context_update(*ctx
);
89 wrapper_vmalloc_sync_mappings();
92 EXPORT_SYMBOL_GPL(lttng_add_preemptible_to_ctx
);