]> git.ipfire.org Git - thirdparty/glibc.git/commit
malloc: Don't call __get_thp_mode/__get_thp_size twice master
authorH.J. Lu <hjl.tools@gmail.com>
Sat, 18 Apr 2026 03:36:41 +0000 (11:36 +0800)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 26 Jun 2026 02:18:36 +0000 (10:18 +0800)
commitccd4cd5324caaaea1bb12bc8cef073170da51a19
tree2ab9fa2aa4206f2a27740f1028b8500b234e7d90
parent96773cc77bc7596fb503dc1f0cf916a98f1c3701
malloc: Don't call __get_thp_mode/__get_thp_size twice

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.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
NEWS
malloc/malloc.c
manual/tunables.texi
sysdeps/generic/hugepages.h
sysdeps/unix/sysv/linux/dl-exec-post.h