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,
250 * Round to a pagesize multiple, presumably 4k works
253 rnd_pagesize (u64 size)
257 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
262 * svm_data_region_setup
265 svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
271 map_size = rp->virtual_size - (MMAP_PAGESIZE +
272 (a->pvt_heap_size ? a->pvt_heap_size :
273 SVM_PVT_MHEAP_SIZE));
275 if (a->flags & SVM_FLAGS_FILE)
279 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
283 clib_unix_warning ("open");
287 if (fstat (fd, &statb) < 0)
289 clib_unix_warning ("fstat");
294 if (statb.st_mode & S_IFREG)
296 if (statb.st_size == 0)
298 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
300 clib_unix_warning ("seek region size");
304 if (write (fd, &junk, 1) != 1)
306 clib_unix_warning ("set region size");
313 map_size = rnd_pagesize (statb.st_size);
318 map_size = a->backing_mmap_size;
321 ASSERT (map_size <= rp->virtual_size -
322 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
324 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
325 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
327 clib_unix_warning ("mmap");
332 rp->backing_file = (char *) format (0, "%s\0", a->backing_file);
333 rp->flags |= SVM_FLAGS_FILE;
336 if (a->flags & SVM_FLAGS_MHEAP)
338 rp->data_heap = create_mspace_with_base (rp->data_base,
339 map_size, 1 /* locked */ );
340 mspace_disable_expand (rp->data_heap);
342 rp->flags |= SVM_FLAGS_MHEAP;
348 svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
355 map_size = rp->virtual_size -
357 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
359 if (a->flags & SVM_FLAGS_FILE)
362 fd = open (a->backing_file, O_RDWR, 0777);
366 clib_unix_warning ("open");
370 if (fstat (fd, &statb) < 0)
372 clib_unix_warning ("fstat");
377 if (statb.st_mode & S_IFREG)
379 if (statb.st_size == 0)
381 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
383 clib_unix_warning ("seek region size");
387 if (write (fd, &junk, 1) != 1)
389 clib_unix_warning ("set region size");
396 map_size = rnd_pagesize (statb.st_size);
401 map_size = a->backing_mmap_size;
404 ASSERT (map_size <= rp->virtual_size
407 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
409 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
410 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
412 clib_unix_warning ("mmap");
422 shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
425 int root_path_offset = 0;
430 /* Tolerate present or absent slashes */
431 if (a->root_path[0] == '/')
434 if (a->name[0] == '/')
437 shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset],
438 &a->name[name_offset], 0);
441 shm_name = format (0, "%s%c", a->name, 0);
446 svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp)
448 pthread_mutexattr_t attr;
449 pthread_condattr_t cattr;
450 int nbits, words, bit;
457 clib_memset (rp, 0, sizeof (*rp));
459 if (pthread_mutexattr_init (&attr))
460 clib_unix_warning ("mutexattr_init");
462 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
463 clib_unix_warning ("mutexattr_setpshared");
465 if (pthread_mutex_init (&rp->mutex, &attr))
466 clib_unix_warning ("mutex_init");
468 if (pthread_mutexattr_destroy (&attr))
469 clib_unix_warning ("mutexattr_destroy");
471 if (pthread_condattr_init (&cattr))
472 clib_unix_warning ("condattr_init");
474 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
475 clib_unix_warning ("condattr_setpshared");
477 if (pthread_cond_init (&rp->condvar, &cattr))
478 clib_unix_warning ("cond_init");
480 if (pthread_condattr_destroy (&cattr))
481 clib_unix_warning ("condattr_destroy");
485 rp->virtual_base = a->baseva;
486 rp->virtual_size = a->size;
488 rp->region_heap = create_mspace_with_base
489 (uword_to_pointer (a->baseva + MMAP_PAGESIZE, void *),
491 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, 1 /* locked */ );
493 mspace_disable_expand (rp->region_heap);
495 oldheap = svm_push_pvt_heap (rp);
497 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
498 vec_add1 (rp->client_pids, getpid ());
500 nbits = rp->virtual_size / MMAP_PAGESIZE;
503 rp->bitmap_size = nbits;
504 words = (nbits + BITS (uword) - 1) / BITS (uword);
505 vec_validate (rp->bitmap, words - 1);
507 overhead_space = MMAP_PAGESIZE /* header */ +
508 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
511 data_base = (uword) rp->virtual_base;
513 if (a->flags & SVM_FLAGS_NODATA)
514 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
518 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
520 overhead_space -= MMAP_PAGESIZE;
521 data_base += MMAP_PAGESIZE;
523 while (overhead_space > 0);
525 rp->data_base = (void *) data_base;
528 * Note: although the POSIX spec guarantees that only one
529 * process enters this block, we have to play games
530 * to hold off clients until e.g. the mutex is ready
532 rp->version = SVM_VERSION;
534 /* setup the data portion of the region */
536 rv = svm_data_region_create (a, rp);
539 clib_warning ("data_region_create: %d", rv);
544 svm_pop_heap (oldheap);
551 svm_map_region (svm_map_region_args_t * a)
559 int pid_holding_region_lock;
561 int dead_region_recovery = 0;
564 struct timespec ts, tsrem;
566 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
569 shm_name = shm_name_from_svm_map_region_args (a);
572 clib_warning ("[%d] map region %s: shm_open (%s)",
573 getpid (), a->name, shm_name);
575 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
579 if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
580 clib_unix_warning ("segment chmod");
581 /* This turns out to fail harmlessly if the client starts first */
582 if (fchown (svm_fd, a->uid, a->gid) < 0)
583 clib_unix_warning ("segment chown [ok if client starts first]");
587 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
589 clib_warning ("seek region size");
593 if (write (svm_fd, &junk, 1) != 1)
595 clib_warning ("set region size");
600 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
601 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
603 if (rp == (svm_region_t *) MAP_FAILED)
605 clib_unix_warning ("mmap create");
611 svm_region_init_mapped_region (a, rp);
613 return ((void *) rp);
617 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
623 perror ("svm_region_map(mmap open)");
627 /* Reset ownership in case the client started first */
628 if (fchown (svm_fd, a->uid, a->gid) < 0)
629 clib_unix_warning ("segment chown [ok if client starts first]");
634 if (0 != fstat (svm_fd, &stat))
636 clib_warning ("fstat failed: %d", errno);
640 if (stat.st_size > 0)
646 clib_warning ("waiting for resize of shm file timed out");
651 ts.tv_nsec = 100000000;
652 while (nanosleep (&ts, &tsrem) < 0)
657 rp = mmap (0, MMAP_PAGESIZE,
658 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
660 if (rp == (svm_region_t *) MAP_FAILED)
663 clib_warning ("mmap");
667 * We lost the footrace to create this region; make sure
668 * the winner has crossed the finish line.
670 while (rp->version == 0 && deadman++ < 5)
678 if (rp->version == 0)
680 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
682 munmap (rp, a->size);
685 /* Remap now that the region has been placed */
686 a->baseva = rp->virtual_base;
687 a->size = rp->virtual_size;
688 munmap (rp, MMAP_PAGESIZE);
690 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
691 PROT_READ | PROT_WRITE,
692 MAP_SHARED | MAP_FIXED, svm_fd, 0);
693 if ((uword) rp == (uword) MAP_FAILED)
695 clib_unix_warning ("mmap");
702 if ((uword) rp != rp->virtual_base)
704 clib_warning ("mmap botch");
708 * Try to fix the region mutex if it is held by
711 pid_holding_region_lock = rp->mutex_owner_pid;
712 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
715 ("region %s mutex held by dead pid %d, tag %d, force unlock",
716 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
717 /* owner pid is nonexistent */
718 rp->mutex.__data.__owner = 0;
719 rp->mutex.__data.__lock = 0;
720 dead_region_recovery = 1;
723 if (dead_region_recovery)
724 clib_warning ("recovery: attempt to re-lock region");
727 oldheap = svm_push_pvt_heap (rp);
728 vec_add1 (rp->client_pids, getpid ());
730 if (dead_region_recovery)
731 clib_warning ("recovery: attempt svm_data_region_map");
733 rv = svm_data_region_map (a, rp);
736 clib_warning ("data_region_map: %d", rv);
739 if (dead_region_recovery)
740 clib_warning ("unlock and continue");
744 svm_pop_heap (oldheap);
746 return ((void *) rp);
749 return 0; /* NOTREACHED */
753 svm_mutex_cleanup (void)
756 for (i = 0; i < nheld; i++)
758 pthread_mutex_unlock (mutexes_held[i]);
763 svm_region_init_internal (svm_map_region_args_t * a)
766 u64 ticks = clib_cpu_time_now ();
767 uword randomize_baseva;
769 /* guard against klutz calls */
775 atexit (svm_mutex_cleanup);
777 /* Randomize the shared-VM base at init time */
778 if (MMAP_PAGESIZE <= (4 << 10))
779 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
781 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
783 a->baseva += randomize_baseva;
785 rp = svm_map_region (a);
791 /* Set up the main region data structures */
792 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
794 svm_main_region_t *mp = 0;
797 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
799 oldheap = svm_push_pvt_heap (rp);
800 vec_validate (mp, 0);
801 mp->name_hash = hash_create_string (0, sizeof (uword));
802 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
806 svm_pop_heap (oldheap);
815 svm_region_init (void)
817 svm_map_region_args_t _a, *a = &_a;
819 clib_memset (a, 0, sizeof (*a));
821 a->name = SVM_GLOBAL_REGION_NAME;
822 a->baseva = svm_get_global_region_base_va ();
823 a->size = SVM_GLOBAL_REGION_SIZE;
824 a->flags = SVM_FLAGS_NODATA;
828 svm_region_init_internal (a);
832 svm_region_init_chroot (const char *root_path)
834 svm_map_region_args_t _a, *a = &_a;
836 clib_memset (a, 0, sizeof (*a));
837 a->root_path = root_path;
838 a->name = SVM_GLOBAL_REGION_NAME;
839 a->baseva = svm_get_global_region_base_va ();
840 a->size = SVM_GLOBAL_REGION_SIZE;
841 a->flags = SVM_FLAGS_NODATA;
845 return svm_region_init_internal (a);
849 svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
851 svm_map_region_args_t _a, *a = &_a;
853 clib_memset (a, 0, sizeof (*a));
854 a->root_path = root_path;
855 a->name = SVM_GLOBAL_REGION_NAME;
856 a->baseva = svm_get_global_region_base_va ();
857 a->size = SVM_GLOBAL_REGION_SIZE;
858 a->flags = SVM_FLAGS_NODATA;
862 svm_region_init_internal (a);
866 svm_region_init_args (svm_map_region_args_t * a)
868 svm_region_init_internal (a);
872 svm_region_find_or_create (svm_map_region_args_t * a)
874 svm_main_region_t *mp;
881 svm_subregion_t *subp;
885 a->size += MMAP_PAGESIZE +
886 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
887 a->size = rnd_pagesize (a->size);
889 region_lock (root_rp, 4);
890 oldheap = svm_push_pvt_heap (root_rp);
891 mp = root_rp->data_base;
895 /* Map the named region from the correct chroot environment */
896 if (a->root_path == NULL)
897 a->root_path = (char *) mp->root_path;
900 * See if this region is already known. If it is, we're
903 p = hash_get_mem (mp->name_hash, a->name);
907 rp = svm_map_region (a);
908 region_unlock (root_rp);
909 svm_pop_heap (oldheap);
913 /* Create the region. */
914 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
916 need_nbits = a->size / MMAP_PAGESIZE;
918 index = 1; /* $$$ fixme, figure out how many bit to really skip */
921 * Scan the virtual space allocation bitmap, looking for a large
926 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
928 for (i = 0; i < (need_nbits - 1); i++)
930 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
941 while (index < root_rp->bitmap_size);
943 /* Completely out of VM? */
944 if (index >= root_rp->bitmap_size)
946 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
947 root_rp->region_name, a->size, a->size);
948 svm_pop_heap (oldheap);
949 region_unlock (root_rp);
954 * Mark virtual space allocated
957 clib_warning ("set %d bits at index %d", need_nbits, index);
960 for (i = 0; i < need_nbits; i++)
962 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
965 /* Place this region where it goes... */
966 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
968 rp = svm_map_region (a);
970 pool_get (mp->subregions, subp);
971 name = format (0, "%s%c", a->name, 0);
972 subp->subregion_name = name;
974 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
976 svm_pop_heap (oldheap);
978 region_unlock (root_rp);
984 svm_region_unlink (svm_region_t * rp)
986 svm_map_region_args_t _a, *a = &_a;
987 svm_main_region_t *mp;
992 ASSERT (vec_c_string_is_terminated (rp->region_name));
994 mp = root_rp->data_base;
997 a->root_path = (char *) mp->root_path;
998 a->name = rp->region_name;
999 shm_name = shm_name_from_svm_map_region_args (a);
1001 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
1002 shm_unlink ((const char *) shm_name);
1003 vec_free (shm_name);
1009 * Let go of the indicated region. If the calling process
1010 * is the last customer, throw it away completely.
1011 * The root region mutex guarantees atomicity with respect to
1012 * a new region client showing up at the wrong moment.
1015 svm_region_unmap_internal (void *rp_arg, u8 is_client)
1017 int i, mypid = getpid ();
1020 uword virtual_base, virtual_size;
1021 svm_region_t *rp = rp_arg;
1025 * If we take a signal while holding one or more shared-memory
1026 * mutexes, we may end up back here from an otherwise
1027 * benign exit handler. Bail out to avoid a recursive
1037 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
1039 region_lock (root_rp, 5);
1040 region_lock (rp, 6);
1042 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1044 /* Remove the caller from the list of mappers */
1045 for (i = 0; i < vec_len (rp->client_pids); i++)
1047 if (rp->client_pids[i] == mypid)
1049 vec_delete (rp->client_pids, 1, i);
1053 clib_warning ("pid %d AWOL", mypid);
1057 svm_pop_heap (oldheap);
1059 nclients_left = vec_len (rp->client_pids);
1060 virtual_base = rp->virtual_base;
1061 virtual_size = rp->virtual_size;
1063 if (nclients_left == 0)
1065 int index, nbits, i;
1066 svm_main_region_t *mp;
1068 svm_subregion_t *subp;
1070 /* Kill the region, last guy on his way out */
1072 oldheap = svm_push_pvt_heap (root_rp);
1073 name = vec_dup (rp->region_name);
1075 virtual_base = rp->virtual_base;
1076 virtual_size = rp->virtual_size;
1078 /* Figure out which bits to clear in the root region bitmap */
1079 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
1081 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
1084 clib_warning ("clear %d bits at index %d", nbits, index);
1086 /* Give back the allocated VM */
1087 for (i = 0; i < nbits; i++)
1089 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1092 mp = root_rp->data_base;
1094 p = hash_get_mem (mp->name_hash, name);
1096 /* Better never happen ... */
1100 region_unlock (root_rp);
1101 svm_pop_heap (oldheap);
1102 clib_warning ("Region name '%s' not found?", name);
1106 /* Remove from the root region subregion pool */
1107 subp = mp->subregions + p[0];
1108 pool_put (mp->subregions, subp);
1110 hash_unset_mem (mp->name_hash, name);
1116 /* If a client asks for the cleanup, don't unlink the backing
1117 * file since we can't tell if it has been recreated. */
1119 svm_region_unlink (rp);
1121 munmap ((void *) virtual_base, virtual_size);
1122 region_unlock (root_rp);
1123 svm_pop_heap (oldheap);
1128 region_unlock (root_rp);
1130 munmap ((void *) virtual_base, virtual_size);
1134 svm_region_unmap (void *rp_arg)
1136 svm_region_unmap_internal (rp_arg, 0 /* is_client */ );
1140 svm_region_unmap_client (void *rp_arg)
1142 svm_region_unmap_internal (rp_arg, 1 /* is_client */ );
1149 svm_region_exit_internal (u8 is_client)
1152 int i, mypid = getpid ();
1153 uword virtual_base, virtual_size;
1155 /* It felt so nice we did it twice... */
1159 if (--root_rp_refcount > 0)
1163 * If we take a signal while holding one or more shared-memory
1164 * mutexes, we may end up back here from an otherwise
1165 * benign exit handler. Bail out to avoid a recursive
1171 region_lock (root_rp, 7);
1172 oldheap = svm_push_pvt_heap (root_rp);
1174 virtual_base = root_rp->virtual_base;
1175 virtual_size = root_rp->virtual_size;
1177 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1179 if (root_rp->client_pids[i] == mypid)
1181 vec_delete (root_rp->client_pids, 1, i);
1185 clib_warning ("pid %d AWOL", mypid);
1189 if (!is_client && vec_len (root_rp->client_pids) == 0)
1190 svm_region_unlink (root_rp);
1192 region_unlock (root_rp);
1193 svm_pop_heap (oldheap);
1196 munmap ((void *) virtual_base, virtual_size);
1200 svm_region_exit (void)
1202 svm_region_exit_internal (0 /* is_client */ );
1206 svm_region_exit_client (void)
1208 svm_region_exit_internal (1 /* is_client */ );
1212 svm_client_scan_this_region_nolock (svm_region_t * rp)
1215 int mypid = getpid ();
1218 for (j = 0; j < vec_len (rp->client_pids); j++)
1220 if (mypid == rp->client_pids[j])
1222 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1224 clib_warning ("%s: cleanup ghost pid %d",
1225 rp->region_name, rp->client_pids[j]);
1226 /* nb: client vec in rp->region_heap */
1227 oldheap = svm_push_pvt_heap (rp);
1228 vec_delete (rp->client_pids, 1, j);
1230 svm_pop_heap (oldheap);
1237 * Scan svm regions for dead clients
1240 svm_client_scan (const char *root_path)
1243 svm_main_region_t *mp;
1244 svm_map_region_args_t *a = 0;
1245 svm_region_t *root_rp;
1247 svm_subregion_t *subp;
1251 int mypid = getpid ();
1253 vec_validate (a, 0);
1255 svm_region_init_chroot (root_path);
1257 root_rp = svm_get_root_rp ();
1259 pthread_mutex_lock (&root_rp->mutex);
1261 mp = root_rp->data_base;
1263 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1265 if (mypid == root_rp->client_pids[j])
1267 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1269 clib_warning ("%s: cleanup ghost pid %d",
1270 root_rp->region_name, root_rp->client_pids[j]);
1271 /* nb: client vec in root_rp->region_heap */
1272 oldheap = svm_push_pvt_heap (root_rp);
1273 vec_delete (root_rp->client_pids, 1, j);
1275 svm_pop_heap (oldheap);
1280 * Snapshoot names, can't hold root rp mutex across
1284 pool_foreach (subp, mp->subregions, ({
1285 name = vec_dup (subp->subregion_name);
1286 vec_add1(svm_names, name);
1290 pthread_mutex_unlock (&root_rp->mutex);
1292 for (i = 0; i < vec_len (svm_names); i++)
1294 vec_validate (a, 0);
1295 a->root_path = root_path;
1296 a->name = (char *) svm_names[i];
1297 rp = svm_region_find_or_create (a);
1300 pthread_mutex_lock (&rp->mutex);
1302 svm_client_scan_this_region_nolock (rp);
1304 pthread_mutex_unlock (&rp->mutex);
1305 svm_region_unmap (rp);
1306 vec_free (svm_names[i]);
1310 vec_free (svm_names);
1318 * fd.io coding-style-patch-verification: ON
1321 * eval: (c-set-style "gnu")