[PATCH v8 5/6] elf: Align large load segments to PMD huge page size for THP

H.J. Lu hjl.tools@gmail.com
Fri Apr 10 03:22:26 GMT 2026


On Fri, Apr 10, 2026 at 10:59 AM WANG Rui <wangrui@loongson.cn> wrote:
>
> On Fri, Apr 10, 2026 at 8:58 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > > > +ElfW (Addr)
> > > > +_dl_map_segment_align (const struct loadcmd *c, ElfW (Addr) p_align_max)
> > > > +{
> > > > +  static enum thp_mode_t thp_mode = thp_mode_not_supported;
> > > > +  static unsigned long int thp_pagesize;
> > > > +
> > > > +  if (TUNABLE_GET (glibc, elf, thp, int32_t, NULL) == 0)
> > > > +    return p_align_max;
> > > > +
> > > > +  if (__glibc_unlikely (thp_mode == thp_mode_not_supported
> > > > +                       || thp_pagesize == 0))
> > > > +    {
> > > > +      unsigned long int default_thp_pagesize = DL_MAP_DEFAULT_THP_PAGESIZE;
> > > > +      thp_mode = default_thp_pagesize ? thp_mode_always : __get_thp_mode ();
> > >
> > > Isn't THP mode controlled by kernel?  I think we should also call
> >
> > We should always do
> >
> > thp_mode = __get_thp_mode ();
> >
> > > __madvise with MADV_HUGEPAGE to enable THP.
> >
> > We can call  __madvise after _dl_postprocess_loadcmd is called if
> > thp_mode == thp_mode_madvise.
>
> The goal of this patch is simply to be aware of THP being enabled and,
> when possible, increase the alignment of LOAD segments so THP can work
> more effectively. Overall it's a passive approach, the decision of
> whether a mapping actually ends up using huge pages is entirely left
> to THP.
>
> From an impl point of view, it tries to avoid introducing any
> additional overhead as much as possible, including extra syscalls and
> the like.
>

My kernel has

[hjl@gnu-tgl-3 linux]$ cat /sys/kernel/mm/transparent_hugepage/enabled
always [madvise] never
[hjl@gnu-tgl-3 linux]$

Why can't ld.so enable THP?  A single __madvise call won't kill the THP
performance.

-- 
H.J.


More information about the Libc-alpha mailing list