2 *------------------------------------------------------------------
3 * svm.c - shared VM allocation, mmap(...MAP_FIXED...)
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:
11 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
23 #include <sys/types.h>
26 #include <netinet/in.h>
33 #include <vppinfra/clib.h>
34 #include <vppinfra/vec.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/bitmap.h>
37 #include <vppinfra/fifo.h>
38 #include <vppinfra/time.h>
39 #include <vppinfra/mheap.h>
40 #include <vppinfra/heap.h>
41 #include <vppinfra/pool.h>
42 #include <vppinfra/format.h>
46 static svm_region_t *root_rp;
47 static int root_rp_refcount;
50 static pthread_mutex_t *mutexes_held[MAXLOCK];
54 svm_get_root_rp (void)
62 svm_get_global_region_base_va ()
64 #ifdef CLIB_SANITIZE_ADDR
65 return 0x200000000000;
69 /* On AArch64 VA space can have different size, from 36 to 48 bits.
70 Here we are trying to detect VA bits by parsing /proc/self/maps
73 unformat_input_t input;
77 if ((fd = open ("/proc/self/maps", 0)) < 0)
78 clib_unix_error ("open '/proc/self/maps'");
80 unformat_init_clib_file (&input, fd);
81 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
83 if (unformat (&input, "%llx-%llx", &start, &end))
85 unformat_skip_line (&input);
87 unformat_free (&input);
90 bits = count_leading_zeros (end);
92 if (bits >= 36 && bits <= 48)
93 return ((1ul << bits) / 4) - (2 * SVM_GLOBAL_REGION_SIZE);
95 clib_unix_error ("unexpected va bits '%u'", bits);
99 return 0x130000000ULL;
103 region_lock (svm_region_t * rp, int tag)
105 pthread_mutex_lock (&rp->mutex);
107 rp->mutex_owner_pid = getpid ();
108 rp->mutex_owner_tag = tag;
110 ASSERT (nheld < MAXLOCK);
112 * Keep score of held mutexes so we can try to exit
113 * cleanly if the world comes to an end at the worst possible
116 mutexes_held[nheld++] = &rp->mutex;
120 region_unlock (svm_region_t * rp)
124 rp->mutex_owner_pid = 0;
125 rp->mutex_owner_tag = 0;
128 for (i = nheld - 1; i >= 0; i--)
130 if (mutexes_held[i] == &rp->mutex)
132 for (j = i; j < MAXLOCK - 1; j++)
133 mutexes_held[j] = mutexes_held[j + 1];
141 CLIB_MEMORY_BARRIER ();
142 pthread_mutex_unlock (&rp->mutex);
147 format_svm_flags (u8 * s, va_list * args)
149 uword f = va_arg (*args, uword);
151 if (f & SVM_FLAGS_MHEAP)
152 s = format (s, "MHEAP ");
153 if (f & SVM_FLAGS_FILE)
154 s = format (s, "FILE ");
155 if (f & SVM_FLAGS_NODATA)
156 s = format (s, "NODATA ");
157 if (f & SVM_FLAGS_NEED_DATA_INIT)
158 s = format (s, "INIT ");
164 format_svm_size (u8 * s, va_list * args)
166 uword size = va_arg (*args, uword);
168 if (size >= (1 << 20))
170 s = format (s, "(%d mb)", size >> 20);
172 else if (size >= (1 << 10))
174 s = format (s, "(%d kb)", size >> 10);
178 s = format (s, "(%d bytes)", size);
184 format_svm_region (u8 * s, va_list * args)
186 svm_region_t *rp = va_arg (*args, svm_region_t *);
187 int verbose = va_arg (*args, int);
191 s = format (s, "%s: base va 0x%x size 0x%x %U\n",
192 rp->region_name, rp->virtual_base,
193 rp->virtual_size, format_svm_size, rp->virtual_size);
194 s = format (s, " user_ctx 0x%x, bitmap_size %d\n",
195 rp->user_ctx, rp->bitmap_size);
199 s = format (s, " flags: 0x%x %U\n", rp->flags,
200 format_svm_flags, rp->flags);
202 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
203 rp->region_heap, rp->data_base, rp->data_heap);
206 s = format (s, " %d clients, pids: ", vec_len (rp->client_pids));
208 for (i = 0; i < vec_len (rp->client_pids); i++)
209 s = format (s, "%d ", rp->client_pids[i]);
211 s = format (s, "\n");
217 s = format (s, " VM in use: ");
219 for (i = 0; i < rp->bitmap_size; i++)
221 if (clib_bitmap_get_no_check (rp->bitmap, i) != 0)
225 hi = lo = rp->virtual_base + i * MMAP_PAGESIZE;
229 hi = rp->virtual_base + i * MMAP_PAGESIZE;
236 hi = rp->virtual_base + i * MMAP_PAGESIZE - 1;
237 s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi,
243 #if USE_DLMALLOC == 0
244 s = format (s, " rgn heap stats: %U", format_mheap,
246 if ((rp->flags & SVM_FLAGS_MHEAP) && rp->data_heap)
248 s = format (s, "\n data heap stats: %U", format_mheap,
251 s = format (s, "\n");
260 * Round to a pagesize multiple, presumably 4k works
263 rnd_pagesize (u64 size)
267 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
272 * svm_data_region_setup
275 svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
281 map_size = rp->virtual_size - (MMAP_PAGESIZE +
282 (a->pvt_heap_size ? a->pvt_heap_size :
283 SVM_PVT_MHEAP_SIZE));
285 if (a->flags & SVM_FLAGS_FILE)
289 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
293 clib_unix_warning ("open");
297 if (fstat (fd, &statb) < 0)
299 clib_unix_warning ("fstat");
304 if (statb.st_mode & S_IFREG)
306 if (statb.st_size == 0)
308 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
310 clib_unix_warning ("seek region size");
314 if (write (fd, &junk, 1) != 1)
316 clib_unix_warning ("set region size");
323 map_size = rnd_pagesize (statb.st_size);
328 map_size = a->backing_mmap_size;
331 ASSERT (map_size <= rp->virtual_size -
332 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
334 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
335 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
337 clib_unix_warning ("mmap");
342 rp->backing_file = (char *) format (0, "%s\0", a->backing_file);
343 rp->flags |= SVM_FLAGS_FILE;
346 if (a->flags & SVM_FLAGS_MHEAP)
348 #if USE_DLMALLOC == 0
349 mheap_t *heap_header;
351 mheap_alloc_with_flags ((void *) (rp->data_base), map_size,
352 MHEAP_FLAG_DISABLE_VM);
353 heap_header = mheap_header (rp->data_heap);
354 heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
356 rp->data_heap = create_mspace_with_base (rp->data_base,
357 map_size, 1 /* locked */ );
358 mspace_disable_expand (rp->data_heap);
361 rp->flags |= SVM_FLAGS_MHEAP;
367 svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
374 map_size = rp->virtual_size -
376 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
378 if (a->flags & SVM_FLAGS_FILE)
381 fd = open (a->backing_file, O_RDWR, 0777);
385 clib_unix_warning ("open");
389 if (fstat (fd, &statb) < 0)
391 clib_unix_warning ("fstat");
396 if (statb.st_mode & S_IFREG)
398 if (statb.st_size == 0)
400 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
402 clib_unix_warning ("seek region size");
406 if (write (fd, &junk, 1) != 1)
408 clib_unix_warning ("set region size");
415 map_size = rnd_pagesize (statb.st_size);
420 map_size = a->backing_mmap_size;
423 ASSERT (map_size <= rp->virtual_size
426 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
428 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
429 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
431 clib_unix_warning ("mmap");
441 shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
444 int root_path_offset = 0;
449 /* Tolerate present or absent slashes */
450 if (a->root_path[0] == '/')
453 if (a->name[0] == '/')
456 shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset],
457 &a->name[name_offset], 0);
460 shm_name = format (0, "%s%c", a->name, 0);
465 svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp)
467 pthread_mutexattr_t attr;
468 pthread_condattr_t cattr;
469 int nbits, words, bit;
476 clib_memset (rp, 0, sizeof (*rp));
478 if (pthread_mutexattr_init (&attr))
479 clib_unix_warning ("mutexattr_init");
481 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
482 clib_unix_warning ("mutexattr_setpshared");
484 if (pthread_mutex_init (&rp->mutex, &attr))
485 clib_unix_warning ("mutex_init");
487 if (pthread_mutexattr_destroy (&attr))
488 clib_unix_warning ("mutexattr_destroy");
490 if (pthread_condattr_init (&cattr))
491 clib_unix_warning ("condattr_init");
493 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
494 clib_unix_warning ("condattr_setpshared");
496 if (pthread_cond_init (&rp->condvar, &cattr))
497 clib_unix_warning ("cond_init");
499 if (pthread_condattr_destroy (&cattr))
500 clib_unix_warning ("condattr_destroy");
504 rp->virtual_base = a->baseva;
505 rp->virtual_size = a->size;
507 #if USE_DLMALLOC == 0
509 mheap_alloc_with_flags (uword_to_pointer
510 (a->baseva + MMAP_PAGESIZE, void *),
512 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
513 MHEAP_FLAG_DISABLE_VM);
515 rp->region_heap = create_mspace_with_base
516 (uword_to_pointer (a->baseva + MMAP_PAGESIZE, void *),
518 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, 1 /* locked */ );
520 mspace_disable_expand (rp->region_heap);
523 oldheap = svm_push_pvt_heap (rp);
525 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
526 vec_add1 (rp->client_pids, getpid ());
528 nbits = rp->virtual_size / MMAP_PAGESIZE;
531 rp->bitmap_size = nbits;
532 words = (nbits + BITS (uword) - 1) / BITS (uword);
533 vec_validate (rp->bitmap, words - 1);
535 overhead_space = MMAP_PAGESIZE /* header */ +
536 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
539 data_base = (uword) rp->virtual_base;
541 if (a->flags & SVM_FLAGS_NODATA)
542 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
546 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
548 overhead_space -= MMAP_PAGESIZE;
549 data_base += MMAP_PAGESIZE;
551 while (overhead_space > 0);
553 rp->data_base = (void *) data_base;
556 * Note: although the POSIX spec guarantees that only one
557 * process enters this block, we have to play games
558 * to hold off clients until e.g. the mutex is ready
560 rp->version = SVM_VERSION;
562 /* setup the data portion of the region */
564 rv = svm_data_region_create (a, rp);
567 clib_warning ("data_region_create: %d", rv);
572 svm_pop_heap (oldheap);
579 svm_map_region (svm_map_region_args_t * a)
587 int pid_holding_region_lock;
589 int dead_region_recovery = 0;
592 struct timespec ts, tsrem;
594 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
597 shm_name = shm_name_from_svm_map_region_args (a);
599 if (1 || CLIB_DEBUG > 1)
600 clib_warning ("[%d] map region %s: shm_open (%s)",
601 getpid (), a->name, shm_name);
603 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
607 if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
608 clib_unix_warning ("segment chmod");
609 /* This turns out to fail harmlessly if the client starts first */
610 if (fchown (svm_fd, a->uid, a->gid) < 0)
611 clib_unix_warning ("segment chown [ok if client starts first]");
615 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
617 clib_warning ("seek region size");
621 if (write (svm_fd, &junk, 1) != 1)
623 clib_warning ("set region size");
628 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
629 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
631 if (rp == (svm_region_t *) MAP_FAILED)
633 clib_unix_warning ("mmap create");
639 svm_region_init_mapped_region (a, rp);
641 return ((void *) rp);
645 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
651 perror ("svm_region_map(mmap open)");
655 /* Reset ownership in case the client started first */
656 if (fchown (svm_fd, a->uid, a->gid) < 0)
657 clib_unix_warning ("segment chown [ok if client starts first]");
662 if (0 != fstat (svm_fd, &stat))
664 clib_warning ("fstat failed: %d", errno);
668 if (stat.st_size > 0)
674 clib_warning ("waiting for resize of shm file timed out");
679 ts.tv_nsec = 100000000;
680 while (nanosleep (&ts, &tsrem) < 0)
685 rp = mmap (0, MMAP_PAGESIZE,
686 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
688 if (rp == (svm_region_t *) MAP_FAILED)
691 clib_warning ("mmap");
695 * We lost the footrace to create this region; make sure
696 * the winner has crossed the finish line.
698 while (rp->version == 0 && deadman++ < 5)
706 if (rp->version == 0)
708 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
710 munmap (rp, a->size);
713 /* Remap now that the region has been placed */
714 a->baseva = rp->virtual_base;
715 a->size = rp->virtual_size;
716 munmap (rp, MMAP_PAGESIZE);
718 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
719 PROT_READ | PROT_WRITE,
720 MAP_SHARED | MAP_FIXED, svm_fd, 0);
721 if ((uword) rp == (uword) MAP_FAILED)
723 clib_unix_warning ("mmap");
730 if ((uword) rp != rp->virtual_base)
732 clib_warning ("mmap botch");
736 * Try to fix the region mutex if it is held by
739 pid_holding_region_lock = rp->mutex_owner_pid;
740 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
743 ("region %s mutex held by dead pid %d, tag %d, force unlock",
744 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
745 /* owner pid is nonexistent */
746 rp->mutex.__data.__owner = 0;
747 rp->mutex.__data.__lock = 0;
748 dead_region_recovery = 1;
751 if (dead_region_recovery)
752 clib_warning ("recovery: attempt to re-lock region");
755 oldheap = svm_push_pvt_heap (rp);
756 vec_add1 (rp->client_pids, getpid ());
758 if (dead_region_recovery)
759 clib_warning ("recovery: attempt svm_data_region_map");
761 rv = svm_data_region_map (a, rp);
764 clib_warning ("data_region_map: %d", rv);
767 if (dead_region_recovery)
768 clib_warning ("unlock and continue");
772 svm_pop_heap (oldheap);
774 return ((void *) rp);
777 return 0; /* NOTREACHED */
781 svm_mutex_cleanup (void)
784 for (i = 0; i < nheld; i++)
786 pthread_mutex_unlock (mutexes_held[i]);
791 svm_region_init_internal (svm_map_region_args_t * a)
794 u64 ticks = clib_cpu_time_now ();
795 uword randomize_baseva;
797 /* guard against klutz calls */
803 atexit (svm_mutex_cleanup);
805 /* Randomize the shared-VM base at init time */
806 if (MMAP_PAGESIZE <= (4 << 10))
807 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
809 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
811 a->baseva += randomize_baseva;
813 rp = svm_map_region (a);
819 /* Set up the main region data structures */
820 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
822 svm_main_region_t *mp = 0;
825 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
827 oldheap = svm_push_pvt_heap (rp);
828 vec_validate (mp, 0);
829 mp->name_hash = hash_create_string (0, sizeof (uword));
830 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
834 svm_pop_heap (oldheap);
843 svm_region_init (void)
845 svm_map_region_args_t _a, *a = &_a;
847 clib_memset (a, 0, sizeof (*a));
849 a->name = SVM_GLOBAL_REGION_NAME;
850 a->baseva = svm_get_global_region_base_va ();
851 a->size = SVM_GLOBAL_REGION_SIZE;
852 a->flags = SVM_FLAGS_NODATA;
856 svm_region_init_internal (a);
860 svm_region_init_chroot (const char *root_path)
862 svm_map_region_args_t _a, *a = &_a;
864 clib_memset (a, 0, sizeof (*a));
865 a->root_path = root_path;
866 a->name = SVM_GLOBAL_REGION_NAME;
867 a->baseva = svm_get_global_region_base_va ();
868 a->size = SVM_GLOBAL_REGION_SIZE;
869 a->flags = SVM_FLAGS_NODATA;
873 return svm_region_init_internal (a);
877 svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
879 svm_map_region_args_t _a, *a = &_a;
881 clib_memset (a, 0, sizeof (*a));
882 a->root_path = root_path;
883 a->name = SVM_GLOBAL_REGION_NAME;
884 a->baseva = svm_get_global_region_base_va ();
885 a->size = SVM_GLOBAL_REGION_SIZE;
886 a->flags = SVM_FLAGS_NODATA;
890 svm_region_init_internal (a);
894 svm_region_init_args (svm_map_region_args_t * a)
896 svm_region_init_internal (a);
900 svm_region_find_or_create (svm_map_region_args_t * a)
902 svm_main_region_t *mp;
909 svm_subregion_t *subp;
913 a->size += MMAP_PAGESIZE +
914 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
915 a->size = rnd_pagesize (a->size);
917 region_lock (root_rp, 4);
918 oldheap = svm_push_pvt_heap (root_rp);
919 mp = root_rp->data_base;
923 /* Map the named region from the correct chroot environment */
924 if (a->root_path == NULL)
925 a->root_path = (char *) mp->root_path;
928 * See if this region is already known. If it is, we're
931 p = hash_get_mem (mp->name_hash, a->name);
935 rp = svm_map_region (a);
936 region_unlock (root_rp);
937 svm_pop_heap (oldheap);
941 /* Create the region. */
942 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
944 need_nbits = a->size / MMAP_PAGESIZE;
946 index = 1; /* $$$ fixme, figure out how many bit to really skip */
949 * Scan the virtual space allocation bitmap, looking for a large
954 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
956 for (i = 0; i < (need_nbits - 1); i++)
958 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
969 while (index < root_rp->bitmap_size);
971 /* Completely out of VM? */
972 if (index >= root_rp->bitmap_size)
974 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
975 root_rp->region_name, a->size, a->size);
976 svm_pop_heap (oldheap);
977 region_unlock (root_rp);
982 * Mark virtual space allocated
985 clib_warning ("set %d bits at index %d", need_nbits, index);
988 for (i = 0; i < need_nbits; i++)
990 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
993 /* Place this region where it goes... */
994 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
996 rp = svm_map_region (a);
998 pool_get (mp->subregions, subp);
999 name = format (0, "%s%c", a->name, 0);
1000 subp->subregion_name = name;
1002 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
1004 svm_pop_heap (oldheap);
1006 region_unlock (root_rp);
1012 svm_region_unlink (svm_region_t * rp)
1014 svm_map_region_args_t _a, *a = &_a;
1015 svm_main_region_t *mp;
1020 ASSERT (vec_c_string_is_terminated (rp->region_name));
1022 mp = root_rp->data_base;
1025 a->root_path = (char *) mp->root_path;
1026 a->name = rp->region_name;
1027 shm_name = shm_name_from_svm_map_region_args (a);
1029 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
1030 shm_unlink ((const char *) shm_name);
1031 vec_free (shm_name);
1037 * Let go of the indicated region. If the calling process
1038 * is the last customer, throw it away completely.
1039 * The root region mutex guarantees atomicity with respect to
1040 * a new region client showing up at the wrong moment.
1043 svm_region_unmap_internal (void *rp_arg, u8 is_client)
1045 int i, mypid = getpid ();
1048 uword virtual_base, virtual_size;
1049 svm_region_t *rp = rp_arg;
1053 * If we take a signal while holding one or more shared-memory
1054 * mutexes, we may end up back here from an otherwise
1055 * benign exit handler. Bail out to avoid a recursive
1065 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
1067 region_lock (root_rp, 5);
1068 region_lock (rp, 6);
1070 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1072 /* Remove the caller from the list of mappers */
1073 for (i = 0; i < vec_len (rp->client_pids); i++)
1075 if (rp->client_pids[i] == mypid)
1077 vec_delete (rp->client_pids, 1, i);
1081 clib_warning ("pid %d AWOL", mypid);
1085 svm_pop_heap (oldheap);
1087 nclients_left = vec_len (rp->client_pids);
1088 virtual_base = rp->virtual_base;
1089 virtual_size = rp->virtual_size;
1091 if (nclients_left == 0)
1093 int index, nbits, i;
1094 svm_main_region_t *mp;
1096 svm_subregion_t *subp;
1098 /* Kill the region, last guy on his way out */
1100 oldheap = svm_push_pvt_heap (root_rp);
1101 name = vec_dup (rp->region_name);
1103 virtual_base = rp->virtual_base;
1104 virtual_size = rp->virtual_size;
1106 /* Figure out which bits to clear in the root region bitmap */
1107 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
1109 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
1112 clib_warning ("clear %d bits at index %d", nbits, index);
1114 /* Give back the allocated VM */
1115 for (i = 0; i < nbits; i++)
1117 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1120 mp = root_rp->data_base;
1122 p = hash_get_mem (mp->name_hash, name);
1124 /* Better never happen ... */
1128 region_unlock (root_rp);
1129 svm_pop_heap (oldheap);
1130 clib_warning ("Region name '%s' not found?", name);
1134 /* Remove from the root region subregion pool */
1135 subp = mp->subregions + p[0];
1136 pool_put (mp->subregions, subp);
1138 hash_unset_mem (mp->name_hash, name);
1144 /* If a client asks for the cleanup, don't unlink the backing
1145 * file since we can't tell if it has been recreated. */
1147 svm_region_unlink (rp);
1149 munmap ((void *) virtual_base, virtual_size);
1150 region_unlock (root_rp);
1151 svm_pop_heap (oldheap);
1156 region_unlock (root_rp);
1158 munmap ((void *) virtual_base, virtual_size);
1162 svm_region_unmap (void *rp_arg)
1164 svm_region_unmap_internal (rp_arg, 0 /* is_client */ );
1168 svm_region_unmap_client (void *rp_arg)
1170 svm_region_unmap_internal (rp_arg, 1 /* is_client */ );
1177 svm_region_exit_internal (u8 is_client)
1180 int i, mypid = getpid ();
1181 uword virtual_base, virtual_size;
1183 /* It felt so nice we did it twice... */
1187 if (--root_rp_refcount > 0)
1191 * If we take a signal while holding one or more shared-memory
1192 * mutexes, we may end up back here from an otherwise
1193 * benign exit handler. Bail out to avoid a recursive
1199 region_lock (root_rp, 7);
1200 oldheap = svm_push_pvt_heap (root_rp);
1202 virtual_base = root_rp->virtual_base;
1203 virtual_size = root_rp->virtual_size;
1205 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1207 if (root_rp->client_pids[i] == mypid)
1209 vec_delete (root_rp->client_pids, 1, i);
1213 clib_warning ("pid %d AWOL", mypid);
1217 if (!is_client && vec_len (root_rp->client_pids) == 0)
1218 svm_region_unlink (root_rp);
1220 region_unlock (root_rp);
1221 svm_pop_heap (oldheap);
1224 munmap ((void *) virtual_base, virtual_size);
1228 svm_region_exit (void)
1230 svm_region_exit_internal (0 /* is_client */ );
1234 svm_region_exit_client (void)
1236 svm_region_exit_internal (1 /* is_client */ );
1240 svm_client_scan_this_region_nolock (svm_region_t * rp)
1243 int mypid = getpid ();
1246 for (j = 0; j < vec_len (rp->client_pids); j++)
1248 if (mypid == rp->client_pids[j])
1250 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1252 clib_warning ("%s: cleanup ghost pid %d",
1253 rp->region_name, rp->client_pids[j]);
1254 /* nb: client vec in rp->region_heap */
1255 oldheap = svm_push_pvt_heap (rp);
1256 vec_delete (rp->client_pids, 1, j);
1258 svm_pop_heap (oldheap);
1265 * Scan svm regions for dead clients
1268 svm_client_scan (const char *root_path)
1271 svm_main_region_t *mp;
1272 svm_map_region_args_t *a = 0;
1273 svm_region_t *root_rp;
1275 svm_subregion_t *subp;
1279 int mypid = getpid ();
1281 vec_validate (a, 0);
1283 svm_region_init_chroot (root_path);
1285 root_rp = svm_get_root_rp ();
1287 pthread_mutex_lock (&root_rp->mutex);
1289 mp = root_rp->data_base;
1291 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1293 if (mypid == root_rp->client_pids[j])
1295 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1297 clib_warning ("%s: cleanup ghost pid %d",
1298 root_rp->region_name, root_rp->client_pids[j]);
1299 /* nb: client vec in root_rp->region_heap */
1300 oldheap = svm_push_pvt_heap (root_rp);
1301 vec_delete (root_rp->client_pids, 1, j);
1303 svm_pop_heap (oldheap);
1308 * Snapshoot names, can't hold root rp mutex across
1312 pool_foreach (subp, mp->subregions, ({
1313 name = vec_dup (subp->subregion_name);
1314 vec_add1(svm_names, name);
1318 pthread_mutex_unlock (&root_rp->mutex);
1320 for (i = 0; i < vec_len (svm_names); i++)
1322 vec_validate (a, 0);
1323 a->root_path = root_path;
1324 a->name = (char *) svm_names[i];
1325 rp = svm_region_find_or_create (a);
1328 pthread_mutex_lock (&rp->mutex);
1330 svm_client_scan_this_region_nolock (rp);
1332 pthread_mutex_unlock (&rp->mutex);
1333 svm_region_unmap (rp);
1334 vec_free (svm_names[i]);
1338 vec_free (svm_names);
1346 * fd.io coding-style-patch-verification: ON
1349 * eval: (c-set-style "gnu")