From patchwork Sun Apr 5 03:53:17 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 132702 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 4011E4BA2E2F for ; Sun, 5 Apr 2026 03:55:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4011E4BA2E2F X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 7E5314BA2E24 for ; Sun, 5 Apr 2026 03:54:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7E5314BA2E24 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7E5314BA2E24 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361248; cv=none; b=FyE2957nWd4qBw3ucXBcy1GsPUaL3dJXB8F0DzWoXCNFO2qpDwWY7Um58hOIfZ19YppBwioyf/lxJBOWmWvJgWuVIw/l52bJvuH2YkGDxF9ZvYRfUeHTmQDG1pDTw3lcog4u0z0Qq6Q1MfC7BqLb/Iu4Uem8NJ+ukGJqAxh7guw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361248; c=relaxed/simple; bh=2QoRue9+GOUrEgPoMFpUKF9s+pYaEgpPicbdeiPK72k=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=gy18FoLj3JLJmiJ5eOFeY5/lEQspAjvNfG9VX1jhjAasuMUjr3hDfNrlU4i2jAuO+4qcJ+rOM7EtNyk0dq8kgODuPmRPyUniH3fAqgWFzkknKOCUtE0kStxFDciu+gGwOMCBnBQs1Ia7Vgz9WLkdoNcSfJycoAyeYedxBAOXGCA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E5314BA2E24 Received: from loongson.cn (unknown [223.64.120.66]) by gateway (Coremail) with SMTP id _____8BxVcHc3NFphBgiAA--.48242S3; Sun, 05 Apr 2026 11:54:04 +0800 (CST) Received: from localhost (unknown [223.64.120.66]) by front1 (Coremail) with SMTP id qMiowJBxDOHK3NFpP1JlAA--.44692S3; Sun, 05 Apr 2026 11:53:58 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Dev Jain , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , Huacai Chen , hjl.tools@gmail.com, WANG Rui Subject: [PATCH v8 1/6] hugepages: Move THP helpers to generic hugepages abstraction Date: Sun, 5 Apr 2026 11:53:17 +0800 Message-ID: <20260405035323.558335-2-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260405035323.558335-1-wangrui@loongson.cn> References: <20260405035323.558335-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJBxDOHK3NFpP1JlAA--.44692S3 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj9fXoWfJFWxuFykWw1rtF48Aw4DJrc_yoW8JF4kGo WfGFWrJF4rXrn8ZrWxCa90qa1jk34kuw4rJFnxZrZxWayjqr1rAr40ka95Wr9xGF45KFyx JFyrK3W5tF18tr95l-sFpf9Il3svdjkaLaAFLSUrUUUU8b8apTn2vfkv8UJUUUU8wcxFpf 9Il3svdxBIdaVrn0xqx4xG64xvF2IEw4CE5I8CrVC2j2Jv73VFW2AGmfu7bjvjm3AaLaJ3 UjIYCTnIWjp_UUUYX7kC6x804xWl14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI 8IcIk0rVWrJVCq3wAFIxvE14AKwVWUAVWUZwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xG Y2AK021l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14 v26r4j6F4UM28EF7xvwVC2z280aVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4j6r4UJwAaw2AFwI0_Jrv_JF1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0c Ia020Ex4CE44I27wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jw0_ WrylYx0Ex4A2jsIE14v26r4j6F4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwI xGrwCY1x0262kKe7AKxVWUAVWUtwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwCFI7km07C267AKxVWUXVWUAwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4 vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IY x2IY67AKxVW8JVW5JwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26c xKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2jsIEc7CjxVAF wI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jzc_-UUUUU= X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org 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 Signed-off-by: WANG Rui --- 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 #include #include -#include +#include #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 #include #include -#include +#include #include /* 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 . */ -#include +#include 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 . */ -#ifndef _MALLOC_HUGEPAGES_H -#define _MALLOC_HUGEPAGES_H +#ifndef _HUGEPAGES_H +#define _HUGEPAGES_H #include /* 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 . */ -#define DEFAULT_THP_PAGESIZE (1UL << 21) +#define MALLOC_DEFAULT_THP_PAGESIZE (1UL << 21) -#include_next +#include_next 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 #include -#include +#include #include #include 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) */ From patchwork Sun Apr 5 03:53:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 132699 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id F257B4BA903E for ; Sun, 5 Apr 2026 03:54:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F257B4BA903E X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 79F8F4BA2E20 for ; Sun, 5 Apr 2026 03:54:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 79F8F4BA2E20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 79F8F4BA2E20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361248; cv=none; b=ArWFCGaVqqCw2XS7Y/YpT/qe+fSK0dgwlINR69vf+0AxFELsa5/+6HcmfgGZciybLefUjH0/kADwTShYTswYXIJjUg8SRbUQOPRmSfE867FH6ZzHDT6Eiq2skGf3eZrYpeV79a9zuHBR1lkqYvSyhQkIDygCWJX2gMD6mQV+ijs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361248; c=relaxed/simple; bh=5kV+tPN5ipcRFgmNHgXAmm5K+NVaULoBm8ktOeNgHZ4=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Zyz0eQdHmiS0XfqN+HCZTw63g6LQJaxjwTaRNDNAcPpRrDU4ZMDGhSqM0dJCfaOlJKGuNK5zsh68DWYK53TOuoFMkJKcbIxT36fmfY1pDc8Pf22zDRMf2u8JFzWXNfmvGUhPKpKeSqx0BEbWk+uFLpwsgq2liVLTTo5dcOC7JZs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 79F8F4BA2E20 Received: from loongson.cn (unknown [223.64.120.66]) by gateway (Coremail) with SMTP id _____8DxccDd3NFpixgiAA--.36673S3; Sun, 05 Apr 2026 11:54:05 +0800 (CST) Received: from localhost (unknown [223.64.120.66]) by front1 (Coremail) with SMTP id qMiowJBxDOHK3NFpP1JlAA--.44692S4; Sun, 05 Apr 2026 11:54:02 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Dev Jain , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , Huacai Chen , hjl.tools@gmail.com, WANG Rui Subject: [PATCH v8 2/6] elf: Remove redundant _dl_map_segments declaration from dl-load.h Date: Sun, 5 Apr 2026 11:53:18 +0800 Message-ID: <20260405035323.558335-3-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260405035323.558335-1-wangrui@loongson.cn> References: <20260405035323.558335-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJBxDOHK3NFpP1JlAA--.44692S4 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxJr4kZry7Jw4kWrW8CFW5XFc_yoW8Cw45pF 1Yk34rKFn3Cw4ruayDKa17Ka13Zrn5tw15Wry3W397Xrn7Wr4xXF4fK3WY9a4fJrZ5t3WS vF9rKr15ZF4FkrXCm3ZEXasCq-sJn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUBYb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1q6r4UM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ Gr0_Gr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYI kI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUtVWr XwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI4 8JMxkF7I0En4kS14v26r126r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMxCIbckI1I0E14v26r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwV AFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv2 0xvE14v26r4j6ryUMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4 v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AK xVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU1mii3UUUUU== X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The function `_dl_map_segments` is defined in ``, which provides the canonical implementation (optionally overridden by sysdeps variants). All call sites include `` directly, so declaring `_dl_map_segments` in `dl-load.h` is unnecessary. Keeping a static prototype in `dl-load.h` can trigger -Wunused-function errors when the header is included by translation units that do not include `` and do not reference `_dl_map_segments`. Since glibc builds with `-Werror`, this results in build failures [1]. Remove the redundant declaration from `dl-load.h` to avoid these spurious warnings and keep the declaration colocated with the definition as intended. [1] https://ci.linaro.org/job/tcwg_glibc_build--master-aarch64-precommit/4624/artifact/artifacts/artifacts.precommit/notify/mail-body.txt Reviewed-by: Wilco Dijkstra Signed-off-by: WANG Rui --- elf/dl-load.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/elf/dl-load.h b/elf/dl-load.h index dbc29b335e8..897c4034c5e 100644 --- a/elf/dl-load.h +++ b/elf/dl-load.h @@ -109,14 +109,15 @@ _dl_postprocess_loadcmd (struct link_map *l, const ElfW(Ehdr) *header, The file defines this function. The canonical implementation in elf/dl-map-segments.h might be replaced by a sysdeps - version. */ + version. + static const char *_dl_map_segments (struct link_map *l, int fd, const ElfW(Ehdr) *header, int type, const struct loadcmd loadcmds[], size_t nloadcmds, const size_t maplength, bool has_holes, - struct link_map *loader); + struct link_map *loader); */ /* All the error message strings _dl_map_segments might return are listed here so that different implementations in different sysdeps From patchwork Sun Apr 5 03:53:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 132700 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 86B994BA2E24 for ; Sun, 5 Apr 2026 03:54:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 86B994BA2E24 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id E7EB54BA2E28 for ; Sun, 5 Apr 2026 03:54:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E7EB54BA2E28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E7EB54BA2E28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361248; cv=none; b=dAu0IYs75FDP7tKJAJrSuy4UfEHYtXkibpmPxG5HnBA9FnreAA9fyb6kwB9P9UlCShnBjz3dMGmXsrS1j+4HIljNEwFMFigiWWo3/A2/NxYGyt59Ak9hLUB2/myNJvkVb7u9IYwJ0LQWhwc2+aG6qHYnacvclfA9ki9OuRLC3o0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361248; c=relaxed/simple; bh=x9MSzKjqsgs0fJ0s0Mep4EJ3ZieOH4+Wg87O2Nd3Bb4=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=ZsmZmORKfwtRj9AMChDGh+bFUFFritxsmqLdZA/gzeHfPgltaw1DZ4L3tx1ynC39DoD4Zv8pOgxsKzfTNNo8RoPxAiHJfnV6Looycg84tKJm4L6LZbwY06lBnXsI1vj7ze/dYvAcW8VwSJ1sfl3pnwm0JUW4fg+Ia68IPy0i0Bc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E7EB54BA2E28 Received: from loongson.cn (unknown [223.64.120.66]) by gateway (Coremail) with SMTP id _____8BxFMHe3NFplRgiAA--.48927S3; Sun, 05 Apr 2026 11:54:06 +0800 (CST) Received: from localhost (unknown [223.64.120.66]) by front1 (Coremail) with SMTP id qMiowJBxDOHK3NFpP1JlAA--.44692S5; Sun, 05 Apr 2026 11:54:05 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Dev Jain , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , Huacai Chen , hjl.tools@gmail.com, WANG Rui Subject: [PATCH v8 3/6] elf: Introduce _dl_map_segment_align hook for segment alignment tuning Date: Sun, 5 Apr 2026 11:53:19 +0800 Message-ID: <20260405035323.558335-4-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260405035323.558335-1-wangrui@loongson.cn> References: <20260405035323.558335-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJBxDOHK3NFpP1JlAA--.44692S5 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCFyktFW8tF4Dtr1ftw1xtFc_yoW5Ww45pF 97CF4rKF1rJF17CFZ3t3W2kr15X3WrGF4YyrnxuwnrZrs7GFy8XrZIv3WYgryrGrW8CF4S vFZFqF98Ca1rArcCm3ZEXasCq-sJn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUBYb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1a6r1DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ Gr0_Gr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYI kI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUtVWr XwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI4 8JMxkF7I0En4kS14v26r126r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMxCIbckI1I0E14v26r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwV AFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv2 0xvE14v26ryj6F1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4 v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AK xVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU1mii3UUUUU== X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Introduce a new helper function, _dl_map_segment_align, to allow architecture-specific adjustment of ELF load segment alignment during object mapping. The generic ELF loader now calls this hook when determining the maximum segment alignment. The generic implementation is a no-op and preserves existing behavior. This provides a well-defined extension point for architectures that need to adjust segment alignment policies (for example, to improve mapping efficiency or enable platform-specific optimizations) without embedding such logic directly in the generic loader. Reviewed-by: Wilco Dijkstra Signed-off-by: WANG Rui --- elf/dl-load.c | 4 ++++ sysdeps/generic/dl-map-segment-align.h | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 sysdeps/generic/dl-map-segment-align.h diff --git a/elf/dl-load.c b/elf/dl-load.c index 7355eef8e76..f3d943e99c0 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -71,6 +71,7 @@ struct filebuf #include #include #include +#include #include #include #include @@ -1171,6 +1172,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, /* Optimize a common case. */ c->prot = pf_to_prot (ph->p_flags); + + /* Architecture-specific adjustment of segment alignment. */ + p_align_max = _dl_map_segment_align (c, p_align_max); break; case PT_TLS: diff --git a/sysdeps/generic/dl-map-segment-align.h b/sysdeps/generic/dl-map-segment-align.h new file mode 100644 index 00000000000..f4a671f25f8 --- /dev/null +++ b/sysdeps/generic/dl-map-segment-align.h @@ -0,0 +1,26 @@ +/* _dl_map_segment_align. Generic 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 + . */ + +#include + +static inline ElfW(Addr) +_dl_map_segment_align (const struct loadcmd *c, ElfW(Addr) p_align_max) +{ + return p_align_max; +} From patchwork Sun Apr 5 03:53:20 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 132701 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 28F544BA9028 for ; Sun, 5 Apr 2026 03:54:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 28F544BA9028 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 891824BA23FE for ; Sun, 5 Apr 2026 03:54:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 891824BA23FE Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 891824BA23FE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361256; cv=none; b=YmtC2uQb54WFnYJKNuU5KP/ByyyKsu0OtPDu0YssxDCuTI1QhhfXdV9bzPiao4QkGEHr9Qi+bxuNN84EHozQI5XgFwGV7UWQSLGu7cvkwzMwaM4xFqSKVlh5VwN6WItyUb7eHFw/IpgLdvMixwpMc7a2JK1rfxjljc4rDw2ic1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361256; c=relaxed/simple; bh=R6Yv29w4nzNxZJlsdjeV5hcaHovFXPv2WKsbGFl1ft0=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=gpGktMyHzkNr59Wlf2WdEBxJ98XNpn84uswha8eijsLWPW9Uycxrvomhxkm3iSqQ7qGYa/uzVWMCCTHaO1ORrbCexaAj4HZx1ErWatzI7/rWzngCPD1hd8aiJkqJtq86jFlvo603QdkjbN8WgUaGiOEuwMShe75cq57Z3O1lYmE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 891824BA23FE Received: from loongson.cn (unknown [223.64.120.66]) by gateway (Coremail) with SMTP id _____8DxAfHi3NFpoBgiAA--.30399S3; Sun, 05 Apr 2026 11:54:10 +0800 (CST) Received: from localhost (unknown [223.64.120.66]) by front1 (Coremail) with SMTP id qMiowJBxDOHK3NFpP1JlAA--.44692S6; Sun, 05 Apr 2026 11:54:06 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Dev Jain , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , Huacai Chen , hjl.tools@gmail.com, WANG Rui Subject: [PATCH v8 4/6] tunables: Add glibc.elf.thp tunable for THP-aware segment alignment Date: Sun, 5 Apr 2026 11:53:20 +0800 Message-ID: <20260405035323.558335-5-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260405035323.558335-1-wangrui@loongson.cn> References: <20260405035323.558335-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJBxDOHK3NFpP1JlAA--.44692S6 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCF4xKFyUZrW8ur15tw4UJrc_yoW5AFy3pr y3WFWYkr43AFnxCFWay3W5Aa15Jw4fKa4UGry7Kwn7Xw43Gryftay2gr47Jay7Jr47AF47 ZrZFgr47Cw4kG3cCm3ZEXasCq-sJn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUBYb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ Gr0_Gr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYI kI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWrXVW3 AwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI4 8JMxkF7I0En4kS14v26r126r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMxCIbckI1I0E14v26r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwV AFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv2 0xvE14v26ryj6F1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4 v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AK xVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IUneRRtUUUUU== X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Introduce a new tunable, `glibc.elf.thp`, to control Transparent Huge Page (THP) aware alignment of ELF loadable segments. When set to `1`, the dynamic loader will attempt to align sufficiently large `PT_LOAD` segments to the PMD huge page size when mapping them. This increases the likelihood that the kernel backs these mappings with Transparent Huge Pages. The default value is `0`, which preserves the traditional page-sized alignment and keeps existing behavior unchanged. On systems without THP support, or when THP is disabled in the kernel, enabling this tunable has no effect. Reviewed-by: Wilco Dijkstra Signed-off-by: WANG Rui --- elf/dl-tunables.list | 8 ++++++++ manual/tunables.texi | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 040a544c0e0..1bbf70d36fa 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -24,6 +24,14 @@ # env_alias: An alias environment variable glibc { + elf { + thp { + type: INT_32 + minval: 0 + maxval: 1 + } + } + malloc { check { type: INT_32 diff --git a/manual/tunables.texi b/manual/tunables.texi index 72769428e8c..389690d840b 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -64,6 +64,7 @@ glibc.pthread.mutex_spin_count: 100 (min: 0, max: 32767) glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0xffffffffffffffff) glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0xffffffffffffffff) glibc.malloc.check: 0 (min: 0, max: 3) +glibc.elf.thp: 0 (min: 0, max: 1) @end example @menu @@ -77,6 +78,8 @@ glibc.malloc.check: 0 (min: 0, max: 3) @theglibc{}. * gmon Tunables:: Tunables that control the gmon profiler, used in conjunction with gprof +* ELF Tunables:: Tunables that control Transparent Huge Page (THP) + aware alignment of ELF loadable segments @end menu @@ -763,3 +766,24 @@ whose size exceeds the available memory; in that case, an out of memory error will be printed at program startup, the profiler will be disabled, and no @file{gmon.out} file will be generated. @end deftp + +@node ELF Tunables +@section ELF Tunables +@cindex elf tunables + +@deftp Tunable glibc.elf.thp +This tunable controls Transparent Huge Page (THP) aware alignment of +ELF loadable segments. + +The default value is @code{0}, which preserves the traditional page-sized +alignment for loadable segments. + +If set to @code{1}, glibc will attempt to align sufficiently large +@code{PT_LOAD} segments to the PMD huge page size when mapping them +with @code{mmap}. This increases the likelihood that the kernel can +back these mappings with Transparent Huge Pages. + +This tunable has an effect only on systems that support Transparent +Huge Pages (currently Linux). On other systems, or if THP is disabled +at the kernel level, enabling this tunable has no effect. +@end deftp From patchwork Sun Apr 5 03:55:54 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 132703 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 911DE4BA2E0D for ; Sun, 5 Apr 2026 03:57:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 911DE4BA2E0D X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 531674BA2E20 for ; Sun, 5 Apr 2026 03:56:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 531674BA2E20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 531674BA2E20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361391; cv=none; b=Qt6tIBplJMZ8Yw3h8rGAOVy+RqACkCPnPIkzBS2QNvEGvNQGZqUElPvc6BCJRleOcEqHDqZzG07FiaQT5dwECZ9A9JPJpVsAFCylhQsnXWZOmSNNO3ymDBzysF1laYFN7yYFmURZdRZw9i+UnxHpdlqdxCbTu3FpnoqpotgHkBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361391; c=relaxed/simple; bh=otQ7k8tq/Qy+oOHD5v0Tac2pXLQ2dHOPyzIBNPy3LcE=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=P7SS1yevHnMNqPO4PetJ7HfQK0Fl0e/LJNIimWPphuyVQLO2C+JLWUffk9kn5/N6YRKY4yhBMCUDCiFOWJ1nU5bt8ZhfiZz+m52p7Cp3alKjCHRRF6FZIUKQwcQFnqF3dQnuWcLllqs4xMdRbaTeWf4EW0gOnBZmph5W8MU+Y68= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 531674BA2E20 Received: from loongson.cn (unknown [223.64.120.66]) by gateway (Coremail) with SMTP id _____8DxssBs3dFpxBgiAA--.36586S3; Sun, 05 Apr 2026 11:56:28 +0800 (CST) Received: from localhost (unknown [223.64.120.66]) by front1 (Coremail) with SMTP id qMiowJBxrsJo3dFpj1JlAA--.45307S2; Sun, 05 Apr 2026 11:56:27 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Dev Jain , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , Huacai Chen , hjl.tools@gmail.com, WANG Rui Subject: [PATCH v8 5/6] elf: Align large load segments to PMD huge page size for THP Date: Sun, 5 Apr 2026 11:55:54 +0800 Message-ID: <20260405035555.558396-1-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260405035323.558335-1-wangrui@loongson.cn> References: <20260405035323.558335-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJBxrsJo3dFpj1JlAA--.45307S2 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3GryrKryfJryUuF4rKw4xXwc_yoW7uw1fpF WFkF4FkF48JFyjkFWfK3Wjyr1SgF1rGF1jkr13uF9xAw1DGFyIgFZFka43JryxGrWfCa1r ZrsFqF98Cr1rZagCm3ZEXasCq-sJn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUBIb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4j6r4UJwAaw2AFwI0_Jrv_JF1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0c Ia020Ex4CE44I27wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Wrv_ ZF1lYx0Ex4A2jsIE14v26F4j6r4UJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41lc7CjxVAaw2AFwI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1l4IxYO2xFxVAFwI0_Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8Gjc xK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0 cI8IcVAFwI0_Ar0_tr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw2 0EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x02 67AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU0fhLUUUUUU== X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Mapping segments that are at least the size of a PMD huge page to huge-page-aligned addresses helps make them eligible for Transparent Huge Pages (THP). This patch introduces a Linux-specific helper, `_dl_map_segment_align`, to determine an appropriate maximum alignment for ELF load segments based on the system THP policy. The optimization is enabled only when the glibc tunable `glibc.elf.thp=1` is set and THP is configured to be used unconditionally. The optimization depends on Linux kernel support for file-backed THP, specifically: * `CONFIG_READ_ONLY_THP_FOR_FS` (available since Linux kernel 5.4), and * `CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS`. When enabled, the helper queries the default THP page size and uses it to align sufficiently large load segments that are already properly aligned in both virtual address and file offset (e.g., zero). For eligible segments, the alignment is bumped to the THP page size, which improves THP eligibility, reduces TLB pressure, and improves performance for large objects. To avoid excessive address space padding on systems with very large THP sizes, the alignment is capped at 32MB. The optimization is applied only to non-writable segments, matching typical THP usage. Signed-off-by: WANG Rui --- sysdeps/unix/sysv/linux/Makefile | 1 + .../unix/sysv/linux/dl-map-segment-align.c | 53 +++++++++++++++++++ .../unix/sysv/linux/dl-map-segment-align.h | 27 ++++++++++ 3 files changed, 81 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/dl-map-segment-align.c create mode 100644 sysdeps/unix/sysv/linux/dl-map-segment-align.h diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index c6bd97abf1a..9879fec9a93 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -673,6 +673,7 @@ endif ifeq ($(subdir),elf) dl-routines += \ + dl-map-segment-align \ dl-rseq-symbols \ # dl-routines diff --git a/sysdeps/unix/sysv/linux/dl-map-segment-align.c b/sysdeps/unix/sysv/linux/dl-map-segment-align.c new file mode 100644 index 00000000000..75fb0496730 --- /dev/null +++ b/sysdeps/unix/sysv/linux/dl-map-segment-align.c @@ -0,0 +1,53 @@ +/* _dl_map_segment_align. 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 + . */ + +#include +#include +#include + +ElfW (Addr) +_dl_map_segment_align (const struct loadcmd *c, ElfW (Addr) p_align_max) +{ + static enum thp_mode_t thp_mode = thp_mode_not_supported; + static unsigned long int thp_pagesize; + + if (TUNABLE_GET (glibc, elf, thp, int32_t, NULL) == 0) + return p_align_max; + + if (__glibc_unlikely (thp_mode == thp_mode_not_supported + || thp_pagesize == 0)) + { + unsigned long int default_thp_pagesize = DL_MAP_DEFAULT_THP_PAGESIZE; + thp_mode = default_thp_pagesize ? thp_mode_always : __get_thp_mode (); + thp_pagesize = default_thp_pagesize ? : __get_thp_size (); + } + + /* Aligning load segments that are large enough to the PMD size helps + improve THP eligibility and reduces TLB pressure. + We cap the huge page size at MAX_THP_PAGESIZE to avoid over-aligning + on systems with very large normal pages (like 64K pages with 512M + huge pages). */ + if (thp_mode == thp_mode_always && thp_pagesize <= MAX_THP_PAGESIZE + && ((c->mapstart | c->mapoff) & (thp_pagesize - 1)) == 0 + && (c->mapend - c->mapstart) >= thp_pagesize + && p_align_max < thp_pagesize && (c->prot & PROT_WRITE) == 0) + return thp_pagesize; + + return p_align_max; +} diff --git a/sysdeps/unix/sysv/linux/dl-map-segment-align.h b/sysdeps/unix/sysv/linux/dl-map-segment-align.h new file mode 100644 index 00000000000..83794a03986 --- /dev/null +++ b/sysdeps/unix/sysv/linux/dl-map-segment-align.h @@ -0,0 +1,27 @@ +/* _dl_map_segment_align. 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 + . */ + +#include + +#ifndef DL_MAP_DEFAULT_THP_PAGESIZE +# define DL_MAP_DEFAULT_THP_PAGESIZE 0 +#endif + +ElfW (Addr) + _dl_map_segment_align (const struct loadcmd *c, ElfW (Addr) p_align_max); From patchwork Sun Apr 5 03:55:55 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 132704 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 6827C4BA23FA for ; Sun, 5 Apr 2026 03:57:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6827C4BA23FA X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 119B34BA2E24 for ; Sun, 5 Apr 2026 03:56:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 119B34BA2E24 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 119B34BA2E24 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361392; cv=none; b=g9ALDytfWCDvpGHoBwm29tXjxaEY5CauTBY+fyMaxjmc24wzqr6JdHyQWQsd+7K2NsTsdaoKVGKUb/Go5qr5ljiNfOiE6xK2RVVBi2i28/7YXquxelFFiN8jgxALKdvqhPgnIsBJeKAA+7q6YWiyj5UhMqDsWv3szoKYgGgPk3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775361392; c=relaxed/simple; bh=eZHFaLBd7oXRxrYYGmvQftnnh8LTLsZDZfT/vdpUyeQ=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Pp6Nf4qf0owYyZG/wjCcJMsISVy74Atduk0lyxK/vpIWjrWwHXh7ELhqcWycRca6n0VWJoqPjGFKuu7JR45fAwM3lt2p/xOm4HoSgZ03WxVEAeUQnfLaTJF7xlLvKzFZUt/I/0CdVGS0uodifhUpTNt+YjqsYhHizHK6mGfYjZk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 119B34BA2E24 Received: from loongson.cn (unknown [223.64.120.66]) by gateway (Coremail) with SMTP id _____8BxUcBt3dFpzRgiAA--.36704S3; Sun, 05 Apr 2026 11:56:29 +0800 (CST) Received: from localhost (unknown [223.64.120.66]) by front1 (Coremail) with SMTP id qMiowJBxrsJo3dFpj1JlAA--.45307S3; Sun, 05 Apr 2026 11:56:28 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Dev Jain , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , Huacai Chen , hjl.tools@gmail.com, WANG Rui Subject: [PATCH v8 6/6] loongarch: Enable THP-aligned load segments by default on 64-bit Date: Sun, 5 Apr 2026 11:55:55 +0800 Message-ID: <20260405035555.558396-2-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260405035555.558396-1-wangrui@loongson.cn> References: <20260405035323.558335-1-wangrui@loongson.cn> <20260405035555.558396-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJBxrsJo3dFpj1JlAA--.45307S3 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw1UXw43KF1fGr4rtw1xXrc_yoWrWr1Upr WakFs5CF4rWr17C3yak3W7Z3W5WF4rCFs8Cr9xCw4UZryUJr1xZFsFyF1fJFy7J3yxGF4x uFnFq3WDuF1rAacCm3ZEXasCq-sJn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUBSb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r126r13M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8JVW8Jr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26rWY 6Fy7McIj6I8E87Iv67AKxVWxJVW8Jr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxkF7I0En4kS14v26r126r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY 6r1j6r4UMxCIbckI1I0E14v26r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7 xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xII jxv20xvE14v26F1j6w1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1lIxAIcVCF04 k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26F4j6r4UJwCI42IY6I8E87Iv6xkF 7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUIeHqUUUUU X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org 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 setting `glibc.elf.thp=1` during startup. Define the default THP page size for load segment alignment on LoongArch64 as 32MB. This allows the dynamic loader to apply THP-friendly alignment without requiring the `glibc.elf.thp` tunable to be explicitly set. Benchmarks Machine: Loongson 3A6000 (LoongArch64) Kernel: 6.18.13 CONFIG_READ_ONLY_THP_FOR_FS=y CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS=y Workload 1: building Cargo 1.93.0 Rustc: nightly-2026-02-26 Without patch With patch instructions 3,690,358,948,176 3,690,301,774,568 cpu-cycles 4,233,025,766,760 4,035,866,635,741 itlb-misses 9,708,829,532 2,700,014,717 time elapsed 302.40 s 289.68 s Instructions remain essentially unchanged. iTLB misses drop by about 72%, reducing CPU cycles by about 4.7% and wall time by about 4.2%. Workload 2: building Linux kernel v7.0-rc1 LLVM: 21.1.8 Without patch With patch instructions 14,163,739,876,387 14,169,418,598,675 cpu-cycles 19,231,890,317,741 16,851,494,928,181 itlb-misses 91,142,010,440 90,779,245 time elapsed 1022.09 s 893.22 s Instructions remain roughly the same. iTLB misses drop from about 91B to about 90M (roughly 99.9% reduction), reducing CPU cycles by about 12% and wall time by about 12.6%. Reviewed-by: caiyinyu Signed-off-by: WANG Rui --- .../unix/sysv/linux/loongarch/cpu-features.c | 6 +++++ .../loongarch/lp64/dl-map-segment-align.h | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h diff --git a/sysdeps/unix/sysv/linux/loongarch/cpu-features.c b/sysdeps/unix/sysv/linux/loongarch/cpu-features.c index 0c125e64e2b..7ed54ff53e7 100644 --- a/sysdeps/unix/sysv/linux/loongarch/cpu-features.c +++ b/sysdeps/unix/sysv/linux/loongarch/cpu-features.c @@ -27,4 +27,10 @@ init_cpu_features (struct cpu_features *cpu_features) GLRO(dl_larch_cpu_features).hwcap = GLRO(dl_hwcap); TUNABLE_GET (glibc, cpu, hwcaps, tunable_val_t *, TUNABLE_CALLBACK (set_hwcaps)); + +#ifdef __LP64__ + /* Enable THP-based load segment alignment by default on LoongArch64. */ + if (!TUNABLE_IS_INITIALIZED (glibc, elf, thp)) + TUNABLE_SET (glibc, elf, thp, 1); +#endif } 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 00000000000..c51ee4ac47e --- /dev/null +++ b/sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h @@ -0,0 +1,22 @@ +/* _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 + . */ + +#define DL_MAP_DEFAULT_THP_PAGESIZE (32 * 1024 * 1024) + +#include_next