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 | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 219 | static u64 |
| 220 | rnd_pagesize (u64 size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 222 | u64 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 | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 238 | map_size = rp->virtual_size - (MMAP_PAGESIZE + |
| 239 | (a->pvt_heap_size ? a->pvt_heap_size : |
| 240 | SVM_PVT_MHEAP_SIZE)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 242 | if (a->flags & SVM_FLAGS_FILE) |
| 243 | { |
| 244 | struct stat statb; |
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 | fd = open (a->backing_file, O_RDWR | O_CREAT, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 248 | if (fd < 0) |
| 249 | { |
| 250 | clib_unix_warning ("open"); |
| 251 | return -1; |
| 252 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 254 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 302 | |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 310 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 313 | static int |
| 314 | 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] | 315 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 316 | int fd; |
| 317 | u8 junk = 0; |
| 318 | uword map_size; |
| 319 | struct stat statb; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 321 | map_size = rp->virtual_size - |
| 322 | (MMAP_PAGESIZE |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 323 | + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 325 | if (a->flags & SVM_FLAGS_FILE) |
| 326 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 327 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 328 | fd = open (a->backing_file, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 330 | if (fd < 0) |
| 331 | { |
| 332 | clib_unix_warning ("open"); |
| 333 | return -1; |
| 334 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 336 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 371 | - (MMAP_PAGESIZE |
| 372 | + |
| 373 | (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE))); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 374 | |
| 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 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 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 387 | u8 * |
| 388 | 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] | 389 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 390 | 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 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 | if (a->root_path) |
| 398 | { |
| 399 | /* Tolerate present or absent slashes */ |
| 400 | if (a->root_path[0] == '/') |
| 401 | root_path_offset++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 403 | /* create the root_path under /dev/shm |
| 404 | iterate through path creating directories */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 406 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 432 | else |
| 433 | shm_name = format (0, "%s%c", a->name, 0); |
| 434 | return (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
| 438 | * svm_map_region |
| 439 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 440 | void * |
| 441 | svm_map_region (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 442 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 443 | 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 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 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
| 462 | ASSERT (a->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 464 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 466 | if (CLIB_DEBUG > 1) |
| 467 | clib_warning ("[%d] map region %s: shm_open (%s)", |
| 468 | getpid (), a->name, shm_name); |
| 469 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 470 | 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] | 471 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 472 | if (svm_fd >= 0) |
| 473 | { |
| 474 | if (fchmod (svm_fd, 0770) < 0) |
| 475 | clib_unix_warning ("segment chmod"); |
| 476 | /* This turns out to fail harmlessly if the client starts first */ |
| 477 | if (fchown (svm_fd, a->uid, a->gid) < 0) |
| 478 | clib_unix_warning ("segment chown [ok if client starts first]"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 480 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 482 | if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1) |
| 483 | { |
| 484 | clib_warning ("seek region size"); |
| 485 | close (svm_fd); |
| 486 | return (0); |
| 487 | } |
| 488 | if (write (svm_fd, &junk, 1) != 1) |
| 489 | { |
| 490 | clib_warning ("set region size"); |
| 491 | close (svm_fd); |
| 492 | return (0); |
| 493 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 495 | rp = mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 496 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 498 | if (rp == (svm_region_t *) MAP_FAILED) |
| 499 | { |
| 500 | clib_unix_warning ("mmap create"); |
| 501 | close (svm_fd); |
| 502 | return (0); |
| 503 | } |
| 504 | close (svm_fd); |
| 505 | memset (rp, 0, sizeof (*rp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 506 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 507 | if (pthread_mutexattr_init (&attr)) |
| 508 | clib_unix_warning ("mutexattr_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 510 | if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED)) |
| 511 | clib_unix_warning ("mutexattr_setpshared"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 513 | if (pthread_mutex_init (&rp->mutex, &attr)) |
| 514 | clib_unix_warning ("mutex_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 516 | if (pthread_mutexattr_destroy (&attr)) |
| 517 | clib_unix_warning ("mutexattr_destroy"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 519 | if (pthread_condattr_init (&cattr)) |
| 520 | clib_unix_warning ("condattr_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 522 | if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED)) |
| 523 | clib_unix_warning ("condattr_setpshared"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 525 | if (pthread_cond_init (&rp->condvar, &cattr)) |
| 526 | clib_unix_warning ("cond_init"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 527 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 528 | if (pthread_condattr_destroy (&cattr)) |
| 529 | clib_unix_warning ("condattr_destroy"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 530 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 531 | region_lock (rp, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 533 | rp->virtual_base = a->baseva; |
| 534 | rp->virtual_size = a->size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 536 | rp->region_heap = |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 537 | mheap_alloc_with_flags (uword_to_pointer |
| 538 | (a->baseva + MMAP_PAGESIZE, void *), |
| 539 | (a->pvt_heap_size != |
| 540 | 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 541 | MHEAP_FLAG_DISABLE_VM); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 542 | oldheap = svm_push_pvt_heap (rp); |
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 | rp->region_name = (char *) format (0, "%s%c", a->name, 0); |
| 545 | vec_add1 (rp->client_pids, getpid ()); |
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 | nbits = rp->virtual_size / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 549 | ASSERT (nbits > 0); |
| 550 | rp->bitmap_size = nbits; |
| 551 | words = (nbits + BITS (uword) - 1) / BITS (uword); |
| 552 | vec_validate (rp->bitmap, words - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 554 | overhead_space = MMAP_PAGESIZE /* header */ + |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 555 | ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 557 | bit = 0; |
| 558 | data_base = (uword) rp->virtual_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 560 | if (a->flags & SVM_FLAGS_NODATA) |
| 561 | rp->flags |= SVM_FLAGS_NEED_DATA_INIT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 563 | do |
| 564 | { |
| 565 | clib_bitmap_set_no_check (rp->bitmap, bit, 1); |
| 566 | bit++; |
| 567 | overhead_space -= MMAP_PAGESIZE; |
| 568 | data_base += MMAP_PAGESIZE; |
| 569 | } |
| 570 | while (overhead_space > 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 571 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 572 | rp->data_base = (void *) data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 574 | /* |
| 575 | * Note: although the POSIX spec guarantees that only one |
| 576 | * process enters this block, we have to play games |
| 577 | * to hold off clients until e.g. the mutex is ready |
| 578 | */ |
| 579 | rp->version = SVM_VERSION; |
Ed 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 | /* setup the data portion of the region */ |
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 | rv = svm_data_region_create (a, rp); |
| 584 | if (rv) |
| 585 | { |
| 586 | clib_warning ("data_region_create: %d", rv); |
| 587 | } |
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 | region_unlock (rp); |
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 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 593 | return ((void *) rp); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 599 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 600 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 601 | if (svm_fd < 0) |
| 602 | { |
| 603 | perror ("svm_region_map(mmap open)"); |
| 604 | return (0); |
| 605 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 606 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 607 | time_left = 20; |
| 608 | while (1) |
| 609 | { |
| 610 | if (0 != fstat (svm_fd, &stat)) |
| 611 | { |
| 612 | clib_warning ("fstat failed: %d", errno); |
| 613 | close (svm_fd); |
| 614 | return (0); |
| 615 | } |
| 616 | if (stat.st_size > 0) |
| 617 | { |
| 618 | break; |
| 619 | } |
| 620 | if (0 == time_left) |
| 621 | { |
| 622 | clib_warning ("waiting for resize of shm file timed out"); |
| 623 | close (svm_fd); |
| 624 | return (0); |
| 625 | } |
| 626 | ts.tv_sec = 0; |
| 627 | ts.tv_nsec = 100000000; |
| 628 | while (nanosleep (&ts, &tsrem) < 0) |
| 629 | ts = tsrem; |
| 630 | time_left--; |
| 631 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 632 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 633 | rp = mmap (0, MMAP_PAGESIZE, |
| 634 | PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 636 | if (rp == (svm_region_t *) MAP_FAILED) |
| 637 | { |
| 638 | close (svm_fd); |
| 639 | clib_warning ("mmap"); |
| 640 | return (0); |
| 641 | } |
| 642 | /* |
| 643 | * We lost the footrace to create this region; make sure |
| 644 | * the winner has crossed the finish line. |
| 645 | */ |
| 646 | while (rp->version == 0 && deadman++ < 5) |
| 647 | { |
| 648 | sleep (1); |
| 649 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 651 | /* |
| 652 | * <bleep>-ed? |
| 653 | */ |
| 654 | if (rp->version == 0) |
| 655 | { |
| 656 | clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION); |
| 657 | close (svm_fd); |
| 658 | munmap (rp, a->size); |
| 659 | return (0); |
| 660 | } |
| 661 | /* Remap now that the region has been placed */ |
| 662 | a->baseva = rp->virtual_base; |
| 663 | a->size = rp->virtual_size; |
| 664 | munmap (rp, MMAP_PAGESIZE); |
| 665 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 666 | rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 667 | PROT_READ | PROT_WRITE, |
| 668 | MAP_SHARED | MAP_FIXED, svm_fd, 0); |
| 669 | if ((uword) rp == (uword) MAP_FAILED) |
| 670 | { |
| 671 | clib_unix_warning ("mmap"); |
| 672 | close (svm_fd); |
| 673 | return (0); |
| 674 | } |
| 675 | |
| 676 | if ((uword) rp != rp->virtual_base) |
| 677 | { |
| 678 | clib_warning ("mmap botch"); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Try to fix the region mutex if it is held by |
| 683 | * a dead process |
| 684 | */ |
| 685 | pid_holding_region_lock = rp->mutex_owner_pid; |
| 686 | if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0) |
| 687 | { |
| 688 | clib_warning |
| 689 | ("region %s mutex held by dead pid %d, tag %d, force unlock", |
| 690 | rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag); |
| 691 | /* owner pid is nonexistent */ |
| 692 | rp->mutex.__data.__owner = 0; |
| 693 | rp->mutex.__data.__lock = 0; |
| 694 | dead_region_recovery = 1; |
| 695 | } |
| 696 | |
| 697 | if (dead_region_recovery) |
| 698 | clib_warning ("recovery: attempt to re-lock region"); |
| 699 | |
| 700 | region_lock (rp, 2); |
| 701 | oldheap = svm_push_pvt_heap (rp); |
| 702 | vec_add1 (rp->client_pids, getpid ()); |
| 703 | |
| 704 | if (dead_region_recovery) |
| 705 | clib_warning ("recovery: attempt svm_data_region_map"); |
| 706 | |
| 707 | rv = svm_data_region_map (a, rp); |
| 708 | if (rv) |
| 709 | { |
| 710 | clib_warning ("data_region_map: %d", rv); |
| 711 | } |
| 712 | |
| 713 | if (dead_region_recovery) |
| 714 | clib_warning ("unlock and continue"); |
| 715 | |
| 716 | region_unlock (rp); |
| 717 | |
| 718 | svm_pop_heap (oldheap); |
| 719 | |
| 720 | return ((void *) rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | |
| 722 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 723 | return 0; /* NOTREACHED */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 726 | static void |
| 727 | svm_mutex_cleanup (void) |
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 | int i; |
| 730 | for (i = 0; i < nheld; i++) |
| 731 | { |
| 732 | pthread_mutex_unlock (mutexes_held[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame^] | 736 | static int |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 737 | svm_region_init_internal (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 739 | svm_region_t *rp; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 740 | u64 ticks = clib_cpu_time_now (); |
| 741 | uword randomize_baseva; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 743 | /* guard against klutz calls */ |
| 744 | if (root_rp) |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame^] | 745 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 747 | root_rp_refcount++; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 748 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 749 | atexit (svm_mutex_cleanup); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 750 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 751 | /* Randomize the shared-VM base at init time */ |
| 752 | if (MMAP_PAGESIZE <= (4 << 10)) |
| 753 | randomize_baseva = (ticks & 15) * MMAP_PAGESIZE; |
| 754 | else |
| 755 | randomize_baseva = (ticks & 3) * MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 756 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 757 | a->baseva += randomize_baseva; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 759 | rp = svm_map_region (a); |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame^] | 760 | if (!rp) |
| 761 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 763 | region_lock (rp, 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 764 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 765 | /* Set up the main region data structures */ |
| 766 | if (rp->flags & SVM_FLAGS_NEED_DATA_INIT) |
| 767 | { |
| 768 | svm_main_region_t *mp = 0; |
| 769 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 770 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 771 | rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 773 | oldheap = svm_push_pvt_heap (rp); |
| 774 | vec_validate (mp, 0); |
| 775 | mp->name_hash = hash_create_string (0, sizeof (uword)); |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 776 | mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 777 | rp->data_base = mp; |
| 778 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 779 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 780 | region_unlock (rp); |
| 781 | root_rp = rp; |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame^] | 782 | |
| 783 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 786 | void |
| 787 | svm_region_init (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 789 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 790 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 791 | memset (a, 0, sizeof (*a)); |
| 792 | a->root_path = 0; |
| 793 | a->name = SVM_GLOBAL_REGION_NAME; |
| 794 | a->baseva = SVM_GLOBAL_REGION_BASEVA; |
| 795 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 796 | a->flags = SVM_FLAGS_NODATA; |
| 797 | a->uid = 0; |
| 798 | a->gid = 0; |
| 799 | |
| 800 | svm_region_init_internal (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame^] | 803 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 804 | svm_region_init_chroot (const char *root_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 805 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 806 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 807 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 808 | memset (a, 0, sizeof (*a)); |
| 809 | a->root_path = root_path; |
| 810 | a->name = SVM_GLOBAL_REGION_NAME; |
| 811 | a->baseva = SVM_GLOBAL_REGION_BASEVA; |
| 812 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 813 | a->flags = SVM_FLAGS_NODATA; |
| 814 | a->uid = 0; |
| 815 | a->gid = 0; |
| 816 | |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame^] | 817 | return svm_region_init_internal (a); |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 818 | } |
| 819 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 820 | void |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 821 | svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid) |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 822 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 823 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 824 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 825 | memset (a, 0, sizeof (*a)); |
| 826 | a->root_path = root_path; |
| 827 | a->name = SVM_GLOBAL_REGION_NAME; |
| 828 | a->baseva = SVM_GLOBAL_REGION_BASEVA; |
| 829 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 830 | a->flags = SVM_FLAGS_NODATA; |
| 831 | a->uid = uid; |
| 832 | a->gid = gid; |
| 833 | |
| 834 | svm_region_init_internal (a); |
| 835 | } |
| 836 | |
| 837 | void |
| 838 | svm_region_init_args (svm_map_region_args_t * a) |
| 839 | { |
| 840 | svm_region_init_internal (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 843 | void * |
| 844 | svm_region_find_or_create (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 845 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 846 | svm_main_region_t *mp; |
| 847 | svm_region_t *rp; |
| 848 | uword need_nbits; |
| 849 | int index, i; |
| 850 | void *oldheap; |
| 851 | uword *p; |
| 852 | u8 *name; |
| 853 | svm_subregion_t *subp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 854 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 855 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 856 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 857 | a->size += MMAP_PAGESIZE + |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 858 | ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 859 | a->size = rnd_pagesize (a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 860 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 861 | region_lock (root_rp, 4); |
| 862 | oldheap = svm_push_pvt_heap (root_rp); |
| 863 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 864 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 865 | ASSERT (mp); |
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 | /* Map the named region from the correct chroot environment */ |
Jan Srnicek | 5beec81 | 2017-03-24 10:18:11 +0100 | [diff] [blame] | 868 | if (a->root_path == NULL) |
| 869 | a->root_path = (char *) mp->root_path; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 870 | |
| 871 | /* |
| 872 | * See if this region is already known. If it is, we're |
| 873 | * almost done... |
| 874 | */ |
| 875 | p = hash_get_mem (mp->name_hash, a->name); |
| 876 | |
| 877 | if (p) |
| 878 | { |
| 879 | rp = svm_map_region (a); |
| 880 | region_unlock (root_rp); |
| 881 | svm_pop_heap (oldheap); |
| 882 | return rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 883 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 884 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 885 | /* Create the region. */ |
| 886 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 887 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 888 | need_nbits = a->size / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 889 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 890 | index = 1; /* $$$ fixme, figure out how many bit to really skip */ |
| 891 | |
| 892 | /* |
| 893 | * Scan the virtual space allocation bitmap, looking for a large |
| 894 | * enough chunk |
| 895 | */ |
| 896 | do |
| 897 | { |
| 898 | if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0) |
| 899 | { |
| 900 | for (i = 0; i < (need_nbits - 1); i++) |
| 901 | { |
| 902 | if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1) |
| 903 | { |
| 904 | index = index + i; |
| 905 | goto next; |
| 906 | } |
| 907 | } |
| 908 | break; |
| 909 | } |
| 910 | index++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 911 | next:; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 912 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 913 | while (index < root_rp->bitmap_size); |
| 914 | |
| 915 | /* Completely out of VM? */ |
| 916 | if (index >= root_rp->bitmap_size) |
| 917 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 918 | clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)", |
| 919 | root_rp->region_name, a->size, a->size); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 920 | svm_pop_heap (oldheap); |
| 921 | region_unlock (root_rp); |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Mark virtual space allocated |
| 927 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 928 | #if CLIB_DEBUG > 1 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 929 | clib_warning ("set %d bits at index %d", need_nbits, index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 930 | #endif |
| 931 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 932 | for (i = 0; i < need_nbits; i++) |
| 933 | { |
| 934 | clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 937 | /* Place this region where it goes... */ |
| 938 | a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 939 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 940 | rp = svm_map_region (a); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 941 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 942 | pool_get (mp->subregions, subp); |
| 943 | name = format (0, "%s%c", a->name, 0); |
| 944 | subp->subregion_name = name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 945 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 946 | hash_set_mem (mp->name_hash, name, subp - mp->subregions); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 947 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 948 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 949 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 950 | region_unlock (root_rp); |
| 951 | |
| 952 | return (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 955 | void |
| 956 | svm_region_unlink (svm_region_t * rp) |
| 957 | { |
| 958 | svm_map_region_args_t _a, *a = &_a; |
| 959 | svm_main_region_t *mp; |
| 960 | u8 *shm_name; |
| 961 | |
| 962 | ASSERT (root_rp); |
| 963 | ASSERT (rp); |
| 964 | ASSERT (vec_c_string_is_terminated (rp->region_name)); |
| 965 | |
| 966 | mp = root_rp->data_base; |
| 967 | ASSERT (mp); |
| 968 | |
| 969 | a->root_path = (char *) mp->root_path; |
| 970 | a->name = rp->region_name; |
| 971 | shm_name = shm_name_from_svm_map_region_args (a); |
| 972 | if (CLIB_DEBUG > 1) |
| 973 | clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name); |
| 974 | shm_unlink ((const char *) shm_name); |
| 975 | vec_free (shm_name); |
| 976 | } |
| 977 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 978 | /* |
| 979 | * svm_region_unmap |
| 980 | * |
| 981 | * Let go of the indicated region. If the calling process |
| 982 | * is the last customer, throw it away completely. |
| 983 | * The root region mutex guarantees atomicity with respect to |
| 984 | * a new region client showing up at the wrong moment. |
| 985 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 986 | void |
| 987 | svm_region_unmap (void *rp_arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 988 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 989 | int i, mypid = getpid (); |
| 990 | int nclients_left; |
| 991 | void *oldheap; |
| 992 | uword virtual_base, virtual_size; |
| 993 | svm_region_t *rp = rp_arg; |
| 994 | char *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 995 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 996 | /* |
| 997 | * If we take a signal while holding one or more shared-memory |
| 998 | * mutexes, we may end up back here from an otherwise |
| 999 | * benign exit handler. Bail out to avoid a recursive |
| 1000 | * mutex screw-up. |
| 1001 | */ |
| 1002 | if (nheld) |
| 1003 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1004 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1005 | ASSERT (rp); |
| 1006 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1007 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1008 | if (CLIB_DEBUG > 1) |
| 1009 | clib_warning ("[%d] unmap region %s", getpid (), rp->region_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1010 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1011 | region_lock (root_rp, 5); |
| 1012 | region_lock (rp, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1013 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1014 | oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */ |
| 1015 | |
| 1016 | /* Remove the caller from the list of mappers */ |
| 1017 | for (i = 0; i < vec_len (rp->client_pids); i++) |
| 1018 | { |
| 1019 | if (rp->client_pids[i] == mypid) |
| 1020 | { |
| 1021 | vec_delete (rp->client_pids, 1, i); |
| 1022 | goto found; |
| 1023 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1024 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1025 | clib_warning ("pid %d AWOL", mypid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1026 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1027 | found: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1028 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1029 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1030 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1031 | nclients_left = vec_len (rp->client_pids); |
| 1032 | virtual_base = rp->virtual_base; |
| 1033 | virtual_size = rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1034 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1035 | if (nclients_left == 0) |
| 1036 | { |
| 1037 | int index, nbits, i; |
| 1038 | svm_main_region_t *mp; |
| 1039 | uword *p; |
| 1040 | svm_subregion_t *subp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1041 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1042 | /* Kill the region, last guy on his way out */ |
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 | oldheap = svm_push_pvt_heap (root_rp); |
| 1045 | name = vec_dup (rp->region_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1046 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1047 | virtual_base = rp->virtual_base; |
| 1048 | virtual_size = rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1049 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1050 | /* Figure out which bits to clear in the root region bitmap */ |
| 1051 | index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1052 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1053 | nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1054 | |
| 1055 | #if CLIB_DEBUG > 1 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1056 | clib_warning ("clear %d bits at index %d", nbits, index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1057 | #endif |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1058 | /* Give back the allocated VM */ |
| 1059 | for (i = 0; i < nbits; i++) |
| 1060 | { |
| 1061 | clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0); |
| 1062 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1063 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1064 | mp = root_rp->data_base; |
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 | p = hash_get_mem (mp->name_hash, name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1067 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1068 | /* Better never happen ... */ |
| 1069 | if (p == NULL) |
| 1070 | { |
| 1071 | region_unlock (rp); |
| 1072 | region_unlock (root_rp); |
| 1073 | svm_pop_heap (oldheap); |
| 1074 | clib_warning ("Region name '%s' not found?", name); |
| 1075 | return; |
| 1076 | } |
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 | /* Remove from the root region subregion pool */ |
| 1079 | subp = mp->subregions + p[0]; |
| 1080 | pool_put (mp->subregions, subp); |
| 1081 | |
| 1082 | hash_unset_mem (mp->name_hash, name); |
| 1083 | |
| 1084 | vec_free (name); |
| 1085 | |
| 1086 | region_unlock (rp); |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 1087 | svm_region_unlink (rp); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1088 | munmap ((void *) virtual_base, virtual_size); |
| 1089 | region_unlock (root_rp); |
| 1090 | svm_pop_heap (oldheap); |
| 1091 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1094 | region_unlock (rp); |
| 1095 | region_unlock (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1096 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1097 | munmap ((void *) virtual_base, virtual_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * svm_region_exit |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1102 | */ |
| 1103 | void |
| 1104 | svm_region_exit () |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1105 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1106 | void *oldheap; |
| 1107 | int i, mypid = getpid (); |
| 1108 | uword virtual_base, virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1109 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1110 | /* It felt so nice we did it twice... */ |
| 1111 | if (root_rp == 0) |
| 1112 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1113 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1114 | if (--root_rp_refcount > 0) |
| 1115 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1116 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1117 | /* |
| 1118 | * If we take a signal while holding one or more shared-memory |
| 1119 | * mutexes, we may end up back here from an otherwise |
| 1120 | * benign exit handler. Bail out to avoid a recursive |
| 1121 | * mutex screw-up. |
| 1122 | */ |
| 1123 | if (nheld) |
| 1124 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1125 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1126 | region_lock (root_rp, 7); |
| 1127 | oldheap = svm_push_pvt_heap (root_rp); |
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 | virtual_base = root_rp->virtual_base; |
| 1130 | virtual_size = root_rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1131 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1132 | for (i = 0; i < vec_len (root_rp->client_pids); i++) |
| 1133 | { |
| 1134 | if (root_rp->client_pids[i] == mypid) |
| 1135 | { |
| 1136 | vec_delete (root_rp->client_pids, 1, i); |
| 1137 | goto found; |
| 1138 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1139 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1140 | clib_warning ("pid %d AWOL", mypid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1141 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1142 | found: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1143 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 1144 | if (vec_len (root_rp->client_pids) == 0) |
| 1145 | svm_region_unlink (root_rp); |
| 1146 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1147 | region_unlock (root_rp); |
| 1148 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1149 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1150 | root_rp = 0; |
| 1151 | munmap ((void *) virtual_base, virtual_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1154 | void |
| 1155 | svm_client_scan_this_region_nolock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1156 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1157 | int j; |
| 1158 | int mypid = getpid (); |
| 1159 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1160 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1161 | for (j = 0; j < vec_len (rp->client_pids); j++) |
| 1162 | { |
| 1163 | if (mypid == rp->client_pids[j]) |
| 1164 | continue; |
| 1165 | if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0)) |
| 1166 | { |
| 1167 | clib_warning ("%s: cleanup ghost pid %d", |
| 1168 | rp->region_name, rp->client_pids[j]); |
| 1169 | /* nb: client vec in rp->region_heap */ |
| 1170 | oldheap = svm_push_pvt_heap (rp); |
| 1171 | vec_delete (rp->client_pids, 1, j); |
| 1172 | j--; |
| 1173 | svm_pop_heap (oldheap); |
| 1174 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1179 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1180 | * Scan svm regions for dead clients |
| 1181 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1182 | void |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 1183 | svm_client_scan (const char *root_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1184 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1185 | int i, j; |
| 1186 | svm_main_region_t *mp; |
| 1187 | svm_map_region_args_t *a = 0; |
| 1188 | svm_region_t *root_rp; |
| 1189 | svm_region_t *rp; |
| 1190 | svm_subregion_t *subp; |
| 1191 | u8 *name = 0; |
| 1192 | u8 **svm_names = 0; |
| 1193 | void *oldheap; |
| 1194 | int mypid = getpid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1195 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1196 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1197 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1198 | svm_region_init_chroot (root_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1199 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1200 | root_rp = svm_get_root_rp (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1201 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1202 | pthread_mutex_lock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1203 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1204 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1205 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1206 | for (j = 0; j < vec_len (root_rp->client_pids); j++) |
| 1207 | { |
| 1208 | if (mypid == root_rp->client_pids[j]) |
| 1209 | continue; |
| 1210 | if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0)) |
| 1211 | { |
| 1212 | clib_warning ("%s: cleanup ghost pid %d", |
| 1213 | root_rp->region_name, root_rp->client_pids[j]); |
| 1214 | /* nb: client vec in root_rp->region_heap */ |
| 1215 | oldheap = svm_push_pvt_heap (root_rp); |
| 1216 | vec_delete (root_rp->client_pids, 1, j); |
| 1217 | j--; |
| 1218 | svm_pop_heap (oldheap); |
| 1219 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1222 | /* |
| 1223 | * Snapshoot names, can't hold root rp mutex across |
| 1224 | * find_or_create. |
| 1225 | */ |
| 1226 | /* *INDENT-OFF* */ |
| 1227 | pool_foreach (subp, mp->subregions, ({ |
| 1228 | name = vec_dup (subp->subregion_name); |
| 1229 | vec_add1(svm_names, name); |
| 1230 | })); |
| 1231 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1232 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1233 | pthread_mutex_unlock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1234 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1235 | for (i = 0; i < vec_len (svm_names); i++) |
| 1236 | { |
| 1237 | vec_validate (a, 0); |
| 1238 | a->root_path = root_path; |
| 1239 | a->name = (char *) svm_names[i]; |
| 1240 | rp = svm_region_find_or_create (a); |
| 1241 | if (rp) |
| 1242 | { |
| 1243 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1244 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1245 | svm_client_scan_this_region_nolock (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1246 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1247 | pthread_mutex_unlock (&rp->mutex); |
| 1248 | svm_region_unmap (rp); |
| 1249 | vec_free (svm_names[i]); |
| 1250 | } |
| 1251 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1252 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1253 | vec_free (svm_names); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1254 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1255 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1256 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1257 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1258 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1259 | |
| 1260 | /* |
| 1261 | * fd.io coding-style-patch-verification: ON |
| 1262 | * |
| 1263 | * Local Variables: |
| 1264 | * eval: (c-set-style "gnu") |
| 1265 | * End: |
| 1266 | */ |