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/heap.h>
40 #include <vppinfra/pool.h>
41 #include <vppinfra/format.h>
45 static svm_region_t *root_rp;
46 static int root_rp_refcount;
49 static pthread_mutex_t *mutexes_held[MAXLOCK];
53 svm_get_root_rp (void)
61 svm_get_global_region_base_va ()
63 #ifdef CLIB_SANITIZE_ADDR
64 return 0x200000000000;
68 /* On AArch64 VA space can have different size, from 36 to 48 bits.
69 Here we are trying to detect VA bits by parsing /proc/self/maps
72 unformat_input_t input;
76 if ((fd = open ("/proc/self/maps", 0)) < 0)
77 clib_unix_error ("open '/proc/self/maps'");
79 unformat_init_clib_file (&input, fd);
80 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
82 if (unformat (&input, "%llx-%llx", &start, &end))
84 unformat_skip_line (&input);
86 unformat_free (&input);
89 bits = count_leading_zeros (end);
91 if (bits >= 36 && bits <= 48)
92 return ((1ul << bits) / 4) - (2 * SVM_GLOBAL_REGION_SIZE);
94 clib_unix_error ("unexpected va bits '%u'", bits);
98 return 0x130000000ULL;
102 region_lock (svm_region_t * rp, int tag)
104 pthread_mutex_lock (&rp->mutex);
106 rp->mutex_owner_pid = getpid ();
107 rp->mutex_owner_tag = tag;
109 ASSERT (nheld < MAXLOCK); //NOSONAR
111 * Keep score of held mutexes so we can try to exit
112 * cleanly if the world comes to an end at the worst possible
115 mutexes_held[nheld++] = &rp->mutex;
119 region_unlock (svm_region_t * rp)
123 rp->mutex_owner_pid = 0;
124 rp->mutex_owner_tag = 0;
127 for (i = nheld - 1; i >= 0; i--)
129 if (mutexes_held[i] == &rp->mutex)
131 for (j = i; j < MAXLOCK - 1; j++)
132 mutexes_held[j] = mutexes_held[j + 1];
140 CLIB_MEMORY_BARRIER ();
141 pthread_mutex_unlock (&rp->mutex);
146 format_svm_flags (u8 * s, va_list * args)
148 uword f = va_arg (*args, uword);
150 if (f & SVM_FLAGS_MHEAP)
151 s = format (s, "MHEAP ");
152 if (f & SVM_FLAGS_FILE)
153 s = format (s, "FILE ");
154 if (f & SVM_FLAGS_NODATA)
155 s = format (s, "NODATA ");
156 if (f & SVM_FLAGS_NEED_DATA_INIT)
157 s = format (s, "INIT ");
163 format_svm_size (u8 * s, va_list * args)
165 uword size = va_arg (*args, uword);
167 if (size >= (1 << 20))
169 s = format (s, "(%d mb)", size >> 20);
171 else if (size >= (1 << 10))
173 s = format (s, "(%d kb)", size >> 10);
177 s = format (s, "(%d bytes)", size);
183 format_svm_region (u8 * s, va_list * args)
185 svm_region_t *rp = va_arg (*args, svm_region_t *);
186 int verbose = va_arg (*args, int);
190 s = format (s, "%s: base va 0x%x size 0x%x %U\n",
191 rp->region_name, rp->virtual_base,
192 rp->virtual_size, format_svm_size, rp->virtual_size);
193 s = format (s, " user_ctx 0x%x, bitmap_size %d\n",
194 rp->user_ctx, rp->bitmap_size);
198 s = format (s, " flags: 0x%x %U\n", rp->flags,
199 format_svm_flags, rp->flags);
201 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
202 rp->region_heap, rp->data_base, rp->data_heap);
205 s = format (s, " %d clients, pids: ", vec_len (rp->client_pids));
207 for (i = 0; i < vec_len (rp->client_pids); i++)
208 s = format (s, "%d ", rp->client_pids[i]);
210 s = format (s, "\n");
216 s = format (s, " VM in use: ");
218 for (i = 0; i < rp->bitmap_size; i++)
220 if (clib_bitmap_get_no_check (rp->bitmap, i) != 0)
224 hi = lo = rp->virtual_base + i * MMAP_PAGESIZE;
228 hi = rp->virtual_base + i * MMAP_PAGESIZE;
235 hi = rp->virtual_base + i * MMAP_PAGESIZE - 1;
236 s = format (s, " 0x%x - 0x%x (%dk)\n", lo, hi,
249 * Round to a pagesize multiple, presumably 4k works
252 rnd_pagesize (u64 size)
256 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
261 * svm_data_region_setup
264 svm_data_region_create (svm_map_region_args_t * a, svm_region_t * rp)
270 map_size = rp->virtual_size - (MMAP_PAGESIZE +
271 (a->pvt_heap_size ? a->pvt_heap_size :
272 SVM_PVT_MHEAP_SIZE));
274 if (a->flags & SVM_FLAGS_FILE)
278 fd = open (a->backing_file, O_RDWR | O_CREAT, 0777);
282 clib_unix_warning ("open");
286 if (fstat (fd, &statb) < 0)
288 clib_unix_warning ("fstat");
293 if (statb.st_mode & S_IFREG)
295 if (statb.st_size == 0)
297 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
299 clib_unix_warning ("seek region size");
303 if (write (fd, &junk, 1) != 1)
305 clib_unix_warning ("set region size");
312 map_size = rnd_pagesize (statb.st_size);
317 map_size = a->backing_mmap_size;
320 ASSERT (map_size <= rp->virtual_size -
321 (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE));
323 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
324 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
326 clib_unix_warning ("mmap");
331 CLIB_MEM_UNPOISON (rp->data_base, map_size);
332 rp->backing_file = (char *) format (0, "%s%c", a->backing_file, 0);
333 rp->flags |= SVM_FLAGS_FILE;
336 if (a->flags & SVM_FLAGS_MHEAP)
338 rp->data_heap = clib_mem_create_heap (rp->data_base, map_size,
339 1 /* locked */ , "svm data");
341 rp->flags |= SVM_FLAGS_MHEAP;
347 svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp)
354 map_size = rp->virtual_size -
356 + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE));
358 if (a->flags & SVM_FLAGS_FILE)
361 fd = open (a->backing_file, O_RDWR, 0777);
365 clib_unix_warning ("open");
369 if (fstat (fd, &statb) < 0)
371 clib_unix_warning ("fstat");
376 if (statb.st_mode & S_IFREG)
378 if (statb.st_size == 0)
380 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
382 clib_unix_warning ("seek region size");
386 if (write (fd, &junk, 1) != 1)
388 clib_unix_warning ("set region size");
395 map_size = rnd_pagesize (statb.st_size);
400 map_size = a->backing_mmap_size;
403 ASSERT (map_size <= rp->virtual_size
406 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)));
408 if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE,
409 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
411 clib_unix_warning ("mmap");
416 CLIB_MEM_UNPOISON (rp->data_base, map_size);
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 = clib_mem_create_heap
489 (uword_to_pointer (a->baseva + MMAP_PAGESIZE, void *),
491 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, 1 /* locked */ ,
494 oldheap = svm_push_pvt_heap (rp);
496 rp->region_name = (char *) format (0, "%s%c", a->name, 0);
497 vec_add1 (rp->client_pids, getpid ());
499 nbits = rp->virtual_size / MMAP_PAGESIZE;
502 rp->bitmap_size = nbits;
503 words = (nbits + BITS (uword) - 1) / BITS (uword);
504 vec_validate (rp->bitmap, words - 1);
506 overhead_space = MMAP_PAGESIZE /* header */ +
507 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
510 data_base = (uword) rp->virtual_base;
512 if (a->flags & SVM_FLAGS_NODATA)
513 rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
517 clib_bitmap_set_no_check (rp->bitmap, bit, 1);
519 overhead_space -= MMAP_PAGESIZE;
520 data_base += MMAP_PAGESIZE;
522 while (overhead_space > 0);
524 rp->data_base = (void *) data_base;
527 * Note: although the POSIX spec guarantees that only one
528 * process enters this block, we have to play games
529 * to hold off clients until e.g. the mutex is ready
531 rp->version = SVM_VERSION;
533 /* setup the data portion of the region */
535 rv = svm_data_region_create (a, rp);
538 clib_warning ("data_region_create: %d", rv);
543 svm_pop_heap (oldheap);
550 svm_map_region (svm_map_region_args_t * a)
558 int pid_holding_region_lock;
560 int dead_region_recovery = 0;
563 struct timespec ts, tsrem;
565 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
568 shm_name = shm_name_from_svm_map_region_args (a);
571 clib_warning ("[%d] map region %s: shm_open (%s)",
572 getpid (), a->name, shm_name);
574 svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
578 if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
579 clib_unix_warning ("segment chmod");
580 /* This turns out to fail harmlessly if the client starts first */
581 if (fchown (svm_fd, a->uid, a->gid) < 0)
582 clib_unix_warning ("segment chown [ok if client starts first]");
586 if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1)
588 clib_warning ("seek region size");
592 if (write (svm_fd, &junk, 1) != 1)
594 clib_warning ("set region size");
599 rp = mmap (uword_to_pointer (a->baseva, void *), a->size,
600 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
602 if (rp == (svm_region_t *) MAP_FAILED)
604 clib_unix_warning ("mmap create");
609 CLIB_MEM_UNPOISON (rp, a->size);
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 CLIB_MEM_UNPOISON (rp, MMAP_PAGESIZE);
670 * We lost the footrace to create this region; make sure
671 * the winner has crossed the finish line.
673 while (rp->version == 0 && deadman++ < 5)
681 if (rp->version == 0)
683 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
685 munmap (rp, a->size);
688 /* Remap now that the region has been placed */
689 a->baseva = rp->virtual_base;
690 a->size = rp->virtual_size;
691 munmap (rp, MMAP_PAGESIZE);
693 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
694 PROT_READ | PROT_WRITE,
695 MAP_SHARED | MAP_FIXED, svm_fd, 0);
696 if ((uword) rp == (uword) MAP_FAILED)
698 clib_unix_warning ("mmap");
705 CLIB_MEM_UNPOISON (rp, a->size);
707 if ((uword) rp != rp->virtual_base)
709 clib_warning ("mmap botch");
713 * Try to fix the region mutex if it is held by
716 pid_holding_region_lock = rp->mutex_owner_pid;
717 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
719 pthread_mutexattr_t attr;
721 ("region %s mutex held by dead pid %d, tag %d, force unlock",
722 rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag);
723 /* owner pid is nonexistent */
724 if (pthread_mutexattr_init (&attr))
725 clib_unix_warning ("mutexattr_init");
726 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
727 clib_unix_warning ("mutexattr_setpshared");
728 if (pthread_mutex_init (&rp->mutex, &attr))
729 clib_unix_warning ("mutex_init");
730 dead_region_recovery = 1;
733 if (dead_region_recovery)
734 clib_warning ("recovery: attempt to re-lock region");
737 oldheap = svm_push_pvt_heap (rp);
738 vec_add1 (rp->client_pids, getpid ());
740 if (dead_region_recovery)
741 clib_warning ("recovery: attempt svm_data_region_map");
743 rv = svm_data_region_map (a, rp);
746 clib_warning ("data_region_map: %d", rv);
749 if (dead_region_recovery)
750 clib_warning ("unlock and continue");
754 svm_pop_heap (oldheap);
756 return ((void *) rp);
759 return 0; /* NOTREACHED *///NOSONAR
763 svm_mutex_cleanup (void)
766 for (i = 0; i < nheld; i++)
768 pthread_mutex_unlock (mutexes_held[i]); //NOSONAR
773 svm_region_init_internal (svm_map_region_args_t * a)
776 u64 ticks = clib_cpu_time_now ();
777 uword randomize_baseva;
779 /* guard against klutz calls */
785 atexit (svm_mutex_cleanup);
787 /* Randomize the shared-VM base at init time */
788 if (MMAP_PAGESIZE <= (4 << 10))
789 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
791 randomize_baseva = (ticks & 3) * MMAP_PAGESIZE;
793 a->baseva += randomize_baseva;
795 rp = svm_map_region (a);
801 /* Set up the main region data structures */
802 if (rp->flags & SVM_FLAGS_NEED_DATA_INIT)
804 svm_main_region_t *mp = 0;
807 rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT);
809 oldheap = svm_push_pvt_heap (rp);
810 vec_validate (mp, 0);
811 mp->name_hash = hash_create_string (0, sizeof (uword));
812 mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0;
816 svm_pop_heap (oldheap);
825 svm_region_init (void)
827 svm_map_region_args_t _a, *a = &_a;
829 clib_memset (a, 0, sizeof (*a));
831 a->name = SVM_GLOBAL_REGION_NAME;
832 a->baseva = svm_get_global_region_base_va ();
833 a->size = SVM_GLOBAL_REGION_SIZE;
834 a->flags = SVM_FLAGS_NODATA;
838 svm_region_init_internal (a);
842 svm_region_init_chroot (const char *root_path)
844 svm_map_region_args_t _a, *a = &_a;
846 clib_memset (a, 0, sizeof (*a));
847 a->root_path = root_path;
848 a->name = SVM_GLOBAL_REGION_NAME;
849 a->baseva = svm_get_global_region_base_va ();
850 a->size = SVM_GLOBAL_REGION_SIZE;
851 a->flags = SVM_FLAGS_NODATA;
855 return svm_region_init_internal (a);
859 svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid)
861 svm_map_region_args_t _a, *a = &_a;
863 clib_memset (a, 0, sizeof (*a));
864 a->root_path = root_path;
865 a->name = SVM_GLOBAL_REGION_NAME;
866 a->baseva = svm_get_global_region_base_va ();
867 a->size = SVM_GLOBAL_REGION_SIZE;
868 a->flags = SVM_FLAGS_NODATA;
872 svm_region_init_internal (a);
876 svm_region_init_args (svm_map_region_args_t * a)
878 svm_region_init_internal (a);
882 svm_region_find_or_create (svm_map_region_args_t * a)
884 svm_main_region_t *mp;
891 svm_subregion_t *subp;
895 a->size += MMAP_PAGESIZE +
896 ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
897 a->size = rnd_pagesize (a->size);
899 region_lock (root_rp, 4);
900 oldheap = svm_push_pvt_heap (root_rp);
901 mp = root_rp->data_base;
905 /* Map the named region from the correct chroot environment */
906 if (a->root_path == NULL)
907 a->root_path = (char *) mp->root_path;
910 * See if this region is already known. If it is, we're
913 p = hash_get_mem (mp->name_hash, a->name);
917 rp = svm_map_region (a);
918 region_unlock (root_rp);
919 svm_pop_heap (oldheap);
923 /* Create the region. */
924 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
926 need_nbits = a->size / MMAP_PAGESIZE;
928 index = 1; /* $$$ fixme, figure out how many bit to really skip */
931 * Scan the virtual space allocation bitmap, looking for a large
936 if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0)
938 for (i = 0; i < (need_nbits - 1); i++)
940 if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1)
951 while (index < root_rp->bitmap_size);
953 /* Completely out of VM? */
954 if (index >= root_rp->bitmap_size)
956 clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)",
957 root_rp->region_name, a->size, a->size);
958 svm_pop_heap (oldheap);
959 region_unlock (root_rp);
964 * Mark virtual space allocated
967 clib_warning ("set %d bits at index %d", need_nbits, index);
970 for (i = 0; i < need_nbits; i++)
972 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1);
975 /* Place this region where it goes... */
976 a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE;
978 rp = svm_map_region (a);
980 pool_get (mp->subregions, subp);
981 name = format (0, "%s%c", a->name, 0);
982 subp->subregion_name = name;
984 hash_set_mem (mp->name_hash, name, subp - mp->subregions);
986 svm_pop_heap (oldheap);
988 region_unlock (root_rp);
994 svm_region_unlink (svm_region_t * rp)
996 svm_map_region_args_t _a, *a = &_a;
997 svm_main_region_t *mp;
1002 ASSERT (vec_c_string_is_terminated (rp->region_name));
1004 mp = root_rp->data_base;
1007 a->root_path = (char *) mp->root_path;
1008 a->name = rp->region_name;
1009 shm_name = shm_name_from_svm_map_region_args (a);
1011 clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name);
1012 shm_unlink ((const char *) shm_name);
1013 vec_free (shm_name);
1019 * Let go of the indicated region. If the calling process
1020 * is the last customer, throw it away completely.
1021 * The root region mutex guarantees atomicity with respect to
1022 * a new region client showing up at the wrong moment.
1025 svm_region_unmap_internal (void *rp_arg, u8 is_client)
1027 int i, mypid = getpid ();
1030 uword virtual_base, virtual_size;
1031 svm_region_t *rp = rp_arg;
1035 * If we take a signal while holding one or more shared-memory
1036 * mutexes, we may end up back here from an otherwise
1037 * benign exit handler. Bail out to avoid a recursive
1047 clib_warning ("[%d] unmap region %s", getpid (), rp->region_name);
1049 region_lock (root_rp, 5);
1050 region_lock (rp, 6);
1052 oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */
1054 /* Remove the caller from the list of mappers */
1055 CLIB_MEM_UNPOISON (rp->client_pids, vec_bytes (rp->client_pids));
1056 for (i = 0; i < vec_len (rp->client_pids); i++)
1058 if (rp->client_pids[i] == mypid)
1060 vec_delete (rp->client_pids, 1, i);
1064 clib_warning ("pid %d AWOL", mypid);
1068 svm_pop_heap (oldheap);
1070 nclients_left = vec_len (rp->client_pids);
1071 virtual_base = rp->virtual_base;
1072 virtual_size = rp->virtual_size;
1074 if (nclients_left == 0)
1076 int index, nbits, i;
1077 svm_main_region_t *mp;
1079 svm_subregion_t *subp;
1081 /* Kill the region, last guy on his way out */
1083 oldheap = svm_push_pvt_heap (root_rp);
1084 name = vec_dup (rp->region_name);
1086 virtual_base = rp->virtual_base;
1087 virtual_size = rp->virtual_size;
1089 /* Figure out which bits to clear in the root region bitmap */
1090 index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE;
1092 nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE;
1095 clib_warning ("clear %d bits at index %d", nbits, index);
1097 /* Give back the allocated VM */
1098 for (i = 0; i < nbits; i++)
1100 clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0);
1103 mp = root_rp->data_base;
1105 p = hash_get_mem (mp->name_hash, name);
1107 /* Better never happen ... */
1111 region_unlock (root_rp);
1112 svm_pop_heap (oldheap);
1113 clib_warning ("Region name '%s' not found?", name);
1117 /* Remove from the root region subregion pool */
1118 subp = mp->subregions + p[0];
1119 pool_put (mp->subregions, subp);
1121 hash_unset_mem (mp->name_hash, name);
1127 /* If a client asks for the cleanup, don't unlink the backing
1128 * file since we can't tell if it has been recreated. */
1130 svm_region_unlink (rp);
1132 munmap ((void *) virtual_base, virtual_size);
1133 region_unlock (root_rp);
1134 svm_pop_heap (oldheap);
1139 region_unlock (root_rp);
1141 munmap ((void *) virtual_base, virtual_size);
1145 svm_region_unmap (void *rp_arg)
1147 svm_region_unmap_internal (rp_arg, 0 /* is_client */ );
1151 svm_region_unmap_client (void *rp_arg)
1153 svm_region_unmap_internal (rp_arg, 1 /* is_client */ );
1160 svm_region_exit_internal (u8 is_client)
1163 int i, mypid = getpid ();
1164 uword virtual_base, virtual_size;
1166 /* It felt so nice we did it twice... */
1170 if (--root_rp_refcount > 0)
1174 * If we take a signal while holding one or more shared-memory
1175 * mutexes, we may end up back here from an otherwise
1176 * benign exit handler. Bail out to avoid a recursive
1182 region_lock (root_rp, 7);
1183 oldheap = svm_push_pvt_heap (root_rp);
1185 virtual_base = root_rp->virtual_base;
1186 virtual_size = root_rp->virtual_size;
1188 CLIB_MEM_UNPOISON (root_rp->client_pids, vec_bytes (root_rp->client_pids));
1189 for (i = 0; i < vec_len (root_rp->client_pids); i++)
1191 if (root_rp->client_pids[i] == mypid)
1193 vec_delete (root_rp->client_pids, 1, i);
1197 clib_warning ("pid %d AWOL", mypid);
1201 if (!is_client && vec_len (root_rp->client_pids) == 0)
1202 svm_region_unlink (root_rp);
1204 region_unlock (root_rp);
1205 svm_pop_heap (oldheap);
1208 munmap ((void *) virtual_base, virtual_size);
1212 svm_region_exit (void)
1214 svm_region_exit_internal (0 /* is_client */ );
1218 svm_region_exit_client (void)
1220 svm_region_exit_internal (1 /* is_client */ );
1224 svm_client_scan_this_region_nolock (svm_region_t * rp)
1227 int mypid = getpid ();
1230 for (j = 0; j < vec_len (rp->client_pids); j++)
1232 if (mypid == rp->client_pids[j])
1234 if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0))
1236 clib_warning ("%s: cleanup ghost pid %d",
1237 rp->region_name, rp->client_pids[j]);
1238 /* nb: client vec in rp->region_heap */
1239 oldheap = svm_push_pvt_heap (rp);
1240 vec_delete (rp->client_pids, 1, j);
1242 svm_pop_heap (oldheap);
1249 * Scan svm regions for dead clients
1252 svm_client_scan (const char *root_path)
1255 svm_main_region_t *mp;
1256 svm_map_region_args_t *a = 0;
1257 svm_region_t *root_rp;
1259 svm_subregion_t *subp;
1263 int mypid = getpid ();
1265 vec_validate (a, 0);
1267 svm_region_init_chroot (root_path);
1269 root_rp = svm_get_root_rp ();
1271 pthread_mutex_lock (&root_rp->mutex);
1273 mp = root_rp->data_base;
1275 for (j = 0; j < vec_len (root_rp->client_pids); j++)
1277 if (mypid == root_rp->client_pids[j])
1279 if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0))
1281 clib_warning ("%s: cleanup ghost pid %d",
1282 root_rp->region_name, root_rp->client_pids[j]);
1283 /* nb: client vec in root_rp->region_heap */
1284 oldheap = svm_push_pvt_heap (root_rp);
1285 vec_delete (root_rp->client_pids, 1, j);
1287 svm_pop_heap (oldheap);
1292 * Snapshoot names, can't hold root rp mutex across
1296 pool_foreach (subp, mp->subregions) {
1297 name = vec_dup (subp->subregion_name);
1298 vec_add1(svm_names, name);
1302 pthread_mutex_unlock (&root_rp->mutex);
1304 for (i = 0; i < vec_len (svm_names); i++)
1306 vec_validate (a, 0);
1307 a->root_path = root_path;
1308 a->name = (char *) svm_names[i];
1309 rp = svm_region_find_or_create (a);
1312 pthread_mutex_lock (&rp->mutex);
1314 svm_client_scan_this_region_nolock (rp);
1316 pthread_mutex_unlock (&rp->mutex);
1317 svm_region_unmap (rp);
1318 vec_free (svm_names[i]);
1322 vec_free (svm_names);
1330 * fd.io coding-style-patch-verification: ON
1333 * eval: (c-set-style "gnu")