[PATCH] malloc: remember that thp_mode got probed
Dev Jain
dev.jain@arm.com
Sat Feb 28 08:03:58 GMT 2026
On 26/02/26 3:06 am, Aurelien Jarno wrote:
> On aarch64, following the new 2MB THP default introduced in 321e1fc73f53
> and when /sys/kernel is not accessible (as in some containers [1]),
> /sys/kernel/mm/transparent_hugepage/enabled is probed on every malloc()
> call.
>
> This causes some performance regression, and also sets errno to ENOENT
> on each malloc call. Some programs are not prepared for that (even
> though they arguably should be), causing some regressions [2].
>
> Fix that by introducing a new THP state, malloc_thp_mode_unknown, which
> is distinct from malloc_thp_mode_not_supported. This makes possible to
> record that the THP mode has been probed and found as not supported,
> avoiding repeatedly poking the file. In turns this "fixes" half of the
> regressions found so far.
>
> [1] https://systemd.io/CONTAINER_INTERFACE/
> [2] https://bugs.debian.org/cgi-bin/pkgreport.cgi?tag=glibc-2.43-arm64-thp;users=debian-glibc@lists.debian.org
> ---
Sorry for late reply, Thunderbird chose not to highlight my glibc folder
to hint I received a glibc email.
The patch makes sense to me, thanks for fixing this.
Reviewed-by: Dev Jain <dev.jain@arm.com>
> malloc/malloc.c | 4 ++--
> sysdeps/generic/malloc-hugepages.h | 3 ++-
> 2 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index a49e211925..408363c7f0 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -1826,7 +1826,7 @@ static struct malloc_par mp_ =
> .trim_threshold = DEFAULT_TRIM_THRESHOLD,
> #define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8))
> .arena_test = NARENAS_FROM_NCORES (1),
> - .thp_mode = malloc_thp_mode_not_supported
> + .thp_mode = malloc_thp_mode_unknown
> #if USE_TCACHE
> ,
> .tcache_count = TCACHE_FILL_COUNT,
> @@ -1906,7 +1906,7 @@ static __always_inline void
> thp_init (void)
> {
> /* Initialize only once if DEFAULT_THP_PAGESIZE is defined. */
> - if (DEFAULT_THP_PAGESIZE == 0 || mp_.thp_mode != malloc_thp_mode_not_supported)
> + if (DEFAULT_THP_PAGESIZE == 0 || mp_.thp_mode != malloc_thp_mode_unknown)
> return;
>
> /* Set thp_pagesize even if thp_mode is never. This reduces frequency
> diff --git a/sysdeps/generic/malloc-hugepages.h b/sysdeps/generic/malloc-hugepages.h
> index ab2690f7b8..7d2c81904b 100644
> --- a/sysdeps/generic/malloc-hugepages.h
> +++ b/sysdeps/generic/malloc-hugepages.h
> @@ -29,7 +29,8 @@ enum malloc_thp_mode_t
> malloc_thp_mode_always,
> malloc_thp_mode_madvise,
> malloc_thp_mode_never,
> - malloc_thp_mode_not_supported
> + malloc_thp_mode_not_supported,
> + malloc_thp_mode_unknown
> };
>
> enum malloc_thp_mode_t __malloc_thp_mode (void) attribute_hidden;
More information about the Libc-alpha
mailing list