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 | * svmtool.c |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <signal.h> |
| 27 | #include <pthread.h> |
| 28 | #include <unistd.h> |
| 29 | #include <time.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <string.h> |
| 32 | #include <vppinfra/clib.h> |
| 33 | #include <vppinfra/vec.h> |
| 34 | #include <vppinfra/hash.h> |
| 35 | #include <vppinfra/bitmap.h> |
| 36 | #include <vppinfra/fifo.h> |
| 37 | #include <vppinfra/time.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | #include <vppinfra/heap.h> |
| 39 | #include <vppinfra/pool.h> |
| 40 | #include <vppinfra/format.h> |
| 41 | |
| 42 | #include "svm.h" |
| 43 | |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * format_all_svm_regions |
| 48 | * Maps / unmaps regions. Do NOT call from client code! |
| 49 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 50 | u8 * |
| 51 | format_all_svm_regions (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 53 | int verbose = va_arg (*args, int); |
| 54 | svm_region_t *root_rp = svm_get_root_rp (); |
| 55 | svm_main_region_t *mp; |
| 56 | svm_subregion_t *subp; |
| 57 | svm_region_t *rp; |
| 58 | svm_map_region_args_t *a = 0; |
| 59 | u8 **svm_names = 0; |
| 60 | u8 *name = 0; |
| 61 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 63 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 65 | pthread_mutex_lock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 67 | s = format (s, "%U", format_svm_region, root_rp, verbose); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 69 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 71 | /* |
| 72 | * Snapshoot names, can't hold root rp mutex across |
| 73 | * find_or_create. |
| 74 | */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 75 | pool_foreach (subp, mp->subregions) { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 76 | name = vec_dup (subp->subregion_name); |
| 77 | vec_add1(svm_names, name); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 78 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 80 | pthread_mutex_unlock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 82 | for (i = 0; i < vec_len (svm_names); i++) |
| 83 | { |
| 84 | vec_validate (a, 0); |
| 85 | a->name = (char *) svm_names[i]; |
| 86 | rp = svm_region_find_or_create (a); |
| 87 | if (rp) |
| 88 | { |
| 89 | pthread_mutex_lock (&rp->mutex); |
| 90 | s = format (s, "%U", format_svm_region, rp, verbose); |
| 91 | pthread_mutex_unlock (&rp->mutex); |
| 92 | svm_region_unmap (rp); |
| 93 | vec_free (svm_names[i]); |
| 94 | } |
| 95 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 97 | vec_free (svm_names); |
| 98 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 101 | void |
| 102 | show (char *chroot_path, int verbose) |
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 | svm_map_region_args_t *a = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 106 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 108 | svm_region_init_chroot (chroot_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 110 | fformat (stdout, "My pid is %d\n", getpid ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 112 | fformat (stdout, "%U", format_all_svm_regions, verbose); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 114 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 116 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 120 | static void * |
| 121 | svm_map_region_nolock (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 123 | int svm_fd; |
| 124 | svm_region_t *rp; |
| 125 | int deadman = 0; |
| 126 | u8 *shm_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 128 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 130 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 132 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 134 | if (svm_fd < 0) |
| 135 | { |
| 136 | perror ("svm_region_map(mmap open)"); |
| 137 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 139 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 141 | rp = mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
| 142 | |
| 143 | if (rp == (svm_region_t *) MAP_FAILED) |
| 144 | { |
| 145 | close (svm_fd); |
| 146 | clib_warning ("mmap"); |
| 147 | return (0); |
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 | /* |
| 150 | * We lost the footrace to create this region; make sure |
| 151 | * the winner has crossed the finish line. |
| 152 | */ |
| 153 | while (rp->version == 0 && deadman++ < 5) |
| 154 | { |
| 155 | sleep (1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 158 | /* |
| 159 | * <bleep>-ed? |
| 160 | */ |
| 161 | if (rp->version == 0) |
| 162 | { |
| 163 | clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION); |
Dave Barach | ac0798d | 2016-07-26 10:30:50 -0400 | [diff] [blame] | 164 | munmap (rp, MMAP_PAGESIZE); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 165 | return (0); |
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 | /* Remap now that the region has been placed */ |
| 168 | a->baseva = rp->virtual_base; |
| 169 | a->size = rp->virtual_size; |
| 170 | munmap (rp, MMAP_PAGESIZE); |
| 171 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 172 | rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 173 | PROT_READ | PROT_WRITE, |
| 174 | MAP_SHARED | MAP_FIXED, svm_fd, 0); |
| 175 | if ((uword) rp == (uword) MAP_FAILED) |
| 176 | { |
| 177 | clib_unix_warning ("mmap"); |
| 178 | return (0); |
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 | if ((uword) rp != rp->virtual_base) |
| 182 | { |
| 183 | clib_warning ("mmap botch"); |
| 184 | } |
| 185 | |
| 186 | if (pthread_mutex_trylock (&rp->mutex)) |
| 187 | { |
| 188 | clib_warning ("rp->mutex LOCKED by pid %d, tag %d, cleared...", |
| 189 | rp->mutex_owner_pid, rp->mutex_owner_tag); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 190 | clib_memset (&rp->mutex, 0, sizeof (rp->mutex)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 191 | |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | clib_warning ("mutex OK...\n"); |
| 196 | pthread_mutex_unlock (&rp->mutex); |
| 197 | } |
| 198 | |
| 199 | return ((void *) rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
| 203 | * rnd_pagesize |
| 204 | * Round to a pagesize multiple, presumably 4k works |
| 205 | */ |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 206 | static u64 |
| 207 | rnd_pagesize (u64 size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 209 | u64 rv; |
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 | rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1); |
| 212 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | #define MUTEX_DEBUG |
| 216 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 217 | always_inline void |
| 218 | region_lock (svm_region_t * rp, int tag) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 220 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 222 | rp->mutex_owner_pid = getpid (); |
| 223 | rp->mutex_owner_tag = tag; |
| 224 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 227 | always_inline void |
| 228 | region_unlock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | { |
| 230 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 231 | rp->mutex_owner_pid = 0; |
| 232 | rp->mutex_owner_tag = 0; |
| 233 | #endif |
| 234 | pthread_mutex_unlock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 238 | static void * |
| 239 | svm_existing_region_map_nolock (void *root_arg, svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 241 | svm_region_t *root_rp = root_arg; |
| 242 | svm_main_region_t *mp; |
| 243 | svm_region_t *rp; |
| 244 | void *oldheap; |
| 245 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 247 | a->size += MMAP_PAGESIZE + |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 248 | (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 249 | a->size = rnd_pagesize (a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 251 | region_lock (root_rp, 4); |
| 252 | oldheap = svm_push_pvt_heap (root_rp); |
| 253 | mp = root_rp->data_base; |
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 | ASSERT (mp); |
| 256 | |
| 257 | p = hash_get_mem (mp->name_hash, a->name); |
| 258 | |
| 259 | if (p) |
| 260 | { |
| 261 | rp = svm_map_region_nolock (a); |
| 262 | region_unlock (root_rp); |
| 263 | svm_pop_heap (oldheap); |
| 264 | return rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | } |
Dave Barach | 99c6dc6 | 2021-02-15 12:46:47 -0500 | [diff] [blame] | 266 | region_unlock (root_rp); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 267 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
| 269 | } |
| 270 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 271 | static void |
| 272 | trace (char *chroot_path, char *name, int enable_disable) |
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 | svm_map_region_args_t *a = 0; |
| 275 | svm_region_t *db_rp; |
| 276 | void *oldheap; |
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 | vec_validate (a, 0); |
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 | svm_region_init_chroot (chroot_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 282 | a->name = name; |
| 283 | a->size = 1 << 20; |
| 284 | a->flags = SVM_FLAGS_MHEAP; |
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 | db_rp = svm_region_find_or_create (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 288 | ASSERT (db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 290 | region_lock (db_rp, 20); |
| 291 | |
| 292 | oldheap = svm_push_data_heap (db_rp); |
| 293 | |
| 294 | mheap_trace (db_rp->data_heap, enable_disable); |
| 295 | |
| 296 | svm_pop_heap (oldheap); |
| 297 | region_unlock (db_rp); |
| 298 | |
| 299 | svm_region_unmap ((void *) db_rp); |
| 300 | svm_region_exit (); |
| 301 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 306 | static void |
| 307 | subregion_repair (char *chroot_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 309 | int i; |
| 310 | svm_main_region_t *mp; |
| 311 | svm_map_region_args_t a; |
| 312 | svm_region_t *root_rp; |
| 313 | svm_region_t *rp; |
| 314 | svm_subregion_t *subp; |
| 315 | u8 *name = 0; |
| 316 | u8 **svm_names = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 318 | svm_region_init_chroot (chroot_path); |
| 319 | root_rp = svm_get_root_rp (); |
| 320 | |
| 321 | pthread_mutex_lock (&root_rp->mutex); |
| 322 | |
| 323 | mp = root_rp->data_base; |
| 324 | |
| 325 | /* |
| 326 | * Snapshoot names, can't hold root rp mutex across |
| 327 | * find_or_create. |
| 328 | */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 329 | pool_foreach (subp, mp->subregions) { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 330 | name = vec_dup (subp->subregion_name); |
| 331 | vec_add1(svm_names, name); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 332 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 333 | |
| 334 | pthread_mutex_unlock (&root_rp->mutex); |
| 335 | |
| 336 | for (i = 0; i < vec_len (svm_names); i++) |
| 337 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 338 | clib_memset (&a, 0, sizeof (a)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 339 | a.root_path = chroot_path; |
| 340 | a.name = (char *) svm_names[i]; |
| 341 | fformat (stdout, "Checking %s region...\n", a.name); |
| 342 | rp = svm_existing_region_map_nolock (root_rp, &a); |
| 343 | if (rp) |
| 344 | { |
| 345 | svm_region_unmap (rp); |
| 346 | vec_free (svm_names[i]); |
| 347 | } |
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 | vec_free (svm_names); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 352 | void |
| 353 | repair (char *chroot_path, int crash_root_region) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 355 | svm_region_t *root_rp = 0; |
| 356 | svm_map_region_args_t *a = 0; |
| 357 | void *svm_map_region (svm_map_region_args_t * a); |
| 358 | int svm_fd; |
| 359 | u8 *shm_name; |
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 | fformat (stdout, "our pid: %d\n", getpid ()); |
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 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 365 | a->root_path = chroot_path; |
| 366 | a->name = SVM_GLOBAL_REGION_NAME; |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 367 | a->baseva = svm_get_global_region_base_va (); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 368 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 369 | a->flags = SVM_FLAGS_NODATA; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 371 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 373 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 375 | if (svm_fd < 0) |
| 376 | { |
| 377 | perror ("svm_region_map(mmap open)"); |
| 378 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 381 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 383 | root_rp = mmap (0, MMAP_PAGESIZE, |
| 384 | PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 386 | if (root_rp == (svm_region_t *) MAP_FAILED) |
| 387 | { |
| 388 | close (svm_fd); |
| 389 | clib_warning ("mmap"); |
| 390 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 393 | /* Remap now that the region has been placed */ |
| 394 | clib_warning ("remap to 0x%x", root_rp->virtual_base); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 396 | a->baseva = root_rp->virtual_base; |
| 397 | a->size = root_rp->virtual_size; |
| 398 | munmap (root_rp, MMAP_PAGESIZE); |
| 399 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 400 | root_rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 401 | PROT_READ | PROT_WRITE, |
| 402 | MAP_SHARED | MAP_FIXED, svm_fd, 0); |
| 403 | if ((uword) root_rp == (uword) MAP_FAILED) |
| 404 | { |
| 405 | clib_unix_warning ("mmap"); |
| 406 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 408 | |
| 409 | close (svm_fd); |
| 410 | |
| 411 | if ((uword) root_rp != root_rp->virtual_base) |
| 412 | { |
| 413 | clib_warning ("mmap botch"); |
| 414 | goto out; |
| 415 | } |
| 416 | |
| 417 | if (pthread_mutex_trylock (&root_rp->mutex)) |
| 418 | { |
| 419 | clib_warning ("root_rp->mutex LOCKED by pid %d, tag %d, cleared...", |
| 420 | root_rp->mutex_owner_pid, root_rp->mutex_owner_tag); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 421 | clib_memset (&root_rp->mutex, 0, sizeof (root_rp->mutex)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 422 | goto out; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | clib_warning ("root_rp->mutex OK...\n"); |
| 427 | pthread_mutex_unlock (&root_rp->mutex); |
| 428 | } |
| 429 | |
| 430 | out: |
| 431 | vec_free (a); |
| 432 | /* |
| 433 | * Now that the root region is known to be OK, |
| 434 | * fix broken subregions |
| 435 | */ |
| 436 | subregion_repair (chroot_path); |
| 437 | |
| 438 | if (crash_root_region) |
| 439 | { |
| 440 | clib_warning ("Leaving root region locked on purpose..."); |
| 441 | pthread_mutex_lock (&root_rp->mutex); |
| 442 | root_rp->mutex_owner_pid = getpid (); |
| 443 | root_rp->mutex_owner_tag = 99; |
| 444 | } |
| 445 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 448 | int |
| 449 | main (int argc, char **argv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 451 | unformat_input_t input; |
| 452 | int parsed = 0; |
| 453 | char *name; |
| 454 | char *chroot_path = 0; |
| 455 | u8 *chroot_u8; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 456 | |
Guanghua Zhang | 60f16c2 | 2019-08-12 09:04:35 +0800 | [diff] [blame] | 457 | clib_mem_init_thread_safe (0, 128 << 20); |
| 458 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 459 | unformat_init_command_line (&input, argv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 461 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 462 | { |
| 463 | if (unformat (&input, "show-verbose")) |
| 464 | { |
| 465 | show (chroot_path, 1); |
| 466 | parsed++; |
| 467 | } |
| 468 | else if (unformat (&input, "show")) |
| 469 | { |
| 470 | show (chroot_path, 0); |
| 471 | parsed++; |
| 472 | } |
| 473 | else if (unformat (&input, "client-scan")) |
| 474 | { |
| 475 | svm_client_scan (chroot_path); |
| 476 | parsed++; |
| 477 | } |
| 478 | else if (unformat (&input, "repair")) |
| 479 | { |
| 480 | repair (chroot_path, 0 /* fix it */ ); |
| 481 | parsed++; |
| 482 | } |
| 483 | else if (unformat (&input, "crash")) |
| 484 | { |
| 485 | repair (chroot_path, 1 /* crash it */ ); |
| 486 | parsed++; |
| 487 | } |
| 488 | else if (unformat (&input, "trace-on %s", &name)) |
| 489 | { |
| 490 | trace (chroot_path, name, 1); |
| 491 | parsed++; |
| 492 | } |
| 493 | else if (unformat (&input, "trace-off %s", &name)) |
| 494 | { |
| 495 | trace (chroot_path, name, 0); |
| 496 | parsed++; |
| 497 | } |
| 498 | else if (unformat (&input, "chroot %s", &chroot_u8)) |
| 499 | { |
| 500 | chroot_path = (char *) chroot_u8; |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | break; |
| 505 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 508 | unformat_free (&input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 510 | if (!parsed) |
| 511 | { |
| 512 | fformat (stdout, |
| 513 | "%s: show | show-verbose | client-scan | trace-on <region-name>\n", |
| 514 | argv[0]); |
| 515 | fformat (stdout, " trace-off <region-name>\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 517 | exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * fd.io coding-style-patch-verification: ON |
| 522 | * |
| 523 | * Local Variables: |
| 524 | * eval: (c-set-style "gnu") |
| 525 | * End: |
| 526 | */ |