]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Enable huge page support on main arena
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 30 Aug 2021 17:01:00 +0000 (14:01 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 15 Dec 2021 20:35:39 +0000 (17:35 -0300)
This patch adds support huge page support on main arena allocation,
enable with tunable glibc.malloc.hugetlb=2.  The patch essentially
disable the __glibc_morecore() sbrk() call (similar when memory
tag does when sbrk() call does not support it) and fallback to
default page size if the memory allocation fails.

Checked on x86_64-linux-gnu.

Reviewed-by: DJ Delorie <dj@redhat.com>
malloc/arena.c
malloc/malloc.c
malloc/morecore.c

index e1852f8597bcba8a4dfef0a109f301b2b78c6b0a..3ed4ef3f05614a3a28aa056f747fd8368bc83f59 100644 (file)
@@ -364,6 +364,10 @@ ptmalloc_init (void)
 # endif
   TUNABLE_GET (mxfast, size_t, TUNABLE_CALLBACK (set_mxfast));
   TUNABLE_GET (hugetlb, size_t, TUNABLE_CALLBACK (set_hugetlb));
+  if (mp_.hp_pagesize > 0)
+    /* Force mmap for main arena instead of sbrk, so hugepages are explicitly
+       used.  */
+    __always_fail_morecore = true;
 #else
   if (__glibc_likely (_environ != NULL))
     {
index 32050be4cc3ab0f1c14138c54b1273e2ed2dc951..b67f2c84ee22c77633d452284cfd3b7456512fc4 100644 (file)
@@ -2741,8 +2741,16 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av)
              segregated mmap region.
            */
 
-         char *mbrk = sysmalloc_mmap_fallback (&size, nb, old_size, pagesize,
-                                               MMAP_AS_MORECORE_SIZE, 0, av);
+         char *mbrk = MAP_FAILED;
+#if HAVE_TUNABLES
+         if (mp_.hp_pagesize > 0)
+           mbrk = sysmalloc_mmap_fallback (&size, nb, old_size,
+                                           mp_.hp_pagesize, mp_.hp_pagesize,
+                                           mp_.hp_flags, av);
+#endif
+         if (mbrk == MAP_FAILED)
+           mbrk = sysmalloc_mmap_fallback (&size, nb, old_size, pagesize,
+                                           MMAP_AS_MORECORE_SIZE, 0, av);
          if (mbrk != MAP_FAILED)
            {
              /* We do not need, and cannot use, another sbrk call to find end */
index 8168ef158cdbd6018f70ce9426bc4b2ede610f2c..004cd3ead48cea1d69a7f44356d6cf56d05502de 100644 (file)
@@ -15,9 +15,7 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#if defined(SHARED) || defined(USE_MTAG)
 static bool __always_fail_morecore = false;
-#endif
 
 /* Allocate INCREMENT more bytes of data space,
    and return the start of data space, or NULL on errors.
@@ -25,10 +23,8 @@ static bool __always_fail_morecore = false;
 void *
 __glibc_morecore (ptrdiff_t increment)
 {
-#if defined(SHARED) || defined(USE_MTAG)
   if (__always_fail_morecore)
     return NULL;
-#endif
 
   void *result = (void *) __sbrk (increment);
   if (result == (void *) -1)