Take NUMA configuration into account for UST buffer allocation
[lttng-ust.git] / libringbuffer / shm.h
1 #ifndef _LIBRINGBUFFER_SHM_H
2 #define _LIBRINGBUFFER_SHM_H
3
4 /*
5 * libringbuffer/shm.h
6 *
7 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <stdint.h>
25 #include <usterr-signal-safe.h>
26 #include <urcu/compiler.h>
27 #include "shm_types.h"
28
29 /* channel_handle_create - for UST. */
30 extern
31 struct lttng_ust_shm_handle *channel_handle_create(void *data,
32 uint64_t memory_map_size, int wakeup_fd);
33 /* channel_handle_add_stream - for UST. */
34 extern
35 int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
36 int shm_fd, int wakeup_fd, uint32_t stream_nr,
37 uint64_t memory_map_size);
38 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle);
39 extern
40 void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
41 int consumer);
42
43 /*
44 * Pointer dereferencing. We don't trust the shm_ref, so we validate
45 * both the index and offset with known boundaries.
46 *
47 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
48 * target type, even in the occurrence of shm_ref modification by an
49 * untrusted process having write access to the shm_ref. We return a
50 * NULL pointer if the ranges are invalid.
51 */
52 static inline
53 char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
54 size_t idx, size_t elem_size)
55 {
56 struct shm_object *obj;
57 size_t objindex, ref_offset;
58
59 objindex = (size_t) ref->index;
60 if (caa_unlikely(objindex >= table->allocated_len))
61 return NULL;
62 obj = &table->objects[objindex];
63 ref_offset = (size_t) ref->offset;
64 ref_offset += idx * elem_size;
65 /* Check if part of the element returned would exceed the limits. */
66 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
67 return NULL;
68 return &obj->memory_map[ref_offset];
69 }
70
71 #define shmp_index(handle, ref, index) \
72 ({ \
73 __typeof__((ref)._type) ____ptr_ret; \
74 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
75 ____ptr_ret; \
76 })
77
78 #define shmp(handle, ref) shmp_index(handle, ref, 0)
79
80 static inline
81 void _set_shmp(struct shm_ref *ref, struct shm_ref src)
82 {
83 *ref = src;
84 }
85
86 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
87
88 struct shm_object_table *shm_object_table_create(size_t max_nb_obj);
89 struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
90 size_t memory_map_size,
91 enum shm_object_type type,
92 const int stream_fd,
93 int cpu);
94 struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
95 int shm_fd, int wakeup_fd, uint32_t stream_nr,
96 size_t memory_map_size);
97 /* mem ownership is passed to shm_object_table_append_mem(). */
98 struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
99 void *mem, size_t memory_map_size, int wakeup_fd);
100 void shm_object_table_destroy(struct shm_object_table *table, int consumer);
101
102 /*
103 * zalloc_shm - allocate memory within a shm object.
104 *
105 * Shared memory is already zeroed by shmget.
106 * *NOT* multithread-safe (should be protected by mutex).
107 * Returns a -1, -1 tuple on error.
108 */
109 struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
110 void align_shm(struct shm_object *obj, size_t align);
111
112 static inline
113 int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
114 {
115 struct shm_object_table *table = handle->table;
116 struct shm_object *obj;
117 size_t index;
118
119 index = (size_t) ref->index;
120 if (caa_unlikely(index >= table->allocated_len))
121 return -EPERM;
122 obj = &table->objects[index];
123 return obj->wait_fd[0];
124 }
125
126 static inline
127 int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
128 {
129 struct shm_object_table *table = handle->table;
130 struct shm_object *obj;
131 size_t index;
132
133 index = (size_t) ref->index;
134 if (caa_unlikely(index >= table->allocated_len))
135 return -EPERM;
136 obj = &table->objects[index];
137 return obj->wait_fd[1];
138 }
139
140 static inline
141 int shm_close_wait_fd(struct lttng_ust_shm_handle *handle,
142 struct shm_ref *ref)
143 {
144 struct shm_object_table *table = handle->table;
145 struct shm_object *obj;
146 int wait_fd;
147 size_t index;
148 int ret;
149
150 index = (size_t) ref->index;
151 if (caa_unlikely(index >= table->allocated_len))
152 return -EPERM;
153 obj = &table->objects[index];
154 wait_fd = obj->wait_fd[0];
155 if (wait_fd < 0)
156 return -ENOENT;
157 obj->wait_fd[0] = -1;
158 ret = close(wait_fd);
159 if (ret) {
160 ret = -errno;
161 return ret;
162 }
163 return 0;
164 }
165
166 static inline
167 int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle,
168 struct shm_ref *ref)
169 {
170 struct shm_object_table *table = handle->table;
171 struct shm_object *obj;
172 int wakeup_fd;
173 size_t index;
174 int ret;
175
176 index = (size_t) ref->index;
177 if (caa_unlikely(index >= table->allocated_len))
178 return -EPERM;
179 obj = &table->objects[index];
180 wakeup_fd = obj->wait_fd[1];
181 if (wakeup_fd < 0)
182 return -ENOENT;
183 obj->wait_fd[1] = -1;
184 ret = close(wakeup_fd);
185 if (ret) {
186 ret = -errno;
187 return ret;
188 }
189 return 0;
190 }
191
192 static inline
193 int shm_get_shm_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
194 {
195 struct shm_object_table *table = handle->table;
196 struct shm_object *obj;
197 size_t index;
198
199 index = (size_t) ref->index;
200 if (caa_unlikely(index >= table->allocated_len))
201 return -EPERM;
202 obj = &table->objects[index];
203 return obj->shm_fd;
204 }
205
206
207 static inline
208 int shm_get_shm_size(struct lttng_ust_shm_handle *handle, struct shm_ref *ref,
209 uint64_t *size)
210 {
211 struct shm_object_table *table = handle->table;
212 struct shm_object *obj;
213 size_t index;
214
215 index = (size_t) ref->index;
216 if (caa_unlikely(index >= table->allocated_len))
217 return -EPERM;
218 obj = &table->objects[index];
219 *size = obj->memory_map_size;
220 return 0;
221 }
222
223 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.033647 seconds and 4 git commands to generate.