public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: WANG Rui <wangrui@loongson.cn>
To: libc-alpha@sourceware.org
Cc: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	Dev Jain <dev.jain@arm.com>, Florian Weimer <fweimer@redhat.com>,
	Wilco Dijkstra <Wilco.Dijkstra@arm.com>,
	Xi Ruoyao <xry111@xry111.site>, WANG Xuerui <git@xen0n.name>,
	caiyinyu <caiyinyu@loongson.cn>,
	mengqinggang <mengqinggang@loongson.cn>,
	Huacai Chen <chenhuacai@kernel.org>,
	hjl.tools@gmail.com, WANG Rui <wangrui@loongson.cn>
Subject: [PATCH v8 1/6] hugepages: Move THP helpers to generic hugepages abstraction
Date: Sun,  5 Apr 2026 11:53:17 +0800	[thread overview]
Message-ID: <20260405035323.558335-2-wangrui@loongson.cn> (raw)
In-Reply-To: <20260405035323.558335-1-wangrui@loongson.cn>

The helpers for determining the default transparent huge page size
and THP mode are currently implemented in malloc-hugepages. However,
these interfaces are not malloc-specific and are also needed by other
subsystems (e.g. the dynamic loader for segment alignment).

Introduce a new generic hugepages abstraction and move the THP mode
detection, default THP page size probing, and hugepage configuration
helpers there. The malloc code now calls into the generic helpers
instead of duplicating the sysfs parsing.

There is no functional change. This is a pure refactoring to make
the THP detection reusable outside of malloc.

Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>
Signed-off-by: WANG Rui <wangrui@loongson.cn>
---
 elf/dl-tunables.c                             |  7 ++--
 malloc/malloc-internal.h                      |  2 +-
 malloc/malloc.c                               | 27 ++++++++-------
 sysdeps/generic/Makefile                      |  4 +--
 .../{malloc-hugepages.c => hugepages.c}       | 13 ++++----
 .../{malloc-hugepages.h => hugepages.h}       | 32 ++++++++++--------
 .../{malloc-hugepages.h => hugepages.h}       |  4 +--
 .../linux/{malloc-hugepages.c => hugepages.c} | 33 ++++++++++---------
 8 files changed, 63 insertions(+), 59 deletions(-)
 rename sysdeps/generic/{malloc-hugepages.c => hugepages.c} (76%)
 rename sysdeps/generic/{malloc-hugepages.h => hugepages.h} (68%)
 rename sysdeps/unix/sysv/linux/aarch64/{malloc-hugepages.h => hugepages.h} (91%)
 rename sysdeps/unix/sysv/linux/{malloc-hugepages.c => hugepages.c} (89%)

diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
index 1440d3fa6a5..5c65e8b4583 100644
--- a/elf/dl-tunables.c
+++ b/elf/dl-tunables.c
@@ -33,7 +33,7 @@
 #include <array_length.h>
 #include <dl-minimal-malloc.h>
 #include <dl-symbol-redir-ifunc.h>
-#include <malloc-hugepages.h>
+#include <hugepages.h>
 
 #define TUNABLES_INTERNAL 1
 #include "dl-tunables.h"
@@ -297,8 +297,9 @@ __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)
+  /* Default to glibc.malloc.hugetlb=1 if MALLOC_DEFAULT_THP_PAGESIZE
+     is non-zero.  */
+  if (MALLOC_DEFAULT_THP_PAGESIZE > 0)
     TUNABLE_SET (glibc, malloc, hugetlb, 1);
 
   /* Ignore tunables for AT_SECURE programs.  */
diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h
index dd326af1ea0..a6340bfd888 100644
--- a/malloc/malloc-internal.h
+++ b/malloc/malloc-internal.h
@@ -22,7 +22,7 @@
 #include <malloc-machine.h>
 #include <malloc-sysdep.h>
 #include <malloc-size.h>
-#include <malloc-hugepages.h>
+#include <hugepages.h>
 #include <calloc-clear-memory.h>
 
 /* Called in the parent process before a fork.  */
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 6a888b0eb7d..febd7b516f4 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1759,7 +1759,7 @@ struct malloc_par
   INTERNAL_SIZE_T arena_max;
 
   /* Transparent Large Page support.  */
