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