Fix: pass private data to context callbacks
[lttng-ust.git] / libringbuffer / shm.c
CommitLineData
1d498196 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
1d498196 3 *
e92f3e28 4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
1d498196
MD
5 */
6
3fbec7dc 7#define _LGPL_SOURCE
1d498196
MD
8#include "shm.h"
9#include <unistd.h>
10#include <fcntl.h>
11#include <sys/mman.h>
a9ff648c 12#include <sys/types.h>
1d498196
MD
13#include <sys/stat.h> /* For mode constants */
14#include <fcntl.h> /* For O_* constants */
15#include <assert.h>
8da6cd6d
MD
16#include <stdio.h>
17#include <signal.h>
18#include <dirent.h>
96e80018 19#include <limits.h>
8a208943 20#include <stdbool.h>
fb31eb73 21#include <stdint.h>
3d3a2bb8 22
bfcda6ce 23#ifdef HAVE_LIBNUMA
4b68c31f 24#include <numa.h>
8a208943 25#include <numaif.h>
bfcda6ce 26#endif
3d3a2bb8 27
eae3c729 28#include <lttng/ust-utils.h>
3d3a2bb8 29
864a1eda 30#include <ust-helper.h>
6548fca4 31#include <ust-fd.h>
4d4838ba 32#include "mmap.h"
3a81f31d
MD
33
34/*
35 * Ensure we have the required amount of space available by writing 0
36 * into the entire buffer. Not doing so can trigger SIGBUS when going
37 * beyond the available shm space.
38 */
39static
40int zero_file(int fd, size_t len)
41{
42 ssize_t retlen;
43 size_t written = 0;
44 char *zeropage;
45 long pagelen;
46 int ret;
47
48 pagelen = sysconf(_SC_PAGESIZE);
49 if (pagelen < 0)
50 return (int) pagelen;
51 zeropage = calloc(pagelen, 1);
52 if (!zeropage)
53 return -ENOMEM;
54
55 while (len > written) {
56 do {
57 retlen = write(fd, zeropage,
58 min_t(size_t, pagelen, len - written));
59 } while (retlen == -1UL && errno == EINTR);
60 if (retlen < 0) {
61 ret = (int) retlen;
62 goto error;
63 }
64 written += retlen;
65 }
66 ret = 0;
67error:
68 free(zeropage);
69 return ret;
70}
1d498196
MD
71
72struct shm_object_table *shm_object_table_create(size_t max_nb_obj)
73{
74 struct shm_object_table *table;
75
76 table = zmalloc(sizeof(struct shm_object_table) +
77 max_nb_obj * sizeof(table->objects[0]));
74d48abe
MD
78 if (!table)
79 return NULL;
1d498196
MD
80 table->size = max_nb_obj;
81 return table;
82}
83
74d81a6c
MD
84static
85struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table,
a9ff648c 86 size_t memory_map_size,
5ea386c3 87 int stream_fd)
1d498196 88{
5ea386c3 89 int shmfd, waitfd[2], ret, i;
1d498196
MD
90 struct shm_object *obj;
91 char *memory_map;
92
5ea386c3
MD
93 if (stream_fd < 0)
94 return NULL;
1d498196
MD
95 if (table->allocated_len >= table->size)
96 return NULL;
7a9c21bd 97 obj = &table->objects[table->allocated_len];
1d498196
MD
98
99 /* wait_fd: create pipe */
100 ret = pipe(waitfd);
101 if (ret < 0) {
102 PERROR("pipe");
103 goto error_pipe;
104 }
105 for (i = 0; i < 2; i++) {
106 ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC);
107 if (ret < 0) {
108 PERROR("fcntl");
109 goto error_fcntl;
110 }
111 }
5d61a504
MD
112 /* The write end of the pipe needs to be non-blocking */
113 ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK);
114 if (ret < 0) {
115 PERROR("fcntl");
116 goto error_fcntl;
117 }
7a9c21bd 118 memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
1d498196 119
053e6e24
MJ
120 /*
121 * Set POSIX shared memory object size
122 *
123 * First, use ftruncate() to set its size, some implementations won't
124 * allow writes past the size set by ftruncate.
125 * Then, use write() to fill it with zeros, this allows us to fully
126 * allocate it and detect a shortage of shm space without dealing with
127 * a SIGBUS.
128 */
a9ff648c 129
5ea386c3 130 shmfd = stream_fd;
1d498196
MD
131 ret = ftruncate(shmfd, memory_map_size);
132 if (ret) {
133 PERROR("ftruncate");
134 goto error_ftruncate;
135 }
053e6e24
MJ
136 ret = zero_file(shmfd, memory_map_size);
137 if (ret) {
138 PERROR("zero_file");
139 goto error_zero_file;
140 }
71be0c53 141
d0f6cf57
MD
142 /*
143 * Also ensure the file metadata is synced with the storage by using
71be0c53
MJ
144 * fsync(2). Some platforms don't allow fsync on POSIX shm fds, ignore
145 * EINVAL accordingly.
d0f6cf57
MD
146 */
147 ret = fsync(shmfd);
71be0c53 148 if (ret && errno != EINVAL) {
d0f6cf57
MD
149 PERROR("fsync");
150 goto error_fsync;
151 }
5ea386c3 152 obj->shm_fd_ownership = 0;
1d498196
MD
153 obj->shm_fd = shmfd;
154
155 /* memory_map: mmap */
156 memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
4d4838ba 157 MAP_SHARED | LTTNG_MAP_POPULATE, shmfd, 0);
1d498196
MD
158 if (memory_map == MAP_FAILED) {
159 PERROR("mmap");
160 goto error_mmap;
161 }
74d81a6c 162 obj->type = SHM_OBJECT_SHM;
1d498196
MD
163 obj->memory_map = memory_map;
164 obj->memory_map_size = memory_map_size;
165 obj->allocated_len = 0;
dc613eb9 166 obj->index = table->allocated_len++;
7a9c21bd 167
1d498196
MD
168 return obj;
169
170error_mmap:
d0f6cf57 171error_fsync:
1d498196 172error_ftruncate:
3a81f31d 173error_zero_file:
1d498196
MD
174error_fcntl:
175 for (i = 0; i < 2; i++) {
176 ret = close(waitfd[i]);
177 if (ret) {
178 PERROR("close");
179 assert(0);
180 }
181 }
182error_pipe:
1d498196 183 return NULL;
1d498196
MD
184}
185
74d81a6c
MD
186static
187struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table,
188 size_t memory_map_size)
189{
190 struct shm_object *obj;
191 void *memory_map;
ff0f5728 192 int waitfd[2], i, ret;
74d81a6c
MD
193
194 if (table->allocated_len >= table->size)
195 return NULL;
196 obj = &table->objects[table->allocated_len];
197
198 memory_map = zmalloc(memory_map_size);
199 if (!memory_map)
200 goto alloc_error;
201
ff0f5728
MD
202 /* wait_fd: create pipe */
203 ret = pipe(waitfd);
204 if (ret < 0) {
205 PERROR("pipe");
206 goto error_pipe;
207 }
208 for (i = 0; i < 2; i++) {
209 ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC);
210 if (ret < 0) {
211 PERROR("fcntl");
212 goto error_fcntl;
213 }
214 }
215 /* The write end of the pipe needs to be non-blocking */
216 ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK);
217 if (ret < 0) {
218 PERROR("fcntl");
219 goto error_fcntl;
220 }
221 memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
222
223 /* no shm_fd */
74d81a6c 224 obj->shm_fd = -1;
5ea386c3 225 obj->shm_fd_ownership = 0;
74d81a6c
MD
226
227 obj->type = SHM_OBJECT_MEM;
228 obj->memory_map = memory_map;
229 obj->memory_map_size = memory_map_size;
230 obj->allocated_len = 0;
231 obj->index = table->allocated_len++;
232
233 return obj;
234
ff0f5728
MD
235error_fcntl:
236 for (i = 0; i < 2; i++) {
237 ret = close(waitfd[i]);
238 if (ret) {
239 PERROR("close");
240 assert(0);
241 }
242 }
243error_pipe:
244 free(memory_map);
74d81a6c
MD
245alloc_error:
246 return NULL;
247}
248
8a208943
MD
249/*
250 * libnuma prints errors on the console even for numa_available().
251 * Work-around this limitation by using get_mempolicy() directly to
252 * check whether the kernel supports mempolicy.
253 */
254#ifdef HAVE_LIBNUMA
255static bool lttng_is_numa_available(void)
256{
257 int ret;
258
259 ret = get_mempolicy(NULL, NULL, 0, NULL, 0);
260 if (ret && errno == ENOSYS) {
261 return false;
262 }
263 return numa_available() > 0;
264}
265#endif
266
74d81a6c
MD
267struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
268 size_t memory_map_size,
a9ff648c 269 enum shm_object_type type,
4b68c31f
MD
270 int stream_fd,
271 int cpu)
74d81a6c 272{
4b68c31f 273 struct shm_object *shm_object;
bfcda6ce 274#ifdef HAVE_LIBNUMA
8a208943
MD
275 int oldnode = 0, node;
276 bool numa_avail;
4b68c31f 277
8a208943
MD
278 numa_avail = lttng_is_numa_available();
279 if (numa_avail) {
280 oldnode = numa_preferred();
281 if (cpu >= 0) {
282 node = numa_node_of_cpu(cpu);
283 if (node >= 0)
284 numa_set_preferred(node);
285 }
286 if (cpu < 0 || node < 0)
287 numa_set_localalloc();
4b68c31f 288 }
bfcda6ce 289#endif /* HAVE_LIBNUMA */
74d81a6c
MD
290 switch (type) {
291 case SHM_OBJECT_SHM:
4b68c31f 292 shm_object = _shm_object_table_alloc_shm(table, memory_map_size,
5ea386c3 293 stream_fd);
4b68c31f 294 break;
74d81a6c 295 case SHM_OBJECT_MEM:
4b68c31f
MD
296 shm_object = _shm_object_table_alloc_mem(table, memory_map_size);
297 break;
74d81a6c
MD
298 default:
299 assert(0);
300 }
bfcda6ce 301#ifdef HAVE_LIBNUMA
8a208943
MD
302 if (numa_avail)
303 numa_set_preferred(oldnode);
bfcda6ce 304#endif /* HAVE_LIBNUMA */
4b68c31f 305 return shm_object;
74d81a6c
MD
306}
307
308struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
309 int shm_fd, int wakeup_fd, uint32_t stream_nr,
310 size_t memory_map_size)
193183fb
MD
311{
312 struct shm_object *obj;
313 char *memory_map;
74d81a6c 314 int ret;
193183fb
MD
315
316 if (table->allocated_len >= table->size)
317 return NULL;
74d81a6c
MD
318 /* streams _must_ be received in sequential order, else fail. */
319 if (stream_nr + 1 != table->allocated_len)
320 return NULL;
321
193183fb
MD
322 obj = &table->objects[table->allocated_len];
323
74d81a6c
MD
324 /* wait_fd: set write end of the pipe. */
325 obj->wait_fd[0] = -1; /* read end is unset */
326 obj->wait_fd[1] = wakeup_fd;
193183fb 327 obj->shm_fd = shm_fd;
5ea386c3 328 obj->shm_fd_ownership = 1;
193183fb 329
74d81a6c
MD
330 /* The write end of the pipe needs to be non-blocking */
331 ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK);
332 if (ret < 0) {
333 PERROR("fcntl");
334 goto error_fcntl;
335 }
336
193183fb
MD
337 /* memory_map: mmap */
338 memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
4d4838ba 339 MAP_SHARED | LTTNG_MAP_POPULATE, shm_fd, 0);
193183fb
MD
340 if (memory_map == MAP_FAILED) {
341 PERROR("mmap");
342 goto error_mmap;
343 }
74d81a6c 344 obj->type = SHM_OBJECT_SHM;
193183fb
MD
345 obj->memory_map = memory_map;
346 obj->memory_map_size = memory_map_size;
347 obj->allocated_len = memory_map_size;
348 obj->index = table->allocated_len++;
349
350 return obj;
351
74d81a6c 352error_fcntl:
193183fb
MD
353error_mmap:
354 return NULL;
355}
356
74d81a6c
MD
357/*
358 * Passing ownership of mem to object.
359 */
360struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
ff0f5728 361 void *mem, size_t memory_map_size, int wakeup_fd)
74d81a6c
MD
362{
363 struct shm_object *obj;
ff0f5728 364 int ret;
74d81a6c
MD
365
366 if (table->allocated_len >= table->size)
367 return NULL;
368 obj = &table->objects[table->allocated_len];
369
ff0f5728
MD
370 obj->wait_fd[0] = -1; /* read end is unset */
371 obj->wait_fd[1] = wakeup_fd;
74d81a6c 372 obj->shm_fd = -1;
5ea386c3 373 obj->shm_fd_ownership = 0;
74d81a6c 374
ff0f5728
MD
375 ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC);
376 if (ret < 0) {
377 PERROR("fcntl");
378 goto error_fcntl;
379 }
380 /* The write end of the pipe needs to be non-blocking */
381 ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK);
382 if (ret < 0) {
383 PERROR("fcntl");
384 goto error_fcntl;
385 }
386
74d81a6c
MD
387 obj->type = SHM_OBJECT_MEM;
388 obj->memory_map = mem;
389 obj->memory_map_size = memory_map_size;
390 obj->allocated_len = memory_map_size;
391 obj->index = table->allocated_len++;
392
393 return obj;
ff0f5728
MD
394
395error_fcntl:
396 return NULL;
74d81a6c
MD
397}
398
1d498196 399static
6548fca4 400void shmp_object_destroy(struct shm_object *obj, int consumer)
1d498196 401{
74d81a6c
MD
402 switch (obj->type) {
403 case SHM_OBJECT_SHM:
404 {
405 int ret, i;
1d498196 406
7a784989
MD
407 ret = munmap(obj->memory_map, obj->memory_map_size);
408 if (ret) {
409 PERROR("umnmap");
410 assert(0);
411 }
6548fca4 412
5ea386c3 413 if (obj->shm_fd_ownership) {
6548fca4
MD
414 /* Delete FDs only if called from app (not consumer). */
415 if (!consumer) {
416 lttng_ust_lock_fd_tracker();
417 ret = close(obj->shm_fd);
418 if (!ret) {
419 lttng_ust_delete_fd_from_tracker(obj->shm_fd);
420 } else {
421 PERROR("close");
422 assert(0);
423 }
424 lttng_ust_unlock_fd_tracker();
425 } else {
426 ret = close(obj->shm_fd);
427 if (ret) {
428 PERROR("close");
429 assert(0);
430 }
a9ff648c
MD
431 }
432 }
74d81a6c
MD
433 for (i = 0; i < 2; i++) {
434 if (obj->wait_fd[i] < 0)
435 continue;
6548fca4
MD
436 if (!consumer) {
437 lttng_ust_lock_fd_tracker();
438 ret = close(obj->wait_fd[i]);
439 if (!ret) {
440 lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]);
441 } else {
442 PERROR("close");
443 assert(0);
444 }
445 lttng_ust_unlock_fd_tracker();
446 } else {
447 ret = close(obj->wait_fd[i]);
448 if (ret) {
449 PERROR("close");
450 assert(0);
451 }
74d81a6c 452 }
1d498196 453 }
74d81a6c
MD
454 break;
455 }
456 case SHM_OBJECT_MEM:
ff0f5728
MD
457 {
458 int ret, i;
459
460 for (i = 0; i < 2; i++) {
461 if (obj->wait_fd[i] < 0)
462 continue;
6548fca4
MD
463 if (!consumer) {
464 lttng_ust_lock_fd_tracker();
465 ret = close(obj->wait_fd[i]);
466 if (!ret) {
467 lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]);
468 } else {
469 PERROR("close");
470 assert(0);
471 }
472 lttng_ust_unlock_fd_tracker();
473 } else {
474 ret = close(obj->wait_fd[i]);
475 if (ret) {
476 PERROR("close");
477 assert(0);
478 }
ff0f5728
MD
479 }
480 }
74d81a6c
MD
481 free(obj->memory_map);
482 break;
ff0f5728 483 }
74d81a6c
MD
484 default:
485 assert(0);
1d498196
MD
486 }
487}
488
6548fca4 489void shm_object_table_destroy(struct shm_object_table *table, int consumer)
1d498196
MD
490{
491 int i;
492
493 for (i = 0; i < table->allocated_len; i++)
6548fca4 494 shmp_object_destroy(&table->objects[i], consumer);
1d498196
MD
495 free(table);
496}
497
498/*
499 * zalloc_shm - allocate memory within a shm object.
500 *
501 * Shared memory is already zeroed by shmget.
502 * *NOT* multithread-safe (should be protected by mutex).
503 * Returns a -1, -1 tuple on error.
504 */
505struct shm_ref zalloc_shm(struct shm_object *obj, size_t len)
506{
507 struct shm_ref ref;
508 struct shm_ref shm_ref_error = { -1, -1 };
509
510 if (obj->memory_map_size - obj->allocated_len < len)
511 return shm_ref_error;
512 ref.index = obj->index;
513 ref.offset = obj->allocated_len;
514 obj->allocated_len += len;
515 return ref;
516}
517
518void align_shm(struct shm_object *obj, size_t align)
519{
b72687b8 520 size_t offset_len = lttng_ust_offset_align(obj->allocated_len, align);
1d498196
MD
521 obj->allocated_len += offset_len;
522}
This page took 0.062603 seconds and 4 git commands to generate.