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)
343 mheap_alloc_with_flags ((void *) (rp->data_base), map_size,
344 MHEAP_FLAG_DISABLE_VM);
345 rp->flags |= SVM_FLAGS_MHEAP;
351 svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
358 map_size = rp->virtual_size -
360 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
362 if (a->flags & SVM_FLAGS_FILE)
365 fd = open (a->backing_file, O_RDWR, 0777);
369 clib_unix_warning ("open");
373 if (fstat (fd, &statb) < 0)
375 clib_unix_warning ("fstat");
380 if (statb.st_mode & S_IFREG)
382 if (statb.st_size == 0)
384 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
386 clib_unix_warning ("seek region size");
390 if (write (fd, &junk, 1) != 1)
392 clib_unix_warning ("set region size");
399 map_size = rnd_pagesize (statb.st_size);
404 map_size = a->backing_mmap_size;
407 ASSERT (map_size <= rp->virtual_size
410 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
412 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
413 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
415 clib_unix_warning ("mmap");
425 shm_name_from_svm_map_region_args (svm_map_region_args_t * a)
431 int root_path_offset = 0;
436 /* Tolerate present or absent slashes */
437 if (a->root_path[0] == '/')
440 /* create the root_path under /dev/shm
441 iterate through path creating directories */
443 path = format (0, "/dev/shm/%s%c", &a->root_path[root_path_offset], 0);
444 split_point = path + 1;
445 vec_add1 (mkdir_arg, '-');
449 while (*split_point && *split_point != '/')
451 vec_add1 (mkdir_arg, *split_point);
454 vec_add1 (mkdir_arg, 0);
456 /* ready to descend another level */
457 mkdir_arg[vec_len (mkdir_arg) - 1] = '-';
460 vec_free (mkdir_arg);
463 if (a->name[0] == '/')
466 shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset],
467 &a->name[name_offset], 0);
470 shm_name = format (0, "%s%c", a->name, 0);
475 svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp)
477 pthread_mutexattr_t attr;
478 pthread_condattr_t cattr;
479 int nbits, words, bit;
486 memset (rp, 0, sizeof (*rp));
488 if (pthread_mutexattr_init (&attr))
489 clib_unix_warning ("mutexattr_init");
491 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
492 clib_unix_warning ("mutexattr_setpshared");
494 if (pthread_mutex_init (&rp->mutex, &attr))
495 clib_unix_warning ("mutex_init");
497 if (pthread_mutexattr_destroy (&attr))
498 clib_unix_warning ("mutexattr_destroy");
500 if (pthread_condattr_init (&cattr))
501 clib_unix_warning ("condattr_init");
503 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
504 clib_unix_warning ("condattr_setpshared");
506 if (pthread_cond_init (&rp->condvar, &cattr))
507 clib_unix_warning ("cond_init");
509 if (pthread_condattr_destroy (&cattr))
510 clib_unix_warning ("condattr_destroy");
514 rp->virtual_base = a->baseva;
515 rp->virtual_size = a->size;
518 mheap_alloc_with_flags (uword_to_pointer
519 (a->baseva + MMAP_PAGESIZE, void *),
521 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
522 MHEAP_FLAG_DISABLE_VM);
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);
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 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 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 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")