[PATCH v3] malloc: Don't call __get_thp_mode/__get_thp_size twice

H.J. Lu hjl.tools@gmail.com
Tue Jun 23 04:58:28 GMT 2026


Changes in v3:

1. Add NEWS entries for glibc.elf.thp and its impact
on THP usage in malloc.
2. Update tunables.texi to document glibc.elf.thp impact
on THP usage in malloc.
3. Add get_capped_thp_size to cap THP page size in
ld.so and malloc.
4. Remove a strayed space.

-- 
H.J.
---

Both ld.so and malloc track kernel THP mode and THP page size when THP
in ld.so is enabled by

GLIBC_TUNABLES=glibc.elf.thp=1

and THP in malloc is enabled by

GLIBC_TUNABLES=glibc.malloc.hugetlb=1

But DL_MAP_DEFAULT_THP_PAGESIZE and MALLOC_DEFAULT_THP_PAGESIZE may be
different when they are defined in <hugepages.h>.

If THP in ld.so is enabled, change malloc to use kernel THP mode from
ld.so, instead of calling __get_thp_mode, and use THP page size from
ld.so if it came from __get_thp_size.  This avoids calling __get_thp_mode
and __get_thp_size again, which are quite expensive:

1. Initialize mp_.thp_mode and GL(dl_thp_mode) to thp_mode_unknown.
2. Set mp_.thp_mode to GL(dl_thp_mode) if GL(dl_thp_mode) isn't
thp_mode_unknown.  Otherwise call __get_thp_mode to set mp_.thp_mode.
3. GL(dl_elf_thp_pagesize) is set to DL_MAP_DEFAULT_THP_PAGESIZE without
calling __get_thp_size and THP page size for malloc may be different from
THP page size for ld.so.  Set mp_.thp_pagesize to GL(dl_elf_thp_pagesize)
if DL_MAP_DEFAULT_THP_PAGESIZE is defined.  Otherwise call __get_thp_size
to set mp_.thp_pagesize.
4. THP page size in malloc is capped to MAX_THP_PAGESIZE.  If THP page
size is above MAX_THP_PAGESIZE, THP in malloc is disabled.

These result in when glibc.elf.thp is set to 1, malloc uses the actual
kernel THP mode instead of defaulting to madvise mode and madvise_thp
will stop issuing MADV_HUGEPAGE if kernel THP mode is always.

This fixes BZ #34083.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v3-0001-malloc-Don-t-call-__get_thp_mode-__get_thp_size-t.patch
Type: text/x-patch
Size: 8207 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20260623/4f546e23/attachment-0001.bin>


More information about the Libc-alpha mailing list