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