Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 3 | * svm.c - shared VM allocation, mmap(...MAP_FIXED...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | * 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 | |
| 46 | static svm_region_t *root_rp; |
| 47 | static int root_rp_refcount; |
| 48 | |
| 49 | #define MAXLOCK 2 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 50 | static pthread_mutex_t *mutexes_held[MAXLOCK]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | static int nheld; |
| 52 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 53 | svm_region_t * |
| 54 | svm_get_root_rp (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 56 | return root_rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | #define MUTEX_DEBUG |
| 60 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 61 | static void |
| 62 | region_lock (svm_region_t * rp, int tag) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 64 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 66 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 78 | static void |
| 79 | region_unlock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 81 | int i, j; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 83 | rp->mutex_owner_pid = 0; |
| 84 | rp->mutex_owner_tag = 0; |
| 85 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 87 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 97 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | found: |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 100 | CLIB_MEMORY_BARRIER (); |
| 101 | pthread_mutex_unlock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 105 | static u8 * |
| 106 | format_svm_flags (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 108 | uword f = va_arg (*args, uword); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 110 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 122 | static u8 * |
| 123 | format_svm_size (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 125 | uword size = va_arg (*args, uword); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 127 | if (size >= (1 << 20)) |
| 128 | { |
| 129 | s = format (s, "(%d mb)", size >> 20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 131 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 142 | u8 * |
| 143 | format_svm_region (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 145 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 150 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 156 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 165 | s = format (s, " %d clients, pids: ", vec_len (rp->client_pids)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 167 | for (i = 0; i < vec_len (rp->client_pids); i++) |
| 168 | s = format (s, "%d ", rp->client_pids[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 170 | s = format (s, "\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 172 | if (verbose) |
| 173 | { |
| 174 | lo = hi = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 176 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 212 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * rnd_pagesize |
| 217 | * Round to a pagesize multiple, presumably 4k works |
| 218 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 219 | static unsigned int |
| 220 | rnd_pagesize (unsigned int size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 222 | unsigned int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 224 | rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1); |
| 225 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 228 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | * svm_data_region_setup |
| 230 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 231 | static int |
| 232 | svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 234 | int fd; |
| 235 | u8 junk = 0; |
| 236 | uword map_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 238 | map_size = rp->virtual_size - (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 240 | if (a->flags & SVM_FLAGS_FILE) |
| 241 | { |
| 242 | struct stat statb; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 244 | fd = open (a->backing_file, O_RDWR | O_CREAT, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 246 | if (fd < 0) |
| 247 | { |
| 248 | clib_unix_warning ("open"); |
| 249 | return -1; |
| 250 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 252 | if (fstat (fd, &statb) < 0) |
| 253 | { |
| 254 | clib_unix_warning ("fstat"); |
| 255 | close (fd); |
| 256 | return -2; |
| 257 | } |
| 258 | |
| 259 | if (statb.st_mode & S_IFREG) |
| 260 | { |
| 261 | if (statb.st_size == 0) |
| 262 | { |
| 263 | if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1) |
| 264 | { |
| 265 | clib_unix_warning ("seek region size"); |
| 266 | close (fd); |
| 267 | return -3; |
| 268 | } |
| 269 | if (write (fd, &junk, 1) != 1) |
| 270 | { |
| 271 | clib_unix_warning ("set region size"); |
| 272 | close (fd); |
| 273 | return -3; |
| 274 | } |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | map_size = rnd_pagesize (statb.st_size); |
| 279 | } |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | map_size = a->backing_mmap_size; |
| 284 | } |
| 285 | |
| 286 | ASSERT (map_size <= rp->virtual_size - |
| 287 | (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE)); |
| 288 | |
| 289 | if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE, |
| 290 | MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED) |
| 291 | { |
| 292 | clib_unix_warning ("mmap"); |
| 293 | close (fd); |
| 294 | return -3; |
| 295 | } |
| 296 | close (fd); |
| 297 | rp->backing_file = (char *) format (0, "%s\0", a->backing_file); |
| 298 | rp->flags |= SVM_FLAGS_FILE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 300 | |
| 301 | if (a->flags & SVM_FLAGS_MHEAP) |
| 302 | { |
| 303 | rp->data_heap = |
| 304 | mheap_alloc_with_flags ((void *) (rp->data_base), map_size, |
| 305 | MHEAP_FLAG_DISABLE_VM); |
| 306 | rp->flags |= SVM_FLAGS_MHEAP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 308 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 311 | static int |
| 312 | svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 314 | int fd; |
| 315 | u8 junk = 0; |
| 316 | uword map_size; |
| 317 | struct stat statb; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 319 | map_size = rp->virtual_size - (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 321 | if (a->flags & SVM_FLAGS_FILE) |
| 322 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 324 | fd = open (a->backing_file, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 326 | if (fd < 0) |
| 327 | { |
| 328 | clib_unix_warning ("open"); |
| 329 | return -1; |
| 330 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 332 | if (fstat (fd, &statb) < 0) |
| 333 | { |
| 334 | clib_unix_warning ("fstat"); |
| 335 | close (fd); |
| 336 | return -2; |
| 337 | } |
| 338 | |
| 339 | if (statb.st_mode & S_IFREG) |
| 340 | { |
| 341 | if (statb.st_size == 0) |
| 342 | { |
| 343 | if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1) |
| 344 | { |
| 345 | clib_unix_warning ("seek region size"); |
| 346 | close (fd); |
| 347 | return -3; |
| 348 | } |
| 349 | if (write (fd, &junk, 1) != 1) |
| 350 | { |
| 351 | clib_unix_warning ("set region size"); |
| 352 | close (fd); |
| 353 | return -3; |
| 354 | } |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | map_size = rnd_pagesize (statb.st_size); |
| 359 | } |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | map_size = a->backing_mmap_size; |
| 364 | } |
| 365 | |
| 366 | ASSERT (map_size <= rp->virtual_size |
| 367 | - (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE)); |
| 368 | |
| 369 | if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE, |
| 370 | MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED) |
| 371 | { |
| 372 | clib_unix_warning ("mmap"); |
| 373 | close (fd); |
| 374 | return -3; |
| 375 | } |
| 376 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 378 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 381 | u8 * |
| 382 | shm_name_from_svm_map_region_args (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 384 | u8 *path; |
| 385 | u8 *shm_name; |
| 386 | u8 *split_point; |
| 387 | u8 *mkdir_arg = 0; |
| 388 | int root_path_offset = 0; |
| 389 | int name_offset = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 391 | if (a->root_path) |
| 392 | { |
| 393 | /* Tolerate present or absent slashes */ |
| 394 | if (a->root_path[0] == '/') |
| 395 | root_path_offset++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 397 | /* create the root_path under /dev/shm |
| 398 | iterate through path creating directories */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 400 | path = format (0, "/dev/shm/%s%c", &a->root_path[root_path_offset], 0); |
| 401 | split_point = path + 1; |
| 402 | vec_add1 (mkdir_arg, '-'); |
| 403 | |
| 404 | while (*split_point) |
| 405 | { |
| 406 | while (*split_point && *split_point != '/') |
| 407 | { |
| 408 | vec_add1 (mkdir_arg, *split_point); |
| 409 | split_point++; |
| 410 | } |
| 411 | vec_add1 (mkdir_arg, 0); |
| 412 | |
| 413 | /* ready to descend another level */ |
| 414 | mkdir_arg[vec_len (mkdir_arg) - 1] = '-'; |
| 415 | split_point++; |
| 416 | } |
| 417 | vec_free (mkdir_arg); |
| 418 | vec_free (path); |
| 419 | |
| 420 | if (a->name[0] == '/') |
| 421 | name_offset = 1; |
| 422 | |
| 423 | shm_name = format (0, "/%s-%s%c", a->root_path, |
| 424 | &a->name[name_offset], 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 426 | else |
| 427 | shm_name = format (0, "%s%c", a->name, 0); |
| 428 | return (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* |
| 432 | * svm_map_region |
| 433 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 434 | void * |
| 435 | svm_map_region (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 437 | int svm_fd; |
| 438 | svm_region_t *rp; |
| 439 | pthread_mutexattr_t attr; |
| 440 | pthread_condattr_t cattr; |
| 441 | int deadman = 0; |
| 442 | u8 junk = 0; |
| 443 | void *oldheap; |
| 444 | int overhead_space; |
| 445 | int rv; |
| 446 | uword data_base; |
| 447 | int nbits, words, bit; |
| 448 | int pid_holding_region_lock; |
| 449 | u8 *shm_name; |
| 450 | int dead_region_recovery = 0; |
| 451 | int time_left; |
| 452 | struct stat stat; |
| 453 | struct timespec ts, tsrem; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 455 | if (CLIB_DEBUG > 1) |
| 456 | clib_warning ("[%d] map region %s", getpid (), a->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 458 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
| 459 | ASSERT (a->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 461 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 463 | svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 465 | if (svm_fd >= 0) |
| 466 | { |
| 467 | if (fchmod (svm_fd, 0770) < 0) |
| 468 | clib_unix_warning ("segment chmod"); |
| 469 | /* This turns out to fail harmlessly if the client starts first */ |
| 470 | if (fchown (svm_fd, a->uid, a->gid) < 0) |
| 471 | clib_unix_warning ("segment chown [ok if client starts first]"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 473 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 475 | if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1) |
| 476 | { |
| 477 | clib_warning ("seek region size"); |
| 478 | close (svm_fd); |
| 479 | return (0); |
| 480 | } |
| 481 | if (write (svm_fd, &junk, 1) != 1) |
| 482 | { |
| 483 | clib_warning ("set region size"); |
| 484 | close (svm_fd); |
| 485 | return (0); |
| 486 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 488 | rp = mmap ((void *) a->baseva, a->size, |
| 489 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 491 | if (rp == (svm_region_t *) MAP_FAILED) |
| 492 | { |
| 493 | clib_unix_warning ("mmap create"); |
| 494 | close (svm_fd); |
| 495 | return (0); |
| 496 | } |
| 497 | close (svm_fd); |
| 498 | memset (rp, 0, sizeof (*rp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 500 | if (pthread_mutexattr_init (&attr)) |
| 501 | clib_unix_warning ("mutexattr_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 502 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 503 | if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED)) |
| 504 | clib_unix_warning ("mutexattr_setpshared"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 506 | if (pthread_mutex_init (&rp->mutex, &attr)) |
| 507 | clib_unix_warning ("mutex_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 509 | if (pthread_mutexattr_destroy (&attr)) |
| 510 | clib_unix_warning ("mutexattr_destroy"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 512 | if (pthread_condattr_init (&cattr)) |
| 513 | clib_unix_warning ("condattr_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 515 | if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED)) |
| 516 | clib_unix_warning ("condattr_setpshared"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 518 | if (pthread_cond_init (&rp->condvar, &cattr)) |
| 519 | clib_unix_warning ("cond_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 521 | if (pthread_condattr_destroy (&cattr)) |
| 522 | clib_unix_warning ("condattr_destroy"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 524 | region_lock (rp, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 526 | rp->virtual_base = a->baseva; |
| 527 | rp->virtual_size = a->size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 528 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 529 | rp->region_heap = |
| 530 | mheap_alloc_with_flags ((void *) (a->baseva + MMAP_PAGESIZE), |
| 531 | SVM_PVT_MHEAP_SIZE, MHEAP_FLAG_DISABLE_VM); |
| 532 | oldheap = svm_push_pvt_heap (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 534 | rp->region_name = (char *) format (0, "%s%c", a->name, 0); |
| 535 | vec_add1 (rp->client_pids, getpid ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 537 | nbits = rp->virtual_size / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 538 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 539 | ASSERT (nbits > 0); |
| 540 | rp->bitmap_size = nbits; |
| 541 | words = (nbits + BITS (uword) - 1) / BITS (uword); |
| 542 | vec_validate (rp->bitmap, words - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 544 | overhead_space = MMAP_PAGESIZE /* header */ + |
| 545 | SVM_PVT_MHEAP_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 547 | bit = 0; |
| 548 | data_base = (uword) rp->virtual_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 550 | if (a->flags & SVM_FLAGS_NODATA) |
| 551 | rp->flags |= SVM_FLAGS_NEED_DATA_INIT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 553 | do |
| 554 | { |
| 555 | clib_bitmap_set_no_check (rp->bitmap, bit, 1); |
| 556 | bit++; |
| 557 | overhead_space -= MMAP_PAGESIZE; |
| 558 | data_base += MMAP_PAGESIZE; |
| 559 | } |
| 560 | while (overhead_space > 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 562 | rp->data_base = (void *) data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 564 | /* |
| 565 | * Note: although the POSIX spec guarantees that only one |
| 566 | * process enters this block, we have to play games |
| 567 | * to hold off clients until e.g. the mutex is ready |
| 568 | */ |
| 569 | rp->version = SVM_VERSION; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 570 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 571 | /* setup the data portion of the region */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 573 | rv = svm_data_region_create (a, rp); |
| 574 | if (rv) |
| 575 | { |
| 576 | clib_warning ("data_region_create: %d", rv); |
| 577 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 578 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 579 | region_unlock (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 580 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 581 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 583 | return ((void *) rp); |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 588 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 589 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 590 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 591 | if (svm_fd < 0) |
| 592 | { |
| 593 | perror ("svm_region_map(mmap open)"); |
| 594 | return (0); |
| 595 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 597 | time_left = 20; |
| 598 | while (1) |
| 599 | { |
| 600 | if (0 != fstat (svm_fd, &stat)) |
| 601 | { |
| 602 | clib_warning ("fstat failed: %d", errno); |
| 603 | close (svm_fd); |
| 604 | return (0); |
| 605 | } |
| 606 | if (stat.st_size > 0) |
| 607 | { |
| 608 | break; |
| 609 | } |
| 610 | if (0 == time_left) |
| 611 | { |
| 612 | clib_warning ("waiting for resize of shm file timed out"); |
| 613 | close (svm_fd); |
| 614 | return (0); |
| 615 | } |
| 616 | ts.tv_sec = 0; |
| 617 | ts.tv_nsec = 100000000; |
| 618 | while (nanosleep (&ts, &tsrem) < 0) |
| 619 | ts = tsrem; |
| 620 | time_left--; |
| 621 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 622 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 623 | rp = mmap (0, MMAP_PAGESIZE, |
| 624 | PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 626 | if (rp == (svm_region_t *) MAP_FAILED) |
| 627 | { |
| 628 | close (svm_fd); |
| 629 | clib_warning ("mmap"); |
| 630 | return (0); |
| 631 | } |
| 632 | /* |
| 633 | * We lost the footrace to create this region; make sure |
| 634 | * the winner has crossed the finish line. |
| 635 | */ |
| 636 | while (rp->version == 0 && deadman++ < 5) |
| 637 | { |
| 638 | sleep (1); |
| 639 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 641 | /* |
| 642 | * <bleep>-ed? |
| 643 | */ |
| 644 | if (rp->version == 0) |
| 645 | { |
| 646 | clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION); |
| 647 | close (svm_fd); |
| 648 | munmap (rp, a->size); |
| 649 | return (0); |
| 650 | } |
| 651 | /* Remap now that the region has been placed */ |
| 652 | a->baseva = rp->virtual_base; |
| 653 | a->size = rp->virtual_size; |
| 654 | munmap (rp, MMAP_PAGESIZE); |
| 655 | |
| 656 | rp = (void *) mmap ((void *) a->baseva, a->size, |
| 657 | PROT_READ | PROT_WRITE, |
| 658 | MAP_SHARED | MAP_FIXED, svm_fd, 0); |
| 659 | if ((uword) rp == (uword) MAP_FAILED) |
| 660 | { |
| 661 | clib_unix_warning ("mmap"); |
| 662 | close (svm_fd); |
| 663 | return (0); |
| 664 | } |
| 665 | |
| 666 | if ((uword) rp != rp->virtual_base) |
| 667 | { |
| 668 | clib_warning ("mmap botch"); |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Try to fix the region mutex if it is held by |
| 673 | * a dead process |
| 674 | */ |
| 675 | pid_holding_region_lock = rp->mutex_owner_pid; |
| 676 | if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0) |
| 677 | { |
| 678 | clib_warning |
| 679 | ("region %s mutex held by dead pid %d, tag %d, force unlock", |
| 680 | rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag); |
| 681 | /* owner pid is nonexistent */ |
| 682 | rp->mutex.__data.__owner = 0; |
| 683 | rp->mutex.__data.__lock = 0; |
| 684 | dead_region_recovery = 1; |
| 685 | } |
| 686 | |
| 687 | if (dead_region_recovery) |
| 688 | clib_warning ("recovery: attempt to re-lock region"); |
| 689 | |
| 690 | region_lock (rp, 2); |
| 691 | oldheap = svm_push_pvt_heap (rp); |
| 692 | vec_add1 (rp->client_pids, getpid ()); |
| 693 | |
| 694 | if (dead_region_recovery) |
| 695 | clib_warning ("recovery: attempt svm_data_region_map"); |
| 696 | |
| 697 | rv = svm_data_region_map (a, rp); |
| 698 | if (rv) |
| 699 | { |
| 700 | clib_warning ("data_region_map: %d", rv); |
| 701 | } |
| 702 | |
| 703 | if (dead_region_recovery) |
| 704 | clib_warning ("unlock and continue"); |
| 705 | |
| 706 | region_unlock (rp); |
| 707 | |
| 708 | svm_pop_heap (oldheap); |
| 709 | |
| 710 | return ((void *) rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 711 | |
| 712 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 713 | return 0; /* NOTREACHED */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 716 | static void |
| 717 | svm_mutex_cleanup (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 718 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 719 | int i; |
| 720 | for (i = 0; i < nheld; i++) |
| 721 | { |
| 722 | pthread_mutex_unlock (mutexes_held[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 726 | static void |
| 727 | svm_region_init_internal (char *root_path, int uid, int gid) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 728 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 729 | svm_region_t *rp; |
| 730 | svm_map_region_args_t _a, *a = &_a; |
| 731 | u64 ticks = clib_cpu_time_now (); |
| 732 | uword randomize_baseva; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 733 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 734 | /* guard against klutz calls */ |
| 735 | if (root_rp) |
| 736 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 738 | root_rp_refcount++; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 739 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 740 | atexit (svm_mutex_cleanup); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 741 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 742 | /* Randomize the shared-VM base at init time */ |
| 743 | if (MMAP_PAGESIZE <= (4 << 10)) |
| 744 | randomize_baseva = (ticks & 15) * MMAP_PAGESIZE; |
| 745 | else |
| 746 | randomize_baseva = (ticks & 3) * MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 747 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 748 | memset (a, 0, sizeof (*a)); |
| 749 | a->root_path = root_path; |
| 750 | a->name = SVM_GLOBAL_REGION_NAME; |
| 751 | a->baseva = SVM_GLOBAL_REGION_BASEVA + randomize_baseva; |
| 752 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 753 | a->flags = SVM_FLAGS_NODATA; |
| 754 | a->uid = uid; |
| 755 | a->gid = gid; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 756 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 757 | rp = svm_map_region (a); |
| 758 | ASSERT (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 759 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 760 | region_lock (rp, 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 762 | /* Set up the main region data structures */ |
| 763 | if (rp->flags & SVM_FLAGS_NEED_DATA_INIT) |
| 764 | { |
| 765 | svm_main_region_t *mp = 0; |
| 766 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 768 | rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 769 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 770 | oldheap = svm_push_pvt_heap (rp); |
| 771 | vec_validate (mp, 0); |
| 772 | mp->name_hash = hash_create_string (0, sizeof (uword)); |
| 773 | mp->root_path = root_path ? format (0, "%s%c", root_path, 0) : 0; |
| 774 | rp->data_base = mp; |
| 775 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 776 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 777 | region_unlock (rp); |
| 778 | root_rp = rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 781 | void |
| 782 | svm_region_init (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 784 | svm_region_init_internal (0, 0 /* uid */ , 0 /* gid */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 787 | void |
| 788 | svm_region_init_chroot (char *root_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 790 | svm_region_init_internal (root_path, 0 /* uid */ , 0 /* gid */ ); |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 793 | void |
| 794 | svm_region_init_chroot_uid_gid (char *root_path, int uid, int gid) |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 795 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 796 | svm_region_init_internal (root_path, uid, gid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 799 | void * |
| 800 | svm_region_find_or_create (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 802 | svm_main_region_t *mp; |
| 803 | svm_region_t *rp; |
| 804 | uword need_nbits; |
| 805 | int index, i; |
| 806 | void *oldheap; |
| 807 | uword *p; |
| 808 | u8 *name; |
| 809 | svm_subregion_t *subp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 810 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 811 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 812 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 813 | a->size += MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE; |
| 814 | a->size = rnd_pagesize (a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 815 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 816 | region_lock (root_rp, 4); |
| 817 | oldheap = svm_push_pvt_heap (root_rp); |
| 818 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 819 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 820 | ASSERT (mp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 821 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 822 | /* Map the named region from the correct chroot environment */ |
| 823 | a->root_path = (char *) mp->root_path; |
| 824 | |
| 825 | /* |
| 826 | * See if this region is already known. If it is, we're |
| 827 | * almost done... |
| 828 | */ |
| 829 | p = hash_get_mem (mp->name_hash, a->name); |
| 830 | |
| 831 | if (p) |
| 832 | { |
| 833 | rp = svm_map_region (a); |
| 834 | region_unlock (root_rp); |
| 835 | svm_pop_heap (oldheap); |
| 836 | return rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 837 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 838 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 839 | /* Create the region. */ |
| 840 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 841 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 842 | need_nbits = a->size / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 843 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 844 | index = 1; /* $$$ fixme, figure out how many bit to really skip */ |
| 845 | |
| 846 | /* |
| 847 | * Scan the virtual space allocation bitmap, looking for a large |
| 848 | * enough chunk |
| 849 | */ |
| 850 | do |
| 851 | { |
| 852 | if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0) |
| 853 | { |
| 854 | for (i = 0; i < (need_nbits - 1); i++) |
| 855 | { |
| 856 | if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1) |
| 857 | { |
| 858 | index = index + i; |
| 859 | goto next; |
| 860 | } |
| 861 | } |
| 862 | break; |
| 863 | } |
| 864 | index++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 865 | next:; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 866 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 867 | while (index < root_rp->bitmap_size); |
| 868 | |
| 869 | /* Completely out of VM? */ |
| 870 | if (index >= root_rp->bitmap_size) |
| 871 | { |
| 872 | clib_warning ("region %s: not enough VM to allocate 0x%x", |
| 873 | root_rp->region_name, a->size); |
| 874 | svm_pop_heap (oldheap); |
| 875 | region_unlock (root_rp); |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Mark virtual space allocated |
| 881 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 882 | #if CLIB_DEBUG > 1 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 883 | clib_warning ("set %d bits at index %d", need_nbits, index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 884 | #endif |
| 885 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 886 | for (i = 0; i < need_nbits; i++) |
| 887 | { |
| 888 | clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 891 | /* Place this region where it goes... */ |
| 892 | a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 893 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 894 | rp = svm_map_region (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 895 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 896 | pool_get (mp->subregions, subp); |
| 897 | name = format (0, "%s%c", a->name, 0); |
| 898 | subp->subregion_name = name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 899 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 900 | hash_set_mem (mp->name_hash, name, subp - mp->subregions); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 902 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 903 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 904 | region_unlock (root_rp); |
| 905 | |
| 906 | return (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* |
| 910 | * svm_region_unmap |
| 911 | * |
| 912 | * Let go of the indicated region. If the calling process |
| 913 | * is the last customer, throw it away completely. |
| 914 | * The root region mutex guarantees atomicity with respect to |
| 915 | * a new region client showing up at the wrong moment. |
| 916 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 917 | void |
| 918 | svm_region_unmap (void *rp_arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 919 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 920 | int i, mypid = getpid (); |
| 921 | int nclients_left; |
| 922 | void *oldheap; |
| 923 | uword virtual_base, virtual_size; |
| 924 | svm_region_t *rp = rp_arg; |
| 925 | char *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 926 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 927 | /* |
| 928 | * If we take a signal while holding one or more shared-memory |
| 929 | * mutexes, we may end up back here from an otherwise |
| 930 | * benign exit handler. Bail out to avoid a recursive |
| 931 | * mutex screw-up. |
| 932 | */ |
| 933 | if (nheld) |
| 934 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 935 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 936 | ASSERT (rp); |
| 937 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 938 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 939 | if (CLIB_DEBUG > 1) |
| 940 | clib_warning ("[%d] unmap region %s", getpid (), rp->region_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 941 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 942 | region_lock (root_rp, 5); |
| 943 | region_lock (rp, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 944 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 945 | oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */ |
| 946 | |
| 947 | /* Remove the caller from the list of mappers */ |
| 948 | for (i = 0; i < vec_len (rp->client_pids); i++) |
| 949 | { |
| 950 | if (rp->client_pids[i] == mypid) |
| 951 | { |
| 952 | vec_delete (rp->client_pids, 1, i); |
| 953 | goto found; |
| 954 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 955 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 956 | clib_warning ("pid %d AWOL", mypid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 957 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 958 | found: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 959 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 960 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 961 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 962 | nclients_left = vec_len (rp->client_pids); |
| 963 | virtual_base = rp->virtual_base; |
| 964 | virtual_size = rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 965 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 966 | if (nclients_left == 0) |
| 967 | { |
| 968 | int index, nbits, i; |
| 969 | svm_main_region_t *mp; |
| 970 | uword *p; |
| 971 | svm_subregion_t *subp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 972 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 973 | /* Kill the region, last guy on his way out */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 974 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 975 | oldheap = svm_push_pvt_heap (root_rp); |
| 976 | name = vec_dup (rp->region_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 977 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 978 | virtual_base = rp->virtual_base; |
| 979 | virtual_size = rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 980 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 981 | /* Figure out which bits to clear in the root region bitmap */ |
| 982 | index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 983 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 984 | nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 985 | |
| 986 | #if CLIB_DEBUG > 1 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 987 | clib_warning ("clear %d bits at index %d", nbits, index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 988 | #endif |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 989 | /* Give back the allocated VM */ |
| 990 | for (i = 0; i < nbits; i++) |
| 991 | { |
| 992 | clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0); |
| 993 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 994 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 995 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 996 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 997 | p = hash_get_mem (mp->name_hash, name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 998 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 999 | /* Better never happen ... */ |
| 1000 | if (p == NULL) |
| 1001 | { |
| 1002 | region_unlock (rp); |
| 1003 | region_unlock (root_rp); |
| 1004 | svm_pop_heap (oldheap); |
| 1005 | clib_warning ("Region name '%s' not found?", name); |
| 1006 | return; |
| 1007 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1008 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1009 | /* Remove from the root region subregion pool */ |
| 1010 | subp = mp->subregions + p[0]; |
| 1011 | pool_put (mp->subregions, subp); |
| 1012 | |
| 1013 | hash_unset_mem (mp->name_hash, name); |
| 1014 | |
| 1015 | vec_free (name); |
| 1016 | |
| 1017 | region_unlock (rp); |
| 1018 | shm_unlink (rp->region_name); |
| 1019 | munmap ((void *) virtual_base, virtual_size); |
| 1020 | region_unlock (root_rp); |
| 1021 | svm_pop_heap (oldheap); |
| 1022 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1025 | region_unlock (rp); |
| 1026 | region_unlock (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1027 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1028 | munmap ((void *) virtual_base, virtual_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * svm_region_exit |
| 1033 | * There is no clean way to unlink the |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1034 | * root region when all clients go away, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1035 | * so remove the pid entry and call it a day. |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1036 | */ |
| 1037 | void |
| 1038 | svm_region_exit () |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1039 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1040 | void *oldheap; |
| 1041 | int i, mypid = getpid (); |
| 1042 | uword virtual_base, virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1043 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1044 | /* It felt so nice we did it twice... */ |
| 1045 | if (root_rp == 0) |
| 1046 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1047 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1048 | if (--root_rp_refcount > 0) |
| 1049 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1050 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1051 | /* |
| 1052 | * If we take a signal while holding one or more shared-memory |
| 1053 | * mutexes, we may end up back here from an otherwise |
| 1054 | * benign exit handler. Bail out to avoid a recursive |
| 1055 | * mutex screw-up. |
| 1056 | */ |
| 1057 | if (nheld) |
| 1058 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1059 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1060 | region_lock (root_rp, 7); |
| 1061 | oldheap = svm_push_pvt_heap (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1062 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1063 | virtual_base = root_rp->virtual_base; |
| 1064 | virtual_size = root_rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1065 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1066 | for (i = 0; i < vec_len (root_rp->client_pids); i++) |
| 1067 | { |
| 1068 | if (root_rp->client_pids[i] == mypid) |
| 1069 | { |
| 1070 | vec_delete (root_rp->client_pids, 1, i); |
| 1071 | goto found; |
| 1072 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1073 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1074 | clib_warning ("pid %d AWOL", mypid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1075 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1076 | found: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1077 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1078 | region_unlock (root_rp); |
| 1079 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1080 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1081 | root_rp = 0; |
| 1082 | munmap ((void *) virtual_base, virtual_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1085 | void |
| 1086 | svm_client_scan_this_region_nolock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1087 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1088 | int j; |
| 1089 | int mypid = getpid (); |
| 1090 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1091 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1092 | for (j = 0; j < vec_len (rp->client_pids); j++) |
| 1093 | { |
| 1094 | if (mypid == rp->client_pids[j]) |
| 1095 | continue; |
| 1096 | if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0)) |
| 1097 | { |
| 1098 | clib_warning ("%s: cleanup ghost pid %d", |
| 1099 | rp->region_name, rp->client_pids[j]); |
| 1100 | /* nb: client vec in rp->region_heap */ |
| 1101 | oldheap = svm_push_pvt_heap (rp); |
| 1102 | vec_delete (rp->client_pids, 1, j); |
| 1103 | j--; |
| 1104 | svm_pop_heap (oldheap); |
| 1105 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1110 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1111 | * Scan svm regions for dead clients |
| 1112 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1113 | void |
| 1114 | svm_client_scan (char *root_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1115 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1116 | int i, j; |
| 1117 | svm_main_region_t *mp; |
| 1118 | svm_map_region_args_t *a = 0; |
| 1119 | svm_region_t *root_rp; |
| 1120 | svm_region_t *rp; |
| 1121 | svm_subregion_t *subp; |
| 1122 | u8 *name = 0; |
| 1123 | u8 **svm_names = 0; |
| 1124 | void *oldheap; |
| 1125 | int mypid = getpid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1126 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1127 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1128 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1129 | svm_region_init_chroot (root_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1130 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1131 | root_rp = svm_get_root_rp (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1132 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1133 | pthread_mutex_lock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1134 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1135 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1136 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1137 | for (j = 0; j < vec_len (root_rp->client_pids); j++) |
| 1138 | { |
| 1139 | if (mypid == root_rp->client_pids[j]) |
| 1140 | continue; |
| 1141 | if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0)) |
| 1142 | { |
| 1143 | clib_warning ("%s: cleanup ghost pid %d", |
| 1144 | root_rp->region_name, root_rp->client_pids[j]); |
| 1145 | /* nb: client vec in root_rp->region_heap */ |
| 1146 | oldheap = svm_push_pvt_heap (root_rp); |
| 1147 | vec_delete (root_rp->client_pids, 1, j); |
| 1148 | j--; |
| 1149 | svm_pop_heap (oldheap); |
| 1150 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1153 | /* |
| 1154 | * Snapshoot names, can't hold root rp mutex across |
| 1155 | * find_or_create. |
| 1156 | */ |
| 1157 | /* *INDENT-OFF* */ |
| 1158 | pool_foreach (subp, mp->subregions, ({ |
| 1159 | name = vec_dup (subp->subregion_name); |
| 1160 | vec_add1(svm_names, name); |
| 1161 | })); |
| 1162 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1163 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1164 | pthread_mutex_unlock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1165 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1166 | for (i = 0; i < vec_len (svm_names); i++) |
| 1167 | { |
| 1168 | vec_validate (a, 0); |
| 1169 | a->root_path = root_path; |
| 1170 | a->name = (char *) svm_names[i]; |
| 1171 | rp = svm_region_find_or_create (a); |
| 1172 | if (rp) |
| 1173 | { |
| 1174 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1175 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1176 | svm_client_scan_this_region_nolock (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1177 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1178 | pthread_mutex_unlock (&rp->mutex); |
| 1179 | svm_region_unmap (rp); |
| 1180 | vec_free (svm_names[i]); |
| 1181 | } |
| 1182 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1183 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1184 | vec_free (svm_names); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1185 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1186 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1187 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1188 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1189 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame^] | 1190 | |
| 1191 | /* |
| 1192 | * fd.io coding-style-patch-verification: ON |
| 1193 | * |
| 1194 | * Local Variables: |
| 1195 | * eval: (c-set-style "gnu") |
| 1196 | * End: |
| 1197 | */ |