[v2] malloc: Show hugetlb tunable default in --list-tunables

Message ID PAWPR08MB898260567B3165821B120DE88347A@PAWPR08MB8982.eurprd08.prod.outlook.com (mailing list archive)
State Under Review
Delegated to: DJ Delorie
Headers
Series [v2] malloc: Show hugetlb tunable default in --list-tunables |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

Wilco Dijkstra March 11, 2026, 6:35 p.m. UTC
  v2: Reset thp_pagesize with glibc.malloc.hugetlb=0 as suggested by Dev, move
    tunable to dl-tunables.c, fix broken --list-tunables testcase.

Update the hugetlb tunable default in elf/dl-tunables.c so it is shown as 1
with /lib/ld-linux-aarch64.so.1 --list-tunables.
Move the intitialization of thp_mode/thp_pagesize to do_set_hugetlb() and
avoid accessing /sys/kernel/mm if DEFAULT_THP_PAGESIZE > 0.  Switch off THP if
glibc.malloc.hugetlb=0 is used - this behaves as if DEFAULT_THP_PAGESIZE==0.
Fix the --list-tunables testcase.

Passes regress, OK for commit?

---
  

Comments

DJ Delorie March 18, 2026, 1:17 a.m. UTC | #1
LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>

I wonder if some day we could use macros in dl-tunables.list, so we
could have just said

    default: DEFAULT_HUGETHP
  
WANG Rui March 24, 2026, 7:28 a.m. UTC | #2
Hi,

On Wed, Mar 18, 2026 at 9:17 AM DJ Delorie <dj@redhat.com> wrote:
>
> LGTM
> Reviewed-by: DJ Delorie <dj@redhat.com>

Just checking the status of this patch. I'm preparing a related change
that conflicts with it, so I'd like to coordinate the timing for
rebasing.

Thanks,
Rui
  
Wilco Dijkstra April 1, 2026, 12:28 p.m. UTC | #3
Hi Rui,

> Just checking the status of this patch. I'm preparing a related change
> that conflicts with it, so I'd like to coordinate the timing for
> rebasing.

I've now committed it.

Cheers,
Wilco
  

Patch

diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
index bdb1de4ceba40b17fe5e10be246702b8bd25a812..1440d3fa6a52ad8549e2e3c53b8b6ccf55d82736 100644
--- a/elf/dl-tunables.c
+++ b/elf/dl-tunables.c
@@ -33,6 +33,7 @@ 
 #include <array_length.h>
 #include <dl-minimal-malloc.h>
 #include <dl-symbol-redir-ifunc.h>
+#include <malloc-hugepages.h>
 
 #define TUNABLES_INTERNAL 1
 #include "dl-tunables.h"
@@ -296,6 +297,10 @@  __tunables_init (char **envp)
   char *envval = NULL;
   char **prev_envp = envp;
 
+  /* Default to glibc.malloc.hugetlb=1 if DEFAULT_THP_PAGESIZE is non-zero.  */
+  if (DEFAULT_THP_PAGESIZE > 0)
+    TUNABLE_SET (glibc, malloc, hugetlb, 1);
+
   /* Ignore tunables for AT_SECURE programs.  */
   if (__libc_enable_secure)
     return;
diff --git a/elf/tst-rtld-list-tunables.sh b/elf/tst-rtld-list-tunables.sh
index 669898d8c050b6b5a89f679e37b921d6f457e8e1..11b9b4597a8faceceac43135a61898ceea58c16b 100755
--- a/elf/tst-rtld-list-tunables.sh
+++ b/elf/tst-rtld-list-tunables.sh
@@ -26,16 +26,8 @@  run_program_env=$3
 LC_ALL=C
 export LC_ALL
 
-# Unset tunables and their aliases.
-GLIBC_TUNABLES=
-MALLOC_ARENA_MAX=
-MALLOC_ARENA_TEST=
-MALLOC_CHECK_=
-MALLOC_MMAP_MAX_=
-MALLOC_MMAP_THRESHOLD_=
-MALLOC_PERTURB_=
-MALLOC_TOP_PAD_=
-MALLOC_TRIM_THRESHOLD_=
+# Unset tunables.
+export GLIBC_TUNABLES=glibc.malloc.hugetlb=0
 
 ${test_wrapper_env} \
 ${run_program_env} \
diff --git a/malloc/arena.c b/malloc/arena.c
index 75f2f32b5f61a33a06dc285ca9899a708c78cf10..d2ed6279226856d630cfdf734303c4b936512f58 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -275,15 +275,6 @@  __ptmalloc_init (void)
     __always_fail_morecore = true;
 #endif
 
-  /* Enable THP if DEFAULT_THP_PAGESIZE is non-zero.  Avoid quering the THP
-     page size or mode since accessing /sys/kernel/mm is relatively slow and
-     might not be accessible in containers.  */
-  if (DEFAULT_THP_PAGESIZE > 0)
-    {
-      mp_.thp_mode = malloc_thp_mode_madvise;
-      mp_.thp_pagesize = DEFAULT_THP_PAGESIZE;
-    }
-
   thread_arena = &main_arena;
 
   malloc_init_state (&main_arena);
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 6455a1b0e0b85da0caaba4a5d922e1660fab5bb5..6a888b0eb7de53ae7b814275e86d2bd2f06b5e53 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -5017,10 +5017,26 @@  do_set_mxfast (size_t value)
 static __always_inline int
 do_set_hugetlb (size_t value)
 {
+  /* Enable THP if DEFAULT_THP_PAGESIZE is non-zero.  */
+  if (DEFAULT_THP_PAGESIZE > 0)
+    {
+      mp_.thp_mode = malloc_thp_mode_madvise;
+      mp_.thp_pagesize = DEFAULT_THP_PAGESIZE;
+    }
+
   if (value == 0)
-    mp_.thp_mode = malloc_thp_mode_never;
+    {
+      /* Turn off THP support completely.  */
+      mp_.thp_mode = malloc_thp_mode_never;
+      mp_.thp_pagesize = 0;
+    }
   else if (value == 1)
     {
+      /* Avoid querying the THP page size/mode since accessing /sys/kernel/mm
+	 is relatively slow and might not be accessible in containers.  */
+      if (DEFAULT_THP_PAGESIZE > 0)
+	return 0;
+
       mp_.thp_mode = __malloc_thp_mode ();
       if (mp_.thp_mode == malloc_thp_mode_madvise
           || mp_.thp_mode == malloc_thp_mode_always)