1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * lttng-context-cgroup-ns.c
5 * LTTng cgroup namespace context.
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * 2019 Michael Jeanson <mjeanson@efficios.com>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/cgroup.h>
16 #include <lttng/events.h>
17 #include <ringbuffer/frontend_types.h>
18 #include <wrapper/vmalloc.h>
19 #include <wrapper/namespace.h>
20 #include <lttng/tracer.h>
22 #if defined(CONFIG_CGROUPS) && \
23 ((LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)) || \
24 LTTNG_UBUNTU_KERNEL_RANGE(4,4,0,0, 4,5,0,0))
27 size_t cgroup_ns_get_size(size_t offset
)
31 size
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned int));
32 size
+= sizeof(unsigned int);
37 void cgroup_ns_record(struct lttng_ctx_field
*field
,
38 struct lib_ring_buffer_ctx
*ctx
,
39 struct lttng_channel
*chan
)
41 unsigned int cgroup_ns_inum
= 0;
44 * nsproxy can be NULL when scheduled out of exit.
46 * As documented in 'linux/nsproxy.h' namespaces access rules, no
47 * precautions should be taken when accessing the current task's
48 * namespaces, just dereference the pointers.
51 cgroup_ns_inum
= current
->nsproxy
->cgroup_ns
->lttng_ns_inum
;
53 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(cgroup_ns_inum
));
54 chan
->ops
->event_write(ctx
, &cgroup_ns_inum
, sizeof(cgroup_ns_inum
));
58 void cgroup_ns_get_value(struct lttng_ctx_field
*field
,
59 struct lttng_probe_ctx
*lttng_probe_ctx
,
60 union lttng_ctx_value
*value
)
62 unsigned int cgroup_ns_inum
= 0;
65 * nsproxy can be NULL when scheduled out of exit.
67 * As documented in 'linux/nsproxy.h' namespaces access rules, no
68 * precautions should be taken when accessing the current task's
69 * namespaces, just dereference the pointers.
72 cgroup_ns_inum
= current
->nsproxy
->cgroup_ns
->lttng_ns_inum
;
74 value
->s64
= cgroup_ns_inum
;
77 int lttng_add_cgroup_ns_to_ctx(struct lttng_ctx
**ctx
)
79 struct lttng_ctx_field
*field
;
81 field
= lttng_append_context(ctx
);
84 if (lttng_find_context(*ctx
, "cgroup_ns")) {
85 lttng_remove_context_field(ctx
, field
);
88 field
->event_field
.name
= "cgroup_ns";
89 field
->event_field
.type
.atype
= atype_integer
;
90 field
->event_field
.type
.u
.integer
.size
= sizeof(unsigned int) * CHAR_BIT
;
91 field
->event_field
.type
.u
.integer
.alignment
= lttng_alignof(unsigned int) * CHAR_BIT
;
92 field
->event_field
.type
.u
.integer
.signedness
= lttng_is_signed_type(unsigned int);
93 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
94 field
->event_field
.type
.u
.integer
.base
= 10;
95 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
96 field
->get_size
= cgroup_ns_get_size
;
97 field
->record
= cgroup_ns_record
;
98 field
->get_value
= cgroup_ns_get_value
;
99 lttng_context_update(*ctx
);
100 wrapper_vmalloc_sync_mappings();
103 EXPORT_SYMBOL_GPL(lttng_add_cgroup_ns_to_ctx
);