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 | |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 61 | u64 |
| 62 | svm_get_global_region_base_va () |
| 63 | { |
| 64 | #if __aarch64__ |
| 65 | /* On AArch64 VA space can have different size, from 36 to 48 bits. |
| 66 | Here we are trying to detect VA bits by parsing /proc/self/maps |
| 67 | address ranges */ |
| 68 | int fd; |
| 69 | unformat_input_t input; |
| 70 | u64 start, end = 0; |
| 71 | u8 bits = 0; |
| 72 | |
| 73 | if ((fd = open ("/proc/self/maps", 0)) < 0) |
| 74 | clib_unix_error ("open '/proc/self/maps'"); |
| 75 | |
| 76 | unformat_init_clib_file (&input, fd); |
| 77 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 78 | { |
Gabriel Ganne | c5239ad | 2018-01-11 15:04:19 +0100 | [diff] [blame] | 79 | if (unformat (&input, "%llx-%llx", &start, &end)) |
| 80 | end--; |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 81 | unformat_skip_line (&input); |
| 82 | } |
Gabriel Ganne | 83d4743 | 2018-01-10 11:40:50 +0100 | [diff] [blame] | 83 | unformat_free (&input); |
| 84 | close (fd); |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 85 | |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 86 | bits = count_leading_zeros (end); |
Gabriel Ganne | c5239ad | 2018-01-11 15:04:19 +0100 | [diff] [blame] | 87 | bits = 64 - bits; |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 88 | if (bits >= 36 && bits <= 48) |
| 89 | return ((1ul << bits) / 4) - (2 * SVM_GLOBAL_REGION_SIZE); |
| 90 | else |
| 91 | clib_unix_error ("unexpected va bits '%u'", bits); |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 92 | #endif |
| 93 | |
| 94 | /* default value */ |
| 95 | return 0x30000000; |
| 96 | } |
| 97 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 98 | static void |
| 99 | region_lock (svm_region_t * rp, int tag) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 101 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 103 | rp->mutex_owner_pid = getpid (); |
| 104 | rp->mutex_owner_tag = tag; |
| 105 | #endif |
| 106 | ASSERT (nheld < MAXLOCK); |
| 107 | /* |
| 108 | * Keep score of held mutexes so we can try to exit |
| 109 | * cleanly if the world comes to an end at the worst possible |
| 110 | * moment |
| 111 | */ |
| 112 | mutexes_held[nheld++] = &rp->mutex; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 115 | static void |
| 116 | region_unlock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 118 | int i, j; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 120 | rp->mutex_owner_pid = 0; |
| 121 | rp->mutex_owner_tag = 0; |
| 122 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 124 | for (i = nheld - 1; i >= 0; i--) |
| 125 | { |
| 126 | if (mutexes_held[i] == &rp->mutex) |
| 127 | { |
| 128 | for (j = i; j < MAXLOCK - 1; j++) |
| 129 | mutexes_held[j] = mutexes_held[j + 1]; |
| 130 | nheld--; |
| 131 | goto found; |
| 132 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 134 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
| 136 | found: |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 137 | CLIB_MEMORY_BARRIER (); |
| 138 | pthread_mutex_unlock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 142 | static u8 * |
| 143 | format_svm_flags (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 | uword f = va_arg (*args, uword); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 147 | if (f & SVM_FLAGS_MHEAP) |
| 148 | s = format (s, "MHEAP "); |
| 149 | if (f & SVM_FLAGS_FILE) |
| 150 | s = format (s, "FILE "); |
| 151 | if (f & SVM_FLAGS_NODATA) |
| 152 | s = format (s, "NODATA "); |
| 153 | if (f & SVM_FLAGS_NEED_DATA_INIT) |
| 154 | s = format (s, "INIT "); |
| 155 | |
| 156 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 159 | static u8 * |
| 160 | format_svm_size (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 162 | uword size = va_arg (*args, uword); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 164 | if (size >= (1 << 20)) |
| 165 | { |
| 166 | s = format (s, "(%d mb)", size >> 20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 168 | else if (size >= (1 << 10)) |
| 169 | { |
| 170 | s = format (s, "(%d kb)", size >> 10); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | s = format (s, "(%d bytes)", size); |
| 175 | } |
| 176 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 179 | u8 * |
| 180 | format_svm_region (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 182 | svm_region_t *rp = va_arg (*args, svm_region_t *); |
| 183 | int verbose = va_arg (*args, int); |
| 184 | int i; |
| 185 | uword lo, hi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 187 | s = format (s, "%s: base va 0x%x size 0x%x %U\n", |
| 188 | rp->region_name, rp->virtual_base, |
| 189 | rp->virtual_size, format_svm_size, rp->virtual_size); |
| 190 | s = format (s, " user_ctx 0x%x, bitmap_size %d\n", |
| 191 | rp->user_ctx, rp->bitmap_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 193 | if (verbose) |
| 194 | { |
| 195 | s = format (s, " flags: 0x%x %U\n", rp->flags, |
| 196 | format_svm_flags, rp->flags); |
| 197 | s = format (s, |
| 198 | " region_heap 0x%x data_base 0x%x data_heap 0x%x\n", |
| 199 | rp->region_heap, rp->data_base, rp->data_heap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 202 | s = format (s, " %d clients, pids: ", vec_len (rp->client_pids)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 204 | for (i = 0; i < vec_len (rp->client_pids); i++) |
| 205 | s = format (s, "%d ", rp->client_pids[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 207 | s = format (s, "\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 209 | if (verbose) |
| 210 | { |
| 211 | lo = hi = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 213 | s = format (s, " VM in use: "); |
| 214 | |
| 215 | for (i = 0; i < rp->bitmap_size; i++) |
| 216 | { |
| 217 | if (clib_bitmap_get_no_check (rp->bitmap, i) != 0) |
| 218 | { |
| 219 | if (lo == ~0) |
| 220 | { |
| 221 | hi = lo = rp->virtual_base + i * MMAP_PAGESIZE; |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | hi = rp->virtual_base + i * MMAP_PAGESIZE; |
| 226 | } |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | if (lo != ~0) |
| 231 | { |
| 232 | hi = rp->virtual_base + i * MMAP_PAGESIZE - 1; |
| 233 | s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi, |
| 234 | (hi - lo) >> 10); |
| 235 | lo = hi = ~0; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | s = format (s, " rgn heap stats: %U", format_mheap, |
| 240 | rp->region_heap, 0); |
| 241 | if ((rp->flags & SVM_FLAGS_MHEAP) && rp->data_heap) |
| 242 | { |
| 243 | s = format (s, "\n data heap stats: %U", format_mheap, |
| 244 | rp->data_heap, 1); |
| 245 | } |
| 246 | s = format (s, "\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 249 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * rnd_pagesize |
| 254 | * Round to a pagesize multiple, presumably 4k works |
| 255 | */ |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 256 | static u64 |
| 257 | rnd_pagesize (u64 size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 259 | u64 rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 261 | rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1); |
| 262 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 265 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | * svm_data_region_setup |
| 267 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 268 | static int |
| 269 | 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] | 270 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 271 | int fd; |
| 272 | u8 junk = 0; |
| 273 | uword map_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 275 | map_size = rp->virtual_size - (MMAP_PAGESIZE + |
| 276 | (a->pvt_heap_size ? a->pvt_heap_size : |
| 277 | SVM_PVT_MHEAP_SIZE)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 279 | if (a->flags & SVM_FLAGS_FILE) |
| 280 | { |
| 281 | struct stat statb; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 283 | fd = open (a->backing_file, O_RDWR | O_CREAT, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 285 | if (fd < 0) |
| 286 | { |
| 287 | clib_unix_warning ("open"); |
| 288 | return -1; |
| 289 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 291 | if (fstat (fd, &statb) < 0) |
| 292 | { |
| 293 | clib_unix_warning ("fstat"); |
| 294 | close (fd); |
| 295 | return -2; |
| 296 | } |
| 297 | |
| 298 | if (statb.st_mode & S_IFREG) |
| 299 | { |
| 300 | if (statb.st_size == 0) |
| 301 | { |
| 302 | if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1) |
| 303 | { |
| 304 | clib_unix_warning ("seek region size"); |
| 305 | close (fd); |
| 306 | return -3; |
| 307 | } |
| 308 | if (write (fd, &junk, 1) != 1) |
| 309 | { |
| 310 | clib_unix_warning ("set region size"); |
| 311 | close (fd); |
| 312 | return -3; |
| 313 | } |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | map_size = rnd_pagesize (statb.st_size); |
| 318 | } |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | map_size = a->backing_mmap_size; |
| 323 | } |
| 324 | |
| 325 | ASSERT (map_size <= rp->virtual_size - |
| 326 | (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE)); |
| 327 | |
| 328 | if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE, |
| 329 | MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED) |
| 330 | { |
| 331 | clib_unix_warning ("mmap"); |
| 332 | close (fd); |
| 333 | return -3; |
| 334 | } |
| 335 | close (fd); |
| 336 | rp->backing_file = (char *) format (0, "%s\0", a->backing_file); |
| 337 | rp->flags |= SVM_FLAGS_FILE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 339 | |
| 340 | if (a->flags & SVM_FLAGS_MHEAP) |
| 341 | { |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 342 | mheap_t *heap_header; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 343 | rp->data_heap = |
| 344 | mheap_alloc_with_flags ((void *) (rp->data_base), map_size, |
| 345 | MHEAP_FLAG_DISABLE_VM); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 346 | heap_header = mheap_header (rp->data_heap); |
| 347 | heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 348 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 349 | rp->flags |= SVM_FLAGS_MHEAP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 351 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 354 | static int |
| 355 | 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] | 356 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 357 | int fd; |
| 358 | u8 junk = 0; |
| 359 | uword map_size; |
| 360 | struct stat statb; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 362 | map_size = rp->virtual_size - |
| 363 | (MMAP_PAGESIZE |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 364 | + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 366 | if (a->flags & SVM_FLAGS_FILE) |
| 367 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 369 | fd = open (a->backing_file, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 371 | if (fd < 0) |
| 372 | { |
| 373 | clib_unix_warning ("open"); |
| 374 | return -1; |
| 375 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 377 | if (fstat (fd, &statb) < 0) |
| 378 | { |
| 379 | clib_unix_warning ("fstat"); |
| 380 | close (fd); |
| 381 | return -2; |
| 382 | } |
| 383 | |
| 384 | if (statb.st_mode & S_IFREG) |
| 385 | { |
| 386 | if (statb.st_size == 0) |
| 387 | { |
| 388 | if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1) |
| 389 | { |
| 390 | clib_unix_warning ("seek region size"); |
| 391 | close (fd); |
| 392 | return -3; |
| 393 | } |
| 394 | if (write (fd, &junk, 1) != 1) |
| 395 | { |
| 396 | clib_unix_warning ("set region size"); |
| 397 | close (fd); |
| 398 | return -3; |
| 399 | } |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | map_size = rnd_pagesize (statb.st_size); |
| 404 | } |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | map_size = a->backing_mmap_size; |
| 409 | } |
| 410 | |
| 411 | ASSERT (map_size <= rp->virtual_size |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 412 | - (MMAP_PAGESIZE |
| 413 | + |
| 414 | (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE))); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 415 | |
| 416 | if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE, |
| 417 | MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED) |
| 418 | { |
| 419 | clib_unix_warning ("mmap"); |
| 420 | close (fd); |
| 421 | return -3; |
| 422 | } |
| 423 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 425 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 428 | u8 * |
| 429 | 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] | 430 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 431 | u8 *path; |
| 432 | u8 *shm_name; |
| 433 | u8 *split_point; |
| 434 | u8 *mkdir_arg = 0; |
| 435 | int root_path_offset = 0; |
| 436 | int name_offset = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 438 | if (a->root_path) |
| 439 | { |
| 440 | /* Tolerate present or absent slashes */ |
| 441 | if (a->root_path[0] == '/') |
| 442 | root_path_offset++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 444 | /* create the root_path under /dev/shm |
| 445 | iterate through path creating directories */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 447 | path = format (0, "/dev/shm/%s%c", &a->root_path[root_path_offset], 0); |
| 448 | split_point = path + 1; |
| 449 | vec_add1 (mkdir_arg, '-'); |
| 450 | |
| 451 | while (*split_point) |
| 452 | { |
| 453 | while (*split_point && *split_point != '/') |
| 454 | { |
| 455 | vec_add1 (mkdir_arg, *split_point); |
| 456 | split_point++; |
| 457 | } |
| 458 | vec_add1 (mkdir_arg, 0); |
| 459 | |
| 460 | /* ready to descend another level */ |
| 461 | mkdir_arg[vec_len (mkdir_arg) - 1] = '-'; |
| 462 | split_point++; |
| 463 | } |
| 464 | vec_free (mkdir_arg); |
| 465 | vec_free (path); |
| 466 | |
| 467 | if (a->name[0] == '/') |
| 468 | name_offset = 1; |
| 469 | |
Matej Perina | d135c19 | 2017-07-18 13:59:41 +0200 | [diff] [blame] | 470 | shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset], |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 471 | &a->name[name_offset], 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 473 | else |
| 474 | shm_name = format (0, "%s%c", a->name, 0); |
| 475 | return (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 478 | void |
| 479 | svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp) |
| 480 | { |
| 481 | pthread_mutexattr_t attr; |
| 482 | pthread_condattr_t cattr; |
| 483 | int nbits, words, bit; |
| 484 | int overhead_space; |
| 485 | void *oldheap; |
| 486 | uword data_base; |
| 487 | ASSERT (rp); |
| 488 | int rv; |
| 489 | |
| 490 | memset (rp, 0, sizeof (*rp)); |
| 491 | |
| 492 | if (pthread_mutexattr_init (&attr)) |
| 493 | clib_unix_warning ("mutexattr_init"); |
| 494 | |
| 495 | if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED)) |
| 496 | clib_unix_warning ("mutexattr_setpshared"); |
| 497 | |
| 498 | if (pthread_mutex_init (&rp->mutex, &attr)) |
| 499 | clib_unix_warning ("mutex_init"); |
| 500 | |
| 501 | if (pthread_mutexattr_destroy (&attr)) |
| 502 | clib_unix_warning ("mutexattr_destroy"); |
| 503 | |
| 504 | if (pthread_condattr_init (&cattr)) |
| 505 | clib_unix_warning ("condattr_init"); |
| 506 | |
| 507 | if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED)) |
| 508 | clib_unix_warning ("condattr_setpshared"); |
| 509 | |
| 510 | if (pthread_cond_init (&rp->condvar, &cattr)) |
| 511 | clib_unix_warning ("cond_init"); |
| 512 | |
| 513 | if (pthread_condattr_destroy (&cattr)) |
| 514 | clib_unix_warning ("condattr_destroy"); |
| 515 | |
| 516 | region_lock (rp, 1); |
| 517 | |
| 518 | rp->virtual_base = a->baseva; |
| 519 | rp->virtual_size = a->size; |
| 520 | |
| 521 | rp->region_heap = |
| 522 | mheap_alloc_with_flags (uword_to_pointer |
| 523 | (a->baseva + MMAP_PAGESIZE, void *), |
| 524 | (a->pvt_heap_size != |
| 525 | 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, |
| 526 | MHEAP_FLAG_DISABLE_VM); |
| 527 | oldheap = svm_push_pvt_heap (rp); |
| 528 | |
| 529 | rp->region_name = (char *) format (0, "%s%c", a->name, 0); |
| 530 | vec_add1 (rp->client_pids, getpid ()); |
| 531 | |
| 532 | nbits = rp->virtual_size / MMAP_PAGESIZE; |
| 533 | |
| 534 | ASSERT (nbits > 0); |
| 535 | rp->bitmap_size = nbits; |
| 536 | words = (nbits + BITS (uword) - 1) / BITS (uword); |
| 537 | vec_validate (rp->bitmap, words - 1); |
| 538 | |
| 539 | overhead_space = MMAP_PAGESIZE /* header */ + |
| 540 | ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE); |
| 541 | |
| 542 | bit = 0; |
| 543 | data_base = (uword) rp->virtual_base; |
| 544 | |
| 545 | if (a->flags & SVM_FLAGS_NODATA) |
| 546 | rp->flags |= SVM_FLAGS_NEED_DATA_INIT; |
| 547 | |
| 548 | do |
| 549 | { |
| 550 | clib_bitmap_set_no_check (rp->bitmap, bit, 1); |
| 551 | bit++; |
| 552 | overhead_space -= MMAP_PAGESIZE; |
| 553 | data_base += MMAP_PAGESIZE; |
| 554 | } |
| 555 | while (overhead_space > 0); |
| 556 | |
| 557 | rp->data_base = (void *) data_base; |
| 558 | |
| 559 | /* |
| 560 | * Note: although the POSIX spec guarantees that only one |
| 561 | * process enters this block, we have to play games |
| 562 | * to hold off clients until e.g. the mutex is ready |
| 563 | */ |
| 564 | rp->version = SVM_VERSION; |
| 565 | |
| 566 | /* setup the data portion of the region */ |
| 567 | |
| 568 | rv = svm_data_region_create (a, rp); |
| 569 | if (rv) |
| 570 | { |
| 571 | clib_warning ("data_region_create: %d", rv); |
| 572 | } |
| 573 | |
| 574 | region_unlock (rp); |
| 575 | |
| 576 | svm_pop_heap (oldheap); |
| 577 | } |
| 578 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 579 | /* |
| 580 | * svm_map_region |
| 581 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 582 | void * |
| 583 | svm_map_region (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 584 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 585 | int svm_fd; |
| 586 | svm_region_t *rp; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 587 | int deadman = 0; |
| 588 | u8 junk = 0; |
| 589 | void *oldheap; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 590 | int rv; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 591 | int pid_holding_region_lock; |
| 592 | u8 *shm_name; |
| 593 | int dead_region_recovery = 0; |
| 594 | int time_left; |
| 595 | struct stat stat; |
| 596 | struct timespec ts, tsrem; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 597 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 598 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
| 599 | ASSERT (a->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 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 603 | if (CLIB_DEBUG > 1) |
| 604 | clib_warning ("[%d] map region %s: shm_open (%s)", |
| 605 | getpid (), a->name, shm_name); |
| 606 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 607 | 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] | 608 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 609 | if (svm_fd >= 0) |
| 610 | { |
Dave Wallace | 1929611 | 2017-08-31 15:54:11 -0400 | [diff] [blame] | 611 | if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 612 | clib_unix_warning ("segment chmod"); |
| 613 | /* This turns out to fail harmlessly if the client starts first */ |
| 614 | if (fchown (svm_fd, a->uid, a->gid) < 0) |
| 615 | clib_unix_warning ("segment chown [ok if client starts first]"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 617 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 619 | if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1) |
| 620 | { |
| 621 | clib_warning ("seek region size"); |
| 622 | close (svm_fd); |
| 623 | return (0); |
| 624 | } |
| 625 | if (write (svm_fd, &junk, 1) != 1) |
| 626 | { |
| 627 | clib_warning ("set region size"); |
| 628 | close (svm_fd); |
| 629 | return (0); |
| 630 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 632 | rp = mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 633 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 634 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 635 | if (rp == (svm_region_t *) MAP_FAILED) |
| 636 | { |
| 637 | clib_unix_warning ("mmap create"); |
| 638 | close (svm_fd); |
| 639 | return (0); |
| 640 | } |
| 641 | close (svm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 642 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 643 | svm_region_init_mapped_region (a, rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 645 | return ((void *) rp); |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
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 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 653 | if (svm_fd < 0) |
| 654 | { |
| 655 | perror ("svm_region_map(mmap open)"); |
| 656 | return (0); |
| 657 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | |
Ole Troan | c4f2ef7 | 2018-05-30 22:43:25 +0200 | [diff] [blame] | 659 | /* Reset ownership in case the client started first */ |
| 660 | if (fchown (svm_fd, a->uid, a->gid) < 0) |
| 661 | clib_unix_warning ("segment chown [ok if client starts first]"); |
| 662 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 663 | time_left = 20; |
| 664 | while (1) |
| 665 | { |
| 666 | if (0 != fstat (svm_fd, &stat)) |
| 667 | { |
| 668 | clib_warning ("fstat failed: %d", errno); |
| 669 | close (svm_fd); |
| 670 | return (0); |
| 671 | } |
| 672 | if (stat.st_size > 0) |
| 673 | { |
| 674 | break; |
| 675 | } |
| 676 | if (0 == time_left) |
| 677 | { |
| 678 | clib_warning ("waiting for resize of shm file timed out"); |
| 679 | close (svm_fd); |
| 680 | return (0); |
| 681 | } |
| 682 | ts.tv_sec = 0; |
| 683 | ts.tv_nsec = 100000000; |
| 684 | while (nanosleep (&ts, &tsrem) < 0) |
| 685 | ts = tsrem; |
| 686 | time_left--; |
| 687 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 688 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 689 | rp = mmap (0, MMAP_PAGESIZE, |
| 690 | PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 691 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 692 | if (rp == (svm_region_t *) MAP_FAILED) |
| 693 | { |
| 694 | close (svm_fd); |
| 695 | clib_warning ("mmap"); |
| 696 | return (0); |
| 697 | } |
| 698 | /* |
| 699 | * We lost the footrace to create this region; make sure |
| 700 | * the winner has crossed the finish line. |
| 701 | */ |
| 702 | while (rp->version == 0 && deadman++ < 5) |
| 703 | { |
| 704 | sleep (1); |
| 705 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 707 | /* |
| 708 | * <bleep>-ed? |
| 709 | */ |
| 710 | if (rp->version == 0) |
| 711 | { |
| 712 | clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION); |
| 713 | close (svm_fd); |
| 714 | munmap (rp, a->size); |
| 715 | return (0); |
| 716 | } |
| 717 | /* Remap now that the region has been placed */ |
| 718 | a->baseva = rp->virtual_base; |
| 719 | a->size = rp->virtual_size; |
| 720 | munmap (rp, MMAP_PAGESIZE); |
| 721 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 722 | rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 723 | PROT_READ | PROT_WRITE, |
| 724 | MAP_SHARED | MAP_FIXED, svm_fd, 0); |
| 725 | if ((uword) rp == (uword) MAP_FAILED) |
| 726 | { |
| 727 | clib_unix_warning ("mmap"); |
| 728 | close (svm_fd); |
| 729 | return (0); |
| 730 | } |
| 731 | |
Dave Barach | ada24ea | 2018-05-24 17:32:00 -0400 | [diff] [blame] | 732 | close (svm_fd); |
| 733 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 734 | if ((uword) rp != rp->virtual_base) |
| 735 | { |
| 736 | clib_warning ("mmap botch"); |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * Try to fix the region mutex if it is held by |
| 741 | * a dead process |
| 742 | */ |
| 743 | pid_holding_region_lock = rp->mutex_owner_pid; |
| 744 | if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0) |
| 745 | { |
| 746 | clib_warning |
| 747 | ("region %s mutex held by dead pid %d, tag %d, force unlock", |
| 748 | rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag); |
| 749 | /* owner pid is nonexistent */ |
| 750 | rp->mutex.__data.__owner = 0; |
| 751 | rp->mutex.__data.__lock = 0; |
| 752 | dead_region_recovery = 1; |
| 753 | } |
| 754 | |
| 755 | if (dead_region_recovery) |
| 756 | clib_warning ("recovery: attempt to re-lock region"); |
| 757 | |
| 758 | region_lock (rp, 2); |
| 759 | oldheap = svm_push_pvt_heap (rp); |
| 760 | vec_add1 (rp->client_pids, getpid ()); |
| 761 | |
| 762 | if (dead_region_recovery) |
| 763 | clib_warning ("recovery: attempt svm_data_region_map"); |
| 764 | |
| 765 | rv = svm_data_region_map (a, rp); |
| 766 | if (rv) |
| 767 | { |
| 768 | clib_warning ("data_region_map: %d", rv); |
| 769 | } |
| 770 | |
| 771 | if (dead_region_recovery) |
| 772 | clib_warning ("unlock and continue"); |
| 773 | |
| 774 | region_unlock (rp); |
| 775 | |
| 776 | svm_pop_heap (oldheap); |
| 777 | |
| 778 | return ((void *) 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 | return 0; /* NOTREACHED */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 784 | static void |
| 785 | svm_mutex_cleanup (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 786 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 787 | int i; |
| 788 | for (i = 0; i < nheld; i++) |
| 789 | { |
| 790 | pthread_mutex_unlock (mutexes_held[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 794 | static int |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 795 | svm_region_init_internal (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 796 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 797 | svm_region_t *rp; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 798 | u64 ticks = clib_cpu_time_now (); |
| 799 | uword randomize_baseva; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 800 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 801 | /* guard against klutz calls */ |
| 802 | if (root_rp) |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 803 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 804 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 805 | root_rp_refcount++; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 806 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 807 | atexit (svm_mutex_cleanup); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 808 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 809 | /* Randomize the shared-VM base at init time */ |
| 810 | if (MMAP_PAGESIZE <= (4 << 10)) |
| 811 | randomize_baseva = (ticks & 15) * MMAP_PAGESIZE; |
| 812 | else |
| 813 | randomize_baseva = (ticks & 3) * MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 815 | a->baseva += randomize_baseva; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 816 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 817 | rp = svm_map_region (a); |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 818 | if (!rp) |
| 819 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 820 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 821 | region_lock (rp, 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 822 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 823 | /* Set up the main region data structures */ |
| 824 | if (rp->flags & SVM_FLAGS_NEED_DATA_INIT) |
| 825 | { |
| 826 | svm_main_region_t *mp = 0; |
| 827 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 829 | rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 831 | oldheap = svm_push_pvt_heap (rp); |
| 832 | vec_validate (mp, 0); |
| 833 | mp->name_hash = hash_create_string (0, sizeof (uword)); |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 834 | mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0; |
Dave Wallace | 1929611 | 2017-08-31 15:54:11 -0400 | [diff] [blame] | 835 | mp->uid = a->uid; |
| 836 | mp->gid = a->gid; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 837 | rp->data_base = mp; |
| 838 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 840 | region_unlock (rp); |
| 841 | root_rp = rp; |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 842 | |
| 843 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 846 | void |
| 847 | svm_region_init (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 848 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 849 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 850 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 851 | memset (a, 0, sizeof (*a)); |
| 852 | a->root_path = 0; |
| 853 | a->name = SVM_GLOBAL_REGION_NAME; |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 854 | a->baseva = svm_get_global_region_base_va (); |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 855 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 856 | a->flags = SVM_FLAGS_NODATA; |
| 857 | a->uid = 0; |
| 858 | a->gid = 0; |
| 859 | |
| 860 | svm_region_init_internal (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 863 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 864 | svm_region_init_chroot (const char *root_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 865 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 866 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 867 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 868 | memset (a, 0, sizeof (*a)); |
| 869 | a->root_path = root_path; |
| 870 | a->name = SVM_GLOBAL_REGION_NAME; |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 871 | a->baseva = svm_get_global_region_base_va (); |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 872 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 873 | a->flags = SVM_FLAGS_NODATA; |
| 874 | a->uid = 0; |
| 875 | a->gid = 0; |
| 876 | |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 877 | return svm_region_init_internal (a); |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 878 | } |
| 879 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 880 | void |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 881 | 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] | 882 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 883 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 884 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 885 | memset (a, 0, sizeof (*a)); |
| 886 | a->root_path = root_path; |
| 887 | a->name = SVM_GLOBAL_REGION_NAME; |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 888 | a->baseva = svm_get_global_region_base_va (); |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 889 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 890 | a->flags = SVM_FLAGS_NODATA; |
| 891 | a->uid = uid; |
| 892 | a->gid = gid; |
| 893 | |
| 894 | svm_region_init_internal (a); |
| 895 | } |
| 896 | |
| 897 | void |
| 898 | svm_region_init_args (svm_map_region_args_t * a) |
| 899 | { |
| 900 | svm_region_init_internal (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 903 | void * |
| 904 | svm_region_find_or_create (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 905 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 906 | svm_main_region_t *mp; |
| 907 | svm_region_t *rp; |
| 908 | uword need_nbits; |
| 909 | int index, i; |
| 910 | void *oldheap; |
| 911 | uword *p; |
| 912 | u8 *name; |
| 913 | svm_subregion_t *subp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 914 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 915 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 916 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 917 | a->size += MMAP_PAGESIZE + |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 918 | ((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] | 919 | a->size = rnd_pagesize (a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 920 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 921 | region_lock (root_rp, 4); |
| 922 | oldheap = svm_push_pvt_heap (root_rp); |
| 923 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 924 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 925 | ASSERT (mp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 926 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 927 | /* Map the named region from the correct chroot environment */ |
Jan Srnicek | 5beec81 | 2017-03-24 10:18:11 +0100 | [diff] [blame] | 928 | if (a->root_path == NULL) |
| 929 | a->root_path = (char *) mp->root_path; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 930 | |
| 931 | /* |
| 932 | * See if this region is already known. If it is, we're |
| 933 | * almost done... |
| 934 | */ |
| 935 | p = hash_get_mem (mp->name_hash, a->name); |
| 936 | |
| 937 | if (p) |
| 938 | { |
| 939 | rp = svm_map_region (a); |
| 940 | region_unlock (root_rp); |
| 941 | svm_pop_heap (oldheap); |
| 942 | return rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 943 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 944 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 945 | /* Create the region. */ |
| 946 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
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 | need_nbits = a->size / MMAP_PAGESIZE; |
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 | index = 1; /* $$$ fixme, figure out how many bit to really skip */ |
| 951 | |
| 952 | /* |
| 953 | * Scan the virtual space allocation bitmap, looking for a large |
| 954 | * enough chunk |
| 955 | */ |
| 956 | do |
| 957 | { |
| 958 | if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0) |
| 959 | { |
| 960 | for (i = 0; i < (need_nbits - 1); i++) |
| 961 | { |
| 962 | if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1) |
| 963 | { |
| 964 | index = index + i; |
| 965 | goto next; |
| 966 | } |
| 967 | } |
| 968 | break; |
| 969 | } |
| 970 | index++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 971 | next:; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 972 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 973 | while (index < root_rp->bitmap_size); |
| 974 | |
| 975 | /* Completely out of VM? */ |
| 976 | if (index >= root_rp->bitmap_size) |
| 977 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 978 | clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)", |
| 979 | root_rp->region_name, a->size, a->size); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 980 | svm_pop_heap (oldheap); |
| 981 | region_unlock (root_rp); |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Mark virtual space allocated |
| 987 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 988 | #if CLIB_DEBUG > 1 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 989 | clib_warning ("set %d bits at index %d", need_nbits, index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 990 | #endif |
| 991 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 992 | for (i = 0; i < need_nbits; i++) |
| 993 | { |
| 994 | clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 997 | /* Place this region where it goes... */ |
| 998 | a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE; |
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 | rp = svm_map_region (a); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1001 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1002 | pool_get (mp->subregions, subp); |
| 1003 | name = format (0, "%s%c", a->name, 0); |
| 1004 | subp->subregion_name = name; |
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 | hash_set_mem (mp->name_hash, name, subp - mp->subregions); |
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 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1009 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1010 | region_unlock (root_rp); |
| 1011 | |
| 1012 | return (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 1015 | void |
| 1016 | svm_region_unlink (svm_region_t * rp) |
| 1017 | { |
| 1018 | svm_map_region_args_t _a, *a = &_a; |
| 1019 | svm_main_region_t *mp; |
| 1020 | u8 *shm_name; |
| 1021 | |
| 1022 | ASSERT (root_rp); |
| 1023 | ASSERT (rp); |
| 1024 | ASSERT (vec_c_string_is_terminated (rp->region_name)); |
| 1025 | |
| 1026 | mp = root_rp->data_base; |
| 1027 | ASSERT (mp); |
| 1028 | |
| 1029 | a->root_path = (char *) mp->root_path; |
| 1030 | a->name = rp->region_name; |
| 1031 | shm_name = shm_name_from_svm_map_region_args (a); |
| 1032 | if (CLIB_DEBUG > 1) |
| 1033 | clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name); |
| 1034 | shm_unlink ((const char *) shm_name); |
| 1035 | vec_free (shm_name); |
| 1036 | } |
| 1037 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1038 | /* |
| 1039 | * svm_region_unmap |
| 1040 | * |
| 1041 | * Let go of the indicated region. If the calling process |
| 1042 | * is the last customer, throw it away completely. |
| 1043 | * The root region mutex guarantees atomicity with respect to |
| 1044 | * a new region client showing up at the wrong moment. |
| 1045 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1046 | void |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 1047 | svm_region_unmap_internal (void *rp_arg, u8 is_client) |
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 | int i, mypid = getpid (); |
| 1050 | int nclients_left; |
| 1051 | void *oldheap; |
| 1052 | uword virtual_base, virtual_size; |
| 1053 | svm_region_t *rp = rp_arg; |
| 1054 | char *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1055 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1056 | /* |
| 1057 | * If we take a signal while holding one or more shared-memory |
| 1058 | * mutexes, we may end up back here from an otherwise |
| 1059 | * benign exit handler. Bail out to avoid a recursive |
| 1060 | * mutex screw-up. |
| 1061 | */ |
| 1062 | if (nheld) |
| 1063 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1064 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1065 | ASSERT (rp); |
| 1066 | ASSERT (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 | if (CLIB_DEBUG > 1) |
| 1069 | clib_warning ("[%d] unmap region %s", getpid (), rp->region_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1070 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1071 | region_lock (root_rp, 5); |
| 1072 | region_lock (rp, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1073 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1074 | oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */ |
| 1075 | |
| 1076 | /* Remove the caller from the list of mappers */ |
| 1077 | for (i = 0; i < vec_len (rp->client_pids); i++) |
| 1078 | { |
| 1079 | if (rp->client_pids[i] == mypid) |
| 1080 | { |
| 1081 | vec_delete (rp->client_pids, 1, i); |
| 1082 | goto found; |
| 1083 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1084 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1085 | clib_warning ("pid %d AWOL", mypid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1086 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1087 | found: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1088 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1089 | svm_pop_heap (oldheap); |
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 | nclients_left = vec_len (rp->client_pids); |
| 1092 | virtual_base = rp->virtual_base; |
| 1093 | virtual_size = rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1094 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1095 | if (nclients_left == 0) |
| 1096 | { |
| 1097 | int index, nbits, i; |
| 1098 | svm_main_region_t *mp; |
| 1099 | uword *p; |
| 1100 | svm_subregion_t *subp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1101 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1102 | /* Kill the region, last guy on his way out */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1103 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1104 | oldheap = svm_push_pvt_heap (root_rp); |
| 1105 | name = vec_dup (rp->region_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1106 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1107 | virtual_base = rp->virtual_base; |
| 1108 | virtual_size = rp->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 | /* Figure out which bits to clear in the root region bitmap */ |
| 1111 | index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1112 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1113 | nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1114 | |
| 1115 | #if CLIB_DEBUG > 1 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1116 | clib_warning ("clear %d bits at index %d", nbits, index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1117 | #endif |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1118 | /* Give back the allocated VM */ |
| 1119 | for (i = 0; i < nbits; i++) |
| 1120 | { |
| 1121 | clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0); |
| 1122 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1123 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1124 | mp = root_rp->data_base; |
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 | p = hash_get_mem (mp->name_hash, name); |
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 | /* Better never happen ... */ |
| 1129 | if (p == NULL) |
| 1130 | { |
| 1131 | region_unlock (rp); |
| 1132 | region_unlock (root_rp); |
| 1133 | svm_pop_heap (oldheap); |
| 1134 | clib_warning ("Region name '%s' not found?", name); |
| 1135 | return; |
| 1136 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1137 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1138 | /* Remove from the root region subregion pool */ |
| 1139 | subp = mp->subregions + p[0]; |
| 1140 | pool_put (mp->subregions, subp); |
| 1141 | |
| 1142 | hash_unset_mem (mp->name_hash, name); |
| 1143 | |
| 1144 | vec_free (name); |
| 1145 | |
| 1146 | region_unlock (rp); |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 1147 | |
| 1148 | /* If a client asks for the cleanup, don't unlink the backing |
| 1149 | * file since we can't tell if it has been recreated. */ |
| 1150 | if (!is_client) |
| 1151 | svm_region_unlink (rp); |
| 1152 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1153 | munmap ((void *) virtual_base, virtual_size); |
| 1154 | region_unlock (root_rp); |
| 1155 | svm_pop_heap (oldheap); |
| 1156 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1159 | region_unlock (rp); |
| 1160 | region_unlock (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1161 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1162 | munmap ((void *) virtual_base, virtual_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 1165 | void |
| 1166 | svm_region_unmap (void *rp_arg) |
| 1167 | { |
| 1168 | svm_region_unmap_internal (rp_arg, 0 /* is_client */ ); |
| 1169 | } |
| 1170 | |
| 1171 | void |
| 1172 | svm_region_unmap_client (void *rp_arg) |
| 1173 | { |
| 1174 | svm_region_unmap_internal (rp_arg, 1 /* is_client */ ); |
| 1175 | } |
| 1176 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1177 | /* |
| 1178 | * svm_region_exit |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1179 | */ |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 1180 | static void |
| 1181 | svm_region_exit_internal (u8 is_client) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1182 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1183 | void *oldheap; |
| 1184 | int i, mypid = getpid (); |
| 1185 | uword virtual_base, virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1186 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1187 | /* It felt so nice we did it twice... */ |
| 1188 | if (root_rp == 0) |
| 1189 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1190 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1191 | if (--root_rp_refcount > 0) |
| 1192 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1193 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1194 | /* |
| 1195 | * If we take a signal while holding one or more shared-memory |
| 1196 | * mutexes, we may end up back here from an otherwise |
| 1197 | * benign exit handler. Bail out to avoid a recursive |
| 1198 | * mutex screw-up. |
| 1199 | */ |
| 1200 | if (nheld) |
| 1201 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1202 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1203 | region_lock (root_rp, 7); |
| 1204 | oldheap = svm_push_pvt_heap (root_rp); |
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 | virtual_base = root_rp->virtual_base; |
| 1207 | virtual_size = root_rp->virtual_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1208 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1209 | for (i = 0; i < vec_len (root_rp->client_pids); i++) |
| 1210 | { |
| 1211 | if (root_rp->client_pids[i] == mypid) |
| 1212 | { |
| 1213 | vec_delete (root_rp->client_pids, 1, i); |
| 1214 | goto found; |
| 1215 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1216 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1217 | clib_warning ("pid %d AWOL", mypid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1218 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1219 | found: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1220 | |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 1221 | if (!is_client && vec_len (root_rp->client_pids) == 0) |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 1222 | svm_region_unlink (root_rp); |
| 1223 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1224 | region_unlock (root_rp); |
| 1225 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1226 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1227 | root_rp = 0; |
| 1228 | munmap ((void *) virtual_base, virtual_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1231 | void |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 1232 | svm_region_exit (void) |
| 1233 | { |
| 1234 | svm_region_exit_internal (0 /* is_client */ ); |
| 1235 | } |
| 1236 | |
| 1237 | void |
| 1238 | svm_region_exit_client (void) |
| 1239 | { |
| 1240 | svm_region_exit_internal (1 /* is_client */ ); |
| 1241 | } |
| 1242 | |
| 1243 | void |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1244 | svm_client_scan_this_region_nolock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1245 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1246 | int j; |
| 1247 | int mypid = getpid (); |
| 1248 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1249 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1250 | for (j = 0; j < vec_len (rp->client_pids); j++) |
| 1251 | { |
| 1252 | if (mypid == rp->client_pids[j]) |
| 1253 | continue; |
| 1254 | if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0)) |
| 1255 | { |
| 1256 | clib_warning ("%s: cleanup ghost pid %d", |
| 1257 | rp->region_name, rp->client_pids[j]); |
| 1258 | /* nb: client vec in rp->region_heap */ |
| 1259 | oldheap = svm_push_pvt_heap (rp); |
| 1260 | vec_delete (rp->client_pids, 1, j); |
| 1261 | j--; |
| 1262 | svm_pop_heap (oldheap); |
| 1263 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1268 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1269 | * Scan svm regions for dead clients |
| 1270 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1271 | void |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 1272 | svm_client_scan (const char *root_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1273 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1274 | int i, j; |
| 1275 | svm_main_region_t *mp; |
| 1276 | svm_map_region_args_t *a = 0; |
| 1277 | svm_region_t *root_rp; |
| 1278 | svm_region_t *rp; |
| 1279 | svm_subregion_t *subp; |
| 1280 | u8 *name = 0; |
| 1281 | u8 **svm_names = 0; |
| 1282 | void *oldheap; |
| 1283 | int mypid = getpid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1284 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1285 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1286 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1287 | svm_region_init_chroot (root_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1288 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1289 | root_rp = svm_get_root_rp (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1290 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1291 | pthread_mutex_lock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1292 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1293 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1294 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1295 | for (j = 0; j < vec_len (root_rp->client_pids); j++) |
| 1296 | { |
| 1297 | if (mypid == root_rp->client_pids[j]) |
| 1298 | continue; |
| 1299 | if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0)) |
| 1300 | { |
| 1301 | clib_warning ("%s: cleanup ghost pid %d", |
| 1302 | root_rp->region_name, root_rp->client_pids[j]); |
| 1303 | /* nb: client vec in root_rp->region_heap */ |
| 1304 | oldheap = svm_push_pvt_heap (root_rp); |
| 1305 | vec_delete (root_rp->client_pids, 1, j); |
| 1306 | j--; |
| 1307 | svm_pop_heap (oldheap); |
| 1308 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1311 | /* |
| 1312 | * Snapshoot names, can't hold root rp mutex across |
| 1313 | * find_or_create. |
| 1314 | */ |
| 1315 | /* *INDENT-OFF* */ |
| 1316 | pool_foreach (subp, mp->subregions, ({ |
| 1317 | name = vec_dup (subp->subregion_name); |
| 1318 | vec_add1(svm_names, name); |
| 1319 | })); |
| 1320 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1321 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1322 | pthread_mutex_unlock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1323 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1324 | for (i = 0; i < vec_len (svm_names); i++) |
| 1325 | { |
| 1326 | vec_validate (a, 0); |
| 1327 | a->root_path = root_path; |
| 1328 | a->name = (char *) svm_names[i]; |
| 1329 | rp = svm_region_find_or_create (a); |
| 1330 | if (rp) |
| 1331 | { |
| 1332 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1333 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1334 | svm_client_scan_this_region_nolock (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1335 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1336 | pthread_mutex_unlock (&rp->mutex); |
| 1337 | svm_region_unmap (rp); |
| 1338 | vec_free (svm_names[i]); |
| 1339 | } |
| 1340 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1341 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1342 | vec_free (svm_names); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1343 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1344 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1345 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1346 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1347 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1348 | |
| 1349 | /* |
| 1350 | * fd.io coding-style-patch-verification: ON |
| 1351 | * |
| 1352 | * Local Variables: |
| 1353 | * eval: (c-set-style "gnu") |
| 1354 | * End: |
| 1355 | */ |