2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _LIBCOUNTER_SHM_H
8 #define _LIBCOUNTER_SHM_H
14 #include "common/logging.h"
15 #include <urcu/compiler.h>
16 #include "shm_types.h"
18 /* lttng_counter_handle_create - for UST. */
20 struct lttng_counter_shm_handle
*lttng_counter_handle_create(void *data
,
21 uint64_t memory_map_size
, int wakeup_fd
);
22 /* lttng_counter_handle_add_cpu - for UST. */
24 int lttng_counter_handle_add_cpu(struct lttng_counter_shm_handle
*handle
,
25 int shm_fd
, uint32_t cpu_nr
,
26 uint64_t memory_map_size
);
28 unsigned int lttng_counter_handle_get_nr_cpus(struct lttng_counter_shm_handle
*handle
)
29 __attribute__((visibility("hidden")));
32 * Pointer dereferencing. We don't trust the shm_ref, so we validate
33 * both the index and offset with known boundaries.
35 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
36 * target type, even in the occurrence of shm_ref modification by an
37 * untrusted process having write access to the shm_ref. We return a
38 * NULL pointer if the ranges are invalid.
41 char *_lttng_counter_shmp_offset(struct lttng_counter_shm_object_table
*table
,
42 struct lttng_counter_shm_ref
*ref
,
43 size_t idx
, size_t elem_size
)
45 struct lttng_counter_shm_object
*obj
;
46 size_t objindex
, ref_offset
;
48 objindex
= (size_t) ref
->index
;
49 if (caa_unlikely(objindex
>= table
->allocated_len
))
51 obj
= &table
->objects
[objindex
];
52 ref_offset
= (size_t) ref
->offset
;
53 ref_offset
+= idx
* elem_size
;
54 /* Check if part of the element returned would exceed the limits. */
55 if (caa_unlikely(ref_offset
+ elem_size
> obj
->memory_map_size
))
57 return &obj
->memory_map
[ref_offset
];
60 #define lttng_counter_shmp_index(handle, ref, index) \
62 __typeof__((ref)._type) ____ptr_ret; \
63 ____ptr_ret = (__typeof__(____ptr_ret)) _lttng_counter_shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
67 #define lttng_counter_shmp(handle, ref) lttng_counter_shmp_index(handle, ref, 0)
70 void _lttng_counter_set_shmp(struct lttng_counter_shm_ref
*ref
, struct lttng_counter_shm_ref src
)
75 #define lttng_counter_set_shmp(ref, src) _lttng_counter_set_shmp(&(ref)._ref, src)
77 struct lttng_counter_shm_object_table
*lttng_counter_shm_object_table_create(size_t max_nb_obj
, bool populate
)
78 __attribute__((visibility("hidden")));
80 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table
*table
,
81 size_t memory_map_size
,
82 enum lttng_counter_shm_object_type type
,
84 int cpu
, bool populate
)
85 __attribute__((visibility("hidden")));
87 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table
*table
,
88 int shm_fd
, size_t memory_map_size
, bool populate
)
89 __attribute__((visibility("hidden")));
91 /* mem ownership is passed to lttng_counter_shm_object_table_append_mem(). */
92 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table
*table
,
93 void *mem
, size_t memory_map_size
)
94 __attribute__((visibility("hidden")));
96 void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table
*table
, int consumer
)
97 __attribute__((visibility("hidden")));
100 * lttng_counter_zalloc_shm - allocate memory within a shm object.
102 * Shared memory is already zeroed by shmget.
103 * *NOT* multithread-safe (should be protected by mutex).
104 * Returns a -1, -1 tuple on error.
106 struct lttng_counter_shm_ref
lttng_counter_zalloc_shm(struct lttng_counter_shm_object
*obj
, size_t len
)
107 __attribute__((visibility("hidden")));
109 void lttng_counter_align_shm(struct lttng_counter_shm_object
*obj
, size_t align
)
110 __attribute__((visibility("hidden")));
113 int lttng_counter_shm_get_shm_fd(struct lttng_counter_shm_handle
*handle
, struct lttng_counter_shm_ref
*ref
)
115 struct lttng_counter_shm_object_table
*table
= handle
->table
;
116 struct lttng_counter_shm_object
*obj
;
119 index
= (size_t) ref
->index
;
120 if (caa_unlikely(index
>= table
->allocated_len
))
122 obj
= &table
->objects
[index
];
128 int lttng_counter_shm_get_shm_size(struct lttng_counter_shm_handle
*handle
, struct lttng_counter_shm_ref
*ref
,
131 struct lttng_counter_shm_object_table
*table
= handle
->table
;
132 struct lttng_counter_shm_object
*obj
;
135 index
= (size_t) ref
->index
;
136 if (caa_unlikely(index
>= table
->allocated_len
))
138 obj
= &table
->objects
[index
];
139 *size
= obj
->memory_map_size
;
143 #endif /* _LIBCOUNTER_SHM_H */