From patchwork Wed Feb 25 16:19:16 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 130630 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 9B9574BAD158 for ; Wed, 25 Feb 2026 16:20:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B9574BAD158 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 AC31B4BA23C2 for ; Wed, 25 Feb 2026 16:19:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC31B4BA23C2 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 AC31B4BA23C2 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=1772036400; cv=none; b=XO5SRZL3e81PxcB8LWA1FPLty1fuXII578GkNdYuPJjqoPd3ycjs1W9Te8AuTCWC9r8F7yoxzAbA6EBTclfPqKGZ/l8F/Z0eJoffxJZwsQ+MDwH+jBJyGeHFQlcJN0N5Hese4TFZwW/aetRmi/m3cUYcTT7Q/60MDjHehYqix4w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1772036400; c=relaxed/simple; bh=W9NtmSaM/yCr5jXGUhpyRXzj/9fFRu3r0nZP+cRh+/k=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=dpkNjYQNYZOiE68KB2DpRqB0sKXQxrzc+ovT6SOQa3WW7ozDKrR/gtqNyvouKNK2C5nva+YozJKDFBlihjp6NuzHOU53wAF2j8bKKz5DUVVjXRols94GRneB7VXgvKUzAGwAT+DRECWFkPkPCM70rs2BqcOflxctAcKQ6McinSQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC31B4BA23C2 Received: from loongson.cn (unknown [223.64.120.216]) by gateway (Coremail) with SMTP id _____8CxJMEtIZ9plSEVAA--.17464S3; Thu, 26 Feb 2026 00:19:57 +0800 (CST) Received: from localhost (unknown [223.64.120.216]) by front1 (Coremail) with SMTP id qMiowJAx18AiIZ9ptehKAA--.26265S3; Thu, 26 Feb 2026 00:19:55 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , WANG Rui Subject: [PATCH 1/3] elf: Introduce _dl_map_segment_align hook for segment alignment tuning Date: Thu, 26 Feb 2026 00:19:16 +0800 Message-ID: <20260225161918.1773350-2-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260225161918.1773350-1-wangrui@loongson.cn> References: <20260225161918.1773350-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJAx18AiIZ9ptehKAA--.26265S3 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCFyktFW8tF4Dur1DXFW8GrX_yoW5CrW3pF 92kF4rKF1rJF17CFZ7t3W2yr15X3WrGF4YyrnxuwnrZrs7GFy8XrZIv3W5WryrGrW8CF4a vFZFqF98CF4rA3gCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUvKb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVWUJVW8JwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ Jr0_Gr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2 x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q6rW5 McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr4 1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_Jrv_ JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17 CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0 I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I 8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU 0xZFpf9x07jz2NtUUUUU= X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED 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. * elf/dl-load.c (_dl_map_object_from_fd): Call _dl_map_segment_align to allow sysdeps to adjust maximum segment alignment. * sysdeps/generic/dl-map-segment-align.h: New file with generic no-op implementation. 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 7355eef8e7..f3d943e99c 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 0000000000..5bc2f21288 --- /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 __always_inline ElfW(Addr) +_dl_map_segment_align (const struct loadcmd *c, ElfW(Addr) p_align_max) +{ + return p_align_max; +} From patchwork Wed Feb 25 16:19: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: 130631 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 EBEBE4BAD15F for ; Wed, 25 Feb 2026 16:21:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EBEBE4BAD15F 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 04A744B9DB4A for ; Wed, 25 Feb 2026 16:20:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04A744B9DB4A 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 04A744B9DB4A 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=1772036401; cv=none; b=FG5IKlSw8/2Ry7Vl4dnCXmOlU6qKhbHEdzAnTF0hSZPFl3fSZJB3LZQhOykN0H3WzAVan70m/A5w/7mYK/zJQt/QlIdP3aeeuVAlDpiWwiJf6/Ip/OLYALFsjblToV97S+1zMZDRBoYHymS41/4OXVXcMwSypd7DtIHouN6OinY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1772036401; c=relaxed/simple; bh=1NkSY6zsqMA8XLzGr79y3yvlWIM4i68T2OgGm5FRAaU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=wHbqzU4SGwac1dp9EltZOAI22dXyVgoKfOi8p6JEZaJN++XXxY3Zf8Qi7Nk1uXMI+keqCb3f1+LZnzz5cvI9mf9HuQXOVXlMj0YosjfOhEYGtalHI6KIo6Q/g7ICoC1aGcMVcUUcPKVBsInGEK5WR+V6goVeMUNEG7Lja7AOj1Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 04A744B9DB4A Received: from loongson.cn (unknown [223.64.120.216]) by gateway (Coremail) with SMTP id _____8BxWcIuIZ9poCEVAA--.64867S3; Thu, 26 Feb 2026 00:19:58 +0800 (CST) Received: from localhost (unknown [223.64.120.216]) by front1 (Coremail) with SMTP id qMiowJAx18AiIZ9ptehKAA--.26265S4; Thu, 26 Feb 2026 00:19:57 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , WANG Rui Subject: [PATCH 2/3] elf: Align large load segments to PMD huge page size for THP Date: Thu, 26 Feb 2026 00:19:17 +0800 Message-ID: <20260225161918.1773350-3-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260225161918.1773350-1-wangrui@loongson.cn> References: <20260225161918.1773350-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJAx18AiIZ9ptehKAA--.26265S4 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3Gry7tr47uF1fCw4xJFWxZrc_yoWxCF48pF 4akF45GFWrXFy8CFySg3WUAr1Yqa1rGr98A343uw13AwnrGFyx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ATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED 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 on 64-bit systems based on the system THP policy. The optimization is enabled only when the glibc tunable `glibc.malloc.hugetlb=1` is set and THP is configured to be used unconditionally. 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. 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 writable segments, matching typical THP usage. * sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.c (_dl_map_segment_align): Determine segment alignment based on THP policy and default huge page size. * sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.h: New header. * sysdeps/unix/sysv/linux/Makefile (dl-routines): Add dl-map-segment-align. * sysdeps/unix/sysv/linux/malloc-hugepages.c: Exclude THP helpers from rtld. Signed-off-by: WANG Rui --- sysdeps/unix/sysv/linux/Makefile | 1 + sysdeps/unix/sysv/linux/malloc-hugepages.c | 4 ++ .../linux/wordsize-64/dl-map-segment-align.c | 56 +++++++++++++++++++ .../linux/wordsize-64/dl-map-segment-align.h | 23 ++++++++ 4 files changed, 84 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.c create mode 100644 sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.h diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index c6bd97abf1..9879fec9a9 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/malloc-hugepages.c b/sysdeps/unix/sysv/linux/malloc-hugepages.c index 77b49374e6..28930c0e53 100644 --- a/sysdeps/unix/sysv/linux/malloc-hugepages.c +++ b/sysdeps/unix/sysv/linux/malloc-hugepages.c @@ -81,6 +81,8 @@ __malloc_thp_mode (void) return malloc_thp_mode_not_supported; } +#if !IS_IN (rtld) + static size_t malloc_default_hugepage_size (void) { @@ -205,3 +207,5 @@ __malloc_hugepage_config (size_t requested, size_t *pagesize, int *flags) __close_nocancel (dirfd); } + +#endif /* !IS_IN(rtld) */ diff --git a/sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.c b/sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.c new file mode 100644 index 0000000000..3c57d593bd --- /dev/null +++ b/sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.c @@ -0,0 +1,56 @@ +/* _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 + +#define MAX_THP_PAGESIZE (32 * 1024 * 1024) + +ElfW (Addr) +_dl_map_segment_align (const struct loadcmd *c, ElfW (Addr) p_align_max) +{ + static enum malloc_thp_mode_t thp_mode = malloc_thp_mode_not_supported; + static unsigned long int thp_pagesize; + +#ifndef DL_MAP_SEGMENT_ALIGN_THP_DEFAULT + if (TUNABLE_GET (glibc, malloc, hugetlb, size_t, NULL) != 1) + return p_align_max; +#endif + + if (__glibc_unlikely (thp_mode == malloc_thp_mode_not_supported + || thp_pagesize == 0)) + { + thp_mode = __malloc_thp_mode (); + thp_pagesize = __malloc_default_thp_pagesize (); + } + + /* 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 == malloc_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/wordsize-64/dl-map-segment-align.h b/sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.h new file mode 100644 index 0000000000..2893408133 --- /dev/null +++ b/sysdeps/unix/sysv/linux/wordsize-64/dl-map-segment-align.h @@ -0,0 +1,23 @@ +/* _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 + +ElfW (Addr) + _dl_map_segment_align (const struct loadcmd *c, ElfW (Addr) p_align_max); From patchwork Wed Feb 25 16:19:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: WANG Rui X-Patchwork-Id: 130632 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 289454BAD163 for ; Wed, 25 Feb 2026 16:21:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 289454BAD163 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 690814B9DB54 for ; Wed, 25 Feb 2026 16:20:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 690814B9DB54 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 690814B9DB54 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=1772036403; cv=none; b=sY27luVrRWc7L0oM4ls6Hv6JpAbRQE8SS9otBpBbM2RlswQkCI2DpGvEqm8q8NHMvjnWC4rBvylx4+B29SsR3+HxBq5ebZ7fdPNcFATObA3l8fQL/KkZFfz9OmS33/AWtAyVDOL0nQg5BPYyLuU/UtwoZ/AAmYl5fSt+9jHGImU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1772036403; c=relaxed/simple; bh=N0RJjmPtB9Y45tHT5PNpqeamPKFHJk2rnFGuAkUFCmU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=m80cLWi/4xmrAzQXFEhu4aUS/9hzAZsbZwy/mJ1zp8dMlVrvygc1S/aOACe1otmKIrlGmPzYEBA2wrOdcXUUCY+VM95k2fofGKxial2QYhfoQi6FtqAUYBnR6zZvSD08rgaF3IVEh8xh0GZGH4mbwP4aZBVzWDflJsj93ThBav8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 690814B9DB54 Received: from loongson.cn (unknown [223.64.120.216]) by gateway (Coremail) with SMTP id _____8CxIMAvIZ9ppCEVAA--.5515S3; Thu, 26 Feb 2026 00:19:59 +0800 (CST) Received: from localhost (unknown [223.64.120.216]) by front1 (Coremail) with SMTP id qMiowJAx18AiIZ9ptehKAA--.26265S5; Thu, 26 Feb 2026 00:19:57 +0800 (CST) From: WANG Rui To: libc-alpha@sourceware.org Cc: Adhemerval Zanella , Florian Weimer , Wilco Dijkstra , Xi Ruoyao , WANG Xuerui , caiyinyu , mengqinggang , WANG Rui Subject: [PATCH 3/3] loongarch: Enable THP-aligned load segments by default and define THP page size Date: Thu, 26 Feb 2026 00:19:18 +0800 Message-ID: <20260225161918.1773350-4-wangrui@loongson.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260225161918.1773350-1-wangrui@loongson.cn> References: <20260225161918.1773350-1-wangrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowJAx18AiIZ9ptehKAA--.26265S5 X-CM-SenderInfo: pzdqw2txl6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw1UWr1Uury3ur4xGF17urX_yoWrurWDpr ZxuFn8GF4rWr17CrWS9a4UZF1rJFn5GF90k3sI93yUZryDJryxXFZFkas3WFy8A3yxCF4j vFn2q3WDuFs5AagCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWS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XREP, URIBL_BLOCKED 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 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 --- 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 + . */ + +/* Enable THP-based load segment alignment by default. */ +#define DL_MAP_SEGMENT_ALIGN_THP_DEFAULT 1 + +#include_next 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 . */ + +#define DEFAULT_THP_PAGESIZE (32 * 1024 * 1024) + +#include_next