From: ecordonnier@snap.com
To: openembedded-core@lists.openembedded.org
Cc: Etienne Cordonnier <ecordonnier@snap.com>,
Khem Raj <khem.raj@oss.qualcomm.com>
Subject: [wrynose][PATCH] glibc: Do not use Transparent Huge Pages in malloc by default
Date: Fri, 20 Nov 2026 17:07:46 +0100 [thread overview]
Message-ID: <20261120160746.314-1-ecordonnier@snap.com> (raw)
From: Etienne Cordonnier <ecordonnier@snap.com>
Since glibc 2.43 malloc on aarch64 defaults to glibc.malloc.hugetlb=1
with a 2MB THP size, which considerably increases memory usage on
embedded systems (an aarch64 system went from 465 MiB to 1105 MiB
AnonPages at idle and started swapping).
The default is a compile-time constant in glibc and GLIBC_TUNABLES does
not reach AT_SECURE programs. Add a configure option to glibc to disable
the default and wire it up as the malloc-default-thp PACKAGECONFIG, off
by default to get the pre-2.43 behavior. THP can still be enabled at run
time with glibc.malloc.hugetlb=1.
Backported to wrynose (glibc 2.43): the glibc patch is the 2.43 backport
of the upstream submission, using the pre-refactor malloc-hugepages.h.
[YOCTO #16490]
Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
Tested-by: Etienne Cordonnier <ecordonnier@snap.com>
(cherry picked from commit 766b888b41243980b811e848b40fb09894995ea7)
AI-Generated: Uses Claude Code
Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
---
...le-malloc-default-thp-configure-opti.patch | 189 ++++++++++++++++++
meta/recipes-core/glibc/glibc_2.43.bb | 7 +
2 files changed, 196 insertions(+)
create mode 100644 meta/recipes-core/glibc/glibc/0023-malloc-Add-disable-malloc-default-thp-configure-opti.patch
diff --git a/meta/recipes-core/glibc/glibc/0023-malloc-Add-disable-malloc-default-thp-configure-opti.patch b/meta/recipes-core/glibc/glibc/0023-malloc-Add-disable-malloc-default-thp-configure-opti.patch
new file mode 100644
index 0000000000..9731dc08fd
--- /dev/null
+++ b/meta/recipes-core/glibc/glibc/0023-malloc-Add-disable-malloc-default-thp-configure-opti.patch
@@ -0,0 +1,189 @@
+From f9f37cf138a539263b48e2a6fbe53dce95fe6244 Mon Sep 17 00:00:00 2001
+From: Khem Raj <raj.khem@gmail.com>
+Date: Sat, 14 Nov 2026 21:11:13 +0100
+Subject: [PATCH] malloc: Add --disable-malloc-default-thp configure option
+
+Since glibc 2.43, malloc on aarch64 defaults to glibc.malloc.hugetlb=1
+with a 2MB THP size and also applies MADV_HUGEPAGE to thread arenas. On
+memory constrained embedded systems this significantly increases the
+resident memory of every process (BZ #34081).
+
+Add a configure option, --disable-malloc-default-thp, which makes the
+generic malloc-hugepages.h override any target definition of
+DEFAULT_THP_PAGESIZE with 0. The aarch64 header gets an include guard
+so it cannot redefine DEFAULT_THP_PAGESIZE after the generic header has
+undefined it. This gives the pre-2.43 behavior (glibc.malloc.hugetlb
+defaults to 0, THP can still be enabled at run time with
+glibc.malloc.hugetlb=1). The default is unchanged.
+
+Upstream-Status: Submitted [https://github.com/kraj/glibc/commit/f42cbe83a44f816cd1780516d738a8681044e9d7]
+
+Backported to glibc 2.43 (pre "hugepages abstraction" refactor): the
+sysdeps hunks apply to sysdeps/generic/malloc-hugepages.h and
+sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h using
+DEFAULT_THP_PAGESIZE instead of MALLOC_DEFAULT_THP_PAGESIZE, and the
+manual/tunables.texi hunk is dropped. Functional change is identical.
+
+Signed-off-by: Khem Raj <raj.khem@gmail.com>
+---
+ INSTALL | 9 ++++++++
+ config.h.in | 4 ++++
+ configure | 21 +++++++++++++++++++
+ configure.ac | 8 +++++++
+ manual/install.texi | 8 +++++++
+ sysdeps/generic/malloc-hugepages.h | 4 ++++
+ .../sysv/linux/aarch64/malloc-hugepages.h | 7 +++++++
+ 7 files changed, 61 insertions(+)
+
+diff --git a/INSTALL b/INSTALL
+index 4174c9661fa..b1388784076 100644
+--- a/INSTALL
++++ b/INSTALL
+@@ -250,6 +250,15 @@ passed to 'configure'. For example:
+ Disable using 'scv' instruction for syscalls. All syscalls will
+ use 'sc' instead, even if the kernel supports 'scv'. PowerPC only.
+
++'--disable-malloc-default-thp'
++ Do not make 'malloc' use Transparent Huge Pages by default. This
++ restores the behavior of 'glibc.malloc.hugetlb=0' as the default,
++ which can reduce memory usage on systems with limited memory. Huge
++ pages can still be enabled at run time by setting the
++ 'glibc.malloc.hugetlb' tunable to '1'. This only affects targets
++ which use Transparent Huge Pages in 'malloc' by default, currently
++ AArch64.
++
+ '--build=BUILD-SYSTEM'
+ '--host=HOST-SYSTEM'
+ These options are for cross-compiling. If you specify both options
+diff --git a/config.h.in b/config.h.in
+index 2cd8c90eb66..74b1a2824c7 100644
+--- a/config.h.in
++++ b/config.h.in
+@@ -29,6 +29,10 @@
+ /* On powerpc*, define if scv should be used for syscalls (when available). */
+ #undef USE_PPC_SCV
+
++/* Define if malloc should use Transparent Huge Pages by default on
++ targets which define DEFAULT_THP_PAGESIZE. */
++#undef USE_MALLOC_DEFAULT_THP
++
+ /* Define if _Unwind_Find_FDE should be exported from glibc. */
+ #undef EXPORT_UNWIND_FIND_FDE
+
+diff --git a/configure b/configure
+index 0cd2269859c..d4c8aee085d 100755
+--- a/configure
++++ b/configure
+@@ -820,6 +820,7 @@ enable_pt_chown
+ enable_mathvec
+ enable_cet
+ enable_scv
++enable_malloc_default_thp
+ enable_fortify_source
+ enable_sframe
+ with_cpu
+@@ -1503,6 +1504,10 @@ Optional Features:
+ (CET), x86 only
+ --disable-scv syscalls will not use scv instruction, even if the
+ kernel supports it, powerpc only
++ --disable-malloc-default-thp
++ malloc will not use Transparent Huge Pages by
++ default on targets which enable them,
++ glibc.malloc.hugetlb=1 can still enable them
+ --enable-fortify-source[=1|2|3]
+ Use -D_FORTIFY_SOURCE=[1|2|3] to control code
+ hardening, defaults to highest possible value
+@@ -4872,6 +4877,22 @@ then :
+
+ fi
+
++# Check whether --enable-malloc-default-thp was given.
++if test ${enable_malloc_default_thp+y}
++then :
++ enableval=$enable_malloc_default_thp; use_malloc_default_thp=$enableval
++else case e in #(
++ e) use_malloc_default_thp=yes ;;
++esac
++fi
++
++
++if test "$use_malloc_default_thp" != "no"
++then :
++ printf "%s\n" "#define USE_MALLOC_DEFAULT_THP 1" >>confdefs.h
++
++fi
++
+ # Check whether --enable-fortify-source was given.
+ if test ${enable_fortify_source+y}
+ then :
+diff --git a/configure.ac b/configure.ac
+index 3e4a601c01f..941babaefd0 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -429,6 +429,14 @@ AC_ARG_ENABLE([scv],
+
+ AS_IF([[test "$use_scv" != "no"]],[AC_DEFINE(USE_PPC_SCV)])
+
++AC_ARG_ENABLE([malloc-default-thp],
++ AS_HELP_STRING([--disable-malloc-default-thp],
++ [malloc will not use Transparent Huge Pages by default on targets which enable them, glibc.malloc.hugetlb=1 can still enable them]),
++ [use_malloc_default_thp=$enableval],
++ [use_malloc_default_thp=yes])
++
++AS_IF([[test "$use_malloc_default_thp" != "no"]],[AC_DEFINE(USE_MALLOC_DEFAULT_THP)])
++
+ dnl Build glibc with _FORTIFY_SOURCE
+ AC_ARG_ENABLE(fortify-source,
+ AS_HELP_STRING([--enable-fortify-source@<:@=1|2|3@:>@],
+diff --git a/manual/install.texi b/manual/install.texi
+index 073cda05309..0a6534e3b35 100644
+--- a/manual/install.texi
++++ b/manual/install.texi
+@@ -280,6 +280,14 @@ C++ libraries.
+ Disable using @code{scv} instruction for syscalls. All syscalls will use
+ @code{sc} instead, even if the kernel supports @code{scv}. PowerPC only.
+
++@item --disable-malloc-default-thp
++Do not make @code{malloc} use Transparent Huge Pages by default. This
++restores the behavior of @code{glibc.malloc.hugetlb=0} as the default,
++which can reduce memory usage on systems with limited memory. Huge pages
++can still be enabled at run time by setting the @code{glibc.malloc.hugetlb}
++tunable to @code{1}. This only affects targets which use Transparent
++Huge Pages in @code{malloc} by default, currently AArch64.
++
+ @item --build=@var{build-system}
+ @itemx --host=@var{host-system}
+ These options are for cross-compiling. If you specify both options and
+diff --git a/sysdeps/generic/malloc-hugepages.h b/sysdeps/generic/malloc-hugepages.h
+index ab2690f7b8a..23a4cdd5bce 100644
+--- a/sysdeps/generic/malloc-hugepages.h
++++ b/sysdeps/generic/malloc-hugepages.h
+@@ -41,6 +41,10 @@ enum malloc_thp_mode_t __malloc_thp_mode (void) attribute_hidden;
+ void __malloc_hugepage_config (size_t requested, size_t *pagesize, int *flags)
+ attribute_hidden;
+
++#ifndef USE_MALLOC_DEFAULT_THP
++# undef DEFAULT_THP_PAGESIZE
++#endif
++
+ #ifndef DEFAULT_THP_PAGESIZE
+ # define DEFAULT_THP_PAGESIZE 0
+ #endif
+diff --git a/sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h b/sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h
+index 2204112e4e5..27eef7ecca6 100644
+--- a/sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h
++++ b/sysdeps/unix/sysv/linux/aarch64/malloc-hugepages.h
+@@ -16,6 +16,13 @@
+ License along with the GNU C Library; see the file COPYING.LIB. If
+ not, see <https://www.gnu.org/licenses/>. */
+
++#ifndef _LINUX_AARCH64_MALLOC_HUGEPAGES_H
++#define _LINUX_AARCH64_MALLOC_HUGEPAGES_H
++
++/* Only define this on the first inclusion, the generic malloc-hugepages.h may
++ undefine it if glibc is configured with --disable-malloc-default-thp. */
+ #define DEFAULT_THP_PAGESIZE (1UL << 21)
+
+ #include_next <malloc-hugepages.h>
++
++#endif
diff --git a/meta/recipes-core/glibc/glibc_2.43.bb b/meta/recipes-core/glibc/glibc_2.43.bb
index c962171cf3..1f48ab77f5 100644
--- a/meta/recipes-core/glibc/glibc_2.43.bb
+++ b/meta/recipes-core/glibc/glibc_2.43.bb
@@ -60,6 +60,7 @@ SRC_URI = "${GLIBC_GIT_URI};branch=${SRCBRANCH};name=glibc \
file://0020-fix-create-thread-failed-in-unprivileged-process-BZ-.patch \
file://0021-tests-Skip-2-qemu-tests-that-can-hang-in-oe-selftest.patch \
file://0022-Propagate-ffile-prefix-map-from-CFLAGS-to-ASFLAGS.patch \
+ file://0023-malloc-Add-disable-malloc-default-thp-configure-opti.patch \
"
B = "${WORKDIR}/build-${TARGET_SYS}"
@@ -97,6 +98,12 @@ PACKAGECONFIG ??= "nscd memory-tagging"
PACKAGECONFIG[nscd] = "--enable-nscd,--disable-nscd"
PACKAGECONFIG[memory-tagging] = "--enable-memory-tagging,--disable-memory-tagging"
+# Since glibc 2.43 malloc defaults to 2MB Transparent Huge Pages on aarch64,
+# which considerably increases memory usage on embedded systems
+# (https://bugzilla.yoctoproject.org/show_bug.cgi?id=16490). Keep it disabled
+# by default, THP can still be enabled at run time with glibc.malloc.hugetlb=1
+PACKAGECONFIG[malloc-default-thp] = "--enable-malloc-default-thp,--disable-malloc-default-thp"
+
do_patch:append() {
bb.build.exec_func('do_fix_readlib_c', d)
}
--
2.43.0
reply other threads:[~2026-11-20 16:07 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120160746.314-1-ecordonnier@snap.com \
--to=ecordonnier@snap.com \
--cc=khem.raj@oss.qualcomm.com \
--cc=openembedded-core@lists.openembedded.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.