[PATCH v12] elf: Support THP segment load with madvise enabled THP

H.J. Lu hjl.tools@gmail.com
Wed Jun 3 22:40:43 GMT 2026


Changes in v12:

1. Call _dl_executable_postprocess after ARCH_SETUP_IREL.
2. Add enum dl_elf_thp_control_t and change _dl_elf_thp_control to
enum dl_elf_thp_control_t.
3. Add get_thp_mode and get_thp_size to support/support_check_hugetlb.c.
Update support_thp_is_madvise to use get_thp_mode.   Check path != NULL
in check_align.  Close fd after read in get_thp_mode.
4. Xfail -z noseparate-code THP tests with lld since -z noseparate-code is a
nop for lld.
5. Only xfail tst-thp-1-no-s-code-static and tst-thp-1-static on arm for
static
PIE.
6. Print madvise result for LD_DEBUGS=files:

   4073596: madvise (0x0000560f3a400000, 0x00000000020029fd,
MADV_HUGEPAGE) returns 0

   4077956: file=tst-thp-size-mod.so [0];  dynamically loaded by
elf/tst-thp-align [0]
   4077956: file=tst-thp-size-mod.so [0];  generating link map
   4077956:   madvise (0x00005611e6600000, 0x0000000002001000,
MADV_HUGEPAGE) returns 0
   4077956:   dynamic: 0x00005611e89ffdf8  base: 0x00005611e6600000
size: 0x0000000002400008
   4077956:     entry: 0x00005611e6600000  phdr: 0x00005611e6600040
phnum:                  9

--
H.J.
---
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 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20260604/82ebaa2b/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v12-0001-elf-Support-THP-segment-load-with-madvise-enable.patch
Type: application/x-patch
Size: 53876 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20260604/82ebaa2b/attachment-0001.bin>


More information about the Libc-alpha mailing list