From: notaz Date: Sun, 1 Oct 2023 22:27:58 +0000 (+0300) Subject: plat: add a thp-based huge page fallback X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;ds=sidebyside;h=b0ce6fa8bd3c171debd5589f3ee8a95e26b1d61b;p=libpicofe.git plat: add a thp-based huge page fallback unlike MAP_HUGETLB it's often enabled in distros by default --- diff --git a/linux/plat.c b/linux/plat.c index 707d5f7..b1c3314 100644 --- a/linux/plat.c +++ b/linux/plat.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -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;