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