-  enum malloc_thp_mode_t thp_mode;
+  enum 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.  */
@@ -1814,7 +1814,7 @@ static struct malloc_par mp_ =
   .trim_threshold = DEFAULT_TRIM_THRESHOLD,
 #define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8))
   .arena_test = NARENAS_FROM_NCORES (1),
-  .thp_mode = malloc_thp_mode_not_supported
+  .thp_mode = thp_mode_not_supported
 #if USE_TCACHE
   ,
   .tcache_count = TCACHE_FILL_COUNT,
@@ -1896,7 +1896,7 @@ madvise_thp (void *p, INTERNAL_SIZE_T size)
 
   /* Only use __madvise if the system is using 'madvise' mode and the size
      is at least a huge page, otherwise the call is wasteful. */
-  if (mp_.thp_mode != malloc_thp_mode_madvise || size < mp_.thp_pagesize)
+  if (mp_.thp_mode != thp_mode_madvise || size < mp_.thp_pagesize)
     return;
 
   /* Linux requires the input address to be page-aligned, and unaligned
@@ -5017,33 +5017,32 @@ 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)
+  /* Enable THP if MALLOC_DEFAULT_THP_PAGESIZE is non-zero.  */
+  if (MALLOC_DEFAULT_THP_PAGESIZE > 0)
     {
-      mp_.thp_mode = malloc_thp_mode_madvise;
-      mp_.thp_pagesize = DEFAULT_THP_PAGESIZE;
+      mp_.thp_mode = thp_mode_madvise;
+      mp_.thp_pagesize = MALLOC_DEFAULT_THP_PAGESIZE;
     }
 
   if (value == 0)
     {
       /* Turn off THP support completely.  */
-      mp_.thp_mode = malloc_thp_mode_never;
+      mp_.thp_mode = 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)
+      if (MALLOC_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)
-	mp_.thp_pagesize = __malloc_default_thp_pagesize ();
+      mp_.thp_mode = __get_thp_mode ();
+      if (mp_.thp_mode == thp_mode_madvise || mp_.thp_mode == thp_mode_always)
+	mp_.thp_pagesize = __get_thp_size ();
     }
   else if (value >= 2)
-    __malloc_hugepage_config (value == 2 ? 0 : value, &mp_.hp_pagesize,
+    __get_hugepage_config (value == 2 ? 0 : value, &mp_.hp_pagesize,
 			      &mp_.hp_flags);
   return 0;
 }
diff --git a/sysdeps/generic/Makefile b/sysdeps/generic/Makefile
index 0c25592f35b..53c4efa63e5 100644
--- a/sysdeps/generic/Makefile
+++ b/sysdeps/generic/Makefile
@@ -32,9 +32,9 @@ endif
 endif
 
 ifeq ($(subdir),malloc)
-sysdep_malloc_debug_routines += malloc-hugepages
+sysdep_malloc_debug_routines += hugepages
 endif
 
 ifeq ($(subdir),misc)
-sysdep_routines += malloc-hugepages
+sysdep_routines += hugepages
 endif
diff --git a/sysdeps/generic/malloc-hugepages.c b/sysdeps/generic/hugepages.c
similarity index 76%
rename from sysdeps/generic/malloc-hugepages.c
rename to sysdeps/generic/hugepages.c
index 57b82a2438d..28bab662b13 100644
--- a/sysdeps/generic/malloc-hugepages.c
+++ b/sysdeps/generic/hugepages.c
@@ -16,23 +16,22 @@
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-#include <malloc-hugepages.h>
+#include <hugepages.h>
 
 unsigned long int
-__malloc_default_thp_pagesize (void)
+__get_thp_size (void)
 {
   return 0;
 }
 
-enum malloc_thp_mode_t
-__malloc_thp_mode (void)
+enum thp_mode_t
+__get_thp_mode (void)
 {
-  return malloc_thp_mode_not_supported;
+  return thp_mode_not_supported;
 }
 
-/* Return the default transparent huge page size.  */
 void
