blob: c702625db7e3ea1aa0bd3b8eb89e869d779cf740 [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>
39#include <vppinfra/mheap.h>
40#include <vppinfra/heap.h>
41#include <vppinfra/pool.h>
42#include <vppinfra/format.h>
43
44#include "svm.h"
45
46static svm_region_t *root_rp;
47static int root_rp_refcount;
48
49#define MAXLOCK 2
Dave Barach8a7fb0c2016-07-08 14:44:23 -040050static pthread_mutex_t *mutexes_held[MAXLOCK];
Ed Warnickecb9cada2015-12-08 15:45:58 -070051static int nheld;
52
Dave Barach8a7fb0c2016-07-08 14:44:23 -040053svm_region_t *
54svm_get_root_rp (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070055{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040056 return root_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057}
58
59#define MUTEX_DEBUG
60
Damjan Marionaec8f892018-01-08 16:35:35 +010061u64
62svm_get_global_region_base_va ()
63{
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +020064#ifdef CLIB_SANITIZE_ADDR
65 return 0x200000000000;
66#endif
67
Damjan Marionaec8f892018-01-08 16:35:35 +010068#if __aarch64__
69 /* On AArch64 VA space can have different size, from 36 to 48 bits.
70 Here we are trying to detect VA bits by parsing /proc/self/maps
71 address ranges */
72 int fd;
73 unformat_input_t input;
74 u64 start, end = 0;
75 u8 bits = 0;
76
77 if ((fd = open ("/proc/self/maps", 0)) < 0)
78 clib_unix_error ("open '/proc/self/maps'");
79
80 unformat_init_clib_file (&input, fd);
81 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
82 {
Gabriel Gannec5239ad2018-01-11 15:04:19 +010083 if (unformat (&input, "%llx-%llx", &start, &end))
84 end--;
Damjan Marionaec8f892018-01-08 16:35:35 +010085 unformat_skip_line (&input);
86 }
Gabriel Ganne83d47432018-01-10 11:40:50 +010087 unformat_free (&input);
88 close (fd);
Damjan Marionaec8f892018-01-08 16:35:35 +010089
Damjan Marion11056002018-05-10 13:40:44 +020090 bits = count_leading_zeros (end);
Gabriel Gannec5239ad2018-01-11 15:04:19 +010091 bits = 64 - bits;
Damjan Marionaec8f892018-01-08 16:35:35 +010092 if (bits >= 36 && bits <= 48)
93 return ((1ul << bits) / 4) - (2 * SVM_GLOBAL_REGION_SIZE);
94 else
95 clib_unix_error ("unexpected va bits '%u'", bits);
Damjan Marionaec8f892018-01-08 16:35:35 +010096#endif
97
98 /* default value */
Dave Barach9466c452018-08-24 17:21:14 -040099 return 0x130000000ULL;
Damjan Marionaec8f892018-01-08 16:35:35 +0100100}
101
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400102static void
103region_lock (svm_region_t * rp, int tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400105 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400107 rp->mutex_owner_pid = getpid ();
108 rp->mutex_owner_tag = tag;
109#endif
110 ASSERT (nheld < MAXLOCK);
111 /*
112 * Keep score of held mutexes so we can try to exit
113 * cleanly if the world comes to an end at the worst possible
114 * moment
115 */
116 mutexes_held[nheld++] = &rp->mutex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117}
118
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400119static void
120region_unlock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400122 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400124 rp->mutex_owner_pid = 0;
125 rp->mutex_owner_tag = 0;
126#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400128 for (i = nheld - 1; i >= 0; i--)
129 {
130 if (mutexes_held[i] == &rp->mutex)
131 {
132 for (j = i; j < MAXLOCK - 1; j++)
133 mutexes_held[j] = mutexes_held[j + 1];
134 nheld--;
135 goto found;
136 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400138 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
140found:
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400141 CLIB_MEMORY_BARRIER ();
142 pthread_mutex_unlock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143}
144
145
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400146static u8 *
147format_svm_flags (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400149 uword f = va_arg (*args, uword);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400151 if (f & SVM_FLAGS_MHEAP)
152 s = format (s, "MHEAP ");
153 if (f & SVM_FLAGS_FILE)
154 s = format (s, "FILE ");
155 if (f & SVM_FLAGS_NODATA)
156 s = format (s, "NODATA ");
157 if (f & SVM_FLAGS_NEED_DATA_INIT)
158 s = format (s, "INIT ");
159
160 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161}
162
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400163static u8 *
164format_svm_size (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400166 uword size = va_arg (*args, uword);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400168 if (size >= (1 << 20))
169 {
170 s = format (s, "(%d mb)", size >> 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400172 else if (size >= (1 << 10))
173 {
174 s = format (s, "(%d kb)", size >> 10);
175 }
176 else
177 {
178 s = format (s, "(%d bytes)", size);
179 }
180 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181}
182
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400183u8 *
184format_svm_region (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400186 svm_region_t *rp = va_arg (*args, svm_region_t *);
187 int verbose = va_arg (*args, int);
188 int i;
189 uword lo, hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400191 s = format (s, "%s: base va 0x%x size 0x%x %U\n",
192 rp->region_name, rp->virtual_base,
193 rp->virtual_size, format_svm_size, rp->virtual_size);
194 s = format (s, " user_ctx 0x%x, bitmap_size %d\n",
195 rp->user_ctx, rp->bitmap_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400197 if (verbose)
198 {
199 s = format (s, " flags: 0x%x %U\n", rp->flags,
200 format_svm_flags, rp->flags);
201 s = format (s,
202 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
203 rp->region_heap, rp->data_base, rp->data_heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 }
205
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400206 s = format (s, " %d clients, pids: ", vec_len (rp->client_pids));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400208 for (i = 0; i < vec_len (rp->client_pids); i++)
209 s = format (s, "%d ", rp->client_pids[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400211 s = format (s, "\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400213 if (verbose)
214 {
215 lo = hi = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400217 s = format (s, " VM in use: ");
218
219 for (i = 0; i < rp->bitmap_size; i++)
220 {
221 if (clib_bitmap_get_no_check (rp->bitmap, i) != 0)
222 {
223 if (lo == ~0)
224 {
225 hi = lo = rp->virtual_base + i * MMAP_PAGESIZE;
226 }
227 else
228 {
229 hi = rp->virtual_base + i * MMAP_PAGESIZE;
230 }
231 }
232 else
233 {
234 if (lo != ~0)
235 {
236 hi = rp->virtual_base + i * MMAP_PAGESIZE - 1;
237 s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi,
238 (hi - lo) >> 10);
239 lo = hi = ~0;
240 }
241 }
242 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 }
244
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400245 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246}
247
248/*
249 * rnd_pagesize
250 * Round to a pagesize multiple, presumably 4k works
251 */
Dave Barachb3d93da2016-08-03 14:34:38 -0400252static u64
253rnd_pagesize (u64 size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254{
Dave Barachb3d93da2016-08-03 14:34:38 -0400255 u64 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400257 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
258 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259}
260
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400261/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 * svm_data_region_setup
263 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400264static int
265svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400267 int fd;
268 u8 junk = 0;
269 uword map_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Dave Barachc3799992016-08-15 11:12:27 -0400271 map_size = rp->virtual_size - (MMAP_PAGESIZE +
272 (a->pvt_heap_size ? a->pvt_heap_size :
273 SVM_PVT_MHEAP_SIZE));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400275 if (a->flags & SVM_FLAGS_FILE)
276 {
277 struct stat statb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400279 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400281 if (fd < 0)
282 {
283 clib_unix_warning ("open");
284 return -1;
285 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400287 if (fstat (fd, &statb) < 0)
288 {
289 clib_unix_warning ("fstat");
290 close (fd);
291 return -2;
292 }
293
294 if (statb.st_mode & S_IFREG)
295 {
296 if (statb.st_size == 0)
297 {
298 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
299 {
300 clib_unix_warning ("seek region size");
301 close (fd);
302 return -3;
303 }
304 if (write (fd, &junk, 1) != 1)
305 {
306 clib_unix_warning ("set region size");
307 close (fd);
308 return -3;
309 }
310 }
311 else
312 {
313 map_size = rnd_pagesize (statb.st_size);
314 }
315 }
316 else
317 {
318 map_size = a->backing_mmap_size;
319 }
320
321 ASSERT (map_size <= rp->virtual_size -
322 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
323
324 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
325 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
326 {
327 clib_unix_warning ("mmap");
328 close (fd);
329 return -3;
330 }
331 close (fd);
332 rp->backing_file = (char *) format (0, "%s\0", a->backing_file);
333 rp->flags |= SVM_FLAGS_FILE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400335
336 if (a->flags & SVM_FLAGS_MHEAP)
337 {
Dave Barach6a5adc32018-07-04 10:56:23 -0400338 rp->data_heap = create_mspace_with_base (rp->data_base,
339 map_size, 1 /* locked */ );
340 mspace_disable_expand (rp->data_heap);
Ole Troan73710c72018-06-04 22:27:49 +0200341
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400342 rp->flags |= SVM_FLAGS_MHEAP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400344 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345}
346
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400347static int
348svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400350 int fd;
351 u8 junk = 0;
352 uword map_size;
353 struct stat statb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barachc3799992016-08-15 11:12:27 -0400355 map_size = rp->virtual_size -
356 (MMAP_PAGESIZE
Dave Barachb3d93da2016-08-03 14:34:38 -0400357 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400359 if (a->flags & SVM_FLAGS_FILE)
360 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400362 fd = open (a->backing_file, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400364 if (fd < 0)
365 {
366 clib_unix_warning ("open");
367 return -1;
368 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400370 if (fstat (fd, &statb) < 0)
371 {
372 clib_unix_warning ("fstat");
373 close (fd);
374 return -2;
375 }
376
377 if (statb.st_mode & S_IFREG)
378 {
379 if (statb.st_size == 0)
380 {
381 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
382 {
383 clib_unix_warning ("seek region size");
384 close (fd);
385 return -3;
386 }
387 if (write (fd, &junk, 1) != 1)
388 {
389 clib_unix_warning ("set region size");
390 close (fd);
391 return -3;
392 }
393 }
394 else
395 {
396 map_size = rnd_pagesize (statb.st_size);
397 }
398 }
399 else
400 {
401 map_size = a->backing_mmap_size;
402 }
403
404 ASSERT (map_size <= rp->virtual_size
Dave Barachc3799992016-08-15 11:12:27 -0400405 - (MMAP_PAGESIZE
406 +
407 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400408
409 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
410 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
411 {
412 clib_unix_warning ("mmap");
413 close (fd);
414 return -3;
415 }
416 close (fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400418 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419}
420
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400421u8 *
422shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400424 u8 *shm_name;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400425 int root_path_offset = 0;
426 int name_offset = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400428 if (a->root_path)
429 {
430 /* Tolerate present or absent slashes */
431 if (a->root_path[0] == '/')
432 root_path_offset++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400434 if (a->name[0] == '/')
435 name_offset = 1;
436
Matej Perinad135c192017-07-18 13:59:41 +0200437 shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset],
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400438 &a->name[name_offset], 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400440 else
441 shm_name = format (0, "%s%c", a->name, 0);
442 return (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443}
444
Dave Barach59b25652017-09-10 15:04:27 -0400445void
446svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp)
447{
448 pthread_mutexattr_t attr;
449 pthread_condattr_t cattr;
450 int nbits, words, bit;
451 int overhead_space;
452 void *oldheap;
453 uword data_base;
454 ASSERT (rp);
455 int rv;
456
Dave Barachb7b92992018-10-17 10:38:51 -0400457 clib_memset (rp, 0, sizeof (*rp));
Dave Barach59b25652017-09-10 15:04:27 -0400458
459 if (pthread_mutexattr_init (&attr))
460 clib_unix_warning ("mutexattr_init");
461
462 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
463 clib_unix_warning ("mutexattr_setpshared");
464
465 if (pthread_mutex_init (&rp->mutex, &attr))
466 clib_unix_warning ("mutex_init");
467
468 if (pthread_mutexattr_destroy (&attr))
469 clib_unix_warning ("mutexattr_destroy");
470
471 if (pthread_condattr_init (&cattr))
472 clib_unix_warning ("condattr_init");
473
474 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
475 clib_unix_warning ("condattr_setpshared");
476
477 if (pthread_cond_init (&rp->condvar, &cattr))
478 clib_unix_warning ("cond_init");
479
480 if (pthread_condattr_destroy (&cattr))
481 clib_unix_warning ("condattr_destroy");
482
483 region_lock (rp, 1);
484
485 rp->virtual_base = a->baseva;
486 rp->virtual_size = a->size;
487
Dave Barach6a5adc32018-07-04 10:56:23 -0400488 rp->region_heap = create_mspace_with_base
489 (uword_to_pointer (a->baseva + MMAP_PAGESIZE, void *),
490 (a->pvt_heap_size !=
491 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, 1 /* locked */ );
492
493 mspace_disable_expand (rp->region_heap);
Dave Barach6a5adc32018-07-04 10:56:23 -0400494
Dave Barach59b25652017-09-10 15:04:27 -0400495 oldheap = svm_push_pvt_heap (rp);
496
497 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
498 vec_add1 (rp->client_pids, getpid ());
499
500 nbits = rp->virtual_size / MMAP_PAGESIZE;
501
502 ASSERT (nbits > 0);
503 rp->bitmap_size = nbits;
504 words = (nbits + BITS (uword) - 1) / BITS (uword);
505 vec_validate (rp->bitmap, words - 1);
506
507 overhead_space = MMAP_PAGESIZE /* header */ +
508 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
509
510 bit = 0;
511 data_base = (uword) rp->virtual_base;
512
513 if (a->flags & SVM_FLAGS_NODATA)
514 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
515
516 do
517 {
518 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
519 bit++;
520 overhead_space -= MMAP_PAGESIZE;
521 data_base += MMAP_PAGESIZE;
522 }
523 while (overhead_space > 0);
524
525 rp->data_base = (void *) data_base;
526
527 /*
528 * Note: although the POSIX spec guarantees that only one
529 * process enters this block, we have to play games
530 * to hold off clients until e.g. the mutex is ready
531 */
532 rp->version = SVM_VERSION;
533
534 /* setup the data portion of the region */
535
536 rv = svm_data_region_create (a, rp);
537 if (rv)
538 {
539 clib_warning ("data_region_create: %d", rv);
540 }
541
542 region_unlock (rp);
543
544 svm_pop_heap (oldheap);
545}
546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547/*
548 * svm_map_region
549 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400550void *
551svm_map_region (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400553 int svm_fd;
554 svm_region_t *rp;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400555 int deadman = 0;
556 u8 junk = 0;
557 void *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400558 int rv;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400559 int pid_holding_region_lock;
560 u8 *shm_name;
561 int dead_region_recovery = 0;
562 int time_left;
563 struct stat stat;
564 struct timespec ts, tsrem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400566 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
567 ASSERT (a->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400569 shm_name = shm_name_from_svm_map_region_args (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Florin Coras9f4ac582019-12-17 19:46:45 -0800571 if (CLIB_DEBUG > 1)
Dave Wallaced756b352017-07-03 13:11:38 -0400572 clib_warning ("[%d] map region %s: shm_open (%s)",
573 getpid (), a->name, shm_name);
574
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400575 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400577 if (svm_fd >= 0)
578 {
Dave Wallace19296112017-08-31 15:54:11 -0400579 if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400580 clib_unix_warning ("segment chmod");
581 /* This turns out to fail harmlessly if the client starts first */
582 if (fchown (svm_fd, a->uid, a->gid) < 0)
583 clib_unix_warning ("segment chown [ok if client starts first]");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400585 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400587 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
588 {
589 clib_warning ("seek region size");
590 close (svm_fd);
591 return (0);
592 }
593 if (write (svm_fd, &junk, 1) != 1)
594 {
595 clib_warning ("set region size");
596 close (svm_fd);
597 return (0);
598 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
Damjan Marion7bee80c2017-04-26 15:32:12 +0200600 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400601 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400603 if (rp == (svm_region_t *) MAP_FAILED)
604 {
605 clib_unix_warning ("mmap create");
606 close (svm_fd);
607 return (0);
608 }
609 close (svm_fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610
Dave Barach59b25652017-09-10 15:04:27 -0400611 svm_region_init_mapped_region (a, rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400613 return ((void *) rp);
614 }
615 else
616 {
617 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400619 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400621 if (svm_fd < 0)
622 {
623 perror ("svm_region_map(mmap open)");
624 return (0);
625 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Ole Troanc4f2ef72018-05-30 22:43:25 +0200627 /* Reset ownership in case the client started first */
628 if (fchown (svm_fd, a->uid, a->gid) < 0)
629 clib_unix_warning ("segment chown [ok if client starts first]");
630
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400631 time_left = 20;
632 while (1)
633 {
634 if (0 != fstat (svm_fd, &stat))
635 {
636 clib_warning ("fstat failed: %d", errno);
637 close (svm_fd);
638 return (0);
639 }
640 if (stat.st_size > 0)
641 {
642 break;
643 }
644 if (0 == time_left)
645 {
646 clib_warning ("waiting for resize of shm file timed out");
647 close (svm_fd);
648 return (0);
649 }
650 ts.tv_sec = 0;
651 ts.tv_nsec = 100000000;
652 while (nanosleep (&ts, &tsrem) < 0)
653 ts = tsrem;
654 time_left--;
655 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400657 rp = mmap (0, MMAP_PAGESIZE,
658 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400660 if (rp == (svm_region_t *) MAP_FAILED)
661 {
662 close (svm_fd);
663 clib_warning ("mmap");
664 return (0);
665 }
666 /*
667 * We lost the footrace to create this region; make sure
668 * the winner has crossed the finish line.
669 */
670 while (rp->version == 0 && deadman++ < 5)
671 {
672 sleep (1);
673 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400675 /*
676 * <bleep>-ed?
677 */
678 if (rp->version == 0)
679 {
680 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
681 close (svm_fd);
682 munmap (rp, a->size);
683 return (0);
684 }
685 /* Remap now that the region has been placed */
686 a->baseva = rp->virtual_base;
687 a->size = rp->virtual_size;
688 munmap (rp, MMAP_PAGESIZE);
689
Damjan Marion7bee80c2017-04-26 15:32:12 +0200690 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400691 PROT_READ | PROT_WRITE,
692 MAP_SHARED | MAP_FIXED, svm_fd, 0);
693 if ((uword) rp == (uword) MAP_FAILED)
694 {
695 clib_unix_warning ("mmap");
696 close (svm_fd);
697 return (0);
698 }
699
Dave Barachada24ea2018-05-24 17:32:00 -0400700 close (svm_fd);
701
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400702 if ((uword) rp != rp->virtual_base)
703 {
704 clib_warning ("mmap botch");
705 }
706
707 /*
708 * Try to fix the region mutex if it is held by
709 * a dead process
710 */
711 pid_holding_region_lock = rp->mutex_owner_pid;
712 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
713 {
714 clib_warning
715 ("region %s mutex held by dead pid %d, tag %d, force unlock",
716 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
717 /* owner pid is nonexistent */
718 rp->mutex.__data.__owner = 0;
719 rp->mutex.__data.__lock = 0;
720 dead_region_recovery = 1;
721 }
722
723 if (dead_region_recovery)
724 clib_warning ("recovery: attempt to re-lock region");
725
726 region_lock (rp, 2);
727 oldheap = svm_push_pvt_heap (rp);
728 vec_add1 (rp->client_pids, getpid ());
729
730 if (dead_region_recovery)
731 clib_warning ("recovery: attempt svm_data_region_map");
732
733 rv = svm_data_region_map (a, rp);
734 if (rv)
735 {
736 clib_warning ("data_region_map: %d", rv);
737 }
738
739 if (dead_region_recovery)
740 clib_warning ("unlock and continue");
741
742 region_unlock (rp);
743
744 svm_pop_heap (oldheap);
745
746 return ((void *) rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747
748 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400749 return 0; /* NOTREACHED */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750}
751
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400752static void
753svm_mutex_cleanup (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400755 int i;
756 for (i = 0; i < nheld; i++)
757 {
758 pthread_mutex_unlock (mutexes_held[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759 }
760}
761
Ole Troan3cdc25f2017-08-17 11:07:33 +0200762static int
Dave Barachb3d93da2016-08-03 14:34:38 -0400763svm_region_init_internal (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400765 svm_region_t *rp;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400766 u64 ticks = clib_cpu_time_now ();
767 uword randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400769 /* guard against klutz calls */
770 if (root_rp)
Ole Troan3cdc25f2017-08-17 11:07:33 +0200771 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400773 root_rp_refcount++;
Dave Barach16c75df2016-05-31 14:05:46 -0400774
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400775 atexit (svm_mutex_cleanup);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400777 /* Randomize the shared-VM base at init time */
778 if (MMAP_PAGESIZE <= (4 << 10))
779 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
780 else
781 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
Dave Barachb3d93da2016-08-03 14:34:38 -0400783 a->baseva += randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400785 rp = svm_map_region (a);
Ole Troan3cdc25f2017-08-17 11:07:33 +0200786 if (!rp)
787 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400789 region_lock (rp, 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400791 /* Set up the main region data structures */
792 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
793 {
794 svm_main_region_t *mp = 0;
795 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400797 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400799 oldheap = svm_push_pvt_heap (rp);
800 vec_validate (mp, 0);
801 mp->name_hash = hash_create_string (0, sizeof (uword));
Dave Barachb3d93da2016-08-03 14:34:38 -0400802 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
Dave Wallace19296112017-08-31 15:54:11 -0400803 mp->uid = a->uid;
804 mp->gid = a->gid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400805 rp->data_base = mp;
806 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400808 region_unlock (rp);
809 root_rp = rp;
Ole Troan3cdc25f2017-08-17 11:07:33 +0200810
811 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812}
813
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400814void
815svm_region_init (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816{
Dave Barachb3d93da2016-08-03 14:34:38 -0400817 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400818
Dave Barachb7b92992018-10-17 10:38:51 -0400819 clib_memset (a, 0, sizeof (*a));
Dave Barachb3d93da2016-08-03 14:34:38 -0400820 a->root_path = 0;
821 a->name = SVM_GLOBAL_REGION_NAME;
Damjan Marionaec8f892018-01-08 16:35:35 +0100822 a->baseva = svm_get_global_region_base_va ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400823 a->size = SVM_GLOBAL_REGION_SIZE;
824 a->flags = SVM_FLAGS_NODATA;
825 a->uid = 0;
826 a->gid = 0;
827
828 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829}
830
Ole Troan3cdc25f2017-08-17 11:07:33 +0200831int
Neale Rannse72be392017-04-26 13:59:20 -0700832svm_region_init_chroot (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833{
Dave Barachb3d93da2016-08-03 14:34:38 -0400834 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400835
Dave Barachb7b92992018-10-17 10:38:51 -0400836 clib_memset (a, 0, sizeof (*a));
Dave Barachb3d93da2016-08-03 14:34:38 -0400837 a->root_path = root_path;
838 a->name = SVM_GLOBAL_REGION_NAME;
Damjan Marionaec8f892018-01-08 16:35:35 +0100839 a->baseva = svm_get_global_region_base_va ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400840 a->size = SVM_GLOBAL_REGION_SIZE;
841 a->flags = SVM_FLAGS_NODATA;
842 a->uid = 0;
843 a->gid = 0;
844
Ole Troan3cdc25f2017-08-17 11:07:33 +0200845 return svm_region_init_internal (a);
Dave Barach16c75df2016-05-31 14:05:46 -0400846}
847
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400848void
Neale Rannse72be392017-04-26 13:59:20 -0700849svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400850{
Dave Barachb3d93da2016-08-03 14:34:38 -0400851 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400852
Dave Barachb7b92992018-10-17 10:38:51 -0400853 clib_memset (a, 0, sizeof (*a));
Dave Barachb3d93da2016-08-03 14:34:38 -0400854 a->root_path = root_path;
855 a->name = SVM_GLOBAL_REGION_NAME;
Damjan Marionaec8f892018-01-08 16:35:35 +0100856 a->baseva = svm_get_global_region_base_va ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400857 a->size = SVM_GLOBAL_REGION_SIZE;
858 a->flags = SVM_FLAGS_NODATA;
859 a->uid = uid;
860 a->gid = gid;
861
862 svm_region_init_internal (a);
863}
864
865void
866svm_region_init_args (svm_map_region_args_t * a)
867{
868 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869}
870
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400871void *
872svm_region_find_or_create (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400874 svm_main_region_t *mp;
875 svm_region_t *rp;
876 uword need_nbits;
877 int index, i;
878 void *oldheap;
879 uword *p;
880 u8 *name;
881 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400883 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884
Dave Barachc3799992016-08-15 11:12:27 -0400885 a->size += MMAP_PAGESIZE +
Dave Barachb3d93da2016-08-03 14:34:38 -0400886 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400887 a->size = rnd_pagesize (a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400889 region_lock (root_rp, 4);
890 oldheap = svm_push_pvt_heap (root_rp);
891 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400893 ASSERT (mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400895 /* Map the named region from the correct chroot environment */
Jan Srnicek5beec812017-03-24 10:18:11 +0100896 if (a->root_path == NULL)
897 a->root_path = (char *) mp->root_path;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400898
899 /*
900 * See if this region is already known. If it is, we're
901 * almost done...
902 */
903 p = hash_get_mem (mp->name_hash, a->name);
904
905 if (p)
906 {
907 rp = svm_map_region (a);
908 region_unlock (root_rp);
909 svm_pop_heap (oldheap);
910 return rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400913 /* Create the region. */
914 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400916 need_nbits = a->size / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400918 index = 1; /* $$$ fixme, figure out how many bit to really skip */
919
920 /*
921 * Scan the virtual space allocation bitmap, looking for a large
922 * enough chunk
923 */
924 do
925 {
926 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
927 {
928 for (i = 0; i < (need_nbits - 1); i++)
929 {
930 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
931 {
932 index = index + i;
933 goto next;
934 }
935 }
936 break;
937 }
938 index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 next:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400941 while (index < root_rp->bitmap_size);
942
943 /* Completely out of VM? */
944 if (index >= root_rp->bitmap_size)
945 {
Dave Barachb3d93da2016-08-03 14:34:38 -0400946 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
947 root_rp->region_name, a->size, a->size);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400948 svm_pop_heap (oldheap);
949 region_unlock (root_rp);
950 return 0;
951 }
952
953 /*
954 * Mark virtual space allocated
955 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400957 clib_warning ("set %d bits at index %d", need_nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958#endif
959
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400960 for (i = 0; i < need_nbits; i++)
961 {
962 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963 }
964
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400965 /* Place this region where it goes... */
966 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400968 rp = svm_map_region (a);
Dave Barachc3799992016-08-15 11:12:27 -0400969
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400970 pool_get (mp->subregions, subp);
971 name = format (0, "%s%c", a->name, 0);
972 subp->subregion_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400974 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400976 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400978 region_unlock (root_rp);
979
980 return (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981}
982
Dave Wallaced756b352017-07-03 13:11:38 -0400983void
984svm_region_unlink (svm_region_t * rp)
985{
986 svm_map_region_args_t _a, *a = &_a;
987 svm_main_region_t *mp;
988 u8 *shm_name;
989
990 ASSERT (root_rp);
991 ASSERT (rp);
992 ASSERT (vec_c_string_is_terminated (rp->region_name));
993
994 mp = root_rp->data_base;
995 ASSERT (mp);
996
997 a->root_path = (char *) mp->root_path;
998 a->name = rp->region_name;
999 shm_name = shm_name_from_svm_map_region_args (a);
1000 if (CLIB_DEBUG > 1)
1001 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
1002 shm_unlink ((const char *) shm_name);
1003 vec_free (shm_name);
1004}
1005
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006/*
1007 * svm_region_unmap
1008 *
1009 * Let go of the indicated region. If the calling process
1010 * is the last customer, throw it away completely.
1011 * The root region mutex guarantees atomicity with respect to
1012 * a new region client showing up at the wrong moment.
1013 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001014void
Florin Corasd6c30d92018-01-29 05:11:24 -08001015svm_region_unmap_internal (void *rp_arg, u8 is_client)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001017 int i, mypid = getpid ();
1018 int nclients_left;
1019 void *oldheap;
1020 uword virtual_base, virtual_size;
1021 svm_region_t *rp = rp_arg;
1022 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001024 /*
1025 * If we take a signal while holding one or more shared-memory
1026 * mutexes, we may end up back here from an otherwise
1027 * benign exit handler. Bail out to avoid a recursive
1028 * mutex screw-up.
1029 */
1030 if (nheld)
1031 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001033 ASSERT (rp);
1034 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001036 if (CLIB_DEBUG > 1)
1037 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001039 region_lock (root_rp, 5);
1040 region_lock (rp, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001042 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1043
1044 /* Remove the caller from the list of mappers */
1045 for (i = 0; i < vec_len (rp->client_pids); i++)
1046 {
1047 if (rp->client_pids[i] == mypid)
1048 {
1049 vec_delete (rp->client_pids, 1, i);
1050 goto found;
1051 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001053 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001055found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001057 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001059 nclients_left = vec_len (rp->client_pids);
1060 virtual_base = rp->virtual_base;
1061 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001063 if (nclients_left == 0)
1064 {
1065 int index, nbits, i;
1066 svm_main_region_t *mp;
1067 uword *p;
1068 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001070 /* Kill the region, last guy on his way out */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001071
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001072 oldheap = svm_push_pvt_heap (root_rp);
1073 name = vec_dup (rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001075 virtual_base = rp->virtual_base;
1076 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001078 /* Figure out which bits to clear in the root region bitmap */
1079 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001081 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082
1083#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001084 clib_warning ("clear %d bits at index %d", nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085#endif
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001086 /* Give back the allocated VM */
1087 for (i = 0; i < nbits; i++)
1088 {
1089 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1090 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001092 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001094 p = hash_get_mem (mp->name_hash, name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001096 /* Better never happen ... */
1097 if (p == NULL)
1098 {
1099 region_unlock (rp);
1100 region_unlock (root_rp);
1101 svm_pop_heap (oldheap);
1102 clib_warning ("Region name '%s' not found?", name);
1103 return;
1104 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001106 /* Remove from the root region subregion pool */
1107 subp = mp->subregions + p[0];
1108 pool_put (mp->subregions, subp);
1109
1110 hash_unset_mem (mp->name_hash, name);
1111
1112 vec_free (name);
1113
1114 region_unlock (rp);
Florin Corasd6c30d92018-01-29 05:11:24 -08001115
1116 /* If a client asks for the cleanup, don't unlink the backing
1117 * file since we can't tell if it has been recreated. */
1118 if (!is_client)
1119 svm_region_unlink (rp);
1120
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001121 munmap ((void *) virtual_base, virtual_size);
1122 region_unlock (root_rp);
1123 svm_pop_heap (oldheap);
1124 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125 }
1126
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001127 region_unlock (rp);
1128 region_unlock (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001130 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131}
1132
Florin Corasd6c30d92018-01-29 05:11:24 -08001133void
1134svm_region_unmap (void *rp_arg)
1135{
1136 svm_region_unmap_internal (rp_arg, 0 /* is_client */ );
1137}
1138
1139void
1140svm_region_unmap_client (void *rp_arg)
1141{
1142 svm_region_unmap_internal (rp_arg, 1 /* is_client */ );
1143}
1144
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145/*
1146 * svm_region_exit
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001147 */
Florin Corasd6c30d92018-01-29 05:11:24 -08001148static void
1149svm_region_exit_internal (u8 is_client)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001151 void *oldheap;
1152 int i, mypid = getpid ();
1153 uword virtual_base, virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001155 /* It felt so nice we did it twice... */
1156 if (root_rp == 0)
1157 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001159 if (--root_rp_refcount > 0)
1160 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001162 /*
1163 * If we take a signal while holding one or more shared-memory
1164 * mutexes, we may end up back here from an otherwise
1165 * benign exit handler. Bail out to avoid a recursive
1166 * mutex screw-up.
1167 */
1168 if (nheld)
1169 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001171 region_lock (root_rp, 7);
1172 oldheap = svm_push_pvt_heap (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001174 virtual_base = root_rp->virtual_base;
1175 virtual_size = root_rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001177 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1178 {
1179 if (root_rp->client_pids[i] == mypid)
1180 {
1181 vec_delete (root_rp->client_pids, 1, i);
1182 goto found;
1183 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001185 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001187found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188
Florin Corasd6c30d92018-01-29 05:11:24 -08001189 if (!is_client && vec_len (root_rp->client_pids) == 0)
Dave Wallaced756b352017-07-03 13:11:38 -04001190 svm_region_unlink (root_rp);
1191
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001192 region_unlock (root_rp);
1193 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001195 root_rp = 0;
1196 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197}
1198
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001199void
Florin Corasd6c30d92018-01-29 05:11:24 -08001200svm_region_exit (void)
1201{
1202 svm_region_exit_internal (0 /* is_client */ );
1203}
1204
1205void
1206svm_region_exit_client (void)
1207{
1208 svm_region_exit_internal (1 /* is_client */ );
1209}
1210
1211void
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001212svm_client_scan_this_region_nolock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001214 int j;
1215 int mypid = getpid ();
1216 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001218 for (j = 0; j < vec_len (rp->client_pids); j++)
1219 {
1220 if (mypid == rp->client_pids[j])
1221 continue;
1222 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1223 {
1224 clib_warning ("%s: cleanup ghost pid %d",
1225 rp->region_name, rp->client_pids[j]);
1226 /* nb: client vec in rp->region_heap */
1227 oldheap = svm_push_pvt_heap (rp);
1228 vec_delete (rp->client_pids, 1, j);
1229 j--;
1230 svm_pop_heap (oldheap);
1231 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232 }
1233}
1234
1235
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001236/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237 * Scan svm regions for dead clients
1238 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001239void
Neale Rannse72be392017-04-26 13:59:20 -07001240svm_client_scan (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001242 int i, j;
1243 svm_main_region_t *mp;
1244 svm_map_region_args_t *a = 0;
1245 svm_region_t *root_rp;
1246 svm_region_t *rp;
1247 svm_subregion_t *subp;
1248 u8 *name = 0;
1249 u8 **svm_names = 0;
1250 void *oldheap;
1251 int mypid = getpid ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001253 vec_validate (a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001254
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001255 svm_region_init_chroot (root_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001257 root_rp = svm_get_root_rp ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001259 pthread_mutex_lock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001261 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001263 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1264 {
1265 if (mypid == root_rp->client_pids[j])
1266 continue;
1267 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1268 {
1269 clib_warning ("%s: cleanup ghost pid %d",
1270 root_rp->region_name, root_rp->client_pids[j]);
1271 /* nb: client vec in root_rp->region_heap */
1272 oldheap = svm_push_pvt_heap (root_rp);
1273 vec_delete (root_rp->client_pids, 1, j);
1274 j--;
1275 svm_pop_heap (oldheap);
1276 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001277 }
1278
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001279 /*
1280 * Snapshoot names, can't hold root rp mutex across
1281 * find_or_create.
1282 */
1283 /* *INDENT-OFF* */
1284 pool_foreach (subp, mp->subregions, ({
1285 name = vec_dup (subp->subregion_name);
1286 vec_add1(svm_names, name);
1287 }));
1288 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001289
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001290 pthread_mutex_unlock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001291
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001292 for (i = 0; i < vec_len (svm_names); i++)
1293 {
1294 vec_validate (a, 0);
1295 a->root_path = root_path;
1296 a->name = (char *) svm_names[i];
1297 rp = svm_region_find_or_create (a);
1298 if (rp)
1299 {
1300 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001302 svm_client_scan_this_region_nolock (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001303
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001304 pthread_mutex_unlock (&rp->mutex);
1305 svm_region_unmap (rp);
1306 vec_free (svm_names[i]);
1307 }
1308 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001310 vec_free (svm_names);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001311
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001312 svm_region_exit ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001313
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001314 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315}
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001316
1317/*
1318 * fd.io coding-style-patch-verification: ON
1319 *
1320 * Local Variables:
1321 * eval: (c-set-style "gnu")
1322 * End:
1323 */