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

WANG Rui wangrui@loongson.cn
Fri Apr 10 02:51:05 GMT 2026


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.

Thanks,
Rui



More information about the Libc-alpha mailing list