]> git.ipfire.org Git - thirdparty/glibc.git/commit
elf: Support THP segment load with madvise enabled THP master
authorH.J. Lu <hjl.tools@gmail.com>
Mon, 13 Apr 2026 00:23:05 +0000 (08:23 +0800)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 19 Jun 2026 21:35:24 +0000 (05:35 +0800)
commit53d909a283085c82bdcc71e1cf06c0899506b664
tree20c60761d3126831668442bc8159d7f7ff30a303
parent9310db5a085e80f0c15408675e2e5c054a7d5c9e
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.
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-64
and 64-bit loongarch.
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.
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.

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 <wangrui@loongson.cn>:

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

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
29 files changed:
csu/libc-start.c
elf/dl-load.h
elf/dl-support.c
elf/rtld.c
support/support_check_hugetlb.c
support/support_check_hugetlb.h
sysdeps/generic/dl-exec-post.h [new file with mode: 0644]
sysdeps/generic/dl-load-post.h [new file with mode: 0644]
sysdeps/generic/hugepages.h
sysdeps/generic/ldsodefs.h
sysdeps/unix/sysv/linux/Makefile
sysdeps/unix/sysv/linux/arm/Makefile
sysdeps/unix/sysv/linux/dl-exec-post.h [new file with mode: 0644]
sysdeps/unix/sysv/linux/dl-load-post.h [new file with mode: 0644]
sysdeps/unix/sysv/linux/dl-map-segment-align.c
sysdeps/unix/sysv/linux/dl-map-segment-align.h
sysdeps/unix/sysv/linux/ldsodefs.h
sysdeps/unix/sysv/linux/loongarch/Makefile
sysdeps/unix/sysv/linux/loongarch/lp64/hugepages.h [moved from sysdeps/unix/sysv/linux/loongarch/lp64/dl-map-segment-align.h with 90% similarity]
sysdeps/unix/sysv/linux/strace-tst-thp.sh [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-1-no-s-code-pde.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-1-no-s-code-static.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-1-no-s-code.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-1-pde.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-1-static.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-1.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-align-check.h [new file with mode: 0644]
sysdeps/unix/sysv/linux/tst-thp-align.c
sysdeps/unix/sysv/linux/x86_64/hugepages.h [new file with mode: 0644]