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