pmalloc: support for 4K pages
[vpp.git] / src / vppinfra / linux / mem.c
index 253ae87..8edae74 100644 (file)
 #define F_SEAL_WRITE    0x0008 /* prevent writes */
 #endif
 
+
+uword
+clib_mem_get_page_size (void)
+{
+  return getpagesize ();
+}
+
+uword
+clib_mem_get_default_hugepage_size (void)
+{
+  unformat_input_t input;
+  static u32 size = 0;
+  int fd;
+
+  if (size)
+    goto done;
+
+  if ((fd = open ("/proc/meminfo", 0)) == -1)
+    return 0;
+
+  unformat_init_clib_file (&input, fd);
+
+  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (&input, "Hugepagesize:%_%u kB", &size))
+       ;
+      else
+       unformat_skip_line (&input);
+    }
+  unformat_free (&input);
+  close (fd);
+done:
+  return 1024ULL * size;
+}
+
 u64
-clib_mem_vm_get_page_size (int fd)
+clib_mem_get_fd_page_size (int fd)
 {
   struct stat st = { 0 };
   if (fstat (fd, &st) == -1)
@@ -56,9 +91,9 @@ clib_mem_vm_get_page_size (int fd)
 }
 
 int
-clib_mem_vm_get_log2_page_size (int fd)
+clib_mem_get_fd_log2_page_size (int fd)
 {
-  return min_log2 (clib_mem_vm_get_page_size (fd));
+  return min_log2 (clib_mem_get_fd_page_size (fd));
 }
 
 void
@@ -81,6 +116,26 @@ clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size)
 #define MFD_HUGETLB 0x0004U
 #endif
 
+clib_error_t *
+clib_mem_create_fd (char *name, int *fdp)
+{
+  int fd;
+
+  ASSERT (name);
+
+  if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
+    return clib_error_return_unix (0, "memfd_create");
+
+  if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
+    {
+      close (fd);
+      return clib_error_return_unix (0, "fcntl (F_ADD_SEALS)");
+    }
+
+  *fdp = fd;
+  return 0;
+}
+
 clib_error_t *
 clib_mem_create_hugetlb_fd (char *name, int *fdp)
 {
@@ -177,20 +232,11 @@ clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a)
        }
       else
        {
-         if ((fd = memfd_create (a->name, MFD_ALLOW_SEALING)) == -1)
-           {
-             err = clib_error_return_unix (0, "memfd_create");
-             goto error;
-           }
-
-         if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
-           {
-             err = clib_error_return_unix (0, "fcntl (F_ADD_SEALS)");
-             goto error;
-           }
+         if ((err = clib_mem_create_fd (a->name, &fd)))
+           goto error;
        }
 
-      log2_page_size = clib_mem_vm_get_log2_page_size (fd);
+      log2_page_size = clib_mem_get_fd_log2_page_size (fd);
       if (log2_page_size == 0)
        {
          err = clib_error_return_unix (0, "cannot determine page size");
@@ -221,8 +267,7 @@ clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a)
 
   if (a->flags & CLIB_MEM_VM_F_HUGETLB_PREALLOC)
     {
-      err = clib_sysfs_prealloc_hugepages (a->numa_node,
-                                          1 << (log2_page_size - 10),
+      err = clib_sysfs_prealloc_hugepages (a->numa_node, log2_page_size,
                                           n_pages);
       if (err)
        goto error;