| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-context-cgroup-ns.c |
| 4 | * |
| 5 | * LTTng cgroup namespace context. |
| 6 | * |
| 7 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * 2019 Michael Jeanson <mjeanson@efficios.com> |
| 9 | * |
| 10 | */ |
| 11 | |
| 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> |
| 21 | |
| 22 | #if defined(CONFIG_CGROUPS) && \ |
| 23 | ((LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0)) || \ |
| 24 | LTTNG_UBUNTU_KERNEL_RANGE(4,4,0,0, 4,5,0,0)) |
| 25 | |
| 26 | static |
| 27 | size_t cgroup_ns_get_size(size_t offset) |
| 28 | { |
| 29 | size_t size = 0; |
| 30 | |
| 31 | size += lib_ring_buffer_align(offset, lttng_alignof(unsigned int)); |
| 32 | size += sizeof(unsigned int); |
| 33 | return size; |
| 34 | } |
| 35 | |
| 36 | static |
| 37 | void cgroup_ns_record(struct lttng_ctx_field *field, |
| 38 | struct lib_ring_buffer_ctx *ctx, |
| 39 | struct lttng_channel *chan) |
| 40 | { |
| 41 | unsigned int cgroup_ns_inum = 0; |
| 42 | |
| 43 | /* |
| 44 | * nsproxy can be NULL when scheduled out of exit. |
| 45 | * |
| 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. |
| 49 | */ |
| 50 | if (current->nsproxy) |
| 51 | cgroup_ns_inum = current->nsproxy->cgroup_ns->lttng_ns_inum; |
| 52 | |
| 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)); |
| 55 | } |
| 56 | |
| 57 | static |
| 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) |
| 61 | { |
| 62 | unsigned int cgroup_ns_inum = 0; |
| 63 | |
| 64 | /* |
| 65 | * nsproxy can be NULL when scheduled out of exit. |
| 66 | * |
| 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. |
| 70 | */ |
| 71 | if (current->nsproxy) |
| 72 | cgroup_ns_inum = current->nsproxy->cgroup_ns->lttng_ns_inum; |
| 73 | |
| 74 | value->s64 = cgroup_ns_inum; |
| 75 | } |
| 76 | |
| 77 | int lttng_add_cgroup_ns_to_ctx(struct lttng_ctx **ctx) |
| 78 | { |
| 79 | struct lttng_ctx_field *field; |
| 80 | |
| 81 | field = lttng_append_context(ctx); |
| 82 | if (!field) |
| 83 | return -ENOMEM; |
| 84 | if (lttng_find_context(*ctx, "cgroup_ns")) { |
| 85 | lttng_remove_context_field(ctx, field); |
| 86 | return -EEXIST; |
| 87 | } |
| 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(); |
| 101 | return 0; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(lttng_add_cgroup_ns_to_ctx); |
| 104 | |
| 105 | #endif |