linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP
@ 2026-03-02 15:50 WANG Rui
  2026-03-02 16:45 ` Matthew Wilcox
  0 siblings, 1 reply; 2+ messages in thread
From: WANG Rui @ 2026-03-02 15:50 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner, Jan Kara, Kees Cook
  Cc: linux-fsdevel, linux-mm, linux-kernel, WANG Rui

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@hev.cc>
---
 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



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [RFC PATCH] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP
  2026-03-02 15:50 [RFC PATCH] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP WANG Rui
@ 2026-03-02 16:45 ` Matthew Wilcox
  0 siblings, 0 replies; 2+ messages in thread
From: Matthew Wilcox @ 2026-03-02 16:45 UTC (permalink / raw)
  To: WANG Rui
  Cc: Alexander Viro, Christian Brauner, Jan Kara, Kees Cook,
	linux-fsdevel, linux-mm, linux-kernel

On Mon, Mar 02, 2026 at 11:50:46PM +0800, WANG Rui wrote:
> +config ELF_RO_LOAD_THP_ALIGNMENT
> +	bool "Align read-only ELF load segments for THP (EXPERIMENTAL)"
> +	depends on READ_ONLY_THP_FOR_FS

This doesn't deserve a config option.

> +#if defined(CONFIG_ELF_RO_LOAD_THP_ALIGNMENT) && PMD_SIZE <= SZ_32M

Why 32MB?  This is weird and not justified anywhere.

> +			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;

Normal style is to put the '&&' at the end of the line:

			if (!(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;

But this conditional is too complex to be at this level of indentation.
Factor it out into a helper:

			if (align_to_pmd(cmds) && p_align < PMD_SIZE)
				p_align = PMD_SIZE;



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-03-02 16:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-02 15:50 [RFC PATCH] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP WANG Rui
2026-03-02 16:45 ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox