From patchwork Thu May 14 21:58:15 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 134994 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E42C04C900EB for ; Thu, 14 May 2026 21:59:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E42C04C900EB Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=cIY/Wirs X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id A2F104B1A2CC for ; Thu, 14 May 2026 21:58:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A2F104B1A2CC Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A2F104B1A2CC Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=2607:f8b0:4864:20::532 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1778795934; cv=pass; b=S5c+h1cXXEmS2IVnMELt7VDgjXLyl+INaYPLx7DRJXm5GFyimkf+B6qBsQh2z47AYsOglGPPkVMUiopPOizpwgzBKaXnKOPy+KaefUNLlLu8HXLA7iBu5LkvNcJT21JUbZwjciwiHnsCS3RqSGbTrEhtZbJ0pcOpu+U3NMk6qAM= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1778795934; c=relaxed/simple; bh=pqIJvM1/LIkMSyXDQLVtshScLLUjqKlzXh1cYqzjai0=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=b+Ub9UAllqlsTkiQx2pdEzKIPvtWi/mbBhTAQxq0M6HUzfALA3uQbWJmiMf9b7bpfIkqUBqd5RwWAVsWGjV9ys5h2IWYbxJ5B2tJs81+9ful4IN7zl4M2TvyX5/Bn6mItUHSnYMVhB++08TKfN7bIZyZBTH5fQssR/BoJV3wSeo= ARC-Authentication-Results: i=2; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=cIY/Wirs DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A2F104B1A2CC Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-c8028fa6039so5795136a12.2 for ; Thu, 14 May 2026 14:58:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778795933; cv=none; d=google.com; s=arc-20240605; b=VgIK7nzu3Rg0KjV7G8FwQ+uAH01nY0GDcJzQkPHaOXFIu5k1bYnVGEGvWLV5Mt/L5n NlMM2qDMmqh/sEHUO075f24ekdbenP4nOfzoE4dplxNCQutLsGMU1beUVgs3tdprfZCx GVl+vY+tqYI7b6tnC563rGF+YLOy9nlwflOsx8iygm72qPsiiwM006YrEOhA6H6+4ViO ZfWvCa8n6D/wUWtXAaZyMSXE8jUYQEBbqEpyzYNoTUD7MY6UtGB9ttLB0suH3ee79VP1 gRCdpB6lVqJDeEwE/Zf79JpVlp+5cRTr71fPstW7XrMbnOwPf24qwGgd4SAnYJVtmI4x lCag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=qSpJzAYSw6W8YTYvkq1Nv0KlO9NpIySHOrmSY0tpWOQ=; fh=Ux5alVN9Zjpkf+0wS1xt05Q5nf7UKXt2HhCndghiJPs=; b=Sa+pxe+DKOCfyOIMHTHja5Ptg3gxjBjXCdR29sZzCxtOJJk/wW7kMPEqW/wlyN3v4Z HNSiclPov9p52/6fzJxsb7jb47ReoLz//zqGA8lmw+FBFP1U2ewAe8F56uMwahgLXX5Q iWt24FSEh1MaZNO/0Lu6c+1D69OTg26+ReYlATzRQdMaMNu1iRX2fjy7wbZi4xSw0fbS 3jRB6KrpsVFPTeRXE+Tzb7b2BlqJ/6D4Ru5yb8D0s6pa5NaIE45xprpihFVilsooehGt /cDqd9JXT7k4glcMpqxt3Nmi9d/EDI5tkD5DEfh+Zcmd+NgK/DwFkL2Bub5y8zVOISNb buPg==; darn=sourceware.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778795933; x=1779400733; darn=sourceware.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=qSpJzAYSw6W8YTYvkq1Nv0KlO9NpIySHOrmSY0tpWOQ=; b=cIY/WirsV9z/ZNUc9UHFBofD/toSZkw7zpuWLfUNh4ecpM8NFzgK5ufbIrSxrG+n4d t/EdG1IfIjl/wI7Si5xmvtiQgfeHWK2ejzXqVLhMgvyOlMQjfjqwlY0nCjOHqM3DDtJ1 6CzY5Inw12+7TL35rQ8upla2LEmz4KvYYnphZK6DcUz7UnK4XycYnmfAqKFvIF42Ixxn Ak5LEj1Bjj0y3isjAeBde11siwwf5NxmyXc0h5mH132MhlC3Wp/SBaTv04xowjlBYV4J x05xf11ZY9YYO99bgsOl3cc0c0Ue+9+lp6+qMvEa9Nw25A9d/FCzoBoQVQE9yBgchB8m T8+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778795933; x=1779400733; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qSpJzAYSw6W8YTYvkq1Nv0KlO9NpIySHOrmSY0tpWOQ=; b=Mz9+iw/imlrnj5oZzieC+YylvAHpoosqp0w6+X3hZqLE9xyYu4F4U00OSf1jv/24gp 1RimA+tvD8vWYzGHwOtsdA3yVbmZorQ7nQp68WEAkohgMRkuG/UViNEY7aH73e3YgKSu oAIm8IohEC3/x1bAIxNsMiFcfQbhVComvyu5sr7SZS8ukgNXeBztAtEXxKfc1EChGYXA aXKotqoWwSYSDpP49rgXNL1QOQOAZIW39sRgKNMIZwlsD4KVn8dwWbiwdhvvWcwfYeHF LOAsBBIAupyew649ikQ8PSt/g9qrzpA22b/c6Y1E3ss2Dg14PHTEwyK18/nu4JLK2+90 uF7g== X-Gm-Message-State: AOJu0YzcaV9rYV/okoXIetXRVSdo+00dukJAeJJs4YxT3hzGy4FyZKMm n4woqFXtPMwZDdGYwrSYrZr9tEwZW7PI7c4hyPvvJpsieEx8siSlxQwm33MKqAZd+A6XRiRtTNr B1S8dfJVqCIs58sAW5y8QQ27nefdHXXg= X-Gm-Gg: Acq92OFPmoKVi7n843/tWPFTQBdUNO5WL9bhcRKmkAtXHIOTz6/jCr4bk1GW679RI/E J2IyEuiTPzevkvJP8xUXB3Cdw0S2sEG1nwaxhtSG/MiBGi/XvuHFEwejOpBBEq3tgCY8BKmiDCR YEzsD//0XiMUje/c4gts4EISHvxeJKAal4tN+fz553MTfoBHs3pxZ9pZQVcaG7yy8KwyeFiOFXF 6olFza1vHZCxqcTMk4/PMkHwDcCzVr3pkuBGbNRa3bQ5c9dV5HPhaqJIhWunct7h64wKi06TxqI nlETOSeXkfepUvCm X-Received: by 2002:a05:6a21:3386:b0:39f:794f:94d8 with SMTP id adf61e73a8af0-3b22ec9aecemr1021298637.29.1778795932375; Thu, 14 May 2026 14:58:52 -0700 (PDT) MIME-Version: 1.0 References: <8bbd5f20-6002-4056-b620-7091e490aa52@linaro.org> In-Reply-To: <8bbd5f20-6002-4056-b620-7091e490aa52@linaro.org> From: "H.J. Lu" Date: Fri, 15 May 2026 05:58:15 +0800 X-Gm-Features: AVHnY4IfQ4w-PKnxZIiZRroTHopzO7-VgrHjxlTmQ1Qeshef1JzTIxoIlU5A0sM Message-ID: Subject: [PATCH v8] elf: Support THP segment load with madvise enabled THP To: Adhemerval Zanella Netto Cc: GNU C Library , DJ Delorie X-Spam-Status: No, score=-3010.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no 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 Fri, May 15, 2026 at 1:31 AM Adhemerval Zanella Netto wrote: > On 13/05/26 22:31, H.J. Lu wrote: > > Changes in v7: > > > > Rebased against > > > > commit 389ecf52bc75e6378b2881415b03f3c00a3ef891 > > Author: Shamil Abdulaev > > Date: Wed May 13 07:52:40 2026 +0200 > > > > libio: Fix race in _IO_new_file_init_internal initialization order > > [BZ #33785] > > > > > > -- > > H.J. > > --> From 0172a61ad9a870882dcec0e1a82e646ed23c5614 Mon Sep 17 00:00:00 2001 > > From: "H.J. Lu" > > Date: Mon, 13 Apr 2026 08:23:05 +0800 > > Subject: [PATCH v7] elf: Support THP segment load with madvise enabled THP > > > > The current THP segment load approach works only when THP is enabled > > with always in the kernel. If THP is enabled with madvise in the > > kernel, to enable THP segment load in an application, madvise should > > be called with MADV_HUGEPAGE on all THP eligible PT_LOAD segments: > > > > 1. Define DL_MAP_DEFAULT_THP_PAGESIZE in hugepages.h and default it to 0. > > 2. Update _dl_map_segment_align to support madvise THP mode. This fixes > > BZ #34079. > > 3. Call _dl_executable_postprocess in rtld_setup_main_map for dynamic > > executables and in LIBC_START_MAIN for static executables, which calls > > madvise with MADV_HUGEPAGE on all THP eligible PT_LOAD segments in > > executable. This fixes BZ #34080 for both dynamic and static executables. > > _dl_executable_postprocess is invoked on main_map, never on _dl_rtld_map. > The loader will most likely never be elective for Transparent Hugepage (THP): We can't call madvise on ld.so: madvise(0x7fcbc294b000, 33564605, MADV_HUGEPAGE) = -1 ENOMEM (Cannot allocate memory) > aarch64-linux-gnu $ readelf -lW elf/ld.so | awk '/^[[:space:]]*LOAD/ && /E/ {print $6}' | \ > while read h; do printf "RX MemSiz: %d\n" "$h"; done > RX MemSiz: 164904 > x86_64-linux-gnu $ readelf -lW elf/ld.so | awk '/^[[:space:]]*LOAD/ && /E/ {print $6}' | \ > while read h; do printf "RX MemSiz: %d\n" "$h"; done > RX MemSiz: 164497 > > but I think for completeness we should also check it. > > > 4. Call _dl_postprocess_loadcmd_extra in _dl_postprocess_loadcmd, which > > calls madvise with MADV_HUGEPAGE on all THP eligible PT_LOAD segments > > when loading an object after they have been mapped in. This fixes > > BZ #34080 for shared objects. > > 5. Set the maximum page alignment on THP tests to THP page size as the > > default maximum page alignment may be smaller than THP page size. > > 6. Add tests to verify that large executable PT_LOAD segments in > > executables are mapped at addresses aligned to THP page size when the > > kernel is configured to use THP in "always" mode or "madvise" mode by > > inspecting /proc/self/maps to check that the mapping address is aligned > > to THP page size reported by the kernel. Also verify that madvise is > > called with MADV_HUGEPAGE when the glibc tunable glibc.elf.thp=1 is used > > and madvise isn't called with MADV_HUGEPAGE when the glibc tunable > > glibc.elf.thp=0 is used. > > There is an inconsistency where madvise will be applied for shared libraries > even when the tunable is not set: > > $ uname -m > x86_64 > $ cat /sys/kernel/mm/transparent_hugepage/enabled > [always] madvise never > $ strace -f -e madvise,mmap,openat ./elf/tst-thp-align --direct > [...] > openat(AT_FDCWD, "[...]/x86_64-linux-gnu/elf/tst-thp-size-mod.so", O_RDONLY|O_CLOEXEC) = 3 > mmap(NULL, 33562640, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ad94f000000 > madvise(0x7ad94f000000, 33558528, MADV_HUGEPAGE) = 0 > > Where I would expect that no madvise will be required or applied. Fixed in the v8 patch: > $ uname -m > aarch64 > $ cat /sys/kernel/mm/transparent_hugepage/enabled > [always] madvise never > $ strace -f -e madvise,mmap,openat ./elf/tst-thp-align --direct > [...] > openat(AT_FDCWD, "/[...]/build/aarch64-linux-gnu/elf/tst-thp-size-mod.so", O_RDONLY|O_CLOEXEC) = 3 > mmap(NULL, 39845920, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_DENYWRITE, -1, 0) = 0xffffa745f000 > mmap(0xffffa7600000, 37748768, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0) = 0xffffa7600000 > mmap(0xffffa99ff000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x21ff000) = 0xffffa99ff000 > [...] If DL_MAP_DEFAULT_THP_PAGESIZE is defined, assume kernel THP madvise mode. If kernel THP mode is always or never, there is an extra madvise call which has no impact. DL_MAP_DEFAULT_THP_PAGESIZE is defined for x86 and 64-bit loongarch. Since DL_MAP_DEFAULT_THP_PAGESIZE isn't defined for aarch64, /sys/kernel/mm/transparent_hugepage/enabled is checked and madvise isn't called. > It seems that the two enabled checks at: > > * Executable, in sysdeps/unix/sysv/linux/dl-exec-post.h:80: > > if (GL(dl_elf_thp_control) <= 0) > return; > > * Dependency / dlopen, in sysdeps/unix/sysv/linux/dl-load-post.h:23: > > return GL(dl_thp_mode) == thp_mode_madvise; After dl_thp_mode is set according to dl_elf_thp_control, dl_thp_mode should be checked. The bug in v7 is missing: + else + GL(dl_elf_thp_control) = 0; > Use different control variables, either _dl_postprocess_loadcmd_extra_needed > should additionally require dl_elf_thp_control > 0, or _dl_get_thp_config > should auto-promote dl_elf_thp_control to 1 in the DL_MAP_DEFAULT_THP_PAGESIZE != 0 > and thp_mode == thp_mode_madvise paths. > > The tests only exercise thp=1 / thp=0, so this gap escapes coverage. > > > > > Skip these tests if THP page size cannot be determined or if THP is not > > enabled in "always" mode nor "madvise" mode. > > > > Quote WANG Rui : > > > > From benchmarking a clang build of the Linux kernel on x86_64 with > > your patch in THP madvise mode, I observed that iTLB misses were > > reduced, similar to what we see in THP always mode. > > > > NB: Some THP tests fail on arm due to limitations of arm32 kABI: > > > > https://sourceware.org/bugzilla/show_bug.cgi?id=34096 > > > Changes in v8: 1. Set GL(dl_elf_thp_control) to 0 if no tunables. 2. Add strace tests for tst-thp-align. 3. Add --direct to strace test to check madvise on DSO. diff --git a/sysdeps/unix/sysv/linux/dl-exec-post.h b/sysdeps/unix/sysv/linux/dl -exec-post.h index 367b20ae2a..b7dc7be812 100644 --- a/sysdeps/unix/sysv/linux/dl-exec-post.h +++ b/sysdeps/unix/sysv/linux/dl-exec-post.h @@ -24,6 +24,8 @@ _dl_get_thp_config (void) if (TUNABLE_IS_INITIALIZED_FULL (glibc, elf, thp)) GL(dl_elf_thp_control) = TUNABLE_GET_FULL (glibc, elf, thp, int32_t, NULL); + else + GL(dl_elf_thp_control) = 0; > This does not happens on aarch64: >