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 ()
65 /* On AArch64 VA space can have different size, from 36 to 48 bits.
66 Here we are trying to detect VA bits by parsing /proc/self/maps
69 unformat_input_t input;
73 if ((fd = open ("/proc/self/maps", 0)) < 0)
74 clib_unix_error ("open '/proc/self/maps'");
76 unformat_init_clib_file (&input, fd);
77 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
79 if (unformat (&input, "%llx-%llx", &start, &end))
81 unformat_skip_line (&input);
83 unformat_free (&input);
86 bits = count_leading_zeros (end);
88 if (bits >= 36 && bits <= 48)
89 return ((1ul << bits) / 4) - (2 * SVM_GLOBAL_REGION_SIZE);
91 clib_unix_error ("unexpected va bits '%u'", bits);
99 region_lock (svm_region_t * rp, int tag)
101 pthread_mutex_lock (&rp->mutex);
103 rp->mutex_owner_pid = getpid ();
104 rp->mutex_owner_tag = tag;
106 ASSERT (nheld < MAXLOCK);
108 * Keep score of held mutexes so we can try to exit
109 * cleanly if the world comes to an end at the worst possible
112 mutexes_held[nheld++] = &rp->mutex;
116 region_unlock (svm_region_t * rp)
120 rp->mutex_owner_pid = 0;
121 rp->mutex_owner_tag = 0;
124 for (i = nheld - 1; i >= 0; i--)
126 if (mutexes_held[i] == &rp->mutex)
128 for (j = i; j < MAXLOCK - 1; j++)
129 mutexes_held[j] = mutexes_held[j + 1];
137 CLIB_MEMORY_BARRIER ();
138 pthread_mutex_unlock (&rp->mutex);
143 format_svm_flags (u8 * s, va_list * args)
145 uword f = va_arg (*args, uword);
147 if (f & SVM_FLAGS_MHEAP)
148 s = format (s, "MHEAP ");
149 if (f & SVM_FLAGS_FILE)
150 s = format (s, "FILE ");
151 if (f & SVM_FLAGS_NODATA)
152 s = format (s, "NODATA ");
153 if (f & SVM_FLAGS_NEED_DATA_INIT)
154 s = format (s, "INIT ");
160 format_svm_size (u8 * s, va_list * args)
162 uword size = va_arg (*args, uword);
164 if (size >= (1 << 20))
166 s = format (s, "(%d mb)", size >> 20);
168 else if (size >= (1 << 10))
170 s = format (s, "(%d kb)", size >> 10);
174 s = format (s, "(%d bytes)", size);
180 format_svm_region (u8 * s, va_list * args)
182 svm_region_t *rp = va_arg (*args, svm_region_t *);
183 int verbose = va_arg (*args, int);
187 s = format (s, "%s: base va 0x%x size 0x%x %U\n",
188 rp->region_name, rp->virtual_base,
189 rp->virtual_size, format_svm_size, rp->virtual_size);
190 s = format (s, " user_ctx 0x%x, bitmap_size %d\n",
191 rp->user_ctx, rp->bitmap_size);
195 s = format (s, " flags: 0x%x %U\n", rp->flags,
196 format_svm_flags, rp->flags);
198 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
199 rp->region_heap, rp->data_base, rp->data_heap);
202 s = format (s, " %d clients, pids: ", vec_len (rp->client_pids));
204 for (i = 0; i < vec_len (rp->client_pids); i++)
205 s = format (s, "%d ", rp->client_pids[i]);
207 s = format (s, "\n");
213 s = format (s, " VM in use: ");
215 for (i = 0; i < rp->bitmap_size; i++)
217 if (clib_bitmap_get_no_check (rp->bitmap, i) != 0)
221 hi = lo = rp->virtual_base + i * MMAP_PAGESIZE;
225 hi = rp->virtual_base + i * MMAP_PAGESIZE;
232 hi = rp->virtual_base + i * MMAP_PAGESIZE - 1;
233 s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi,
239 s = format (s, " rgn heap stats: %U", format_mheap,
241 if ((rp->flags & SVM_FLAGS_MHEAP) && rp->data_heap)
243 s = format (s, "\n data heap stats: %U", format_mheap,
246 s = format (s, "\n");
254 * Round to a pagesize multiple, presumably 4k works
257 rnd_pagesize (u64 size)
261 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
266 * svm_data_region_setup
269 svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
275 map_size = rp->virtual_size - (MMAP_PAGESIZE +
276 (a->pvt_heap_size ? a->pvt_heap_size :
277 SVM_PVT_MHEAP_SIZE));
279 if (a->flags & SVM_FLAGS_FILE)
283 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
287 clib_unix_warning ("open");
291 if (fstat (fd, &statb) < 0)
293 clib_unix_warning ("fstat");
298 if (statb.st_mode & S_IFREG)
300 if (statb.st_size == 0)
302 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
304 clib_unix_warning ("seek region size");
308 if (write (fd, &junk, 1) != 1)
310 clib_unix_warning ("set region size");
317 map_size = rnd_pagesize (statb.st_size);
322 map_size = a->backing_mmap_size;
325 ASSERT (map_size <= rp->virtual_size -
326 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
328 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
329 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
331 clib_unix_warning ("mmap");
336 rp->backing_file = (char *) format (0, "%s\0", a->backing_file);
337 rp->flags |= SVM_FLAGS_FILE;
340 if (a->flags & SVM_FLAGS_MHEAP)
342 mheap_t *heap_header;
344 mheap_alloc_with_flags ((void *) (rp->data_base), map_size,
345 MHEAP_FLAG_DISABLE_VM);
346 heap_header = mheap_header (rp->data_heap);
347 heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
349 rp->flags |= SVM_FLAGS_MHEAP;
355 svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
362 map_size = rp->virtual_size -
364 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
366 if (a->flags & SVM_FLAGS_FILE)
369 fd = open (a->backing_file, O_RDWR, 0777);
373 clib_unix_warning ("open");
377 if (fstat (fd, &statb) < 0)
379 clib_unix_warning ("fstat");
384 if (statb.st_mode & S_IFREG)
386 if (statb.st_size == 0)
388 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
390 clib_unix_warning ("seek region size");
394 if (write (fd, &junk, 1) != 1)
396 clib_unix_warning ("set region size");
403 map_size = rnd_pagesize (statb.st_size);
408 map_size = a->backing_mmap_size;
411 ASSERT (map_size <= rp->virtual_size
414 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
416 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
417 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
419 clib_unix_warning ("mmap");
429 shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
435 int root_path_offset = 0;
440 /* Tolerate present or absent slashes */
441 if (a->root_path[0] == '/')
444 /* create the root_path under /dev/shm
445 iterate through path creating directories */
447 path = format (0, "/dev/shm/%s%c", &a->root_path[root_path_offset], 0);
448 split_point = path + 1;
449 vec_add1 (mkdir_arg, '-');
453 while (*split_point && *split_point != '/')
455 vec_add1 (mkdir_arg, *split_point);
458 vec_add1 (mkdir_arg, 0);
460 /* ready to descend another level */
461 mkdir_arg[vec_len (mkdir_arg) - 1] = '-';
464 vec_free (mkdir_arg);
467 if (a->name[0] == '/')
470 shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset],
471 &a->name[name_offset], 0);
474 shm_name = format (0, "%s%c", a->name, 0);
479 svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp)
481 pthread_mutexattr_t attr;
482 pthread_condattr_t cattr;
483 int nbits, words, bit;
490 memset (rp, 0, sizeof (*rp));
492 if (pthread_mutexattr_init (&attr))
493 clib_unix_warning ("mutexattr_init");
495 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
496 clib_unix_warning ("mutexattr_setpshared");
498 if (pthread_mutex_init (&rp->mutex, &attr))
499 clib_unix_warning ("mutex_init");
501 if (pthread_mutexattr_destroy (&attr))
502 clib_unix_warning ("mutexattr_destroy");
504 if (pthread_condattr_init (&cattr))
505 clib_unix_warning ("condattr_init");
507 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
508 clib_unix_warning ("condattr_setpshared");
510 if (pthread_cond_init (&rp->condvar, &cattr))
511 clib_unix_warning ("cond_init");
513 if (pthread_condattr_destroy (&cattr))
514 clib_unix_warning ("condattr_destroy");
518 rp->virtual_base = a->baseva;
519 rp->virtual_size = a->size;
522 mheap_alloc_with_flags (uword_to_pointer
523 (a->baseva + MMAP_PAGESIZE, void *),
525 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
526 MHEAP_FLAG_DISABLE_VM);
527 oldheap = svm_push_pvt_heap (rp);
529 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
530 vec_add1 (rp->client_pids, getpid ());
532 nbits = rp->virtual_size / MMAP_PAGESIZE;
535 rp->bitmap_size = nbits;
536 words = (nbits + BITS (uword) - 1) / BITS (uword);
537 vec_validate (rp->bitmap, words - 1);
539 overhead_space = MMAP_PAGESIZE /* header */ +
540 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
543 data_base = (uword) rp->virtual_base;
545 if (a->flags & SVM_FLAGS_NODATA)
546 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
550 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
552 overhead_space -= MMAP_PAGESIZE;
553 data_base += MMAP_PAGESIZE;
555 while (overhead_space > 0);
557 rp->data_base = (void *) data_base;
560 * Note: although the POSIX spec guarantees that only one
561 * process enters this block, we have to play games
562 * to hold off clients until e.g. the mutex is ready
564 rp->version = SVM_VERSION;
566 /* setup the data portion of the region */
568 rv = svm_data_region_create (a, rp);
571 clib_warning ("data_region_create: %d", rv);
576 svm_pop_heap (oldheap);
583 svm_map_region (svm_map_region_args_t * a)
591 int pid_holding_region_lock;
593 int dead_region_recovery = 0;
596 struct timespec ts, tsrem;
598 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
601 shm_name = shm_name_from_svm_map_region_args (a);
604 clib_warning ("[%d] map region %s: shm_open (%s)",
605 getpid (), a->name, shm_name);
607 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
611 if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
612 clib_unix_warning ("segment chmod");
613 /* This turns out to fail harmlessly if the client starts first */
614 if (fchown (svm_fd, a->uid, a->gid) < 0)
615 clib_unix_warning ("segment chown [ok if client starts first]");
619 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
621 clib_warning ("seek region size");
625 if (write (svm_fd, &junk, 1) != 1)
627 clib_warning ("set region size");
632 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
633 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
635 if (rp == (svm_region_t *) MAP_FAILED)
637 clib_unix_warning ("mmap create");
643 svm_region_init_mapped_region (a, rp);
645 return ((void *) rp);
649 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
655 perror ("svm_region_map(mmap open)");
659 /* Reset ownership in case the client started first */
660 if (fchown (svm_fd, a->uid, a->gid) < 0)
661 clib_unix_warning ("segment chown [ok if client starts first]");
666 if (0 != fstat (svm_fd, &stat))
668 clib_warning ("fstat failed: %d", errno);
672 if (stat.st_size > 0)
678 clib_warning ("waiting for resize of shm file timed out");
683 ts.tv_nsec = 100000000;
684 while (nanosleep (&ts, &tsrem) < 0)
689 rp = mmap (0, MMAP_PAGESIZE,
690 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
692 if (rp == (svm_region_t *) MAP_FAILED)
695 clib_warning ("mmap");
699 * We lost the footrace to create this region; make sure
700 * the winner has crossed the finish line.
702 while (rp->version == 0 && deadman++ < 5)
710 if (rp->version == 0)
712 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
714 munmap (rp, a->size);
717 /* Remap now that the region has been placed */
718 a->baseva = rp->virtual_base;
719 a->size = rp->virtual_size;
720 munmap (rp, MMAP_PAGESIZE);
722 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
723 PROT_READ | PROT_WRITE,
724 MAP_SHARED | MAP_FIXED, svm_fd, 0);
725 if ((uword) rp == (uword) MAP_FAILED)
727 clib_unix_warning ("mmap");
734 if ((uword) rp != rp->virtual_base)
736 clib_warning ("mmap botch");
740 * Try to fix the region mutex if it is held by
743 pid_holding_region_lock = rp->mutex_owner_pid;
744 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
747 ("region %s mutex held by dead pid %d, tag %d, force unlock",
748 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
749 /* owner pid is nonexistent */
750 rp->mutex.__data.__owner = 0;
751 rp->mutex.__data.__lock = 0;
752 dead_region_recovery = 1;
755 if (dead_region_recovery)
756 clib_warning ("recovery: attempt to re-lock region");
759 oldheap = svm_push_pvt_heap (rp);
760 vec_add1 (rp->client_pids, getpid ());
762 if (dead_region_recovery)
763 clib_warning ("recovery: attempt svm_data_region_map");
765 rv = svm_data_region_map (a, rp);
768 clib_warning ("data_region_map: %d", rv);
771 if (dead_region_recovery)
772 clib_warning ("unlock and continue");
776 svm_pop_heap (oldheap);
778 return ((void *) rp);
781 return 0; /* NOTREACHED */
785 svm_mutex_cleanup (void)
788 for (i = 0; i < nheld; i++)
790 pthread_mutex_unlock (mutexes_held[i]);
795 svm_region_init_internal (svm_map_region_args_t * a)
798 u64 ticks = clib_cpu_time_now ();
799 uword randomize_baseva;
801 /* guard against klutz calls */
807 atexit (svm_mutex_cleanup);
809 /* Randomize the shared-VM base at init time */
810 if (MMAP_PAGESIZE <= (4 << 10))
811 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
813 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
815 a->baseva += randomize_baseva;
817 rp = svm_map_region (a);
823 /* Set up the main region data structures */
824 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
826 svm_main_region_t *mp = 0;
829 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
831 oldheap = svm_push_pvt_heap (rp);
832 vec_validate (mp, 0);
833 mp->name_hash = hash_create_string (0, sizeof (uword));
834 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
838 svm_pop_heap (oldheap);
847 svm_region_init (void)
849 svm_map_region_args_t _a, *a = &_a;
851 memset (a, 0, sizeof (*a));
853 a->name = SVM_GLOBAL_REGION_NAME;
854 a->baseva = svm_get_global_region_base_va ();
855 a->size = SVM_GLOBAL_REGION_SIZE;
856 a->flags = SVM_FLAGS_NODATA;
860 svm_region_init_internal (a);
864 svm_region_init_chroot (const char *root_path)
866 svm_map_region_args_t _a, *a = &_a;
868 memset (a, 0, sizeof (*a));
869 a->root_path = root_path;
870 a->name = SVM_GLOBAL_REGION_NAME;
871 a->baseva = svm_get_global_region_base_va ();
872 a->size = SVM_GLOBAL_REGION_SIZE;
873 a->flags = SVM_FLAGS_NODATA;
877 return svm_region_init_internal (a);
881 svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
883 svm_map_region_args_t _a, *a = &_a;
885 memset (a, 0, sizeof (*a));
886 a->root_path = root_path;
887 a->name = SVM_GLOBAL_REGION_NAME;
888 a->baseva = svm_get_global_region_base_va ();
889 a->size = SVM_GLOBAL_REGION_SIZE;
890 a->flags = SVM_FLAGS_NODATA;
894 svm_region_init_internal (a);
898 svm_region_init_args (svm_map_region_args_t * a)
900 svm_region_init_internal (a);
904 svm_region_find_or_create (svm_map_region_args_t * a)
906 svm_main_region_t *mp;
913 svm_subregion_t *subp;
917 a->size += MMAP_PAGESIZE +
918 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
919 a->size = rnd_pagesize (a->size);
921 region_lock (root_rp, 4);
922 oldheap = svm_push_pvt_heap (root_rp);
923 mp = root_rp->data_base;
927 /* Map the named region from the correct chroot environment */
928 if (a->root_path == NULL)
929 a->root_path = (char *) mp->root_path;
932 * See if this region is already known. If it is, we're
935 p = hash_get_mem (mp->name_hash, a->name);
939 rp = svm_map_region (a);
940 region_unlock (root_rp);
941 svm_pop_heap (oldheap);
945 /* Create the region. */
946 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
948 need_nbits = a->size / MMAP_PAGESIZE;
950 index = 1; /* $$$ fixme, figure out how many bit to really skip */
953 * Scan the virtual space allocation bitmap, looking for a large
958 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
960 for (i = 0; i < (need_nbits - 1); i++)
962 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
973 while (index < root_rp->bitmap_size);
975 /* Completely out of VM? */
976 if (index >= root_rp->bitmap_size)
978 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
979 root_rp->region_name, a->size, a->size);
980 svm_pop_heap (oldheap);
981 region_unlock (root_rp);
986 * Mark virtual space allocated
989 clib_warning ("set %d bits at index %d", need_nbits, index);
992 for (i = 0; i < need_nbits; i++)
994 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
997 /* Place this region where it goes... */
998 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
1000 rp = svm_map_region (a);
1002 pool_get (mp->subregions, subp);
1003 name = format (0, "%s%c", a->name, 0);
1004 subp->subregion_name = name;
1006 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
1008 svm_pop_heap (oldheap);
1010 region_unlock (root_rp);
1016 svm_region_unlink (svm_region_t * rp)
1018 svm_map_region_args_t _a, *a = &_a;
1019 svm_main_region_t *mp;
1024 ASSERT (vec_c_string_is_terminated (rp->region_name));
1026 mp = root_rp->data_base;
1029 a->root_path = (char *) mp->root_path;
1030 a->name = rp->region_name;
1031 shm_name = shm_name_from_svm_map_region_args (a);
1033 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
1034 shm_unlink ((const char *) shm_name);
1035 vec_free (shm_name);
1041 * Let go of the indicated region. If the calling process
1042 * is the last customer, throw it away completely.
1043 * The root region mutex guarantees atomicity with respect to
1044 * a new region client showing up at the wrong moment.
1047 svm_region_unmap_internal (void *rp_arg, u8 is_client)
1049 int i, mypid = getpid ();
1052 uword virtual_base, virtual_size;
1053 svm_region_t *rp = rp_arg;
1057 * If we take a signal while holding one or more shared-memory
1058 * mutexes, we may end up back here from an otherwise
1059 * benign exit handler. Bail out to avoid a recursive
1069 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
1071 region_lock (root_rp, 5);
1072 region_lock (rp, 6);
1074 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1076 /* Remove the caller from the list of mappers */
1077 for (i = 0; i < vec_len (rp->client_pids); i++)
1079 if (rp->client_pids[i] == mypid)
1081 vec_delete (rp->client_pids, 1, i);
1085 clib_warning ("pid %d AWOL", mypid);
1089 svm_pop_heap (oldheap);
1091 nclients_left = vec_len (rp->client_pids);
1092 virtual_base = rp->virtual_base;
1093 virtual_size = rp->virtual_size;
1095 if (nclients_left == 0)
1097 int index, nbits, i;
1098 svm_main_region_t *mp;
1100 svm_subregion_t *subp;
1102 /* Kill the region, last guy on his way out */
1104 oldheap = svm_push_pvt_heap (root_rp);
1105 name = vec_dup (rp->region_name);
1107 virtual_base = rp->virtual_base;
1108 virtual_size = rp->virtual_size;
1110 /* Figure out which bits to clear in the root region bitmap */
1111 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
1113 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
1116 clib_warning ("clear %d bits at index %d", nbits, index);
1118 /* Give back the allocated VM */
1119 for (i = 0; i < nbits; i++)
1121 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1124 mp = root_rp->data_base;
1126 p = hash_get_mem (mp->name_hash, name);
1128 /* Better never happen ... */
1132 region_unlock (root_rp);
1133 svm_pop_heap (oldheap);
1134 clib_warning ("Region name '%s' not found?", name);
1138 /* Remove from the root region subregion pool */
1139 subp = mp->subregions + p[0];
1140 pool_put (mp->subregions, subp);
1142 hash_unset_mem (mp->name_hash, name);
1148 /* If a client asks for the cleanup, don't unlink the backing
1149 * file since we can't tell if it has been recreated. */
1151 svm_region_unlink (rp);
1153 munmap ((void *) virtual_base, virtual_size);
1154 region_unlock (root_rp);
1155 svm_pop_heap (oldheap);
1160 region_unlock (root_rp);
1162 munmap ((void *) virtual_base, virtual_size);
1166 svm_region_unmap (void *rp_arg)
1168 svm_region_unmap_internal (rp_arg, 0 /* is_client */ );
1172 svm_region_unmap_client (void *rp_arg)
1174 svm_region_unmap_internal (rp_arg, 1 /* is_client */ );
1181 svm_region_exit_internal (u8 is_client)
1184 int i, mypid = getpid ();
1185 uword virtual_base, virtual_size;
1187 /* It felt so nice we did it twice... */
1191 if (--root_rp_refcount > 0)
1195 * If we take a signal while holding one or more shared-memory
1196 * mutexes, we may end up back here from an otherwise
1197 * benign exit handler. Bail out to avoid a recursive
1203 region_lock (root_rp, 7);
1204 oldheap = svm_push_pvt_heap (root_rp);
1206 virtual_base = root_rp->virtual_base;
1207 virtual_size = root_rp->virtual_size;
1209 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1211 if (root_rp->client_pids[i] == mypid)
1213 vec_delete (root_rp->client_pids, 1, i);
1217 clib_warning ("pid %d AWOL", mypid);
1221 if (!is_client && vec_len (root_rp->client_pids) == 0)
1222 svm_region_unlink (root_rp);
1224 region_unlock (root_rp);
1225 svm_pop_heap (oldheap);
1228 munmap ((void *) virtual_base, virtual_size);
1232 svm_region_exit (void)
1234 svm_region_exit_internal (0 /* is_client */ );
1238 svm_region_exit_client (void)
1240 svm_region_exit_internal (1 /* is_client */ );
1244 svm_client_scan_this_region_nolock (svm_region_t * rp)
1247 int mypid = getpid ();
1250 for (j = 0; j < vec_len (rp->client_pids); j++)
1252 if (mypid == rp->client_pids[j])
1254 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1256 clib_warning ("%s: cleanup ghost pid %d",
1257 rp->region_name, rp->client_pids[j]);
1258 /* nb: client vec in rp->region_heap */
1259 oldheap = svm_push_pvt_heap (rp);
1260 vec_delete (rp->client_pids, 1, j);
1262 svm_pop_heap (oldheap);
1269 * Scan svm regions for dead clients
1272 svm_client_scan (const char *root_path)
1275 svm_main_region_t *mp;
1276 svm_map_region_args_t *a = 0;
1277 svm_region_t *root_rp;
1279 svm_subregion_t *subp;
1283 int mypid = getpid ();
1285 vec_validate (a, 0);
1287 svm_region_init_chroot (root_path);
1289 root_rp = svm_get_root_rp ();
1291 pthread_mutex_lock (&root_rp->mutex);
1293 mp = root_rp->data_base;
1295 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1297 if (mypid == root_rp->client_pids[j])
1299 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1301 clib_warning ("%s: cleanup ghost pid %d",
1302 root_rp->region_name, root_rp->client_pids[j]);
1303 /* nb: client vec in root_rp->region_heap */
1304 oldheap = svm_push_pvt_heap (root_rp);
1305 vec_delete (root_rp->client_pids, 1, j);
1307 svm_pop_heap (oldheap);
1312 * Snapshoot names, can't hold root rp mutex across
1316 pool_foreach (subp, mp->subregions, ({
1317 name = vec_dup (subp->subregion_name);
1318 vec_add1(svm_names, name);
1322 pthread_mutex_unlock (&root_rp->mutex);
1324 for (i = 0; i < vec_len (svm_names); i++)
1326 vec_validate (a, 0);
1327 a->root_path = root_path;
1328 a->name = (char *) svm_names[i];
1329 rp = svm_region_find_or_create (a);
1332 pthread_mutex_lock (&rp->mutex);
1334 svm_client_scan_this_region_nolock (rp);
1336 pthread_mutex_unlock (&rp->mutex);
1337 svm_region_unmap (rp);
1338 vec_free (svm_names[i]);
1342 vec_free (svm_names);
1350 * fd.io coding-style-patch-verification: ON
1353 * eval: (c-set-style "gnu")