From 978d305f399de253107f212797affd81273828f2 Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 2 Oct 2023 01:27:58 +0300 Subject: [PATCH] plat: add a thp-based huge page fallback unlike MAP_HUGETLB it's often enabled in distros by default --- linux/plat.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/linux/plat.c b/linux/plat.c index e750163..a5f70eb 100644 --- a/linux/plat.c +++ b/linux/plat.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -219,6 +220,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; -- 2.39.5