blob: 600fa7448ac3ca75fc6b7fb9c08879fc52a33025 [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
Dave Barach8a7fb0c2016-07-08 14:44:23 -040061static void
62region_lock (svm_region_t * rp, int tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040064 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -070065#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -040066 rp->mutex_owner_pid = getpid ();
67 rp->mutex_owner_tag = tag;
68#endif
69 ASSERT (nheld < MAXLOCK);
70 /*
71 * Keep score of held mutexes so we can try to exit
72 * cleanly if the world comes to an end at the worst possible
73 * moment
74 */
75 mutexes_held[nheld++] = &rp->mutex;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076}
77
Dave Barach8a7fb0c2016-07-08 14:44:23 -040078static void
79region_unlock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040081 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -040083 rp->mutex_owner_pid = 0;
84 rp->mutex_owner_tag = 0;
85#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Dave Barach8a7fb0c2016-07-08 14:44:23 -040087 for (i = nheld - 1; i >= 0; i--)
88 {
89 if (mutexes_held[i] == &rp->mutex)
90 {
91 for (j = i; j < MAXLOCK - 1; j++)
92 mutexes_held[j] = mutexes_held[j + 1];
93 nheld--;
94 goto found;
95 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -040097 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99found:
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400100 CLIB_MEMORY_BARRIER ();
101 pthread_mutex_unlock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102}
103
104
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400105static u8 *
106format_svm_flags (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400108 uword f = va_arg (*args, uword);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400110 if (f & SVM_FLAGS_MHEAP)
111 s = format (s, "MHEAP ");
112 if (f & SVM_FLAGS_FILE)
113 s = format (s, "FILE ");
114 if (f & SVM_FLAGS_NODATA)
115 s = format (s, "NODATA ");
116 if (f & SVM_FLAGS_NEED_DATA_INIT)
117 s = format (s, "INIT ");
118
119 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120}
121
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400122static u8 *
123format_svm_size (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400125 uword size = va_arg (*args, uword);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400127 if (size >= (1 << 20))
128 {
129 s = format (s, "(%d mb)", size >> 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400131 else if (size >= (1 << 10))
132 {
133 s = format (s, "(%d kb)", size >> 10);
134 }
135 else
136 {
137 s = format (s, "(%d bytes)", size);
138 }
139 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140}
141
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400142u8 *
143format_svm_region (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400145 svm_region_t *rp = va_arg (*args, svm_region_t *);
146 int verbose = va_arg (*args, int);
147 int i;
148 uword lo, hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400150 s = format (s, "%s: base va 0x%x size 0x%x %U\n",
151 rp->region_name, rp->virtual_base,
152 rp->virtual_size, format_svm_size, rp->virtual_size);
153 s = format (s, " user_ctx 0x%x, bitmap_size %d\n",
154 rp->user_ctx, rp->bitmap_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400156 if (verbose)
157 {
158 s = format (s, " flags: 0x%x %U\n", rp->flags,
159 format_svm_flags, rp->flags);
160 s = format (s,
161 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
162 rp->region_heap, rp->data_base, rp->data_heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 }
164
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400165 s = format (s, " %d clients, pids: ", vec_len (rp->client_pids));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400167 for (i = 0; i < vec_len (rp->client_pids); i++)
168 s = format (s, "%d ", rp->client_pids[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400170 s = format (s, "\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400172 if (verbose)
173 {
174 lo = hi = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400176 s = format (s, " VM in use: ");
177
178 for (i = 0; i < rp->bitmap_size; i++)
179 {
180 if (clib_bitmap_get_no_check (rp->bitmap, i) != 0)
181 {
182 if (lo == ~0)
183 {
184 hi = lo = rp->virtual_base + i * MMAP_PAGESIZE;
185 }
186 else
187 {
188 hi = rp->virtual_base + i * MMAP_PAGESIZE;
189 }
190 }
191 else
192 {
193 if (lo != ~0)
194 {
195 hi = rp->virtual_base + i * MMAP_PAGESIZE - 1;
196 s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi,
197 (hi - lo) >> 10);
198 lo = hi = ~0;
199 }
200 }
201 }
202 s = format (s, " rgn heap stats: %U", format_mheap,
203 rp->region_heap, 0);
204 if ((rp->flags & SVM_FLAGS_MHEAP) && rp->data_heap)
205 {
206 s = format (s, "\n data heap stats: %U", format_mheap,
207 rp->data_heap, 1);
208 }
209 s = format (s, "\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 }
211
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400212 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213}
214
215/*
216 * rnd_pagesize
217 * Round to a pagesize multiple, presumably 4k works
218 */
Dave Barachb3d93da2016-08-03 14:34:38 -0400219static u64
220rnd_pagesize (u64 size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221{
Dave Barachb3d93da2016-08-03 14:34:38 -0400222 u64 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400224 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
225 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226}
227
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400228/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 * svm_data_region_setup
230 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400231static int
232svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400234 int fd;
235 u8 junk = 0;
236 uword map_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Dave Barachc3799992016-08-15 11:12:27 -0400238 map_size = rp->virtual_size - (MMAP_PAGESIZE +
239 (a->pvt_heap_size ? a->pvt_heap_size :
240 SVM_PVT_MHEAP_SIZE));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400242 if (a->flags & SVM_FLAGS_FILE)
243 {
244 struct stat statb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400246 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400248 if (fd < 0)
249 {
250 clib_unix_warning ("open");
251 return -1;
252 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400254 if (fstat (fd, &statb) < 0)
255 {
256 clib_unix_warning ("fstat");
257 close (fd);
258 return -2;
259 }
260
261 if (statb.st_mode & S_IFREG)
262 {
263 if (statb.st_size == 0)
264 {
265 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
266 {
267 clib_unix_warning ("seek region size");
268 close (fd);
269 return -3;
270 }
271 if (write (fd, &junk, 1) != 1)
272 {
273 clib_unix_warning ("set region size");
274 close (fd);
275 return -3;
276 }
277 }
278 else
279 {
280 map_size = rnd_pagesize (statb.st_size);
281 }
282 }
283 else
284 {
285 map_size = a->backing_mmap_size;
286 }
287
288 ASSERT (map_size <= rp->virtual_size -
289 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
290
291 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
292 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
293 {
294 clib_unix_warning ("mmap");
295 close (fd);
296 return -3;
297 }
298 close (fd);
299 rp->backing_file = (char *) format (0, "%s\0", a->backing_file);
300 rp->flags |= SVM_FLAGS_FILE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400302
303 if (a->flags & SVM_FLAGS_MHEAP)
304 {
305 rp->data_heap =
306 mheap_alloc_with_flags ((void *) (rp->data_base), map_size,
307 MHEAP_FLAG_DISABLE_VM);
308 rp->flags |= SVM_FLAGS_MHEAP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400310 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311}
312
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400313static int
314svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400316 int fd;
317 u8 junk = 0;
318 uword map_size;
319 struct stat statb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Dave Barachc3799992016-08-15 11:12:27 -0400321 map_size = rp->virtual_size -
322 (MMAP_PAGESIZE
Dave Barachb3d93da2016-08-03 14:34:38 -0400323 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400325 if (a->flags & SVM_FLAGS_FILE)
326 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400328 fd = open (a->backing_file, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400330 if (fd < 0)
331 {
332 clib_unix_warning ("open");
333 return -1;
334 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400336 if (fstat (fd, &statb) < 0)
337 {
338 clib_unix_warning ("fstat");
339 close (fd);
340 return -2;
341 }
342
343 if (statb.st_mode & S_IFREG)
344 {
345 if (statb.st_size == 0)
346 {
347 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
348 {
349 clib_unix_warning ("seek region size");
350 close (fd);
351 return -3;
352 }
353 if (write (fd, &junk, 1) != 1)
354 {
355 clib_unix_warning ("set region size");
356 close (fd);
357 return -3;
358 }
359 }
360 else
361 {
362 map_size = rnd_pagesize (statb.st_size);
363 }
364 }
365 else
366 {
367 map_size = a->backing_mmap_size;
368 }
369
370 ASSERT (map_size <= rp->virtual_size
Dave Barachc3799992016-08-15 11:12:27 -0400371 - (MMAP_PAGESIZE
372 +
373 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400374
375 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
376 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
377 {
378 clib_unix_warning ("mmap");
379 close (fd);
380 return -3;
381 }
382 close (fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400384 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385}
386
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400387u8 *
388shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400390 u8 *path;
391 u8 *shm_name;
392 u8 *split_point;
393 u8 *mkdir_arg = 0;
394 int root_path_offset = 0;
395 int name_offset = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400397 if (a->root_path)
398 {
399 /* Tolerate present or absent slashes */
400 if (a->root_path[0] == '/')
401 root_path_offset++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400403 /* create the root_path under /dev/shm
404 iterate through path creating directories */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400406 path = format (0, "/dev/shm/%s%c", &a->root_path[root_path_offset], 0);
407 split_point = path + 1;
408 vec_add1 (mkdir_arg, '-');
409
410 while (*split_point)
411 {
412 while (*split_point && *split_point != '/')
413 {
414 vec_add1 (mkdir_arg, *split_point);
415 split_point++;
416 }
417 vec_add1 (mkdir_arg, 0);
418
419 /* ready to descend another level */
420 mkdir_arg[vec_len (mkdir_arg) - 1] = '-';
421 split_point++;
422 }
423 vec_free (mkdir_arg);
424 vec_free (path);
425
426 if (a->name[0] == '/')
427 name_offset = 1;
428
429 shm_name = format (0, "/%s-%s%c", a->root_path,
430 &a->name[name_offset], 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400432 else
433 shm_name = format (0, "%s%c", a->name, 0);
434 return (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435}
436
437/*
438 * svm_map_region
439 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400440void *
441svm_map_region (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400443 int svm_fd;
444 svm_region_t *rp;
445 pthread_mutexattr_t attr;
446 pthread_condattr_t cattr;
447 int deadman = 0;
448 u8 junk = 0;
449 void *oldheap;
450 int overhead_space;
451 int rv;
452 uword data_base;
453 int nbits, words, bit;
454 int pid_holding_region_lock;
455 u8 *shm_name;
456 int dead_region_recovery = 0;
457 int time_left;
458 struct stat stat;
459 struct timespec ts, tsrem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400461 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
462 ASSERT (a->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400464 shm_name = shm_name_from_svm_map_region_args (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Dave Wallaced756b352017-07-03 13:11:38 -0400466 if (CLIB_DEBUG > 1)
467 clib_warning ("[%d] map region %s: shm_open (%s)",
468 getpid (), a->name, shm_name);
469
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400470 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400472 if (svm_fd >= 0)
473 {
474 if (fchmod (svm_fd, 0770) < 0)
475 clib_unix_warning ("segment chmod");
476 /* This turns out to fail harmlessly if the client starts first */
477 if (fchown (svm_fd, a->uid, a->gid) < 0)
478 clib_unix_warning ("segment chown [ok if client starts first]");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400480 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400482 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
483 {
484 clib_warning ("seek region size");
485 close (svm_fd);
486 return (0);
487 }
488 if (write (svm_fd, &junk, 1) != 1)
489 {
490 clib_warning ("set region size");
491 close (svm_fd);
492 return (0);
493 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Damjan Marion7bee80c2017-04-26 15:32:12 +0200495 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400496 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400498 if (rp == (svm_region_t *) MAP_FAILED)
499 {
500 clib_unix_warning ("mmap create");
501 close (svm_fd);
502 return (0);
503 }
504 close (svm_fd);
505 memset (rp, 0, sizeof (*rp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400507 if (pthread_mutexattr_init (&attr))
508 clib_unix_warning ("mutexattr_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400510 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
511 clib_unix_warning ("mutexattr_setpshared");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400513 if (pthread_mutex_init (&rp->mutex, &attr))
514 clib_unix_warning ("mutex_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400516 if (pthread_mutexattr_destroy (&attr))
517 clib_unix_warning ("mutexattr_destroy");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400519 if (pthread_condattr_init (&cattr))
520 clib_unix_warning ("condattr_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400522 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
523 clib_unix_warning ("condattr_setpshared");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400525 if (pthread_cond_init (&rp->condvar, &cattr))
526 clib_unix_warning ("cond_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400528 if (pthread_condattr_destroy (&cattr))
529 clib_unix_warning ("condattr_destroy");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400531 region_lock (rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400533 rp->virtual_base = a->baseva;
534 rp->virtual_size = a->size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400536 rp->region_heap =
Damjan Marion7bee80c2017-04-26 15:32:12 +0200537 mheap_alloc_with_flags (uword_to_pointer
538 (a->baseva + MMAP_PAGESIZE, void *),
539 (a->pvt_heap_size !=
540 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
Dave Barachc3799992016-08-15 11:12:27 -0400541 MHEAP_FLAG_DISABLE_VM);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400542 oldheap = svm_push_pvt_heap (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400544 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
545 vec_add1 (rp->client_pids, getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400547 nbits = rp->virtual_size / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400549 ASSERT (nbits > 0);
550 rp->bitmap_size = nbits;
551 words = (nbits + BITS (uword) - 1) / BITS (uword);
552 vec_validate (rp->bitmap, words - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400554 overhead_space = MMAP_PAGESIZE /* header */ +
Dave Barachc3799992016-08-15 11:12:27 -0400555 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400557 bit = 0;
558 data_base = (uword) rp->virtual_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400560 if (a->flags & SVM_FLAGS_NODATA)
561 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400563 do
564 {
565 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
566 bit++;
567 overhead_space -= MMAP_PAGESIZE;
568 data_base += MMAP_PAGESIZE;
569 }
570 while (overhead_space > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400572 rp->data_base = (void *) data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400574 /*
575 * Note: although the POSIX spec guarantees that only one
576 * process enters this block, we have to play games
577 * to hold off clients until e.g. the mutex is ready
578 */
579 rp->version = SVM_VERSION;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400581 /* setup the data portion of the region */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400583 rv = svm_data_region_create (a, rp);
584 if (rv)
585 {
586 clib_warning ("data_region_create: %d", rv);
587 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400589 region_unlock (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400591 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400593 return ((void *) rp);
594 }
595 else
596 {
597 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400599 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400601 if (svm_fd < 0)
602 {
603 perror ("svm_region_map(mmap open)");
604 return (0);
605 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400607 time_left = 20;
608 while (1)
609 {
610 if (0 != fstat (svm_fd, &stat))
611 {
612 clib_warning ("fstat failed: %d", errno);
613 close (svm_fd);
614 return (0);
615 }
616 if (stat.st_size > 0)
617 {
618 break;
619 }
620 if (0 == time_left)
621 {
622 clib_warning ("waiting for resize of shm file timed out");
623 close (svm_fd);
624 return (0);
625 }
626 ts.tv_sec = 0;
627 ts.tv_nsec = 100000000;
628 while (nanosleep (&ts, &tsrem) < 0)
629 ts = tsrem;
630 time_left--;
631 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400633 rp = mmap (0, MMAP_PAGESIZE,
634 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400636 if (rp == (svm_region_t *) MAP_FAILED)
637 {
638 close (svm_fd);
639 clib_warning ("mmap");
640 return (0);
641 }
642 /*
643 * We lost the footrace to create this region; make sure
644 * the winner has crossed the finish line.
645 */
646 while (rp->version == 0 && deadman++ < 5)
647 {
648 sleep (1);
649 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400651 /*
652 * <bleep>-ed?
653 */
654 if (rp->version == 0)
655 {
656 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
657 close (svm_fd);
658 munmap (rp, a->size);
659 return (0);
660 }
661 /* Remap now that the region has been placed */
662 a->baseva = rp->virtual_base;
663 a->size = rp->virtual_size;
664 munmap (rp, MMAP_PAGESIZE);
665
Damjan Marion7bee80c2017-04-26 15:32:12 +0200666 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400667 PROT_READ | PROT_WRITE,
668 MAP_SHARED | MAP_FIXED, svm_fd, 0);
669 if ((uword) rp == (uword) MAP_FAILED)
670 {
671 clib_unix_warning ("mmap");
672 close (svm_fd);
673 return (0);
674 }
675
676 if ((uword) rp != rp->virtual_base)
677 {
678 clib_warning ("mmap botch");
679 }
680
681 /*
682 * Try to fix the region mutex if it is held by
683 * a dead process
684 */
685 pid_holding_region_lock = rp->mutex_owner_pid;
686 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
687 {
688 clib_warning
689 ("region %s mutex held by dead pid %d, tag %d, force unlock",
690 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
691 /* owner pid is nonexistent */
692 rp->mutex.__data.__owner = 0;
693 rp->mutex.__data.__lock = 0;
694 dead_region_recovery = 1;
695 }
696
697 if (dead_region_recovery)
698 clib_warning ("recovery: attempt to re-lock region");
699
700 region_lock (rp, 2);
701 oldheap = svm_push_pvt_heap (rp);
702 vec_add1 (rp->client_pids, getpid ());
703
704 if (dead_region_recovery)
705 clib_warning ("recovery: attempt svm_data_region_map");
706
707 rv = svm_data_region_map (a, rp);
708 if (rv)
709 {
710 clib_warning ("data_region_map: %d", rv);
711 }
712
713 if (dead_region_recovery)
714 clib_warning ("unlock and continue");
715
716 region_unlock (rp);
717
718 svm_pop_heap (oldheap);
719
720 return ((void *) rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721
722 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400723 return 0; /* NOTREACHED */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724}
725
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400726static void
727svm_mutex_cleanup (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400729 int i;
730 for (i = 0; i < nheld; i++)
731 {
732 pthread_mutex_unlock (mutexes_held[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 }
734}
735
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400736static void
Dave Barachb3d93da2016-08-03 14:34:38 -0400737svm_region_init_internal (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400739 svm_region_t *rp;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400740 u64 ticks = clib_cpu_time_now ();
741 uword randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400743 /* guard against klutz calls */
744 if (root_rp)
745 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400747 root_rp_refcount++;
Dave Barach16c75df2016-05-31 14:05:46 -0400748
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400749 atexit (svm_mutex_cleanup);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400751 /* Randomize the shared-VM base at init time */
752 if (MMAP_PAGESIZE <= (4 << 10))
753 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
754 else
755 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756
Dave Barachb3d93da2016-08-03 14:34:38 -0400757 a->baseva += randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400759 rp = svm_map_region (a);
760 ASSERT (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400762 region_lock (rp, 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400764 /* Set up the main region data structures */
765 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
766 {
767 svm_main_region_t *mp = 0;
768 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400770 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400772 oldheap = svm_push_pvt_heap (rp);
773 vec_validate (mp, 0);
774 mp->name_hash = hash_create_string (0, sizeof (uword));
Dave Barachb3d93da2016-08-03 14:34:38 -0400775 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400776 rp->data_base = mp;
777 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400779 region_unlock (rp);
780 root_rp = rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781}
782
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400783void
784svm_region_init (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785{
Dave Barachb3d93da2016-08-03 14:34:38 -0400786 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400787
Dave Barachb3d93da2016-08-03 14:34:38 -0400788 memset (a, 0, sizeof (*a));
789 a->root_path = 0;
790 a->name = SVM_GLOBAL_REGION_NAME;
791 a->baseva = SVM_GLOBAL_REGION_BASEVA;
792 a->size = SVM_GLOBAL_REGION_SIZE;
793 a->flags = SVM_FLAGS_NODATA;
794 a->uid = 0;
795 a->gid = 0;
796
797 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798}
799
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400800void
Neale Rannse72be392017-04-26 13:59:20 -0700801svm_region_init_chroot (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802{
Dave Barachb3d93da2016-08-03 14:34:38 -0400803 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400804
Dave Barachb3d93da2016-08-03 14:34:38 -0400805 memset (a, 0, sizeof (*a));
806 a->root_path = root_path;
807 a->name = SVM_GLOBAL_REGION_NAME;
808 a->baseva = SVM_GLOBAL_REGION_BASEVA;
809 a->size = SVM_GLOBAL_REGION_SIZE;
810 a->flags = SVM_FLAGS_NODATA;
811 a->uid = 0;
812 a->gid = 0;
813
814 svm_region_init_internal (a);
Dave Barach16c75df2016-05-31 14:05:46 -0400815}
816
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400817void
Neale Rannse72be392017-04-26 13:59:20 -0700818svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400819{
Dave Barachb3d93da2016-08-03 14:34:38 -0400820 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400821
Dave Barachb3d93da2016-08-03 14:34:38 -0400822 memset (a, 0, sizeof (*a));
823 a->root_path = root_path;
824 a->name = SVM_GLOBAL_REGION_NAME;
825 a->baseva = SVM_GLOBAL_REGION_BASEVA;
826 a->size = SVM_GLOBAL_REGION_SIZE;
827 a->flags = SVM_FLAGS_NODATA;
828 a->uid = uid;
829 a->gid = gid;
830
831 svm_region_init_internal (a);
832}
833
834void
835svm_region_init_args (svm_map_region_args_t * a)
836{
837 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838}
839
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400840void *
841svm_region_find_or_create (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400843 svm_main_region_t *mp;
844 svm_region_t *rp;
845 uword need_nbits;
846 int index, i;
847 void *oldheap;
848 uword *p;
849 u8 *name;
850 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400852 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853
Dave Barachc3799992016-08-15 11:12:27 -0400854 a->size += MMAP_PAGESIZE +
Dave Barachb3d93da2016-08-03 14:34:38 -0400855 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400856 a->size = rnd_pagesize (a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400858 region_lock (root_rp, 4);
859 oldheap = svm_push_pvt_heap (root_rp);
860 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400862 ASSERT (mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400864 /* Map the named region from the correct chroot environment */
865 a->root_path = (char *) mp->root_path;
866
867 /*
868 * See if this region is already known. If it is, we're
869 * almost done...
870 */
871 p = hash_get_mem (mp->name_hash, a->name);
872
873 if (p)
874 {
875 rp = svm_map_region (a);
876 region_unlock (root_rp);
877 svm_pop_heap (oldheap);
878 return rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400881 /* Create the region. */
882 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400884 need_nbits = a->size / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400886 index = 1; /* $$$ fixme, figure out how many bit to really skip */
887
888 /*
889 * Scan the virtual space allocation bitmap, looking for a large
890 * enough chunk
891 */
892 do
893 {
894 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
895 {
896 for (i = 0; i < (need_nbits - 1); i++)
897 {
898 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
899 {
900 index = index + i;
901 goto next;
902 }
903 }
904 break;
905 }
906 index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 next:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400909 while (index < root_rp->bitmap_size);
910
911 /* Completely out of VM? */
912 if (index >= root_rp->bitmap_size)
913 {
Dave Barachb3d93da2016-08-03 14:34:38 -0400914 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
915 root_rp->region_name, a->size, a->size);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400916 svm_pop_heap (oldheap);
917 region_unlock (root_rp);
918 return 0;
919 }
920
921 /*
922 * Mark virtual space allocated
923 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400925 clib_warning ("set %d bits at index %d", need_nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926#endif
927
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400928 for (i = 0; i < need_nbits; i++)
929 {
930 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 }
932
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400933 /* Place this region where it goes... */
934 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400936 rp = svm_map_region (a);
Dave Barachc3799992016-08-15 11:12:27 -0400937
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400938 pool_get (mp->subregions, subp);
939 name = format (0, "%s%c", a->name, 0);
940 subp->subregion_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400942 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400944 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400946 region_unlock (root_rp);
947
948 return (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949}
950
Dave Wallaced756b352017-07-03 13:11:38 -0400951void
952svm_region_unlink (svm_region_t * rp)
953{
954 svm_map_region_args_t _a, *a = &_a;
955 svm_main_region_t *mp;
956 u8 *shm_name;
957
958 ASSERT (root_rp);
959 ASSERT (rp);
960 ASSERT (vec_c_string_is_terminated (rp->region_name));
961
962 mp = root_rp->data_base;
963 ASSERT (mp);
964
965 a->root_path = (char *) mp->root_path;
966 a->name = rp->region_name;
967 shm_name = shm_name_from_svm_map_region_args (a);
968 if (CLIB_DEBUG > 1)
969 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
970 shm_unlink ((const char *) shm_name);
971 vec_free (shm_name);
972}
973
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974/*
975 * svm_region_unmap
976 *
977 * Let go of the indicated region. If the calling process
978 * is the last customer, throw it away completely.
979 * The root region mutex guarantees atomicity with respect to
980 * a new region client showing up at the wrong moment.
981 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400982void
983svm_region_unmap (void *rp_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400985 int i, mypid = getpid ();
986 int nclients_left;
987 void *oldheap;
988 uword virtual_base, virtual_size;
989 svm_region_t *rp = rp_arg;
990 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400992 /*
993 * If we take a signal while holding one or more shared-memory
994 * mutexes, we may end up back here from an otherwise
995 * benign exit handler. Bail out to avoid a recursive
996 * mutex screw-up.
997 */
998 if (nheld)
999 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001001 ASSERT (rp);
1002 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001004 if (CLIB_DEBUG > 1)
1005 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001007 region_lock (root_rp, 5);
1008 region_lock (rp, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001010 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1011
1012 /* Remove the caller from the list of mappers */
1013 for (i = 0; i < vec_len (rp->client_pids); i++)
1014 {
1015 if (rp->client_pids[i] == mypid)
1016 {
1017 vec_delete (rp->client_pids, 1, i);
1018 goto found;
1019 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001021 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001023found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001025 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001027 nclients_left = vec_len (rp->client_pids);
1028 virtual_base = rp->virtual_base;
1029 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001031 if (nclients_left == 0)
1032 {
1033 int index, nbits, i;
1034 svm_main_region_t *mp;
1035 uword *p;
1036 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001038 /* Kill the region, last guy on his way out */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001040 oldheap = svm_push_pvt_heap (root_rp);
1041 name = vec_dup (rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001043 virtual_base = rp->virtual_base;
1044 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001046 /* Figure out which bits to clear in the root region bitmap */
1047 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001049 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
1051#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001052 clib_warning ("clear %d bits at index %d", nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053#endif
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001054 /* Give back the allocated VM */
1055 for (i = 0; i < nbits; i++)
1056 {
1057 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1058 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001060 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001062 p = hash_get_mem (mp->name_hash, name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001064 /* Better never happen ... */
1065 if (p == NULL)
1066 {
1067 region_unlock (rp);
1068 region_unlock (root_rp);
1069 svm_pop_heap (oldheap);
1070 clib_warning ("Region name '%s' not found?", name);
1071 return;
1072 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001074 /* Remove from the root region subregion pool */
1075 subp = mp->subregions + p[0];
1076 pool_put (mp->subregions, subp);
1077
1078 hash_unset_mem (mp->name_hash, name);
1079
1080 vec_free (name);
1081
1082 region_unlock (rp);
Dave Wallaced756b352017-07-03 13:11:38 -04001083 svm_region_unlink (rp);
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001084 munmap ((void *) virtual_base, virtual_size);
1085 region_unlock (root_rp);
1086 svm_pop_heap (oldheap);
1087 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088 }
1089
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001090 region_unlock (rp);
1091 region_unlock (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001093 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094}
1095
1096/*
1097 * svm_region_exit
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001098 */
1099void
1100svm_region_exit ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001102 void *oldheap;
1103 int i, mypid = getpid ();
1104 uword virtual_base, virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001106 /* It felt so nice we did it twice... */
1107 if (root_rp == 0)
1108 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001110 if (--root_rp_refcount > 0)
1111 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001113 /*
1114 * If we take a signal while holding one or more shared-memory
1115 * mutexes, we may end up back here from an otherwise
1116 * benign exit handler. Bail out to avoid a recursive
1117 * mutex screw-up.
1118 */
1119 if (nheld)
1120 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001122 region_lock (root_rp, 7);
1123 oldheap = svm_push_pvt_heap (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001125 virtual_base = root_rp->virtual_base;
1126 virtual_size = root_rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001128 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1129 {
1130 if (root_rp->client_pids[i] == mypid)
1131 {
1132 vec_delete (root_rp->client_pids, 1, i);
1133 goto found;
1134 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001136 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001138found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139
Dave Wallaced756b352017-07-03 13:11:38 -04001140 if (vec_len (root_rp->client_pids) == 0)
1141 svm_region_unlink (root_rp);
1142
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001143 region_unlock (root_rp);
1144 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001146 root_rp = 0;
1147 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148}
1149
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001150void
1151svm_client_scan_this_region_nolock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001153 int j;
1154 int mypid = getpid ();
1155 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001157 for (j = 0; j < vec_len (rp->client_pids); j++)
1158 {
1159 if (mypid == rp->client_pids[j])
1160 continue;
1161 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1162 {
1163 clib_warning ("%s: cleanup ghost pid %d",
1164 rp->region_name, rp->client_pids[j]);
1165 /* nb: client vec in rp->region_heap */
1166 oldheap = svm_push_pvt_heap (rp);
1167 vec_delete (rp->client_pids, 1, j);
1168 j--;
1169 svm_pop_heap (oldheap);
1170 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171 }
1172}
1173
1174
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001175/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176 * Scan svm regions for dead clients
1177 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001178void
Neale Rannse72be392017-04-26 13:59:20 -07001179svm_client_scan (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001181 int i, j;
1182 svm_main_region_t *mp;
1183 svm_map_region_args_t *a = 0;
1184 svm_region_t *root_rp;
1185 svm_region_t *rp;
1186 svm_subregion_t *subp;
1187 u8 *name = 0;
1188 u8 **svm_names = 0;
1189 void *oldheap;
1190 int mypid = getpid ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001192 vec_validate (a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001194 svm_region_init_chroot (root_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001196 root_rp = svm_get_root_rp ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001198 pthread_mutex_lock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001200 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001202 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1203 {
1204 if (mypid == root_rp->client_pids[j])
1205 continue;
1206 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1207 {
1208 clib_warning ("%s: cleanup ghost pid %d",
1209 root_rp->region_name, root_rp->client_pids[j]);
1210 /* nb: client vec in root_rp->region_heap */
1211 oldheap = svm_push_pvt_heap (root_rp);
1212 vec_delete (root_rp->client_pids, 1, j);
1213 j--;
1214 svm_pop_heap (oldheap);
1215 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216 }
1217
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001218 /*
1219 * Snapshoot names, can't hold root rp mutex across
1220 * find_or_create.
1221 */
1222 /* *INDENT-OFF* */
1223 pool_foreach (subp, mp->subregions, ({
1224 name = vec_dup (subp->subregion_name);
1225 vec_add1(svm_names, name);
1226 }));
1227 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001229 pthread_mutex_unlock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001231 for (i = 0; i < vec_len (svm_names); i++)
1232 {
1233 vec_validate (a, 0);
1234 a->root_path = root_path;
1235 a->name = (char *) svm_names[i];
1236 rp = svm_region_find_or_create (a);
1237 if (rp)
1238 {
1239 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001241 svm_client_scan_this_region_nolock (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001243 pthread_mutex_unlock (&rp->mutex);
1244 svm_region_unmap (rp);
1245 vec_free (svm_names[i]);
1246 }
1247 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001249 vec_free (svm_names);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001251 svm_region_exit ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001253 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001254}
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001255
1256/*
1257 * fd.io coding-style-patch-verification: ON
1258 *
1259 * Local Variables:
1260 * eval: (c-set-style "gnu")
1261 * End:
1262 */