blob: c96135cfd5bef71556f16d972eb3deb36d228f44 [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 if (CLIB_DEBUG > 1)
462 clib_warning ("[%d] map region %s", getpid (), a->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400464 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
465 ASSERT (a->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400467 shm_name = shm_name_from_svm_map_region_args (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400469 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400471 if (svm_fd >= 0)
472 {
473 if (fchmod (svm_fd, 0770) < 0)
474 clib_unix_warning ("segment chmod");
475 /* This turns out to fail harmlessly if the client starts first */
476 if (fchown (svm_fd, a->uid, a->gid) < 0)
477 clib_unix_warning ("segment chown [ok if client starts first]");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400479 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400481 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
482 {
483 clib_warning ("seek region size");
484 close (svm_fd);
485 return (0);
486 }
487 if (write (svm_fd, &junk, 1) != 1)
488 {
489 clib_warning ("set region size");
490 close (svm_fd);
491 return (0);
492 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493
Damjan Marion7bee80c2017-04-26 15:32:12 +0200494 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400495 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400497 if (rp == (svm_region_t *) MAP_FAILED)
498 {
499 clib_unix_warning ("mmap create");
500 close (svm_fd);
501 return (0);
502 }
503 close (svm_fd);
504 memset (rp, 0, sizeof (*rp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400506 if (pthread_mutexattr_init (&attr))
507 clib_unix_warning ("mutexattr_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400509 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
510 clib_unix_warning ("mutexattr_setpshared");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400512 if (pthread_mutex_init (&rp->mutex, &attr))
513 clib_unix_warning ("mutex_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400515 if (pthread_mutexattr_destroy (&attr))
516 clib_unix_warning ("mutexattr_destroy");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400518 if (pthread_condattr_init (&cattr))
519 clib_unix_warning ("condattr_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400521 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
522 clib_unix_warning ("condattr_setpshared");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400524 if (pthread_cond_init (&rp->condvar, &cattr))
525 clib_unix_warning ("cond_init");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400527 if (pthread_condattr_destroy (&cattr))
528 clib_unix_warning ("condattr_destroy");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400530 region_lock (rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400532 rp->virtual_base = a->baseva;
533 rp->virtual_size = a->size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400535 rp->region_heap =
Damjan Marion7bee80c2017-04-26 15:32:12 +0200536 mheap_alloc_with_flags (uword_to_pointer
537 (a->baseva + MMAP_PAGESIZE, void *),
538 (a->pvt_heap_size !=
539 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
Dave Barachc3799992016-08-15 11:12:27 -0400540 MHEAP_FLAG_DISABLE_VM);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400541 oldheap = svm_push_pvt_heap (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400543 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
544 vec_add1 (rp->client_pids, getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400546 nbits = rp->virtual_size / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400548 ASSERT (nbits > 0);
549 rp->bitmap_size = nbits;
550 words = (nbits + BITS (uword) - 1) / BITS (uword);
551 vec_validate (rp->bitmap, words - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400553 overhead_space = MMAP_PAGESIZE /* header */ +
Dave Barachc3799992016-08-15 11:12:27 -0400554 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400556 bit = 0;
557 data_base = (uword) rp->virtual_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400559 if (a->flags & SVM_FLAGS_NODATA)
560 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400562 do
563 {
564 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
565 bit++;
566 overhead_space -= MMAP_PAGESIZE;
567 data_base += MMAP_PAGESIZE;
568 }
569 while (overhead_space > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400571 rp->data_base = (void *) data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400573 /*
574 * Note: although the POSIX spec guarantees that only one
575 * process enters this block, we have to play games
576 * to hold off clients until e.g. the mutex is ready
577 */
578 rp->version = SVM_VERSION;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400580 /* setup the data portion of the region */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400582 rv = svm_data_region_create (a, rp);
583 if (rv)
584 {
585 clib_warning ("data_region_create: %d", rv);
586 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400588 region_unlock (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400590 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400592 return ((void *) rp);
593 }
594 else
595 {
596 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400598 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400600 if (svm_fd < 0)
601 {
602 perror ("svm_region_map(mmap open)");
603 return (0);
604 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400606 time_left = 20;
607 while (1)
608 {
609 if (0 != fstat (svm_fd, &stat))
610 {
611 clib_warning ("fstat failed: %d", errno);
612 close (svm_fd);
613 return (0);
614 }
615 if (stat.st_size > 0)
616 {
617 break;
618 }
619 if (0 == time_left)
620 {
621 clib_warning ("waiting for resize of shm file timed out");
622 close (svm_fd);
623 return (0);
624 }
625 ts.tv_sec = 0;
626 ts.tv_nsec = 100000000;
627 while (nanosleep (&ts, &tsrem) < 0)
628 ts = tsrem;
629 time_left--;
630 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400632 rp = mmap (0, MMAP_PAGESIZE,
633 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400635 if (rp == (svm_region_t *) MAP_FAILED)
636 {
637 close (svm_fd);
638 clib_warning ("mmap");
639 return (0);
640 }
641 /*
642 * We lost the footrace to create this region; make sure
643 * the winner has crossed the finish line.
644 */
645 while (rp->version == 0 && deadman++ < 5)
646 {
647 sleep (1);
648 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400650 /*
651 * <bleep>-ed?
652 */
653 if (rp->version == 0)
654 {
655 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
656 close (svm_fd);
657 munmap (rp, a->size);
658 return (0);
659 }
660 /* Remap now that the region has been placed */
661 a->baseva = rp->virtual_base;
662 a->size = rp->virtual_size;
663 munmap (rp, MMAP_PAGESIZE);
664
Damjan Marion7bee80c2017-04-26 15:32:12 +0200665 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400666 PROT_READ | PROT_WRITE,
667 MAP_SHARED | MAP_FIXED, svm_fd, 0);
668 if ((uword) rp == (uword) MAP_FAILED)
669 {
670 clib_unix_warning ("mmap");
671 close (svm_fd);
672 return (0);
673 }
674
675 if ((uword) rp != rp->virtual_base)
676 {
677 clib_warning ("mmap botch");
678 }
679
680 /*
681 * Try to fix the region mutex if it is held by
682 * a dead process
683 */
684 pid_holding_region_lock = rp->mutex_owner_pid;
685 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
686 {
687 clib_warning
688 ("region %s mutex held by dead pid %d, tag %d, force unlock",
689 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
690 /* owner pid is nonexistent */
691 rp->mutex.__data.__owner = 0;
692 rp->mutex.__data.__lock = 0;
693 dead_region_recovery = 1;
694 }
695
696 if (dead_region_recovery)
697 clib_warning ("recovery: attempt to re-lock region");
698
699 region_lock (rp, 2);
700 oldheap = svm_push_pvt_heap (rp);
701 vec_add1 (rp->client_pids, getpid ());
702
703 if (dead_region_recovery)
704 clib_warning ("recovery: attempt svm_data_region_map");
705
706 rv = svm_data_region_map (a, rp);
707 if (rv)
708 {
709 clib_warning ("data_region_map: %d", rv);
710 }
711
712 if (dead_region_recovery)
713 clib_warning ("unlock and continue");
714
715 region_unlock (rp);
716
717 svm_pop_heap (oldheap);
718
719 return ((void *) rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
721 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400722 return 0; /* NOTREACHED */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723}
724
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400725static void
726svm_mutex_cleanup (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400728 int i;
729 for (i = 0; i < nheld; i++)
730 {
731 pthread_mutex_unlock (mutexes_held[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 }
733}
734
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400735static void
Dave Barachb3d93da2016-08-03 14:34:38 -0400736svm_region_init_internal (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400738 svm_region_t *rp;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400739 u64 ticks = clib_cpu_time_now ();
740 uword randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400742 /* guard against klutz calls */
743 if (root_rp)
744 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400746 root_rp_refcount++;
Dave Barach16c75df2016-05-31 14:05:46 -0400747
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400748 atexit (svm_mutex_cleanup);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400750 /* Randomize the shared-VM base at init time */
751 if (MMAP_PAGESIZE <= (4 << 10))
752 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
753 else
754 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755
Dave Barachb3d93da2016-08-03 14:34:38 -0400756 a->baseva += randomize_baseva;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400758 rp = svm_map_region (a);
759 ASSERT (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400761 region_lock (rp, 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400763 /* Set up the main region data structures */
764 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
765 {
766 svm_main_region_t *mp = 0;
767 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400769 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400771 oldheap = svm_push_pvt_heap (rp);
772 vec_validate (mp, 0);
773 mp->name_hash = hash_create_string (0, sizeof (uword));
Dave Barachb3d93da2016-08-03 14:34:38 -0400774 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400775 rp->data_base = mp;
776 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400778 region_unlock (rp);
779 root_rp = rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780}
781
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400782void
783svm_region_init (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784{
Dave Barachb3d93da2016-08-03 14:34:38 -0400785 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400786
Dave Barachb3d93da2016-08-03 14:34:38 -0400787 memset (a, 0, sizeof (*a));
788 a->root_path = 0;
789 a->name = SVM_GLOBAL_REGION_NAME;
790 a->baseva = SVM_GLOBAL_REGION_BASEVA;
791 a->size = SVM_GLOBAL_REGION_SIZE;
792 a->flags = SVM_FLAGS_NODATA;
793 a->uid = 0;
794 a->gid = 0;
795
796 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797}
798
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400799void
Neale Rannse72be392017-04-26 13:59:20 -0700800svm_region_init_chroot (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801{
Dave Barachb3d93da2016-08-03 14:34:38 -0400802 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400803
Dave Barachb3d93da2016-08-03 14:34:38 -0400804 memset (a, 0, sizeof (*a));
805 a->root_path = root_path;
806 a->name = SVM_GLOBAL_REGION_NAME;
807 a->baseva = SVM_GLOBAL_REGION_BASEVA;
808 a->size = SVM_GLOBAL_REGION_SIZE;
809 a->flags = SVM_FLAGS_NODATA;
810 a->uid = 0;
811 a->gid = 0;
812
813 svm_region_init_internal (a);
Dave Barach16c75df2016-05-31 14:05:46 -0400814}
815
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400816void
Neale Rannse72be392017-04-26 13:59:20 -0700817svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400818{
Dave Barachb3d93da2016-08-03 14:34:38 -0400819 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -0400820
Dave Barachb3d93da2016-08-03 14:34:38 -0400821 memset (a, 0, sizeof (*a));
822 a->root_path = root_path;
823 a->name = SVM_GLOBAL_REGION_NAME;
824 a->baseva = SVM_GLOBAL_REGION_BASEVA;
825 a->size = SVM_GLOBAL_REGION_SIZE;
826 a->flags = SVM_FLAGS_NODATA;
827 a->uid = uid;
828 a->gid = gid;
829
830 svm_region_init_internal (a);
831}
832
833void
834svm_region_init_args (svm_map_region_args_t * a)
835{
836 svm_region_init_internal (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837}
838
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400839void *
840svm_region_find_or_create (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400842 svm_main_region_t *mp;
843 svm_region_t *rp;
844 uword need_nbits;
845 int index, i;
846 void *oldheap;
847 uword *p;
848 u8 *name;
849 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400851 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
Dave Barachc3799992016-08-15 11:12:27 -0400853 a->size += MMAP_PAGESIZE +
Dave Barachb3d93da2016-08-03 14:34:38 -0400854 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400855 a->size = rnd_pagesize (a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400857 region_lock (root_rp, 4);
858 oldheap = svm_push_pvt_heap (root_rp);
859 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400861 ASSERT (mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400863 /* Map the named region from the correct chroot environment */
864 a->root_path = (char *) mp->root_path;
865
866 /*
867 * See if this region is already known. If it is, we're
868 * almost done...
869 */
870 p = hash_get_mem (mp->name_hash, a->name);
871
872 if (p)
873 {
874 rp = svm_map_region (a);
875 region_unlock (root_rp);
876 svm_pop_heap (oldheap);
877 return rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400880 /* Create the region. */
881 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400883 need_nbits = a->size / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400885 index = 1; /* $$$ fixme, figure out how many bit to really skip */
886
887 /*
888 * Scan the virtual space allocation bitmap, looking for a large
889 * enough chunk
890 */
891 do
892 {
893 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
894 {
895 for (i = 0; i < (need_nbits - 1); i++)
896 {
897 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
898 {
899 index = index + i;
900 goto next;
901 }
902 }
903 break;
904 }
905 index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 next:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400908 while (index < root_rp->bitmap_size);
909
910 /* Completely out of VM? */
911 if (index >= root_rp->bitmap_size)
912 {
Dave Barachb3d93da2016-08-03 14:34:38 -0400913 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
914 root_rp->region_name, a->size, a->size);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400915 svm_pop_heap (oldheap);
916 region_unlock (root_rp);
917 return 0;
918 }
919
920 /*
921 * Mark virtual space allocated
922 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400924 clib_warning ("set %d bits at index %d", need_nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925#endif
926
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400927 for (i = 0; i < need_nbits; i++)
928 {
929 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 }
931
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400932 /* Place this region where it goes... */
933 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400935 rp = svm_map_region (a);
Dave Barachc3799992016-08-15 11:12:27 -0400936
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400937 pool_get (mp->subregions, subp);
938 name = format (0, "%s%c", a->name, 0);
939 subp->subregion_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400941 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400943 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400945 region_unlock (root_rp);
946
947 return (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948}
949
950/*
951 * svm_region_unmap
952 *
953 * Let go of the indicated region. If the calling process
954 * is the last customer, throw it away completely.
955 * The root region mutex guarantees atomicity with respect to
956 * a new region client showing up at the wrong moment.
957 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400958void
959svm_region_unmap (void *rp_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400961 int i, mypid = getpid ();
962 int nclients_left;
963 void *oldheap;
964 uword virtual_base, virtual_size;
965 svm_region_t *rp = rp_arg;
966 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400968 /*
969 * If we take a signal while holding one or more shared-memory
970 * mutexes, we may end up back here from an otherwise
971 * benign exit handler. Bail out to avoid a recursive
972 * mutex screw-up.
973 */
974 if (nheld)
975 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400977 ASSERT (rp);
978 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400980 if (CLIB_DEBUG > 1)
981 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400983 region_lock (root_rp, 5);
984 region_lock (rp, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400986 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
987
988 /* Remove the caller from the list of mappers */
989 for (i = 0; i < vec_len (rp->client_pids); i++)
990 {
991 if (rp->client_pids[i] == mypid)
992 {
993 vec_delete (rp->client_pids, 1, i);
994 goto found;
995 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400997 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400999found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001001 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001003 nclients_left = vec_len (rp->client_pids);
1004 virtual_base = rp->virtual_base;
1005 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001007 if (nclients_left == 0)
1008 {
1009 int index, nbits, i;
1010 svm_main_region_t *mp;
1011 uword *p;
1012 svm_subregion_t *subp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001014 /* Kill the region, last guy on his way out */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001016 oldheap = svm_push_pvt_heap (root_rp);
1017 name = vec_dup (rp->region_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001019 virtual_base = rp->virtual_base;
1020 virtual_size = rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001022 /* Figure out which bits to clear in the root region bitmap */
1023 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001025 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026
1027#if CLIB_DEBUG > 1
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001028 clib_warning ("clear %d bits at index %d", nbits, index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029#endif
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001030 /* Give back the allocated VM */
1031 for (i = 0; i < nbits; i++)
1032 {
1033 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1034 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001036 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001038 p = hash_get_mem (mp->name_hash, name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001040 /* Better never happen ... */
1041 if (p == NULL)
1042 {
1043 region_unlock (rp);
1044 region_unlock (root_rp);
1045 svm_pop_heap (oldheap);
1046 clib_warning ("Region name '%s' not found?", name);
1047 return;
1048 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001050 /* Remove from the root region subregion pool */
1051 subp = mp->subregions + p[0];
1052 pool_put (mp->subregions, subp);
1053
1054 hash_unset_mem (mp->name_hash, name);
1055
1056 vec_free (name);
1057
1058 region_unlock (rp);
1059 shm_unlink (rp->region_name);
1060 munmap ((void *) virtual_base, virtual_size);
1061 region_unlock (root_rp);
1062 svm_pop_heap (oldheap);
1063 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064 }
1065
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001066 region_unlock (rp);
1067 region_unlock (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001069 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070}
1071
1072/*
1073 * svm_region_exit
1074 * There is no clean way to unlink the
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001075 * root region when all clients go away,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076 * so remove the pid entry and call it a day.
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001077 */
1078void
1079svm_region_exit ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001081 void *oldheap;
1082 int i, mypid = getpid ();
1083 uword virtual_base, virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001085 /* It felt so nice we did it twice... */
1086 if (root_rp == 0)
1087 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001089 if (--root_rp_refcount > 0)
1090 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001092 /*
1093 * If we take a signal while holding one or more shared-memory
1094 * mutexes, we may end up back here from an otherwise
1095 * benign exit handler. Bail out to avoid a recursive
1096 * mutex screw-up.
1097 */
1098 if (nheld)
1099 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001101 region_lock (root_rp, 7);
1102 oldheap = svm_push_pvt_heap (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001104 virtual_base = root_rp->virtual_base;
1105 virtual_size = root_rp->virtual_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001107 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1108 {
1109 if (root_rp->client_pids[i] == mypid)
1110 {
1111 vec_delete (root_rp->client_pids, 1, i);
1112 goto found;
1113 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001115 clib_warning ("pid %d AWOL", mypid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001117found:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001119 region_unlock (root_rp);
1120 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001122 root_rp = 0;
1123 munmap ((void *) virtual_base, virtual_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124}
1125
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001126void
1127svm_client_scan_this_region_nolock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001129 int j;
1130 int mypid = getpid ();
1131 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001133 for (j = 0; j < vec_len (rp->client_pids); j++)
1134 {
1135 if (mypid == rp->client_pids[j])
1136 continue;
1137 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1138 {
1139 clib_warning ("%s: cleanup ghost pid %d",
1140 rp->region_name, rp->client_pids[j]);
1141 /* nb: client vec in rp->region_heap */
1142 oldheap = svm_push_pvt_heap (rp);
1143 vec_delete (rp->client_pids, 1, j);
1144 j--;
1145 svm_pop_heap (oldheap);
1146 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147 }
1148}
1149
1150
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001151/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152 * Scan svm regions for dead clients
1153 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001154void
Neale Rannse72be392017-04-26 13:59:20 -07001155svm_client_scan (const char *root_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156{
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001157 int i, j;
1158 svm_main_region_t *mp;
1159 svm_map_region_args_t *a = 0;
1160 svm_region_t *root_rp;
1161 svm_region_t *rp;
1162 svm_subregion_t *subp;
1163 u8 *name = 0;
1164 u8 **svm_names = 0;
1165 void *oldheap;
1166 int mypid = getpid ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001168 vec_validate (a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001170 svm_region_init_chroot (root_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001172 root_rp = svm_get_root_rp ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001174 pthread_mutex_lock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001176 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001178 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1179 {
1180 if (mypid == root_rp->client_pids[j])
1181 continue;
1182 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1183 {
1184 clib_warning ("%s: cleanup ghost pid %d",
1185 root_rp->region_name, root_rp->client_pids[j]);
1186 /* nb: client vec in root_rp->region_heap */
1187 oldheap = svm_push_pvt_heap (root_rp);
1188 vec_delete (root_rp->client_pids, 1, j);
1189 j--;
1190 svm_pop_heap (oldheap);
1191 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 }
1193
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001194 /*
1195 * Snapshoot names, can't hold root rp mutex across
1196 * find_or_create.
1197 */
1198 /* *INDENT-OFF* */
1199 pool_foreach (subp, mp->subregions, ({
1200 name = vec_dup (subp->subregion_name);
1201 vec_add1(svm_names, name);
1202 }));
1203 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001205 pthread_mutex_unlock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001207 for (i = 0; i < vec_len (svm_names); i++)
1208 {
1209 vec_validate (a, 0);
1210 a->root_path = root_path;
1211 a->name = (char *) svm_names[i];
1212 rp = svm_region_find_or_create (a);
1213 if (rp)
1214 {
1215 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001217 svm_client_scan_this_region_nolock (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001219 pthread_mutex_unlock (&rp->mutex);
1220 svm_region_unmap (rp);
1221 vec_free (svm_names[i]);
1222 }
1223 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001224 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001225 vec_free (svm_names);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001227 svm_region_exit ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001229 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230}
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001231
1232/*
1233 * fd.io coding-style-patch-verification: ON
1234 *
1235 * Local Variables:
1236 * eval: (c-set-style "gnu")
1237 * End:
1238 */