plat: add a thp-based huge page fallback
authornotaz <notasas@gmail.com>
Sun, 1 Oct 2023 22:27:58 +0000 (01:27 +0300)
committernotaz <notasas@gmail.com>
Sun, 1 Oct 2023 22:27:58 +0000 (01:27 +0300)
unlike MAP_HUGETLB it's often enabled in distros by default

linux/plat.c

index 707d5f7..b1c3314 100644 (file)
@@ -13,6 +13,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
+#include <stdint.h>
 #include <dirent.h>
 #include <sys/time.h>
 #include <time.h>
@@ -207,6 +208,16 @@ void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
                }
                flags &= ~MAP_HUGETLB;
                ret = mmap(req, size, prot, flags, -1, 0);
+#ifdef MADV_HUGEPAGE
+               if (ret != MAP_FAILED && ((uintptr_t)ret & (2*1024*1024 - 1))) {
+                       // try to manually realign assuming bottom-to-top alloc
+                       munmap(ret, size);
+                       ret = (void *)((uintptr_t)ret & ~(2*1024*1024 - 1));
+                       ret = mmap(ret, size, prot, flags, -1, 0);
+               }
+               if (ret != MAP_FAILED)
+                       madvise(ret, size, MADV_HUGEPAGE);
+#endif
        }
        if (ret == MAP_FAILED)
                return NULL;