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 region_lock (svm_region_t * rp, int tag)
64 pthread_mutex_lock (&rp->mutex);
66 rp->mutex_owner_pid = getpid ();
67 rp->mutex_owner_tag = tag;
69 ASSERT (nheld < MAXLOCK);
71 * Keep score of held mutexes so we can try to exit
72 * cleanly if the world comes to an end at the worst possible
75 mutexes_held[nheld++] = &rp->mutex;
79 region_unlock (svm_region_t * rp)
83 rp->mutex_owner_pid = 0;
84 rp->mutex_owner_tag = 0;
87 for (i = nheld - 1; i >= 0; i--)
89 if (mutexes_held[i] == &rp->mutex)
91 for (j = i; j < MAXLOCK - 1; j++)
92 mutexes_held[j] = mutexes_held[j + 1];
100 CLIB_MEMORY_BARRIER ();
101 pthread_mutex_unlock (&rp->mutex);
106 format_svm_flags (u8 * s, va_list * args)
108 uword f = va_arg (*args, uword);
110 if (f & SVM_FLAGS_MHEAP)
111 s = format (s, "MHEAP ");
112 if (f & SVM_FLAGS_FILE)
113 s = format (s, "FILE ");
114 if (f & SVM_FLAGS_NODATA)
115 s = format (s, "NODATA ");
116 if (f & SVM_FLAGS_NEED_DATA_INIT)
117 s = format (s, "INIT ");
123 format_svm_size (u8 * s, va_list * args)
125 uword size = va_arg (*args, uword);
127 if (size >= (1 << 20))
129 s = format (s, "(%d mb)", size >> 20);
131 else if (size >= (1 << 10))
133 s = format (s, "(%d kb)", size >> 10);
137 s = format (s, "(%d bytes)", size);
143 format_svm_region (u8 * s, va_list * args)
145 svm_region_t *rp = va_arg (*args, svm_region_t *);
146 int verbose = va_arg (*args, int);
150 s = format (s, "%s: base va 0x%x size 0x%x %U\n",
151 rp->region_name, rp->virtual_base,
152 rp->virtual_size, format_svm_size, rp->virtual_size);
153 s = format (s, " user_ctx 0x%x, bitmap_size %d\n",
154 rp->user_ctx, rp->bitmap_size);
158 s = format (s, " flags: 0x%x %U\n", rp->flags,
159 format_svm_flags, rp->flags);
161 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
162 rp->region_heap, rp->data_base, rp->data_heap);
165 s = format (s, " %d clients, pids: ", vec_len (rp->client_pids));
167 for (i = 0; i < vec_len (rp->client_pids); i++)
168 s = format (s, "%d ", rp->client_pids[i]);
170 s = format (s, "\n");
176 s = format (s, " VM in use: ");
178 for (i = 0; i < rp->bitmap_size; i++)
180 if (clib_bitmap_get_no_check (rp->bitmap, i) != 0)
184 hi = lo = rp->virtual_base + i * MMAP_PAGESIZE;
188 hi = rp->virtual_base + i * MMAP_PAGESIZE;
195 hi = rp->virtual_base + i * MMAP_PAGESIZE - 1;
196 s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi,
202 s = format (s, " rgn heap stats: %U", format_mheap,
204 if ((rp->flags & SVM_FLAGS_MHEAP) && rp->data_heap)
206 s = format (s, "\n data heap stats: %U", format_mheap,
209 s = format (s, "\n");
217 * Round to a pagesize multiple, presumably 4k works
220 rnd_pagesize (u64 size)
224 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
229 * svm_data_region_setup
232 svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
238 map_size = rp->virtual_size - (MMAP_PAGESIZE +
239 (a->pvt_heap_size ? a->pvt_heap_size :
240 SVM_PVT_MHEAP_SIZE));
242 if (a->flags & SVM_FLAGS_FILE)
246 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
250 clib_unix_warning ("open");
254 if (fstat (fd, &statb) < 0)
256 clib_unix_warning ("fstat");
261 if (statb.st_mode & S_IFREG)
263 if (statb.st_size == 0)
265 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
267 clib_unix_warning ("seek region size");
271 if (write (fd, &junk, 1) != 1)
273 clib_unix_warning ("set region size");
280 map_size = rnd_pagesize (statb.st_size);
285 map_size = a->backing_mmap_size;
288 ASSERT (map_size <= rp->virtual_size -
289 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
291 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
292 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
294 clib_unix_warning ("mmap");
299 rp->backing_file = (char *) format (0, "%s\0", a->backing_file);
300 rp->flags |= SVM_FLAGS_FILE;
303 if (a->flags & SVM_FLAGS_MHEAP)
306 mheap_alloc_with_flags ((void *) (rp->data_base), map_size,
307 MHEAP_FLAG_DISABLE_VM);
308 rp->flags |= SVM_FLAGS_MHEAP;
314 svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
321 map_size = rp->virtual_size -
323 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
325 if (a->flags & SVM_FLAGS_FILE)
328 fd = open (a->backing_file, O_RDWR, 0777);
332 clib_unix_warning ("open");
336 if (fstat (fd, &statb) < 0)
338 clib_unix_warning ("fstat");
343 if (statb.st_mode & S_IFREG)
345 if (statb.st_size == 0)
347 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
349 clib_unix_warning ("seek region size");
353 if (write (fd, &junk, 1) != 1)
355 clib_unix_warning ("set region size");
362 map_size = rnd_pagesize (statb.st_size);
367 map_size = a->backing_mmap_size;
370 ASSERT (map_size <= rp->virtual_size
373 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
375 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
376 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
378 clib_unix_warning ("mmap");
388 shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
394 int root_path_offset = 0;
399 /* Tolerate present or absent slashes */
400 if (a->root_path[0] == '/')
403 /* create the root_path under /dev/shm
404 iterate through path creating directories */
406 path = format (0, "/dev/shm/%s%c", &a->root_path[root_path_offset], 0);
407 split_point = path + 1;
408 vec_add1 (mkdir_arg, '-');
412 while (*split_point && *split_point != '/')
414 vec_add1 (mkdir_arg, *split_point);
417 vec_add1 (mkdir_arg, 0);
419 /* ready to descend another level */
420 mkdir_arg[vec_len (mkdir_arg) - 1] = '-';
423 vec_free (mkdir_arg);
426 if (a->name[0] == '/')
429 shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset],
430 &a->name[name_offset], 0);
433 shm_name = format (0, "%s%c", a->name, 0);
441 svm_map_region (svm_map_region_args_t * a)
445 pthread_mutexattr_t attr;
446 pthread_condattr_t cattr;
453 int nbits, words, bit;
454 int pid_holding_region_lock;
456 int dead_region_recovery = 0;
459 struct timespec ts, tsrem;
461 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
464 shm_name = shm_name_from_svm_map_region_args (a);
467 clib_warning ("[%d] map region %s: shm_open (%s)",
468 getpid (), a->name, shm_name);
470 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
474 if (fchmod (svm_fd, 0770) < 0)
475 clib_unix_warning ("segment chmod");
476 /* This turns out to fail harmlessly if the client starts first */
477 if (fchown (svm_fd, a->uid, a->gid) < 0)
478 clib_unix_warning ("segment chown [ok if client starts first]");
482 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
484 clib_warning ("seek region size");
488 if (write (svm_fd, &junk, 1) != 1)
490 clib_warning ("set region size");
495 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
496 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
498 if (rp == (svm_region_t *) MAP_FAILED)
500 clib_unix_warning ("mmap create");
505 memset (rp, 0, sizeof (*rp));
507 if (pthread_mutexattr_init (&attr))
508 clib_unix_warning ("mutexattr_init");
510 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
511 clib_unix_warning ("mutexattr_setpshared");
513 if (pthread_mutex_init (&rp->mutex, &attr))
514 clib_unix_warning ("mutex_init");
516 if (pthread_mutexattr_destroy (&attr))
517 clib_unix_warning ("mutexattr_destroy");
519 if (pthread_condattr_init (&cattr))
520 clib_unix_warning ("condattr_init");
522 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
523 clib_unix_warning ("condattr_setpshared");
525 if (pthread_cond_init (&rp->condvar, &cattr))
526 clib_unix_warning ("cond_init");
528 if (pthread_condattr_destroy (&cattr))
529 clib_unix_warning ("condattr_destroy");
533 rp->virtual_base = a->baseva;
534 rp->virtual_size = a->size;
537 mheap_alloc_with_flags (uword_to_pointer
538 (a->baseva + MMAP_PAGESIZE, void *),
540 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
541 MHEAP_FLAG_DISABLE_VM);
542 oldheap = svm_push_pvt_heap (rp);
544 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
545 vec_add1 (rp->client_pids, getpid ());
547 nbits = rp->virtual_size / MMAP_PAGESIZE;
550 rp->bitmap_size = nbits;
551 words = (nbits + BITS (uword) - 1) / BITS (uword);
552 vec_validate (rp->bitmap, words - 1);
554 overhead_space = MMAP_PAGESIZE /* header */ +
555 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
558 data_base = (uword) rp->virtual_base;
560 if (a->flags & SVM_FLAGS_NODATA)
561 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
565 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
567 overhead_space -= MMAP_PAGESIZE;
568 data_base += MMAP_PAGESIZE;
570 while (overhead_space > 0);
572 rp->data_base = (void *) data_base;
575 * Note: although the POSIX spec guarantees that only one
576 * process enters this block, we have to play games
577 * to hold off clients until e.g. the mutex is ready
579 rp->version = SVM_VERSION;
581 /* setup the data portion of the region */
583 rv = svm_data_region_create (a, rp);
586 clib_warning ("data_region_create: %d", rv);
591 svm_pop_heap (oldheap);
593 return ((void *) rp);
597 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
603 perror ("svm_region_map(mmap open)");
610 if (0 != fstat (svm_fd, &stat))
612 clib_warning ("fstat failed: %d", errno);
616 if (stat.st_size > 0)
622 clib_warning ("waiting for resize of shm file timed out");
627 ts.tv_nsec = 100000000;
628 while (nanosleep (&ts, &tsrem) < 0)
633 rp = mmap (0, MMAP_PAGESIZE,
634 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
636 if (rp == (svm_region_t *) MAP_FAILED)
639 clib_warning ("mmap");
643 * We lost the footrace to create this region; make sure
644 * the winner has crossed the finish line.
646 while (rp->version == 0 && deadman++ < 5)
654 if (rp->version == 0)
656 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
658 munmap (rp, a->size);
661 /* Remap now that the region has been placed */
662 a->baseva = rp->virtual_base;
663 a->size = rp->virtual_size;
664 munmap (rp, MMAP_PAGESIZE);
666 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
667 PROT_READ | PROT_WRITE,
668 MAP_SHARED | MAP_FIXED, svm_fd, 0);
669 if ((uword) rp == (uword) MAP_FAILED)
671 clib_unix_warning ("mmap");
676 if ((uword) rp != rp->virtual_base)
678 clib_warning ("mmap botch");
682 * Try to fix the region mutex if it is held by
685 pid_holding_region_lock = rp->mutex_owner_pid;
686 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
689 ("region %s mutex held by dead pid %d, tag %d, force unlock",
690 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
691 /* owner pid is nonexistent */
692 rp->mutex.__data.__owner = 0;
693 rp->mutex.__data.__lock = 0;
694 dead_region_recovery = 1;
697 if (dead_region_recovery)
698 clib_warning ("recovery: attempt to re-lock region");
701 oldheap = svm_push_pvt_heap (rp);
702 vec_add1 (rp->client_pids, getpid ());
704 if (dead_region_recovery)
705 clib_warning ("recovery: attempt svm_data_region_map");
707 rv = svm_data_region_map (a, rp);
710 clib_warning ("data_region_map: %d", rv);
713 if (dead_region_recovery)
714 clib_warning ("unlock and continue");
718 svm_pop_heap (oldheap);
720 return ((void *) rp);
723 return 0; /* NOTREACHED */
727 svm_mutex_cleanup (void)
730 for (i = 0; i < nheld; i++)
732 pthread_mutex_unlock (mutexes_held[i]);
737 svm_region_init_internal (svm_map_region_args_t * a)
740 u64 ticks = clib_cpu_time_now ();
741 uword randomize_baseva;
743 /* guard against klutz calls */
749 atexit (svm_mutex_cleanup);
751 /* Randomize the shared-VM base at init time */
752 if (MMAP_PAGESIZE <= (4 << 10))
753 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
755 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
757 a->baseva += randomize_baseva;
759 rp = svm_map_region (a);
765 /* Set up the main region data structures */
766 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
768 svm_main_region_t *mp = 0;
771 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
773 oldheap = svm_push_pvt_heap (rp);
774 vec_validate (mp, 0);
775 mp->name_hash = hash_create_string (0, sizeof (uword));
776 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
778 svm_pop_heap (oldheap);
787 svm_region_init (void)
789 svm_map_region_args_t _a, *a = &_a;
791 memset (a, 0, sizeof (*a));
793 a->name = SVM_GLOBAL_REGION_NAME;
794 a->baseva = SVM_GLOBAL_REGION_BASEVA;
795 a->size = SVM_GLOBAL_REGION_SIZE;
796 a->flags = SVM_FLAGS_NODATA;
800 svm_region_init_internal (a);
804 svm_region_init_chroot (const char *root_path)
806 svm_map_region_args_t _a, *a = &_a;
808 memset (a, 0, sizeof (*a));
809 a->root_path = root_path;
810 a->name = SVM_GLOBAL_REGION_NAME;
811 a->baseva = SVM_GLOBAL_REGION_BASEVA;
812 a->size = SVM_GLOBAL_REGION_SIZE;
813 a->flags = SVM_FLAGS_NODATA;
817 return svm_region_init_internal (a);
821 svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
823 svm_map_region_args_t _a, *a = &_a;
825 memset (a, 0, sizeof (*a));
826 a->root_path = root_path;
827 a->name = SVM_GLOBAL_REGION_NAME;
828 a->baseva = SVM_GLOBAL_REGION_BASEVA;
829 a->size = SVM_GLOBAL_REGION_SIZE;
830 a->flags = SVM_FLAGS_NODATA;
834 svm_region_init_internal (a);
838 svm_region_init_args (svm_map_region_args_t * a)
840 svm_region_init_internal (a);
844 svm_region_find_or_create (svm_map_region_args_t * a)
846 svm_main_region_t *mp;
853 svm_subregion_t *subp;
857 a->size += MMAP_PAGESIZE +
858 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
859 a->size = rnd_pagesize (a->size);
861 region_lock (root_rp, 4);
862 oldheap = svm_push_pvt_heap (root_rp);
863 mp = root_rp->data_base;
867 /* Map the named region from the correct chroot environment */
868 if (a->root_path == NULL)
869 a->root_path = (char *) mp->root_path;
872 * See if this region is already known. If it is, we're
875 p = hash_get_mem (mp->name_hash, a->name);
879 rp = svm_map_region (a);
880 region_unlock (root_rp);
881 svm_pop_heap (oldheap);
885 /* Create the region. */
886 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
888 need_nbits = a->size / MMAP_PAGESIZE;
890 index = 1; /* $$$ fixme, figure out how many bit to really skip */
893 * Scan the virtual space allocation bitmap, looking for a large
898 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
900 for (i = 0; i < (need_nbits - 1); i++)
902 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
913 while (index < root_rp->bitmap_size);
915 /* Completely out of VM? */
916 if (index >= root_rp->bitmap_size)
918 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
919 root_rp->region_name, a->size, a->size);
920 svm_pop_heap (oldheap);
921 region_unlock (root_rp);
926 * Mark virtual space allocated
929 clib_warning ("set %d bits at index %d", need_nbits, index);
932 for (i = 0; i < need_nbits; i++)
934 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
937 /* Place this region where it goes... */
938 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
940 rp = svm_map_region (a);
942 pool_get (mp->subregions, subp);
943 name = format (0, "%s%c", a->name, 0);
944 subp->subregion_name = name;
946 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
948 svm_pop_heap (oldheap);
950 region_unlock (root_rp);
956 svm_region_unlink (svm_region_t * rp)
958 svm_map_region_args_t _a, *a = &_a;
959 svm_main_region_t *mp;
964 ASSERT (vec_c_string_is_terminated (rp->region_name));
966 mp = root_rp->data_base;
969 a->root_path = (char *) mp->root_path;
970 a->name = rp->region_name;
971 shm_name = shm_name_from_svm_map_region_args (a);
973 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
974 shm_unlink ((const char *) shm_name);
981 * Let go of the indicated region. If the calling process
982 * is the last customer, throw it away completely.
983 * The root region mutex guarantees atomicity with respect to
984 * a new region client showing up at the wrong moment.
987 svm_region_unmap (void *rp_arg)
989 int i, mypid = getpid ();
992 uword virtual_base, virtual_size;
993 svm_region_t *rp = rp_arg;
997 * If we take a signal while holding one or more shared-memory
998 * mutexes, we may end up back here from an otherwise
999 * benign exit handler. Bail out to avoid a recursive
1009 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
1011 region_lock (root_rp, 5);
1012 region_lock (rp, 6);
1014 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1016 /* Remove the caller from the list of mappers */
1017 for (i = 0; i < vec_len (rp->client_pids); i++)
1019 if (rp->client_pids[i] == mypid)
1021 vec_delete (rp->client_pids, 1, i);
1025 clib_warning ("pid %d AWOL", mypid);
1029 svm_pop_heap (oldheap);
1031 nclients_left = vec_len (rp->client_pids);
1032 virtual_base = rp->virtual_base;
1033 virtual_size = rp->virtual_size;
1035 if (nclients_left == 0)
1037 int index, nbits, i;
1038 svm_main_region_t *mp;
1040 svm_subregion_t *subp;
1042 /* Kill the region, last guy on his way out */
1044 oldheap = svm_push_pvt_heap (root_rp);
1045 name = vec_dup (rp->region_name);
1047 virtual_base = rp->virtual_base;
1048 virtual_size = rp->virtual_size;
1050 /* Figure out which bits to clear in the root region bitmap */
1051 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
1053 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
1056 clib_warning ("clear %d bits at index %d", nbits, index);
1058 /* Give back the allocated VM */
1059 for (i = 0; i < nbits; i++)
1061 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1064 mp = root_rp->data_base;
1066 p = hash_get_mem (mp->name_hash, name);
1068 /* Better never happen ... */
1072 region_unlock (root_rp);
1073 svm_pop_heap (oldheap);
1074 clib_warning ("Region name '%s' not found?", name);
1078 /* Remove from the root region subregion pool */
1079 subp = mp->subregions + p[0];
1080 pool_put (mp->subregions, subp);
1082 hash_unset_mem (mp->name_hash, name);
1087 svm_region_unlink (rp);
1088 munmap ((void *) virtual_base, virtual_size);
1089 region_unlock (root_rp);
1090 svm_pop_heap (oldheap);
1095 region_unlock (root_rp);
1097 munmap ((void *) virtual_base, virtual_size);
1107 int i, mypid = getpid ();
1108 uword virtual_base, virtual_size;
1110 /* It felt so nice we did it twice... */
1114 if (--root_rp_refcount > 0)
1118 * If we take a signal while holding one or more shared-memory
1119 * mutexes, we may end up back here from an otherwise
1120 * benign exit handler. Bail out to avoid a recursive
1126 region_lock (root_rp, 7);
1127 oldheap = svm_push_pvt_heap (root_rp);
1129 virtual_base = root_rp->virtual_base;
1130 virtual_size = root_rp->virtual_size;
1132 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1134 if (root_rp->client_pids[i] == mypid)
1136 vec_delete (root_rp->client_pids, 1, i);
1140 clib_warning ("pid %d AWOL", mypid);
1144 if (vec_len (root_rp->client_pids) == 0)
1145 svm_region_unlink (root_rp);
1147 region_unlock (root_rp);
1148 svm_pop_heap (oldheap);
1151 munmap ((void *) virtual_base, virtual_size);
1155 svm_client_scan_this_region_nolock (svm_region_t * rp)
1158 int mypid = getpid ();
1161 for (j = 0; j < vec_len (rp->client_pids); j++)
1163 if (mypid == rp->client_pids[j])
1165 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1167 clib_warning ("%s: cleanup ghost pid %d",
1168 rp->region_name, rp->client_pids[j]);
1169 /* nb: client vec in rp->region_heap */
1170 oldheap = svm_push_pvt_heap (rp);
1171 vec_delete (rp->client_pids, 1, j);
1173 svm_pop_heap (oldheap);
1180 * Scan svm regions for dead clients
1183 svm_client_scan (const char *root_path)
1186 svm_main_region_t *mp;
1187 svm_map_region_args_t *a = 0;
1188 svm_region_t *root_rp;
1190 svm_subregion_t *subp;
1194 int mypid = getpid ();
1196 vec_validate (a, 0);
1198 svm_region_init_chroot (root_path);
1200 root_rp = svm_get_root_rp ();
1202 pthread_mutex_lock (&root_rp->mutex);
1204 mp = root_rp->data_base;
1206 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1208 if (mypid == root_rp->client_pids[j])
1210 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1212 clib_warning ("%s: cleanup ghost pid %d",
1213 root_rp->region_name, root_rp->client_pids[j]);
1214 /* nb: client vec in root_rp->region_heap */
1215 oldheap = svm_push_pvt_heap (root_rp);
1216 vec_delete (root_rp->client_pids, 1, j);
1218 svm_pop_heap (oldheap);
1223 * Snapshoot names, can't hold root rp mutex across
1227 pool_foreach (subp, mp->subregions, ({
1228 name = vec_dup (subp->subregion_name);
1229 vec_add1(svm_names, name);
1233 pthread_mutex_unlock (&root_rp->mutex);
1235 for (i = 0; i < vec_len (svm_names); i++)
1237 vec_validate (a, 0);
1238 a->root_path = root_path;
1239 a->name = (char *) svm_names[i];
1240 rp = svm_region_find_or_create (a);
1243 pthread_mutex_lock (&rp->mutex);
1245 svm_client_scan_this_region_nolock (rp);
1247 pthread_mutex_unlock (&rp->mutex);
1248 svm_region_unmap (rp);
1249 vec_free (svm_names[i]);
1253 vec_free (svm_names);
1261 * fd.io coding-style-patch-verification: ON
1264 * eval: (c-set-style "gnu")