[RFC PATCH] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP
From: WANG Rui
Date: Mon Mar 02 2026 - 10:54:31 EST
When Transparent Huge Pages (THP) are enabled in "always" mode,
file-backed read-only mappings can be backed by PMD-sized huge pages
if they meet the alignment and size requirements.
For ELF executables loaded by the kernel ELF binary loader, PT_LOAD
segments are normally aligned according to p_align, which is often
only page-sized. As a result, large read-only segments that are
otherwise eligible may fail to be mapped using PMD-sized THP.
Introduce a new Kconfig option, CONFIG_ELF_RO_LOAD_THP_ALIGNMENT,
to allow bumping the maximum alignment of eligible read-only PT_LOAD
segments to PMD_SIZE.
A segment is considered eligible if:
* THP is in "always" mode,
* it is not writable,
* both p_vaddr and p_offset are PMD-aligned,
* its file size is at least PMD_SIZE, and
* its existing p_align is smaller than PMD_SIZE.
To avoid excessive address space padding on systems with very large
PMD_SIZE values, this optimization is applied only when PMD_SIZE <= 32MB.
This increases the likelihood that large text segments of ELF
executables are backed by PMD-sized THP, reducing TLB pressure and
improving performance for large binaries.
This only affects ELF executables loaded directly by the kernel
binary loader. Shared libraries loaded by user space (e.g. via the
dynamic loader) are not affected.
The behavior is guarded by CONFIG_ELF_RO_LOAD_THP_ALIGNMENT and
depends on READ_ONLY_THP_FOR_FS, so existing systems remain
unchanged unless explicitly enabled.
Signed-off-by: WANG Rui <r@xxxxxx>
---
fs/Kconfig.binfmt | 12 ++++++++++++
fs/binfmt_elf.c | 9 +++++++++
2 files changed, 21 insertions(+)
diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
index 1949e25c7741..8d4271769e08 100644
--- a/fs/Kconfig.binfmt
+++ b/fs/Kconfig.binfmt
@@ -74,6 +74,18 @@ config ELFCORE
help
This option enables kernel/elfcore.o.
+config ELF_RO_LOAD_THP_ALIGNMENT
+ bool "Align read-only ELF load segments for THP (EXPERIMENTAL)"
+ depends on READ_ONLY_THP_FOR_FS
+
+ help
+ Align eligible read-only ELF PT_LOAD segments to PMD_SIZE so
+ they can be mapped using PMD-sized Transparent Huge Pages
+ when THP is enabled in "always" mode.
+
+ This only affects ELF executables loaded by the kernel ELF
+ binary loader.
+
config CORE_DUMP_DEFAULT_ELF_HEADERS
bool "Write ELF core dumps with partial segments"
default y
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 8e89cc5b2820..2c2ccb041938 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -28,6 +28,7 @@
#include <linux/highuid.h>
#include <linux/compiler.h>
#include <linux/highmem.h>
+#include <linux/huge_mm.h>
#include <linux/hugetlb.h>
#include <linux/pagemap.h>
#include <linux/vmalloc.h>
@@ -500,6 +501,14 @@ static unsigned long maximum_alignment(struct elf_phdr *cmds, int nr)
/* skip non-power of two alignments as invalid */
if (!is_power_of_2(p_align))
continue;
+
+#if defined(CONFIG_ELF_RO_LOAD_THP_ALIGNMENT) && PMD_SIZE <= SZ_32M
+ if (hugepage_global_always() && !(cmds[i].p_flags & PF_W)
+ && IS_ALIGNED(cmds[i].p_vaddr | cmds[i].p_offset, PMD_SIZE)
+ && cmds[i].p_filesz >= PMD_SIZE && p_align < PMD_SIZE)
+ p_align = PMD_SIZE;
+#endif
+
alignment = max(alignment, p_align);
}
}
--
2.53.0