[PATCH v2 4/4] loongarch: Enable THP-aligned load segments by default and define THP page size

WANG Rui wangrui@loongson.cn
Thu Feb 26 06:33:05 GMT 2026


On LoongArch64 Linux, aligning ELF load segments to Transparent Huge Page
(THP) boundaries provides consistent performance benefits for large
binaries by reducing TLB pressure and improving instruction fetch
efficiency.

Enable THP-based load segment alignment by default on LoongArch64 by
defining `DL_MAP_SEGMENT_ALIGN_THP_DEFAULT`. This allows the dynamic
loader to apply THP-friendly alignment without requiring the
`glibc.malloc.hugetlb` tunable to be explicitly set.

Define the default THP page size as 32MB for LoongArch64, reflecting the
architecture’s normal 16KB page size and PMD-level huge page geometry.
This value is used by the THP infrastructure to determine appropriate
alignment and mapping decisions.

* sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h:
Enable THP-based load segment alignment by default.
* sysdeps/unix/sysv/linux/loongarch/lp64/malloc-hugepages.h:
Define the default THP page size.

Signed-off-by: WANG Rui <wangrui@loongson.cn>
---
 elf/tst-decorate-maps.c                       |  2 +-
 .../loongarch/lp64/dl-map-segment-align.h     | 23 +++++++++++++++++++
 .../linux/loongarch/lp64/malloc-hugepages.h   | 21 +++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h
 create mode 100644 sysdeps/unix/sysv/linux/loongarch/lp64/malloc-hugepages.h

diff --git a/elf/tst-decorate-maps.c b/elf/tst-decorate-maps.c
index fa3637af89..d3e6a5540f 100644
--- a/elf/tst-decorate-maps.c
+++ b/elf/tst-decorate-maps.c
@@ -109,7 +109,7 @@ do_test_threads (bool set_guard)
   xpthread_barrier_init (&b, NULL, num_threads + 1);
 
   /* Issue a large malloc to trigger a mmap call.  */
-  void *p = xmalloc (2 * 1024 * 1024);
+  void *p = xmalloc (32 * 1024 * 1024);
 
   pthread_t thr[num_threads];
   {
diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h b/sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h
new file mode 100644
index 0000000000..8e95419b1c
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h
@@ -0,0 +1,23 @@
+/* _dl_map_segment_align.  LoongArch64 Linux version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Enable THP-based load segment alignment by default. */
+#define DL_MAP_SEGMENT_ALIGN_THP_DEFAULT 1
+
+#include_next <dl-map-segment-align.h>
diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/malloc-hugepages.h b/sysdeps/unix/sysv/linux/loongarch/lp64/malloc-hugepages.h
new file mode 100644
index 0000000000..8409670c7c
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/loongarch/lp64/malloc-hugepages.h
@@ -0,0 +1,21 @@
+/* Huge Page support.  LoongArch64 Linux version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#define DEFAULT_THP_PAGESIZE	(32 * 1024 * 1024)
+
+#include_next <malloc-hugepages.h>
-- 
2.53.0



More information about the Libc-alpha mailing list