blob: b844e20b4cc0874d3ad7bd0f27888b7dea2259ad [file] [log] [blame]
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
Dave Barach8a7fb0c2016-07-08 14:44:23 -04003 * svm.c - shared VM allocation, mmap(...MAP_FIXED...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004 * library
5 *
6 * Copyright (c) 2009 Cisco and/or its affiliates.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *------------------------------------------------------------------
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/mman.h>
25#include <sys/stat.h>
26#include <netinet/in.h>
27#include <signal.h>
28#include <pthread.h>
29#include <unistd.h>
30#include <time.h>
31#include <fcntl.h>
32#include <string.h>
33#include <vppinfra/clib.h>
34#include <vppinfra/vec.h>
35#include <vppinfra/hash.h>
36#include <vppinfra/bitmap.h>
37#include <vppinfra/fifo.h>
38#include <vppinfra/time.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070039#include <vppinfra/heap.h>
40#include <vppinfra/pool.h>
41#include <vppinfra/format.h>
42
43#include "svm.h"
44
45static svm_region_t *root_rp;
46static int root_rp_refcount;
47
48#define MAXLOCK 2
Dave Barach8a7fb0c2016-07-08 14:44:23 -040049static pthread_mutex_t *mutexes_held[MAXLOCK];
Ed Warnickecb9cada2015-12-08 15:45:58 -070050static int nheld;
51
Dave Barach8a7fb0c2016-07-08 14:44:23 -040052svm_region_t *
53svm_get_root_rp (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070054{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040055 return root_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056}
57
58#define MUTEX_DEBUG
59
Damjan Marionaec8f892018-01-08 16:35:35 +010060u64
61svm_get_global_region_base_va ()
62{
63#if __aarch64__
64 /* On AArch64 VA space can have different size, from 36 to 48 bits.
65 Here we are trying to detect VA bits by parsing /proc/self/maps
66 address ranges */
67 int fd;
68 unformat_input_t input;
69 u64 start, end = 0;
70 u8 bits = 0;
71
72 if ((fd = open ("/proc/self/maps", 0)) < 0)
73 clib_unix_error ("open '/proc/self/maps'");
74
75 unformat_init_clib_file (&input, fd);
76 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
77 {
Gabriel Gannec5239ad2018-01-11 15:04:19 +010078 if (unformat (&input, "%llx-%llx", &start, &end))
79 end--;
Damjan Marionaec8f892018-01-08 16:35:35 +010080 unformat_skip_line (&input);
81 }
Gabriel Ganne83d47432018-01-10 11:40:50 +010082 unformat_free (&input);
83 close (fd);
Damjan Marionaec8f892018-01-08 16:35:35 +010084
Damjan Marion11056002018-05-10 13:40:44 +020085 bits = count_leading_zeros (end);
Gabriel Gannec5239ad2018-01-11 15:04:19 +010086 bits = 64 - bits;
Damjan Marionaec8f892018-01-08 16:35:35 +010087 if (bits >= 36 && bits <= 48)
88 return ((1ul << bits) / 4) - (2 * SVM_GLOBAL_REGION_SIZE);
89 else
90 clib_unix_error ("unexpected va bits '%u'", bits);
Damjan Marionaec8f892018-01-08 16:35:35 +010091#endif
92
Tianyu Li1ef38742021-06-17 07:08:32 +000093#ifdef CLIB_SANITIZE_ADDR
94 return 0x200000000000;
95#endif
Damjan Marionaec8f892018-01-08 16:35:35 +010096 /* default value */
Dave Barach9466c452018-08-24 17:21:14 -040097 return 0x130000000ULL;
Damjan Marionaec8f892018-01-08 16:35:35 +010098}
99
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400100static void
101region_lock (svm_region_t * rp, int tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400103 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400105 rp->mutex_owner_pid = getpid ();
106 rp->mutex_owner_tag = tag;
107#endif
Dave Barachc35f3e82020-04-02 10:44:09 -0400108 ASSERT (nheld < MAXLOCK); //NOSONAR
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400109 /*
110 * Keep score of held mutexes so we can try to exit
111 * cleanly if the world comes to an end at the worst possible
112 * moment
113 */
114 mutexes_held[nheld++] = &rp->mutex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115}
116
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400117static void
118region_unlock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400120 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400122 rp->mutex_owner_pid = 0;
123 rp->mutex_owner_tag = 0;
124#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400126 for (i = nheld - 1; i >= 0; i--)
127 {
128 if (mutexes_held[i] == &rp->mutex)
129 {
130 for (j = i; j < MAXLOCK - 1; j++)
131 mutexes_held[j] = mutexes_held[j + 1];
132 nheld--;
133 goto found;
134 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400136 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
138found:
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400139 CLIB_MEMORY_BARRIER ();
140 pthread_mutex_unlock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141}
142
143
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400144static u8 *
145format_svm_flags (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400147 uword f = va_arg (*args, uword);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400149 if (f & SVM_FLAGS_MHEAP)
150 s = format (s, "MHEAP ");
151 if (f & SVM_FLAGS_FILE)
152 s = format (s, "FILE ");
153 if (f & SVM_FLAGS_NODATA)
154 s = format (s, "NODATA ");
155 if (f & SVM_FLAGS_NEED_DATA_INIT)
156 s = format (s, "INIT ");
157
158 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159}
160
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400161static u8 *
162format_svm_size (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400164 uword size = va_arg (*args, uword);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400166 if (size >= (1 << 20))
167 {
168 s = format (s, "(%d mb)", size >> 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400170 else if (size >= (1 << 10))
171 {
172 s = format (s, "(%d kb)", size >> 10);
173 }
174 else
175 {
176 s = format (s, "(%d bytes)", size);
177 }
178 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179}
180
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400181u8 *
182format_svm_region (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400184 svm_region_t *rp = va_arg (*args, svm_region_t *);
185 int verbose = va_arg (*args, int);
186 int i;
187 uword lo, hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400189 s = format (s, "%s: base va 0x%x size 0x%x %U\n",
190 rp->region_name, rp->virtual_base,
191 rp->virtual_size, format_svm_size, rp->virtual_size);
192 s = format (s, " user_ctx 0x%x, bitmap_size %d\n",
193 rp->user_ctx, rp->bitmap_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400195 if (verbose)
196 {
197 s = format (s, " flags: 0x%x %U\n", rp->flags,
198 format_svm_flags, rp->flags);
199 s = format (s,
200 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
201 rp->region_heap, rp->data_base, rp->data_heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 }
203
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400204 s = format (s, " %d clients, pids: ", vec_len (rp->client_pids));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400206 for (i = 0; i < vec_len (rp->client_pids); i++)
207 s = format (s, "%d ", rp->client_pids[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400209 s = format (s, "\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400211 if (verbose)
212 {
213 lo = hi = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400215 s = format (s, " VM in use: ");
216
217 for (i = 0; i < rp->bitmap_size; i++)
218 {
219 if (clib_bitmap_get_no_check (rp->bitmap, i) != 0)
220 {
221 if (lo == ~0)
222 {
223 hi = lo = rp->virtual_base + i * MMAP_PAGESIZE;
224 }
225 else
226 {
227 hi = rp->virtual_base + i * MMAP_PAGESIZE;
228 }
229 }
230 else
231 {
232 if (lo != ~0)
233 {
234 hi = rp->virtual_base + i * MMAP_PAGESIZE - 1;
235 s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi,
236 (hi - lo) >> 10);
237 lo = hi = ~0;
238 }
239 }
240 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 }
242
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400243 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244}
245
246/*
247 * rnd_pagesize
248 * Round to a pagesize multiple, presumably 4k works
249 */
Dave Barachb3d93da2016-08-03 14:34:38 -0400250static u64
251rnd_pagesize (u64 size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252{
Dave Barachb3d93da2016-08-03 14:34:38 -0400253 u64 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400255 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
256 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257}
258
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400259/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 * svm_data_region_setup
261 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400262static int
263svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400265 int fd;
266 u8 junk = 0;
267 uword map_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
Dave Barachc3799992016-08-15 11:12:27 -0400269 map_size = rp->virtual_size - (MMAP_PAGESIZE +
270 (a->pvt_heap_size ? a->pvt_heap_size :
271 SVM_PVT_MHEAP_SIZE));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400273 if (a->flags & SVM_FLAGS_FILE)
274 {
275 struct stat statb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400277 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400279 if (fd < 0)
280 {
281 clib_unix_warning ("open");
282 return -1;
283 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400285 if (fstat (fd, &statb) < 0)
286 {
287 clib_unix_warning ("fstat");
288 close (fd);
289 return -2;
290 }
291
292 if (statb.st_mode & S_IFREG)
293 {
294 if (statb.st_size == 0)
295 {
296 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
297 {
298 clib_unix_warning ("seek region size");
299 close (fd);
300 return -3;
301 }
302 if (write (fd, &junk, 1) != 1)
303 {
304 clib_unix_warning ("set region size");
305 close (fd);
306 return -3;
307 }
308 }
309 else
310 {
311 map_size = rnd_pagesize (statb.st_size);
312 }
313 }
314 else
315 {
316 map_size = a->backing_mmap_size;
317 }
318
319 ASSERT (map_size <= rp->virtual_size -
320 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
321
322 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
323 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
324 {
325 clib_unix_warning ("mmap");
326 close (fd);
327 return -3;
328 }
329 close (fd);
Benoît Ganne77d42fc2020-04-20 09:52:39 +0200330 CLIB_MEM_UNPOISON (rp->data_base, map_size);
Benoît Ganneda5b4ef2020-09-09 10:00:34 +0200331 rp->backing_file = (char *) format (0, "%s%c", a->backing_file, 0);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400332 rp->flags |= SVM_FLAGS_FILE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400334
335 if (a->flags & SVM_FLAGS_MHEAP)
336 {
Damjan Marion4537c302020-09-28 19:03:37 +0200337 rp->data_heap = clib_mem_create_heap (rp->data_base, map_size,
338 1 /* locked */ , "svm data");
Ole Troan73710c72018-06-04 22:27:49 +0200339
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400340 rp->flags |= SVM_FLAGS_MHEAP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400342 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343}
344
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400345static int
346svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400348 int fd;
349 u8 junk = 0;
350 uword map_size;
351 struct stat statb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Dave Barachc3799992016-08-15 11:12:27 -0400353 map_size = rp->virtual_size -
354 (MMAP_PAGESIZE
Dave Barachb3d93da2016-08-03 14:34:38 -0400355 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400357 if (a->flags & SVM_FLAGS_FILE)
358 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400360 fd = open (a->backing_file, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400362 if (fd < 0)
363 {
364 clib_unix_warning ("open");
365 return -1;
366 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400368 if (fstat (fd, &statb) < 0)
369 {
370 clib_unix_warning ("fstat");
371 close (fd);
372 return -2;
373 }
374
375 if (statb.st_mode & S_IFREG)
376 {
377 if (statb.st_size == 0)
378 {
379 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
380 {
381 clib_unix_warning ("seek region size");
382 close (fd);
383 return -3;
384 }
385 if (write (fd, &junk, 1) != 1)
386 {
387 clib_unix_warning ("set region size");
388 close (fd);
389 return -3;
390 }
391 }
392 else
393 {
394 map_size = rnd_pagesize (statb.st_size);
395 }
396 }
397 else
398 {
399 map_size = a->backing_mmap_size;
400 }
401
402 ASSERT (map_size <= rp->virtual_size
Dave Barachc3799992016-08-15 11:12:27 -0400403 - (MMAP_PAGESIZE
404 +
405 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400406
407 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
408 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
409 {
410 clib_unix_warning ("mmap");
411 close (fd);
412 return -3;
413 }
414 close (fd);
Benoît Ganne77d42fc2020-04-20 09:52:39 +0200415 CLIB_MEM_UNPOISON (rp->data_base, map_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400417 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418}
419
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400420u8 *
421shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400423 u8 *shm_name;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400424 int root_path_offset = 0;
425 int name_offset = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400427 if (a->root_path)
428 {
429 /* Tolerate present or absent slashes */
430 if (a->root_path[0] == '/')
431 root_path_offset++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400433 if (a->name[0] == '/')
434 name_offset = 1;
435
Matej Perinad135c192017-07-18 13:59:41 +0200436 shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset],
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400437 &a->name[name_offset], 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400439 else
440 shm_name = format (0, "%s%c", a->name, 0);
441 return (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442}
443
Dave Barach59b25652017-09-10 15:04:27 -0400444void
445svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp)
446{
447 pthread_mutexattr_t attr;
448 pthread_condattr_t cattr;
449 int nbits, words, bit;
450 int overhead_space;
451 void *oldheap;
452 uword data_base;
453 ASSERT (rp);
454 int rv;
455
Dave Barachb7b92992018-10-17 10:38:51 -0400456 clib_memset (rp, 0, sizeof (*rp));
Dave Barach59b25652017-09-10 15:04:27 -0400457
458 if (pthread_mutexattr_init (&attr))
459 clib_unix_warning ("mutexattr_init");
460
461 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
462 clib_unix_warning ("mutexattr_setpshared");
463
464 if (pthread_mutex_init (&rp->mutex, &attr))
465 clib_unix_warning ("mutex_init");
466
467 if (pthread_mutexattr_destroy (&attr))
468 clib_unix_warning ("mutexattr_destroy");
469
470 if (pthread_condattr_init (&cattr))
471 clib_unix_warning ("condattr_init");
472
473 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
474 clib_unix_warning ("condattr_setpshared");
475
476 if (pthread_cond_init (&rp->condvar, &cattr))
477 clib_unix_warning ("cond_init");
478
479 if (pthread_condattr_destroy (&cattr))
480 clib_unix_warning ("condattr_destroy");
481
482 region_lock (rp, 1);
483
484 rp->virtual_base = a->baseva;
485 rp->virtual_size = a->size;
486
Damjan Marion4537c302020-09-28 19:03:37 +0200487 rp->region_heap = clib_mem_create_heap
Dave Barach6a5adc32018-07-04 10:56:23 -0400488 (uword_to_pointer (a->baseva + MMAP_PAGESIZE, void *),
489 (a->pvt_heap_size !=
Damjan Marion4537c302020-09-28 19:03:37 +0200490 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, 1 /* locked */ ,
491 "svm region");
Dave Barach6a5adc32018-07-04 10:56:23 -0400492
Dave Barach59b25652017-09-10 15:04:27 -0400493 oldheap = svm_push_pvt_heap (rp);
494
495 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
496 vec_add1 (rp->client_pids, getpid ());
497
498 nbits = rp->virtual_size / MMAP_PAGESIZE;
499
500 ASSERT (nbits > 0);
501 rp->bitmap_size = nbits;
502 words = (nbits + BITS (uword) - 1) / BITS (uword);
503 vec_validate (rp->bitmap, words - 1);
504
505 overhead_space = MMAP_PAGESIZE /* header */ +
506 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
507
508 bit = 0;
509 data_base = (uword) rp->virtual_base;
510
511 if (a->flags & SVM_FLAGS_NODATA)
512 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
513
514 do
515 {
516 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
517 bit++;
518 overhead_space -= MMAP_PAGESIZE;
519 data_base += MMAP_PAGESIZE;
520 }
521 while (overhead_space > 0);
522
523 rp->data_base = (void *) data_base;
524
525 /*
526 * Note: although the POSIX spec guarantees that only one
527 * process enters this block, we have to play games
528 * to hold off clients until e.g. the mutex is ready
529 */
530 rp->version = SVM_VERSION;
531
532 /* setup the data portion of the region */
533
534 rv = svm_data_region_create (a, rp);
535 if (rv)
536 {
537 clib_warning ("data_region_create: %d", rv);
538 }
539
540 region_unlock (rp);
541
542 svm_pop_heap (oldheap);
543}
544
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545/*
546 * svm_map_region
547 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400548void *
549svm_map_region (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400551 int svm_fd;
552 svm_region_t *rp;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400553 int deadman = 0;
554 u8 junk = 0;
555 void *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400556 int rv;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400557 int pid_holding_region_lock;
558 u8 *shm_name;
559 int dead_region_recovery = 0;
560 int time_left;
561 struct stat stat;
562 struct timespec ts, tsrem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400564 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
565 ASSERT (a->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400567 shm_name = shm_name_from_svm_map_region_args (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568
Florin Coras9f4ac582019-12-17 19:46:45 -0800569 if (CLIB_DEBUG > 1)
Dave Wallaced756b352017-07-03 13:11:38 -0400570 clib_warning ("[%d] map region %s: shm_open (%s)",
571 getpid (), a->name, shm_name);
572
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400573 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400575 if (svm_fd >= 0)
576 {
Dave Wallace19296112017-08-31 15:54:11 -0400577 if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400578 clib_unix_warning ("segment chmod");
579 /* This turns out to fail harmlessly if the client starts first */
580 if (fchown (svm_fd, a->uid, a->gid) < 0)
581 clib_unix_warning ("segment chown [ok if client starts first]");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400583 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400585 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
586 {
587 clib_warning ("seek region size");
588 close (svm_fd);
589 return (0);
590 }
591 if (write (svm_fd, &junk, 1) != 1)
592 {
593 clib_warning ("set region size");
594 close (svm_fd);
595 return (0);
596 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
Damjan Marion7bee80c2017-04-26 15:32:12 +0200598 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400599 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400601 if (rp == (svm_region_t *) MAP_FAILED)
602 {
603 clib_unix_warning ("mmap create");
604 close (svm_fd);
605 return (0);
606 }
607 close (svm_fd);
Benoît Ganne77d42fc2020-04-20 09:52:39 +0200608 CLIB_MEM_UNPOISON (rp, a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
Dave Barach59b25652017-09-10 15:04:27 -0400610 svm_region_init_mapped_region (a, rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400612 return ((void *) rp);
613 }
614 else
615 {
616 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400618 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400620 if (svm_fd < 0)
621 {
622 perror ("svm_region_map(mmap open)");
623 return (0);
624 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
Ole Troanc4f2ef72018-05-30 22:43:25 +0200626 /* Reset ownership in case the client started first */
627 if (fchown (svm_fd, a->uid, a->gid) < 0)
628 clib_unix_warning ("segment chown [ok if client starts first]");
629
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400630 time_left = 20;
631 while (1)
632 {
633 if (0 != fstat (svm_fd, &stat))
634 {
635 clib_warning ("fstat failed: %d", errno);
636 close (svm_fd);
637 return (0);
638 }
639 if (stat.st_size > 0)
640 {
641 break;
642 }
643 if (0 == time_left)
644 {
645 clib_warning ("waiting for resize of shm file timed out");
646 close (svm_fd);
647 return (0);
648 }
649 ts.tv_sec = 0;
650 ts.tv_nsec = 100000000;
651 while (nanosleep (&ts, &tsrem) < 0)
652 ts = tsrem;
653 time_left--;
654 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400656 rp = mmap (0, MMAP_PAGESIZE,
657 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400659 if (rp == (svm_region_t *) MAP_FAILED)
660 {
661 close (svm_fd);
662 clib_warning ("mmap");
663 return (0);
664 }
Benoît Ganne77d42fc2020-04-20 09:52:39 +0200665
666 CLIB_MEM_UNPOISON (rp, MMAP_PAGESIZE);
667
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400668 /*
669 * We lost the footrace to create this region; make sure
670 * the winner has crossed the finish line.
671 */
672 while (rp->version == 0 && deadman++ < 5)
673 {
674 sleep (1);
675 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400677 /*
678 * <bleep>-ed?
679 */
680 if (rp->version == 0)
681 {
682 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
683 close (svm_fd);
684 munmap (rp, a->size);
685 return (0);
686 }
687 /* Remap now that the region has been placed */
688 a->baseva = rp->virtual_base;
689 a->size = rp->virtual_size;
690 munmap (rp, MMAP_PAGESIZE);
691
Damjan Marion7bee80c2017-04-26 15:32:12 +0200692 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400693 PROT_READ | PROT_WRITE,
694 MAP_SHARED | MAP_FIXED, svm_fd, 0);
695 if ((uword) rp == (uword) MAP_FAILED)
696 {
697 clib_unix_warning ("mmap");
698 close (svm_fd);
699 return (0);
700 }
701
Dave Barachada24ea2018-05-24 17:32:00 -0400702 close (svm_fd);
703
Benoît Ganne77d42fc2020-04-20 09:52:39 +0200704 CLIB_MEM_UNPOISON (rp, a->size);
705
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400706 if ((uword) rp != rp->virtual_base)
707 {
708 clib_warning ("mmap botch");
709 }
710
711 /*
712 * Try to fix the region mutex if it is held by
713 * a dead process
714 */
715 pid_holding_region_lock = rp->mutex_owner_pid;
716 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
717 {
Benoît Ganne78de92d2021-01-20 19:10:59 +0100718 pthread_mutexattr_t attr;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400719 clib_warning
720 ("region %s mutex held by dead pid %d, tag %d, force unlock",
721 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
722 /* owner pid is nonexistent */
Benoît Ganne78de92d2021-01-20 19:10:59 +0100723 if (pthread_mutexattr_init (&attr))
724 clib_unix_warning ("mutexattr_init");
725 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
726 clib_unix_warning ("mutexattr_setpshared");
727 if (pthread_mutex_init (&rp->mutex, &attr))
728 clib_unix_warning ("mutex_init");
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400729 dead_region_recovery = 1;
730 }
731
732 if (dead_region_recovery)
733 clib_warning ("recovery: attempt to re-lock region");
734
735 region_lock (rp, 2);
736 oldheap = svm_push_pvt_heap (rp);
737 vec_add1 (rp->client_pids, getpid ());
738
739 if (dead_region_recovery)
740 clib_warning ("recovery: attempt svm_data_region_map");
741
742 rv = svm_data_region_map (a, rp);
743 if (rv)
744 {
745 clib_warning ("data_region_map: %d", rv);
746 }
747
748 if (dead_region_recovery)
749 clib_warning ("unlock and continue");
750
751 region_unlock (rp);
752
753 svm_pop_heap (oldheap);
754
755 return ((void *) rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756
757 }
Dave Barachc35f3e82020-04-02 10:44:09 -0400758 return 0; /* NOTREACHED *///NOSONAR
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759}
760
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400761static void
762svm_mutex_cleanup (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400764 int i;
765 for (i = 0; i < nheld; i++)
766 {
Dave Barachc35f3e82020-04-02 10:44:09 -0400767 pthread_mutex_unlock (mutexes_held[i]); //NOSONAR
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 }
769}
770
Ole Troan3cdc25f2017-08-17 11:07:33 +0200771static int
Dave Barachb3d93da2016-08-03 14:34:38 -0400772svm_region_init_internal (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400774 svm_region_t *rp;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400775 u64 ticks = clib_cpu_time_now ();
776 uword randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400778 /* guard against klutz calls */
779 if (root_rp)
Ole Troan3cdc25f2017-08-17 11:07:33 +0200780 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400782 root_rp_refcount++;
Dave Barach16c75df2016-05-31 14:05:46 -0400783
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400784 atexit (svm_mutex_cleanup);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400786 /* Randomize the shared-VM base at init time */
787 if (MMAP_PAGESIZE <= (4 << 10))
788 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
789 else
790 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
Dave Barachb3d93da2016-08-03 14:34:38 -0400792 a->baseva += randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400794 rp = svm_map_region (a);
Ole Troan3cdc25f2017-08-17 11:07:33 +0200795 if (!rp)
796 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400798 region_lock (rp, 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400800 /* Set up the main region data structures */
801 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
802 {
803 svm_main_region_t *mp = 0;
804 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400806 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400808 oldheap = svm_push_pvt_heap (rp);
809 vec_validate (mp, 0);
810 mp->name_hash = hash_create_string (0, sizeof (uword));
Dave Barachb3d93da2016-08-03 14:34:38 -0400811 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
Dave Wallace19296112017-08-31 15:54:11 -0400812 mp->uid = a->uid;
813 mp->gid = a->gid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400814 rp->data_base = mp;
815 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400817 region_unlock (rp);
818 root_rp = rp;
Ole Troan3cdc25f2017-08-17 11:07:33 +0200819
820 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821}
822
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400823void
824svm_region_init (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825{
Dave Barachb3d93da2016-08-03 14:34:38 -0400826 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400827
Dave Barachb7b92992018-10-17 10:38:51 -0400828 clib_memset (a, 0, sizeof (*a));
Dave Barachb3d93da2016-08-03 14:34:38 -0400829 a->root_path = 0;
830 a->name = SVM_GLOBAL_REGION_NAME;
Damjan Marionaec8f892018-01-08 16:35:35 +0100831 a->baseva = svm_get_global_region_base_va ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400832 a->size = SVM_GLOBAL_REGION_SIZE;
833 a->flags = SVM_FLAGS_NODATA;
834 a->uid = 0;
835 a->gid = 0;
836
837 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838}
839
Ole Troan3cdc25f2017-08-17 11:07:33 +0200840int
Neale Rannse72be392017-04-26 13:59:20 -0700841svm_region_init_chroot (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842{
Dave Barachb3d93da2016-08-03 14:34:38 -0400843 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400844
Dave Barachb7b92992018-10-17 10:38:51 -0400845 clib_memset (a, 0, sizeof (*a));
Dave Barachb3d93da2016-08-03 14:34:38 -0400846 a->root_path = root_path;
847 a->name = SVM_GLOBAL_REGION_NAME;
Damjan Marionaec8f892018-01-08 16:35:35 +0100848 a->baseva = svm_get_global_region_base_va ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400849 a->size = SVM_GLOBAL_REGION_SIZE;
850 a->flags = SVM_FLAGS_NODATA;
851 a->uid = 0;
852 a->gid = 0;
853
Ole Troan3cdc25f2017-08-17 11:07:33 +0200854 return svm_region_init_internal (a);
Dave Barach16c75df2016-05-31 14:05:46 -0400855}
856
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400857void
Neale Rannse72be392017-04-26 13:59:20 -0700858svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400859{
Dave Barachb3d93da2016-08-03 14:34:38 -0400860 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400861
Dave Barachb7b92992018-10-17 10:38:51 -0400862 clib_memset (a, 0, sizeof (*a));
Dave Barachb3d93da2016-08-03 14:34:38 -0400863 a->root_path = root_path;
864 a->name = SVM_GLOBAL_REGION_NAME;
Damjan Marionaec8f892018-01-08 16:35:35 +0100865 a->baseva = svm_get_global_region_base_va ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400866 a->size = SVM_GLOBAL_REGION_SIZE;
867 a->flags = SVM_FLAGS_NODATA;
868 a->uid = uid;
869 a->gid = gid;
870
871 svm_region_init_internal (a);
872}
873
874void
875svm_region_init_args (svm_map_region_args_t * a)
876{
877 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878}
879
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400880void *
881svm_region_find_or_create (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400883 svm_main_region_t *mp;
884 svm_region_t *rp;
885 uword need_nbits;
886 int index, i;
887 void *oldheap;
888 uword *p;
889 u8 *name;
890 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400892 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
Dave Barachc3799992016-08-15 11:12:27 -0400894 a->size += MMAP_PAGESIZE +
Dave Barachb3d93da2016-08-03 14:34:38 -0400895 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400896 a->size = rnd_pagesize (a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400898 region_lock (root_rp, 4);
899 oldheap = svm_push_pvt_heap (root_rp);
900 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400902 ASSERT (mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400904 /* Map the named region from the correct chroot environment */
Jan Srnicek5beec812017-03-24 10:18:11 +0100905 if (a->root_path == NULL)
906 a->root_path = (char *) mp->root_path;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400907
908 /*
909 * See if this region is already known. If it is, we're
910 * almost done...
911 */
912 p = hash_get_mem (mp->name_hash, a->name);
913
914 if (p)
915 {
916 rp = svm_map_region (a);
917 region_unlock (root_rp);
918 svm_pop_heap (oldheap);
919 return rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400922 /* Create the region. */
923 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400925 need_nbits = a->size / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400927 index = 1; /* $$$ fixme, figure out how many bit to really skip */
928
929 /*
930 * Scan the virtual space allocation bitmap, looking for a large
931 * enough chunk
932 */
933 do
934 {
935 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
936 {
937 for (i = 0; i < (need_nbits - 1); i++)
938 {
939 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
940 {
941 index = index + i;
942 goto next;
943 }
944 }
945 break;
946 }
947 index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 next:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400950 while (index < root_rp->bitmap_size);
951
952 /* Completely out of VM? */
953 if (index >= root_rp->bitmap_size)
954 {
Dave Barachb3d93da2016-08-03 14:34:38 -0400955 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
956 root_rp->region_name, a->size, a->size);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400957 svm_pop_heap (oldheap);
958 region_unlock (root_rp);
959 return 0;
960 }
961
962 /*
963 * Mark virtual space allocated
964 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400966 clib_warning ("set %d bits at index %d", need_nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967#endif
968
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400969 for (i = 0; i < need_nbits; i++)
970 {
971 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 }
973
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400974 /* Place this region where it goes... */
975 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400977 rp = svm_map_region (a);
Dave Barachc3799992016-08-15 11:12:27 -0400978
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400979 pool_get (mp->subregions, subp);
980 name = format (0, "%s%c", a->name, 0);
981 subp->subregion_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400983 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400985 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400987 region_unlock (root_rp);
988
989 return (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990}
991
Dave Wallaced756b352017-07-03 13:11:38 -0400992void
993svm_region_unlink (svm_region_t * rp)
994{
995 svm_map_region_args_t _a, *a = &_a;
996 svm_main_region_t *mp;
997 u8 *shm_name;
998
999 ASSERT (root_rp);
1000 ASSERT (rp);
1001 ASSERT (vec_c_string_is_terminated (rp->region_name));
1002
1003 mp = root_rp->data_base;
1004 ASSERT (mp);
1005
1006 a->root_path = (char *) mp->root_path;
1007 a->name = rp->region_name;
1008 shm_name = shm_name_from_svm_map_region_args (a);
1009 if (CLIB_DEBUG > 1)
1010 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
1011 shm_unlink ((const char *) shm_name);
1012 vec_free (shm_name);
1013}
1014
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015/*
1016 * svm_region_unmap
1017 *
1018 * Let go of the indicated region. If the calling process
1019 * is the last customer, throw it away completely.
1020 * The root region mutex guarantees atomicity with respect to
1021 * a new region client showing up at the wrong moment.
1022 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001023void
Florin Corasd6c30d92018-01-29 05:11:24 -08001024svm_region_unmap_internal (void *rp_arg, u8 is_client)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001026 int i, mypid = getpid ();
1027 int nclients_left;
1028 void *oldheap;
1029 uword virtual_base, virtual_size;
1030 svm_region_t *rp = rp_arg;
1031 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001033 /*
1034 * If we take a signal while holding one or more shared-memory
1035 * mutexes, we may end up back here from an otherwise
1036 * benign exit handler. Bail out to avoid a recursive
1037 * mutex screw-up.
1038 */
1039 if (nheld)
1040 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001042 ASSERT (rp);
1043 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001045 if (CLIB_DEBUG > 1)
1046 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001048 region_lock (root_rp, 5);
1049 region_lock (rp, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001051 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1052
1053 /* Remove the caller from the list of mappers */
Benoît Ganne77d42fc2020-04-20 09:52:39 +02001054 CLIB_MEM_UNPOISON (rp->client_pids, vec_bytes (rp->client_pids));
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001055 for (i = 0; i < vec_len (rp->client_pids); i++)
1056 {
1057 if (rp->client_pids[i] == mypid)
1058 {
1059 vec_delete (rp->client_pids, 1, i);
1060 goto found;
1061 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001063 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001065found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001066
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001067 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001069 nclients_left = vec_len (rp->client_pids);
1070 virtual_base = rp->virtual_base;
1071 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001073 if (nclients_left == 0)
1074 {
1075 int index, nbits, i;
1076 svm_main_region_t *mp;
1077 uword *p;
1078 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001080 /* Kill the region, last guy on his way out */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001082 oldheap = svm_push_pvt_heap (root_rp);
1083 name = vec_dup (rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001085 virtual_base = rp->virtual_base;
1086 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001088 /* Figure out which bits to clear in the root region bitmap */
1089 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001091 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092
1093#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001094 clib_warning ("clear %d bits at index %d", nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095#endif
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001096 /* Give back the allocated VM */
1097 for (i = 0; i < nbits; i++)
1098 {
1099 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1100 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001102 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001104 p = hash_get_mem (mp->name_hash, name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001106 /* Better never happen ... */
1107 if (p == NULL)
1108 {
1109 region_unlock (rp);
1110 region_unlock (root_rp);
1111 svm_pop_heap (oldheap);
1112 clib_warning ("Region name '%s' not found?", name);
1113 return;
1114 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001116 /* Remove from the root region subregion pool */
1117 subp = mp->subregions + p[0];
1118 pool_put (mp->subregions, subp);
1119
1120 hash_unset_mem (mp->name_hash, name);
1121
1122 vec_free (name);
1123
1124 region_unlock (rp);
Florin Corasd6c30d92018-01-29 05:11:24 -08001125
1126 /* If a client asks for the cleanup, don't unlink the backing
1127 * file since we can't tell if it has been recreated. */
1128 if (!is_client)
1129 svm_region_unlink (rp);
1130
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001131 munmap ((void *) virtual_base, virtual_size);
1132 region_unlock (root_rp);
1133 svm_pop_heap (oldheap);
1134 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135 }
1136
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001137 region_unlock (rp);
1138 region_unlock (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001140 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141}
1142
Florin Corasd6c30d92018-01-29 05:11:24 -08001143void
1144svm_region_unmap (void *rp_arg)
1145{
1146 svm_region_unmap_internal (rp_arg, 0 /* is_client */ );
1147}
1148
1149void
1150svm_region_unmap_client (void *rp_arg)
1151{
1152 svm_region_unmap_internal (rp_arg, 1 /* is_client */ );
1153}
1154
Ed Warnickecb9cada2015-12-08 15:45:58 -07001155/*
1156 * svm_region_exit
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001157 */
Florin Corasd6c30d92018-01-29 05:11:24 -08001158static void
1159svm_region_exit_internal (u8 is_client)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001161 void *oldheap;
1162 int i, mypid = getpid ();
1163 uword virtual_base, virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001164
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001165 /* It felt so nice we did it twice... */
1166 if (root_rp == 0)
1167 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001169 if (--root_rp_refcount > 0)
1170 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001172 /*
1173 * If we take a signal while holding one or more shared-memory
1174 * mutexes, we may end up back here from an otherwise
1175 * benign exit handler. Bail out to avoid a recursive
1176 * mutex screw-up.
1177 */
1178 if (nheld)
1179 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001181 region_lock (root_rp, 7);
1182 oldheap = svm_push_pvt_heap (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001184 virtual_base = root_rp->virtual_base;
1185 virtual_size = root_rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
Benoît Ganne77d42fc2020-04-20 09:52:39 +02001187 CLIB_MEM_UNPOISON (root_rp->client_pids, vec_bytes (root_rp->client_pids));
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001188 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1189 {
1190 if (root_rp->client_pids[i] == mypid)
1191 {
1192 vec_delete (root_rp->client_pids, 1, i);
1193 goto found;
1194 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001196 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001198found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Florin Corasd6c30d92018-01-29 05:11:24 -08001200 if (!is_client && vec_len (root_rp->client_pids) == 0)
Dave Wallaced756b352017-07-03 13:11:38 -04001201 svm_region_unlink (root_rp);
1202
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001203 region_unlock (root_rp);
1204 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001206 root_rp = 0;
1207 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208}
1209
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001210void
Florin Corasd6c30d92018-01-29 05:11:24 -08001211svm_region_exit (void)
1212{
1213 svm_region_exit_internal (0 /* is_client */ );
1214}
1215
1216void
1217svm_region_exit_client (void)
1218{
1219 svm_region_exit_internal (1 /* is_client */ );
1220}
1221
1222void
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001223svm_client_scan_this_region_nolock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001224{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001225 int j;
1226 int mypid = getpid ();
1227 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001229 for (j = 0; j < vec_len (rp->client_pids); j++)
1230 {
1231 if (mypid == rp->client_pids[j])
1232 continue;
1233 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1234 {
1235 clib_warning ("%s: cleanup ghost pid %d",
1236 rp->region_name, rp->client_pids[j]);
1237 /* nb: client vec in rp->region_heap */
1238 oldheap = svm_push_pvt_heap (rp);
1239 vec_delete (rp->client_pids, 1, j);
1240 j--;
1241 svm_pop_heap (oldheap);
1242 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243 }
1244}
1245
1246
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001247/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248 * Scan svm regions for dead clients
1249 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001250void
Neale Rannse72be392017-04-26 13:59:20 -07001251svm_client_scan (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001253 int i, j;
1254 svm_main_region_t *mp;
1255 svm_map_region_args_t *a = 0;
1256 svm_region_t *root_rp;
1257 svm_region_t *rp;
1258 svm_subregion_t *subp;
1259 u8 *name = 0;
1260 u8 **svm_names = 0;
1261 void *oldheap;
1262 int mypid = getpid ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001264 vec_validate (a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001266 svm_region_init_chroot (root_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001267
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001268 root_rp = svm_get_root_rp ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001270 pthread_mutex_lock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001271
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001272 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001274 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1275 {
1276 if (mypid == root_rp->client_pids[j])
1277 continue;
1278 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1279 {
1280 clib_warning ("%s: cleanup ghost pid %d",
1281 root_rp->region_name, root_rp->client_pids[j]);
1282 /* nb: client vec in root_rp->region_heap */
1283 oldheap = svm_push_pvt_heap (root_rp);
1284 vec_delete (root_rp->client_pids, 1, j);
1285 j--;
1286 svm_pop_heap (oldheap);
1287 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001288 }
1289
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001290 /*
1291 * Snapshoot names, can't hold root rp mutex across
1292 * find_or_create.
1293 */
1294 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001295 pool_foreach (subp, mp->subregions) {
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001296 name = vec_dup (subp->subregion_name);
1297 vec_add1(svm_names, name);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001298 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001299 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001300
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001301 pthread_mutex_unlock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001303 for (i = 0; i < vec_len (svm_names); i++)
1304 {
1305 vec_validate (a, 0);
1306 a->root_path = root_path;
1307 a->name = (char *) svm_names[i];
1308 rp = svm_region_find_or_create (a);
1309 if (rp)
1310 {
1311 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001313 svm_client_scan_this_region_nolock (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001314
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001315 pthread_mutex_unlock (&rp->mutex);
1316 svm_region_unmap (rp);
1317 vec_free (svm_names[i]);
1318 }
1319 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001320 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001321 vec_free (svm_names);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001322
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001323 svm_region_exit ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001324
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001325 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001326}
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001327
1328/*
1329 * fd.io coding-style-patch-verification: ON
1330 *
1331 * Local Variables:
1332 * eval: (c-set-style "gnu")
1333 * End:
1334 */