{"id":2224207,"url":"http://patchwork.ozlabs.org/api/1.2/patches/2224207/?format=json","web_url":"http://patchwork.ozlabs.org/project/glibc/patch/20260417035556.1778246-1-wangrui@loongson.cn/","project":{"id":41,"url":"http://patchwork.ozlabs.org/api/1.2/projects/41/?format=json","name":"GNU C Library","link_name":"glibc","list_id":"libc-alpha.sourceware.org","list_email":"libc-alpha@sourceware.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260417035556.1778246-1-wangrui@loongson.cn>","list_archive_url":null,"date":"2026-04-17T03:55:56","name":"[v10] elf: Add test for THP alignment of large load segments","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"52d1ce65d9af10f92a7aecb8958332249f0113e8","submitter":{"id":85150,"url":"http://patchwork.ozlabs.org/api/1.2/people/85150/?format=json","name":"WANG Rui","email":"wangrui@loongson.cn"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/glibc/patch/20260417035556.1778246-1-wangrui@loongson.cn/mbox/","series":[{"id":500231,"url":"http://patchwork.ozlabs.org/api/1.2/series/500231/?format=json","web_url":"http://patchwork.ozlabs.org/project/glibc/list/?series=500231","date":"2026-04-17T03:55:56","name":"[v10] elf: Add test for THP alignment of large load segments","version":10,"mbox":"http://patchwork.ozlabs.org/series/500231/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2224207/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2224207/checks/","tags":{},"related":[],"headers":{"Return-Path":"<libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org>","X-Original-To":["incoming@patchwork.ozlabs.org","libc-alpha@sourceware.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","libc-alpha@sourceware.org"],"Authentication-Results":["legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org\n (client-ip=38.145.34.32; helo=vm01.sourceware.org;\n envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n dmarc=none (p=none dis=none) header.from=loongson.cn","sourceware.org; spf=pass smtp.mailfrom=loongson.cn","server2.sourceware.org;\n arc=none smtp.remote-ip=114.242.206.163"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fxh0H3g3Vz1yDF\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 17 Apr 2026 13:57:03 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 220734BAD176\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 17 Apr 2026 03:57:01 +0000 (GMT)","from mail.loongson.cn (mail.loongson.cn [114.242.206.163])\n by sourceware.org (Postfix) with ESMTP id B8C1E4BAD147\n for <libc-alpha@sourceware.org>; Fri, 17 Apr 2026 03:56:37 +0000 (GMT)","from loongson.cn (unknown [223.64.120.66])\n by gateway (Coremail) with SMTP id _____8BxJelxr+FpzFoBAA--.14S3;\n Fri, 17 Apr 2026 11:56:33 +0800 (CST)","from localhost (unknown [223.64.120.66])\n by front1 (Coremail) with SMTP id qMiowJBx58Bpr+FpnhJvAA--.44331S2;\n Fri, 17 Apr 2026 11:56:28 +0800 (CST)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 220734BAD176","OpenDKIM Filter v2.11.0 sourceware.org B8C1E4BAD147"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org B8C1E4BAD147","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org B8C1E4BAD147","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776398199; cv=none;\n b=nKRhSTXmylxhCYxoeglhTlSM6PxcAPcKLDfqmJ3E8wMD21S7DFu88sglzUSbajFkwqRa9+sWry0Vxwj9dyVj3dZ3Yw2BJXJQKXGwtoHbHgFTSt8DXpXXaltTIldTMAoU5vvSls9EoB15YjLG94K6KkLBjsvxFBIgs9uCobP5Vwg=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776398199; c=relaxed/simple;\n bh=bimItMJKjjuXcx9HZg5ah6NGQwzWOkpZdGL/DBV6I+o=;\n h=From:To:Subject:Date:Message-ID:MIME-Version;\n b=DhbaYsMlsRTgLcTXgrI9jZOSUtPM05F/CmaqzCyWznoHv4S1xOYYTI9V/9cT5BabudQpE5cdl9EHHxIbybPGZATlIPMxuMFq0C7dqTO6K0IZNAkxBIzEl6IlFFlPHQcBOg6VEshJQwV2BP92fhC8Hcl3sdXJLLbzntzOKzzOxdg=","ARC-Authentication-Results":"i=1; server2.sourceware.org","From":"WANG Rui <wangrui@loongson.cn>","To":"libc-alpha@sourceware.org","Cc":"Adhemerval Zanella <adhemerval.zanella@linaro.org>,\n Dev Jain <dev.jain@arm.com>, Florian Weimer <fweimer@redhat.com>,\n Wilco Dijkstra <Wilco.Dijkstra@arm.com>, Xi Ruoyao <xry111@xry111.site>,\n WANG Xuerui <git@xen0n.name>, caiyinyu <caiyinyu@loongson.cn>,\n mengqinggang <mengqinggang@loongson.cn>,\n Huacai Chen <chenhuacai@kernel.org>, hjl.tools@gmail.com,\n WANG Rui <wangrui@loongson.cn>","Subject":"[PATCH v10] elf: Add test for THP alignment of large load segments","Date":"Fri, 17 Apr 2026 11:55:56 +0800","Message-ID":"<20260417035556.1778246-1-wangrui@loongson.cn>","X-Mailer":"git-send-email 2.53.0","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-CM-TRANSID":"qMiowJBx58Bpr+FpnhJvAA--.44331S2","X-CM-SenderInfo":"pzdqw2txl6z05rqj20fqof0/","X-Coremail-Antispam":"1Uk129KBj93XoWxKF15Wr4DJry5Ar17Zr13Awc_yoW3CryfpF\n s8uF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87Iv6xkF7I0E\n 14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07jOiSdUUUUU=","X-BeenThere":"libc-alpha@sourceware.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Libc-alpha mailing list <libc-alpha.sourceware.org>","List-Unsubscribe":"<https://sourceware.org/mailman/options/libc-alpha>,\n <mailto:libc-alpha-request@sourceware.org?subject=unsubscribe>","List-Archive":"<https://sourceware.org/pipermail/libc-alpha/>","List-Post":"<mailto:libc-alpha@sourceware.org>","List-Help":"<mailto:libc-alpha-request@sourceware.org?subject=help>","List-Subscribe":"<https://sourceware.org/mailman/listinfo/libc-alpha>,\n <mailto:libc-alpha-request@sourceware.org?subject=subscribe>","Errors-To":"libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org"},"content":"Add a new test to verify that large executable PT_LOAD segments are\nmapped at addresses aligned to the THP size when the glibc tunable\nglibc.elf.thp=1 is enabled and the system is configured to use THP\nin \"always\" mode.\n\nThe test loads a shared object with a sufficiently large executable\nsegment via dlopen and inspects /proc/self/maps to check that the\nmapping address is aligned to the THP page size reported by the kernel.\n\nThe test is skipped if the THP size cannot be determined or if THP is\nnot enabled in \"always\" mode.\n\nSigned-off-by: WANG Rui <wangrui@loongson.cn>\n---\nChanges since [v9]:\n  * Use FAIL_UNSUPPORTED instead of printf.\n  * Skip tests when THP size exceeds MAX_THP_PAGESIZE.\n  * Add dependency of tst-thp-align on tst-thp-size-mod.\n  * Rebase onto current master.\n\n[v9]: https://sourceware.org/pipermail/libc-alpha/2026-April/176598.html\n---\n sysdeps/unix/sysv/linux/Makefile           |   5 +\n sysdeps/unix/sysv/linux/tst-thp-align.c    | 155 +++++++++++++++++++++\n sysdeps/unix/sysv/linux/tst-thp-size-mod.S |  22 +++\n 3 files changed, 182 insertions(+)\n create mode 100644 sysdeps/unix/sysv/linux/tst-thp-align.c\n create mode 100644 sysdeps/unix/sysv/linux/tst-thp-size-mod.S","diff":"diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile\nindex b06afbdeaab..0d47e9af57e 100644\n--- a/sysdeps/unix/sysv/linux/Makefile\n+++ b/sysdeps/unix/sysv/linux/Makefile\n@@ -698,6 +698,7 @@ $(objpfx)pldd: $(objpfx)xmalloc.o\n tests += \\\n   tst-rseq-tls-range \\\n   tst-rseq-tls-range-4096 \\\n+  tst-thp-align \\\n # tests\n tests-static += \\\n   tst-rseq-tls-range-4096-static \\\n@@ -705,17 +706,21 @@ tests-static += \\\n # tests-static\n modules-names += \\\n   tst-rseq-tls-range-mod \\\n+  tst-thp-size-mod \\\n # modules-names\n CFLAGS-tst-rseq-tls-range.c += -DMAIN_TLS_ALIGN=4\n CFLAGS-tst-rseq-tls-range-4096.c += -DMAIN_TLS_ALIGN=4096\n CFLAGS-tst-rseq-tls-range-static.c += -DMAIN_TLS_ALIGN=4\n CFLAGS-tst-rseq-tls-range-4096-static.c += -DMAIN_TLS_ALIGN=4096\n+LDFLAGS-tst-thp-size-mod.so += -Wl,-z,noseparate-code\n $(objpfx)tst-rseq-tls-range.out: $(objpfx)tst-rseq-tls-range-mod.so\n $(objpfx)tst-rseq-tls-range-4096.out: $(objpfx)tst-rseq-tls-range-mod.so\n $(objpfx)tst-rseq-tls-range-static.out: $(objpfx)tst-rseq-tls-range-mod.so\n $(objpfx)tst-rseq-tls-range-4096-static.out: $(objpfx)tst-rseq-tls-range-mod.so\n+$(objpfx)tst-thp-align.out: $(objpfx)tst-thp-size-mod.so\n tst-rseq-tls-range-static-ENV = LD_LIBRARY_PATH=$(objpfx):$(common-objpfx)\n tst-rseq-tls-range-4096-static-ENV = LD_LIBRARY_PATH=$(objpfx):$(common-objpfx)\n+tst-thp-align-ENV = GLIBC_TUNABLES=glibc.elf.thp=1\n \n test-internal-extras += tst-nolink-libc\n ifeq ($(run-built-tests),yes)\ndiff --git a/sysdeps/unix/sysv/linux/tst-thp-align.c b/sysdeps/unix/sysv/linux/tst-thp-align.c\nnew file mode 100644\nindex 00000000000..0b3f18e000f\n--- /dev/null\n+++ b/sysdeps/unix/sysv/linux/tst-thp-align.c\n@@ -0,0 +1,155 @@\n+/* Test the THP compatible alignment of PT_LOAD segments.\n+\n+   Copyright (C) 2026 Free Software Foundation, Inc.\n+\n+   The GNU C Library is free software; you can redistribute it and/or\n+   modify it under the terms of the GNU Lesser General Public\n+   License as published by the Free Software Foundation; either\n+   version 2.1 of the License, or (at your option) any later version.\n+\n+   The GNU C Library is distributed in the hope that it will be useful,\n+   but WITHOUT ANY WARRANTY; without even the implied warranty of\n+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+   Lesser General Public License for more details.\n+\n+   You should have received a copy of the GNU Lesser General Public\n+   License along with the GNU C Library; if not, see\n+   <https://www.gnu.org/licenses/>.  */\n+\n+#include <fcntl.h>\n+#include <stdint.h>\n+#include <stdlib.h>\n+#include <string.h>\n+#include <intprops.h>\n+#include <inttypes.h>\n+#include <support/check.h>\n+#include <support/xdlfcn.h>\n+#include <support/xstdio.h>\n+#include <support/xunistd.h>\n+\n+#define THP_SIZE_MOD_NAME \"tst-thp-size-mod.so\"\n+#define MAX_THP_PAGESIZE (32 * 1024 * 1024)\n+\n+enum thp_mode_t\n+{\n+  thp_mode_always,\n+  thp_mode_madvise,\n+  thp_mode_never,\n+  thp_mode_not_supported\n+};\n+\n+static unsigned long int\n+get_thp_size (void)\n+{\n+  int fd = open (\"/sys/kernel/mm/transparent_hugepage/hpage_pmd_size\",\n+                 O_RDONLY, 0);\n+  if (fd == -1)\n+    return 0;\n+\n+  char str[INT_BUFSIZE_BOUND (unsigned long int)];\n+  ssize_t s = read (fd, str, sizeof (str));\n+  close (fd);\n+  if (s < 0)\n+    return 0;\n+\n+  unsigned long int r = 0;\n+  for (ssize_t i = 0; i < s; i++)\n+    {\n+      if (str[i] == '\\n')\n+    break;\n+      r *= 10;\n+      r += str[i] - '0';\n+    }\n+  return r;\n+}\n+\n+static enum thp_mode_t\n+get_thp_mode (void)\n+{\n+  int fd = open (\"/sys/kernel/mm/transparent_hugepage/enabled\", O_RDONLY, 0);\n+  if (fd == -1)\n+    return thp_mode_not_supported;\n+\n+  static const char mode_always[]  = \"[always] madvise never\\n\";\n+  static const char mode_madvise[] = \"always [madvise] never\\n\";\n+  static const char mode_never[]   = \"always madvise [never]\\n\";\n+\n+  char str[sizeof(mode_always)];\n+  ssize_t s = read (fd, str, sizeof (str));\n+  if (s >= sizeof str || s < 0)\n+    return thp_mode_not_supported;\n+  str[s] = '\\0';\n+  close (fd);\n+\n+  if (s == sizeof (mode_always) - 1)\n+    {\n+      if (strcmp (str, mode_always) == 0)\n+    return thp_mode_always;\n+      else if (strcmp (str, mode_madvise) == 0)\n+    return thp_mode_madvise;\n+      else if (strcmp (str, mode_never) == 0)\n+    return thp_mode_never;\n+    }\n+  return thp_mode_not_supported;\n+}\n+\n+static void\n+check_align (void)\n+{\n+  unsigned long int thp_size = get_thp_size ();\n+  enum thp_mode_t thp_mode = get_thp_mode ();\n+\n+  if (thp_size == 0)\n+    {\n+      FAIL_UNSUPPORTED (\"unable to get THP size.\\n\");\n+      return;\n+    }\n+\n+  if (thp_size > MAX_THP_PAGESIZE)\n+    {\n+      FAIL_UNSUPPORTED (\"THP size exceeds MAX_THP_PAGESIZE.\\n\");\n+      return;\n+    }\n+\n+  if (thp_mode != thp_mode_always)\n+    {\n+      FAIL_UNSUPPORTED (\"THP mode is not always.\\n\");\n+      return;\n+    }\n+\n+  FILE *f = xfopen (\"/proc/self/maps\", \"r\");\n+  char *line = NULL;\n+  size_t len;\n+\n+  while (xgetline (&line, &len, f))\n+    {\n+      uintptr_t from, to;\n+      char *prot = NULL, *path = NULL;\n+      int r = sscanf (line, \"%\" SCNxPTR \"-%\" SCNxPTR \"%ms%*s%*s%*s%ms\",\n+                      &from, &to, &prot, &path);\n+\n+      TEST_VERIFY (r == 3 || r == 4);\n+\n+      if (strstr (prot, \"x\") && strstr (path, THP_SIZE_MOD_NAME))\n+        TEST_COMPARE (from % thp_size, 0);\n+\n+      free (path);\n+    }\n+\n+  free (line);\n+  xfclose (f);\n+}\n+\n+static int\n+do_test (void)\n+{\n+  void *dl;\n+\n+  dl = xdlopen (THP_SIZE_MOD_NAME, RTLD_NOW);\n+  check_align ();\n+  xdlclose (dl);\n+\n+  return 0;\n+}\n+\n+#include <support/test-driver.c>\ndiff --git a/sysdeps/unix/sysv/linux/tst-thp-size-mod.S b/sysdeps/unix/sysv/linux/tst-thp-size-mod.S\nnew file mode 100644\nindex 00000000000..3084c2d0e2c\n--- /dev/null\n+++ b/sysdeps/unix/sysv/linux/tst-thp-size-mod.S\n@@ -0,0 +1,22 @@\n+/* A module that provides THP code size for testing.\n+\n+   Copyright (C) 2026 Free Software Foundation, Inc.\n+   This file is part of the GNU C Library.\n+\n+   The GNU C Library is free software; you can redistribute it and/or\n+   modify it under the terms of the GNU Lesser General Public\n+   License as published by the Free Software Foundation; either\n+   version 2.1 of the License, or (at your option) any later version.\n+\n+   The GNU C Library is distributed in the hope that it will be useful,\n+   but WITHOUT ANY WARRANTY; without even the implied warranty of\n+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+   Lesser General Public License for more details.\n+\n+   You should have received a copy of the GNU Lesser General Public\n+   License along with the GNU C Library; if not, see\n+   <https://www.gnu.org/licenses/>.  */\n+\n+    .globl thp_size\n+thp_size:\n+    .space 32 * 1024 * 1024\n","prefixes":["v10"]}