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