-__malloc_hugepage_config (size_t requested, size_t *pagesize, int *flags)
+__get_hugepage_config (size_t requested, size_t *pagesize, int *flags)
 {
   *pagesize = 0;
   *flags = 0;
diff --git a/sysdeps/generic/malloc-hugepages.h b/sysdeps/generic/hugepages.h
similarity index 68%
rename from sysdeps/generic/malloc-hugepages.h
rename to sysdeps/generic/hugepages.h
index ab2690f7b8a..5fc9b5c8dea 100644
--- a/sysdeps/generic/malloc-hugepages.h
+++ b/sysdeps/generic/hugepages.h
@@ -1,4 +1,4 @@
-/* Malloc huge page support.  Generic implementation.
+/* Huge page support.  Generic implementation.
    Copyright (C) 2021-2026 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -16,33 +16,37 @@
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-#ifndef _MALLOC_HUGEPAGES_H
-#define _MALLOC_HUGEPAGES_H
+#ifndef _HUGEPAGES_H
+#define _HUGEPAGES_H
 
 #include <stddef.h>
 
 /* Return the default transparent huge page size.  */
-unsigned long int __malloc_default_thp_pagesize (void) attribute_hidden;
+unsigned long int __get_thp_size (void) attribute_hidden;
 
-enum malloc_thp_mode_t
+enum thp_mode_t
 {
-  malloc_thp_mode_always,
-  malloc_thp_mode_madvise,
-  malloc_thp_mode_never,
-  malloc_thp_mode_not_supported
+  thp_mode_always,
+  thp_mode_madvise,
+  thp_mode_never,
+  thp_mode_not_supported
 };
 
-enum malloc_thp_mode_t __malloc_thp_mode (void) attribute_hidden;
+enum thp_mode_t __get_thp_mode (void) attribute_hidden;
 
 /* Return the supported huge page size from the REQUESTED sizes on PAGESIZE
    along with the required extra mmap flags on FLAGS,  Requesting the value
    of 0 returns the default huge page size, otherwise the value will be
    matched against the sizes supported by the system.  */
-void __malloc_hugepage_config (size_t requested, size_t *pagesize, int *flags)
+void __get_hugepage_config (size_t requested, size_t *pagesize, int *flags)
      attribute_hidden;
 
-#ifndef DEFAULT_THP_PAGESIZE
-# define DEFAULT_THP_PAGESIZE	0
+#ifndef MALLOC_DEFAULT_THP_PAGESIZE
+# define MALLOC_DEFAULT_THP_PAGESIZE	0
 #endif
 
-#endif /* _MALLOC_HUGEPAGES_H */
+#ifndef MAX_THP_PAGESIZE
+# define MAX_THP_PAGESIZE	(32 * 1024 * 1024)
+#endif
+
+#endif /* _HUGEPAGES_H */
diff --git a/sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h b/sysdeps/unix/sysv/linux/aarch64/hugepages.h
similarity index 91%
rename from sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h
rename to sysdeps/unix/sysv/linux/aarch64/hugepages.h
index 2204112e4e5..4f9de8a3949 100644
--- a/sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h
+++ b/sysdeps/unix/sysv/linux/aarch64/hugepages.h
@@ -16,6 +16,6 @@
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-#define DEFAULT_THP_PAGESIZE	(1UL << 21)
+#define MALLOC_DEFAULT_THP_PAGESIZE	(1UL << 21)
 
-#include_next <malloc-hugepages.h>
+#include_next <hugepages.h>
diff --git a/sysdeps/unix/sysv/linux/malloc-hugepages.c b/sysdeps/unix/sysv/linux/hugepages.c
similarity index 89%
rename from sysdeps/unix/sysv/linux/malloc-hugepages.c
rename to sysdeps/unix/sysv/linux/hugepages.c
index 77b49374e60..707a75c2a29 100644
--- a/sysdeps/unix/sysv/linux/malloc-hugepages.c
+++ b/sysdeps/unix/sysv/linux/hugepages.c
@@ -18,16 +18,13 @@
 
 #include <intprops.h>
 #include <dirent.h>
-#include <malloc-hugepages.h>
+#include <hugepages.h>
 #include <not-cancel.h>
 #include <sys/mman.h>
 
 unsigned long int
-__malloc_default_thp_pagesize (void)
+__get_thp_size (void)
 {
-  if (DEFAULT_THP_PAGESIZE != 0)
-    return DEFAULT_THP_PAGESIZE;
-
   int fd = __open64_nocancel (
     "/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", O_RDONLY);
   if (fd == -1)
@@ -50,13 +47,13 @@ __malloc_default_thp_pagesize (void)
   return r;
 }
 
-enum malloc_thp_mode_t
-__malloc_thp_mode (void)
+enum thp_mode_t
+__get_thp_mode (void)
 {
   int fd = __open64_nocancel ("/sys/kernel/mm/transparent_hugepage/enabled",
 			      O_RDONLY);
   if (fd == -1)
-    return malloc_thp_mode_not_supported;
+    return thp_mode_not_supported;
 
   static const char mode_always[]  = "[always] madvise never\n";
   static const char mode_madvise[] = "always [madvise] never\n";
@@ -65,24 +62,26 @@ __malloc_thp_mode (void)
   char str[sizeof(mode_always)];
   ssize_t s = __read_nocancel (fd, str, sizeof (str));
   if (s >= sizeof str || s < 0)
-    return malloc_thp_mode_not_supported;
+    return thp_mode_not_supported;
   str[s] = '\0';
   __close_nocancel (fd);
 
   if (s == sizeof (mode_always) - 1)
     {
       if (strcmp (str, mode_always) == 0)
-	return malloc_thp_mode_always;
+	return thp_mode_always;
       else if (strcmp (str, mode_madvise) == 0)
-	return malloc_thp_mode_madvise;
+	return thp_mode_madvise;
       else if (strcmp (str, mode_never) == 0)
-	return malloc_thp_mode_never;
+	return thp_mode_never;
     }
-  return malloc_thp_mode_not_supported;
+  return thp_mode_not_supported;
 }
 
+#if !IS_IN (rtld)
+
 static size_t
-malloc_default_hugepage_size (void)
+get_default_hugepage_size (void)
 {
   int fd = __open64_nocancel ("/proc/meminfo", O_RDONLY);
   if (fd == -1)
@@ -136,14 +135,14 @@ hugepage_flags (size_t pagesize)
 }
 
 void
-__malloc_hugepage_config (size_t requested, size_t *pagesize, int *flags)
+__get_hugepage_config (size_t requested, size_t *pagesize, int *flags)
 {
   *pagesize = 0;
   *flags = 0;
 
   if (requested == 0)
     {
-      *pagesize = malloc_default_hugepage_size ();
+      *pagesize = get_default_hugepage_size ();
       if (*pagesize != 0)
 	*flags = hugepage_flags (*pagesize);
       return;
@@ -205,3 +204,5 @@ __malloc_hugepage_config (size_t requested, size_t *pagesize, int *flags)
 
   __close_nocancel (dirfd);
 }
+
+#endif /* !IS_IN(rtld) */
-- 
2.53.0


  reply	other threads:[~2026-04-05  3:54 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-05  3:53 [PATCH v8 0/6] elf: THP-aware load segment alignment WANG Rui
2026-04-05  3:53 ` WANG Rui [this message]
2026-04-07 14:28   ` [PATCH v8 1/6] hugepages: Move THP helpers to generic hugepages abstraction Wilco Dijkstra
2026-04-05  3:53 ` [PATCH v8 2/6] elf: Remove redundant _dl_map_segments declaration from dl-load.h WANG Rui
2026-04-05  3:53 ` [PATCH v8 3/6] elf: Introduce _dl_map_segment_align hook for segment alignment tuning WANG Rui
2026-04-06  8:02   ` H.J. Lu
2026-04-06 22:56     ` H.J. Lu
2026-04-06 23:09       ` H.J. Lu
2026-04-07  0:38         ` H.J. Lu
2026-04-07  1:14           ` H.J. Lu
2026-04-07  6:54             ` H.J. Lu
2026-04-08 14:14               ` Wilco Dijkstra
2026-04-08 20:27                 ` H.J. Lu
2026-04-09 10:20                   ` Wilco Dijkstra
2026-04-09 11:47                     ` H.J. Lu
2026-04-09 13:17                       ` Wilco Dijkstra
2026-04-09 20:43                         ` H.J. Lu
2026-04-09 21:04                           ` Wilco Dijkstra
2026-04-09 21:11                             ` H.J. Lu
2026-04-09 23:18                               ` Wilco Dijkstra
2026-04-09 23:44                                 ` H.J. Lu
2026-04-10 12:37                                   ` Wilco Dijkstra
2026-04-10  3:18     ` WANG Rui
2026-04-10  4:25       ` H.J. Lu
2026-04-05  3:53 ` [PATCH v8 4/6] tunables: Add glibc.elf.thp tunable for THP-aware segment alignment WANG Rui
2026-04-05  3:55 ` [PATCH v8 5/6] elf: Align large load segments to PMD huge page size for THP WANG Rui
2026-04-05  3:55   ` [PATCH v8 6/6] loongarch: Enable THP-aligned load segments by default on 64-bit WANG Rui
2026-04-07  1:06     ` H.J. Lu
2026-04-07 17:59       ` Wilco Dijkstra
2026-04-10  1:47     ` H.J. Lu
2026-04-10 10:34       ` Wilco Dijkstra
2026-04-11  0:31         ` H.J. Lu
2026-04-12 14:13           ` Wilco Dijkstra
2026-04-12 22:59             ` H.J. Lu
2026-04-12 23:53               ` WANG Rui
2026-04-06 22:45   ` [PATCH v8 5/6] elf: Align large load segments to PMD huge page size for THP H.J. Lu
2026-04-06 23:10     ` H.J. Lu
2026-04-09 22:38   ` H.J. Lu
2026-04-09 22:43     ` H.J. Lu
2026-04-09 22:45     ` Wilco Dijkstra
2026-04-09 23:47       ` H.J. Lu
2026-04-09 23:58   ` H.J. Lu
2026-04-10  0:57     ` H.J. Lu
2026-04-10  2:51       ` WANG Rui
2026-04-10  3:22         ` H.J. Lu
2026-04-10 10:18           ` Wilco Dijkstra
2026-04-11  0:29             ` H.J. Lu
2026-04-11 12:13               ` Wilco Dijkstra
2026-04-11 23:05                 ` H.J. Lu
2026-04-12 10:06                   ` WANG Rui
2026-04-12 11:26                     ` H.J. Lu
2026-04-12 12:50                       ` WANG Rui
2026-04-12 14:01                         ` Wilco Dijkstra
2026-04-12 14:18                           ` H.J. Lu
2026-04-12 10:51                 ` WANG Rui
2026-04-12 11:49                   ` H.J. Lu
2026-04-12 12:50                     ` WANG Rui
2026-04-12  3:42   ` H.J. Lu
2026-04-12 10:21     ` WANG Rui
2026-04-06 23:32 ` [PATCH v8 0/6] elf: THP-aware load segment alignment H.J. Lu
2026-04-08  2:07   ` H.J. Lu
2026-04-08  7:47     ` [PATCH] elf: Add transparent huge page marker and _dl_map_segment_adjust H.J. Lu
2026-04-09 10:28     ` [PATCH v8 0/6] elf: THP-aware load segment alignment Wilco Dijkstra
2026-04-09 11:50       ` H.J. Lu
2026-04-09 13:29         ` Wilco Dijkstra
2026-04-09 21:18           ` H.J. Lu
2026-04-09 21:39             ` Wilco Dijkstra
2026-04-09 22:24               ` H.J. Lu
2026-04-09 23:32                 ` Wilco Dijkstra
2026-04-10  1:00                   ` H.J. Lu
2026-04-10  2:50                     ` WANG Rui
2026-04-10  3:26                       ` H.J. Lu
2026-04-10  3:51                         ` WANG Rui
2026-04-10  4:19                           ` H.J. Lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260405035323.558335-2-wangrui@loongson.cn \
    --to=wangrui@loongson.cn \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=caiyinyu@loongson.cn \
    --cc=chenhuacai@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=fweimer@redhat.com \
    --cc=git@xen0n.name \
    --cc=hjl.tools@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=mengqinggang@loongson.cn \
    --cc=xry111@xry111.site \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).