svm session: use new clib mem apis for segments

"sh memory map" now reports shared memory segments mapped by session
layer for applications.

Type: improvement

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I7debdcd06c7728abfb5cf817a1ccc37de495472c
diff --git a/src/svm/fifo_segment.c b/src/svm/fifo_segment.c
index 5fb2758..a247929 100644
--- a/src/svm/fifo_segment.c
+++ b/src/svm/fifo_segment.c
@@ -1362,7 +1362,7 @@
   ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
 
   if (st == SSVM_SEGMENT_PRIVATE)
-    s = format (s, "%s", "private-heap");
+    s = format (s, "%s", "private");
   else if (st == SSVM_SEGMENT_MEMFD)
     s = format (s, "%s", "memfd");
   else if (st == SSVM_SEGMENT_SHM)
@@ -1400,7 +1400,7 @@
 
   if (fs == 0)
     {
-      s = format (s, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
+      s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
 		  "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
       return s;
     }
@@ -1409,7 +1409,7 @@
   active_fifos = fifo_segment_num_fifos (fs);
   free_fifos = fifo_segment_num_free_fifos (fs);
 
-  s = format (s, "%-15v%15U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
+  s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
 	      format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
 	      free_fifos, address);
 
diff --git a/src/svm/ssvm.c b/src/svm/ssvm.c
index d8831ec..191dbe5 100644
--- a/src/svm/ssvm.c
+++ b/src/svm/ssvm.c
@@ -29,7 +29,6 @@
 ssvm_master_init_shm (ssvm_private_t * ssvm)
 {
   int ssvm_fd;
-  clib_mem_vm_map_t mapa = { 0 };
   u8 junk = 0, *ssvm_filename;
   ssvm_shared_header_t *sh;
   uword page_size, requested_va = 0;
@@ -84,19 +83,18 @@
       clib_mem_vm_randomize_va (&requested_va, min_log2 (page_size));
     }
 
-  mapa.requested_va = requested_va;
-  mapa.size = ssvm->ssvm_size;
-  mapa.fd = ssvm_fd;
-  mapa.numa_node = ssvm->numa;
-  if (clib_mem_vm_ext_map (&mapa))
+  sh = clib_mem_vm_map_shared (uword_to_pointer (requested_va, void *),
+			       ssvm->ssvm_size, ssvm_fd, 0,
+			       (char *) ssvm->name);
+  if (sh == CLIB_MEM_VM_MAP_FAILED)
     {
       clib_unix_warning ("mmap");
       close (ssvm_fd);
       return SSVM_API_ERROR_MMAP;
     }
+
   close (ssvm_fd);
 
-  sh = mapa.addr;
   CLIB_MEM_UNPOISON (sh, sizeof (*sh));
   sh->master_pid = ssvm->my_pid;
   sh->ssvm_size = ssvm->ssvm_size;
@@ -206,7 +204,10 @@
   vec_free (fn);
   vec_free (ssvm->name);
 
-  munmap ((void *) ssvm->sh, ssvm->ssvm_size);
+  if (ssvm->i_am_master)
+    clib_mem_vm_unmap (ssvm->sh);
+  else
+    munmap ((void *) ssvm->sh, ssvm->ssvm_size);
 }
 
 /**
@@ -215,44 +216,60 @@
 int
 ssvm_master_init_memfd (ssvm_private_t * memfd)
 {
+  int log2_page_size, n_pages;
   uword page_size;
   ssvm_shared_header_t *sh;
   void *oldheap;
-  clib_mem_vm_alloc_t alloc = { 0 };
-  clib_error_t *err;
 
   if (memfd->ssvm_size == 0)
     return SSVM_API_ERROR_NO_SIZE;
 
   ASSERT (vec_c_string_is_terminated (memfd->name));
 
-  alloc.name = (char *) memfd->name;
-  alloc.size = memfd->ssvm_size;
-  alloc.flags = CLIB_MEM_VM_F_SHARED;
-  alloc.requested_va = memfd->requested_va;
-  if (memfd->numa)
+  memfd->fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT,
+				     (char *) memfd->name);
+
+  if (memfd->fd == CLIB_MEM_ERROR)
     {
-      alloc.numa_node = memfd->numa;
-      alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER;
-    }
-  if ((err = clib_mem_vm_ext_alloc (&alloc)))
-    {
-      clib_error_report (err);
+      clib_unix_warning (0, "failed to create memfd");
       return SSVM_API_ERROR_CREATE_FAILURE;
     }
 
-  memfd->fd = alloc.fd;
-  memfd->sh = (ssvm_shared_header_t *) alloc.addr;
+  log2_page_size = clib_mem_get_fd_log2_page_size (memfd->fd);
+  if (log2_page_size == 0)
+    {
+      clib_unix_warning (0, "cannot determine page size");
+      return SSVM_API_ERROR_CREATE_FAILURE;
+    }
+
+  n_pages = ((memfd->ssvm_size - 1) >> log2_page_size) + 1;
+
+  if ((ftruncate (memfd->fd, n_pages << log2_page_size)) == -1)
+    {
+      clib_unix_warning (0, "memfd ftruncate failure");
+      return SSVM_API_ERROR_CREATE_FAILURE;
+    }
+
+  sh = clib_mem_vm_map_shared (uword_to_pointer (memfd->requested_va, void *),
+			       memfd->ssvm_size, memfd->fd, 0,
+			       (char *) memfd->name);
+  if (sh == CLIB_MEM_VM_MAP_FAILED)
+    {
+      clib_unix_warning ("memfd map (fd %d)", memfd->fd);
+      close (memfd->fd);
+      return SSVM_API_ERROR_CREATE_FAILURE;
+    }
+
+  memfd->sh = sh;
   memfd->my_pid = getpid ();
   memfd->i_am_master = 1;
 
-  page_size = 1ull << alloc.log2_page_size;
-  sh = memfd->sh;
   sh->master_pid = memfd->my_pid;
   sh->ssvm_size = memfd->ssvm_size;
   sh->ssvm_va = pointer_to_uword (sh);
   sh->type = SSVM_SEGMENT_MEMFD;
 
+  page_size = 1 << log2_page_size;
   sh->heap = clib_mem_create_heap (((u8 *) sh) + page_size,
 				   memfd->ssvm_size - page_size,
 				   1 /* locked */ , "ssvm master memfd");
