]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Enable THP always support on hugetlb tunable
authorWilliam Hunt <william.hunt@arm.com>
Tue, 29 Jul 2025 15:04:58 +0000 (15:04 +0000)
committerWilco Dijkstra <wilco.dijkstra@arm.com>
Tue, 29 Jul 2025 15:05:51 +0000 (15:05 +0000)
Enable support for THP always when glibc.malloc.hugetlb=1, as the tunable
currently only gives explicit support in malloc for the THP madvise mode
by aligning to a huge page size. Add a thp_mode parameter to mp_ and check
in madvise_thp whether the system is using madvise mode, otherwise the
`__madvise` call is useless. Set the thp_mode to be unsupported by default,
but if the hugetlb tunable is set this updates thp_mode. Performance of
xalancbmk improves by 4.9% on Neoverse V2 when THP always mode is set on the
system and glibc.malloc.hugetlb=1.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
malloc/malloc.c

index cf5c02ff642e1049a46cad2837e0a0dcbcb5277f..ee4ea71d7323bf54da708c9af5fb7d2bcc2a33b5 100644 (file)
@@ -1871,6 +1871,7 @@ struct malloc_par
   INTERNAL_SIZE_T arena_max;
 
   /* Transparent Large Page support.  */
+  enum malloc_thp_mode_t thp_mode;
   INTERNAL_SIZE_T thp_pagesize;
   /* A value different than 0 means to align mmap allocation to hp_pagesize
      add hp_flags on flags.  */
@@ -1927,7 +1928,8 @@ static struct malloc_par mp_ =
   .mmap_threshold = DEFAULT_MMAP_THRESHOLD,
   .trim_threshold = DEFAULT_TRIM_THRESHOLD,
 #define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8))
-  .arena_test = NARENAS_FROM_NCORES (1)
+  .arena_test = NARENAS_FROM_NCORES (1),
+  .thp_mode = malloc_thp_mode_not_supported
 #if USE_TCACHE
   ,
   .tcache_count = TCACHE_FILL_COUNT,
@@ -2011,6 +2013,11 @@ static inline void
 madvise_thp (void *p, INTERNAL_SIZE_T size)
 {
 #ifdef MADV_HUGEPAGE
+  /* Only use __madvise if the system is using 'madvise' mode.
+     Otherwise the call is wasteful. */
+  if (mp_.thp_mode != malloc_thp_mode_madvise)
+    return;
+
   /* Do not consider areas smaller than a huge page or if the tunable is
      not active.  */
   if (mp_.thp_pagesize == 0 || size < mp_.thp_pagesize)
@@ -5648,12 +5655,9 @@ do_set_hugetlb (size_t value)
 {
   if (value == 1)
     {
-      enum malloc_thp_mode_t thp_mode = __malloc_thp_mode ();
-      /*
-        Only enable THP madvise usage if system does support it and
-        has 'madvise' mode.  Otherwise the madvise() call is wasteful.
-       */
-      if (thp_mode == malloc_thp_mode_madvise)
+      mp_.thp_mode = __malloc_thp_mode ();
+      if (mp_.thp_mode == malloc_thp_mode_madvise
+          || mp_.thp_mode == malloc_thp_mode_always)
        mp_.thp_pagesize = __malloc_default_thp_pagesize ();
     }
   else if (value >= 2)