[PATCH v10] elf: Support THP segment load with madvise enabled THP
H.J. Lu
hjl.tools@gmail.com
Wed May 27 21:51:18 GMT 2026
On Thu, May 28, 2026 at 5:41 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Wed, May 27, 2026 at 4:29 AM Adhemerval Zanella Netto
> <adhemerval.zanella@linaro.org> wrote:
> >
> >
> >
> > On 26/05/26 08:24, H.J. Lu wrote:
> > > Changes in v10:
> > >
> > > 1. Rebased against
> > >
> > > commit f531cab5891cc71442dcced4abe8ecc6ef15e61f
> > > Author: Diego Nieto Cid <dnietoc@gmail.com>
> > > Date: Wed May 20 23:49:27 2026 +0100
> > >
> > > hurd: let the root user raise its priority
> > >
> > > From dce423132a5fa7ee69ba860876d7b1dc40022985 Mon Sep 17 00:00:00 2001
> > > From: "H.J. Lu" <hjl.tools@gmail.com>
> > > Date: Mon, 13 Apr 2026 08:23:05 +0800
> > > Subject: [PATCH v10] 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 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:
> >
> > Besides ARM, I am seeing RISCV failures as well:
> >
> > FAIL: elf/tst-thp-1-no-s-code-pde
> > FAIL: elf/tst-thp-1-no-s-code-static
> > FAIL: elf/tst-thp-1-pde
> > FAIL: elf/tst-thp-1-static
>
> Is glibc configured with --enable-static-pie? If yes,
> elf/tst-thp-1-no-s-code-static
> and elf/tst-thp-1-static should work.
I meant --enable-default-pie.
> > riscv64-linux-gnu-rv64imafdc-lp64d-review$ uname -r
> > 6.12.74+deb13+1-riscv64
> >
> > riscv64-linux-gnu-rv64imafdc-lp64d-review$ strace -E GCONV_PATH=iconvdata -E LOCPATH=localedata -E LC_ALL=C -E GLIBC_TUNABLES=glibc.elf.thp=1 elf/tst-thp-1-no-s-code-pde --direct
> > execve("elf/tst-thp-1-no-s-code-pde", ["elf/tst-thp-1-no-s-code-pde", "--direct"], 0x5555c49b6b60 /* 36 vars */) = -1 EPERM (Operation not permitted)
> > +++ killed by SIGSEGV +++
> > Segmentation fault
> >
> > It seems RISCV default linker scripts sets p_vaddr to 0, different than
> > x86_64/aarch64, and /proc/sys/vm/mmap_min_addr is a non-zero value:
> >
> > riscv64-linux-gnu-rv64imafdc-lp64d-review$ readelf -lW elf/tst-thp-1-pde | awk '/LOAD/{print $3}'
> > 0x0000000000000000
> > 0x0000000000200000
> > 0x0000000002400000
> > 0x00000000027ffcb0
> > $ ./elf/tst-thp-1-pde --direct ; echo $?
> > Segmentation fault
> > 139
> > $ echo 0 | sudo tee /proc/sys/vm/mmap_min_addr
> > 0
> > $ ./elf/tst-thp-1-pde --direct ; echo $?
> > 0
> >
> > I think we will need to either bump the text segment base for non-PIE THP tests
> > on RISC-V (e.g. add -Wl,-Ttext-segment=0x200000 or --image-base) alongside
> > THP-PAGE-SIZE-LDFLAGS *or* skip the non-PIE THP tests when the linker default
> > text base < THP page size (detect this at configure).
> >
> > I still reviewing the test, some comments below; but in general it looks ok.
> >
> --
> H.J.
--
H.J.
More information about the Libc-alpha
mailing list