@@ -327,7 +344,10 @@
 ssvm_delete_memfd (ssvm_private_t * memfd)
 {
   vec_free (memfd->name);
-  clib_mem_vm_free (memfd->sh, memfd->ssvm_size);
+  if (memfd->i_am_master)
+    clib_mem_vm_unmap (memfd->sh);
+  else
+    clib_mem_vm_free (memfd->sh, memfd->ssvm_size);
   close (memfd->fd);
 }
 
@@ -337,34 +357,35 @@
 int
 ssvm_master_init_private (ssvm_private_t * ssvm)
 {
-  uword pagesize = clib_mem_get_page_size (), rnd_size = 0;
-  clib_mem_vm_alloc_t alloc = { 0 };
+  uword page_size, log2_page_size, rnd_size = 0;
   ssvm_shared_header_t *sh;
-  clib_error_t *err;
+  void *oldheap;
   u8 *heap;
 
-  rnd_size = clib_max (ssvm->ssvm_size + (pagesize - 1), ssvm->ssvm_size);
-  rnd_size &= ~(pagesize - 1);
-
-  alloc.name = (char *) ssvm->name;
-  alloc.size = rnd_size + pagesize;
-  if (ssvm->numa)
+  log2_page_size = clib_mem_get_log2_page_size ();
+  if (log2_page_size == 0)
     {
-      alloc.numa_node = ssvm->numa;
-      alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER;
-    }
-
-  if ((err = clib_mem_vm_ext_alloc (&alloc)))
-    {
-      clib_error_report (err);
+      clib_unix_warning (0, "cannot determine page size");
       return SSVM_API_ERROR_CREATE_FAILURE;
     }
 
-  heap = clib_mem_create_heap ((u8 *) alloc.addr + pagesize, rnd_size,
+  page_size = 1 << log2_page_size;
+  rnd_size = clib_max (ssvm->ssvm_size + (page_size - 1), ssvm->ssvm_size);
+  rnd_size &= ~(page_size - 1);
+
+  sh = clib_mem_vm_map (0, rnd_size + page_size, log2_page_size,
+			(char *) ssvm->name);
+  if (sh == CLIB_MEM_VM_MAP_FAILED)
+    {
+      clib_unix_warning ("private map failed");
+      return SSVM_API_ERROR_CREATE_FAILURE;
+    }
+
+  heap = clib_mem_create_heap ((u8 *) sh + page_size, rnd_size,
 			       1 /* locked */ , "ssvm master private");
   if (heap == 0)
     {
-      clib_unix_warning ("mheap alloc");
+      clib_unix_warning ("heap alloc");
       return -1;
     }
 
@@ -376,7 +397,6 @@
   ssvm->requested_va = ~0;
 
   /* First page in allocated memory is set aside for the shared header */
-  sh = alloc.addr;
   ssvm->sh = sh;
 
   clib_memset (sh, 0, sizeof (*sh));
@@ -384,7 +404,10 @@
   sh->ssvm_size = rnd_size;
   sh->ssvm_va = pointer_to_uword (heap);
   sh->type = SSVM_SEGMENT_PRIVATE;
-  sh->name = ssvm->name;
+
+  oldheap = ssvm_push_heap (sh);
+  sh->name = format (0, "%s", ssvm->name, 0);
+  ssvm_pop_heap (oldheap);
 
   return 0;
 }
@@ -401,7 +424,7 @@
 {
   vec_free (ssvm->name);
   clib_mem_destroy_heap (ssvm->sh->heap);
-  clib_mem_vm_free (ssvm->sh, ssvm->ssvm_size + clib_mem_get_page_size ());
+  clib_mem_vm_unmap (ssvm->sh);
 }
 
 int
diff --git a/src/svm/ssvm.h b/src/svm/ssvm.h
index 2a9da3d..3a277cb 100644
--- a/src/svm/ssvm.h
+++ b/src/svm/ssvm.h
@@ -85,7 +85,7 @@
   uword requested_va;
   u32 my_pid;
   u8 *name;
-  u8 numa;			/**< Numa requested at alloc time */
+  u8 numa;			/**< UNUSED: numa requested at alloc time */
   int i_am_master;
 
   union