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
13 #include <usterr-signal-safe.h>
14 #include <urcu/compiler.h>
15 #include "shm_types.h"
17 /* lttng_counter_handle_create - for UST. */
19 struct lttng_counter_shm_handle
*lttng_counter_handle_create(void *data
,
20 uint64_t memory_map_size
, int wakeup_fd
);
21 /* lttng_counter_handle_add_cpu - for UST. */
23 int lttng_counter_handle_add_cpu(struct lttng_counter_shm_handle
*handle
,
24 int shm_fd
, uint32_t cpu_nr
,
25 uint64_t memory_map_size
);
27 unsigned int lttng_counter_handle_get_nr_cpus(struct lttng_counter_shm_handle
*handle
)
28 __attribute__((visibility("hidden")));
31 * Pointer dereferencing. We don't trust the shm_ref, so we validate
32 * both the index and offset with known boundaries.
34 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
35 * target type, even in the occurrence of shm_ref modification by an
36 * untrusted process having write access to the shm_ref. We return a
37 * NULL pointer if the ranges are invalid.
40 char *_lttng_counter_shmp_offset(struct lttng_counter_shm_object_table
*table
,
41 struct lttng_counter_shm_ref
*ref
,
42 size_t idx
, size_t elem_size
)
44 struct lttng_counter_shm_object
*obj
;
45 size_t objindex
, ref_offset
;
47 objindex
= (size_t) ref
->index
;
48 if (caa_unlikely(objindex
>= table
->allocated_len
))
50 obj
= &table
->objects
[objindex
];
51 ref_offset
= (size_t) ref
->offset
;
52 ref_offset
+= idx
* elem_size
;
53 /* Check if part of the element returned would exceed the limits. */
54 if (caa_unlikely(ref_offset
+ elem_size
> obj
->memory_map_size
))
56 return &obj
->memory_map
[ref_offset
];
59 #define lttng_counter_shmp_index(handle, ref, index) \
61 __typeof__((ref)._type) ____ptr_ret; \
62 ____ptr_ret = (__typeof__(____ptr_ret)) _lttng_counter_shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
66 #define lttng_counter_shmp(handle, ref) lttng_counter_shmp_index(handle, ref, 0)
69 void _lttng_counter_set_shmp(struct lttng_counter_shm_ref
*ref
, struct lttng_counter_shm_ref src
)
74 #define lttng_counter_set_shmp(ref, src) _lttng_counter_set_shmp(&(ref)._ref, src)
76 struct lttng_counter_shm_object_table
*lttng_counter_shm_object_table_create(size_t max_nb_obj
)
77 __attribute__((visibility("hidden")));
79 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table
*table
,
80 size_t memory_map_size
,
81 enum lttng_counter_shm_object_type type
,
84 __attribute__((visibility("hidden")));
86 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table
*table
,
87 int shm_fd
, size_t memory_map_size
)
88 __attribute__((visibility("hidden")));
90 /* mem ownership is passed to lttng_counter_shm_object_table_append_mem(). */
91 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table
*table
,
92 void *mem
, size_t memory_map_size
)
93 __attribute__((visibility("hidden")));
95 void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table
*table
, int consumer
)
96 __attribute__((visibility("hidden")));
99 * lttng_counter_zalloc_shm - allocate memory within a shm object.
101 * Shared memory is already zeroed by shmget.
102 * *NOT* multithread-safe (should be protected by mutex).
103 * Returns a -1, -1 tuple on error.
105 struct lttng_counter_shm_ref
lttng_counter_zalloc_shm(struct lttng_counter_shm_object
*obj
, size_t len
)
106 __attribute__((visibility("hidden")));
108 void lttng_counter_align_shm(struct lttng_counter_shm_object
*obj
, size_t align
)
109 __attribute__((visibility("hidden")));
112 int lttng_counter_shm_get_shm_fd(struct lttng_counter_shm_handle
*handle
, struct lttng_counter_shm_ref
*ref
)
114 struct lttng_counter_shm_object_table
*table
= handle
->table
;
115 struct lttng_counter_shm_object
*obj
;
118 index
= (size_t) ref
->index
;
119 if (caa_unlikely(index
>= table
->allocated_len
))
121 obj
= &table
->objects
[index
];
127 int lttng_counter_shm_get_shm_size(struct lttng_counter_shm_handle
*handle
, struct lttng_counter_shm_ref
*ref
,
130 struct lttng_counter_shm_object_table
*table
= handle
->table
;
131 struct lttng_counter_shm_object
*obj
;
134 index
= (size_t) ref
->index
;
135 if (caa_unlikely(index
>= table
->allocated_len
))
137 obj
= &table
->objects
[index
];
138 *size
= obj
->memory_map_size
;
142 #endif /* _LIBCOUNTER_SHM_H */