[PATCH v2 3/4] elf: align ET_DYN base to max folio size for PTE coalescing
From: Usama Arif
Date: Fri Mar 20 2026 - 10:11:28 EST
For PIE binaries (ET_DYN), the load address is randomized at PAGE_SIZE
granularity via arch_mmap_rnd(). On arm64 with 64K base pages, this
means the binary is 64K-aligned, but contpte mapping requires 2M
(CONT_PTE_SIZE) alignment.
Without proper virtual address alignment, readahead patches that
allocate 2M folios with 2M-aligned file offsets and physical addresses
cannot benefit from contpte mapping, as the contpte fold check in
contpte_set_ptes() requires the virtual address to be CONT_PTE_SIZE-
aligned.
Fix this by extending maximum_alignment() to consider the maximum folio
size supported by the page cache (via mapping_max_folio_size()). For
each PT_LOAD segment, the alignment is bumped to the largest
power-of-2 that fits within the segment size, capped by the max folio
size the filesystem will allocate, if:
- Both p_vaddr and p_offset are aligned to that size
- The segment is large enough (p_filesz >= size)
This ensures load_bias is folio-aligned so that file-offset-aligned
folios map to properly aligned virtual addresses, enabling hardware PTE
coalescing (e.g. arm64 contpte) and PMD mappings for large folios.
The segment size check avoids reducing ASLR entropy for small binaries
that cannot benefit from large folio alignment.
Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
---
fs/binfmt_elf.c | 38 ++++++++++++++++++++++++++++++++++++--
1 file changed, 36 insertions(+), 2 deletions(-)
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 8e89cc5b28200..042af81766fcd 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -49,6 +49,7 @@
#include <uapi/linux/rseq.h>
#include <asm/param.h>
#include <asm/page.h>
+#include <linux/pagemap.h>
#ifndef ELF_COMPAT
#define ELF_COMPAT 0
@@ -488,19 +489,51 @@ static int elf_read(struct file *file, void *buf, size_t len, loff_t pos)
return 0;
}
-static unsigned long maximum_alignment(struct elf_phdr *cmds, int nr)
+static unsigned long maximum_alignment(struct elf_phdr *cmds, int nr,
+ struct file *filp)
{
unsigned long alignment = 0;
+ unsigned long max_folio_size = PAGE_SIZE;
int i;
+ if (filp && filp->f_mapping)
+ max_folio_size = mapping_max_folio_size(filp->f_mapping);
+
for (i = 0; i < nr; i++) {
if (cmds[i].p_type == PT_LOAD) {
unsigned long p_align = cmds[i].p_align;
+ unsigned long size;
/* skip non-power of two alignments as invalid */
if (!is_power_of_2(p_align))
continue;
alignment = max(alignment, p_align);
+
+ /*
+ * Try to align the binary to the largest folio
+ * size that the page cache supports, so the
+ * hardware can coalesce PTEs (e.g. arm64
+ * contpte) or use PMD mappings for large folios.
+ *
+ * Use the largest power-of-2 that fits within
+ * the segment size, capped by what the page
+ * cache will allocate. Only align when the
+ * segment's virtual address and file offset are
+ * already aligned to the folio size, as
+ * misalignment would prevent coalescing anyway.
+ *
+ * The segment size check avoids reducing ASLR
+ * entropy for small binaries that cannot
+ * benefit.
+ */
+ if (!cmds[i].p_filesz)
+ continue;
+ size = rounddown_pow_of_two(cmds[i].p_filesz);
+ size = min(size, max_folio_size);
+ if (size > PAGE_SIZE &&
+ IS_ALIGNED(cmds[i].p_vaddr, size) &&
+ IS_ALIGNED(cmds[i].p_offset, size))
+ alignment = max(alignment, size);
}
}
@@ -1104,7 +1137,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
}
/* Calculate any requested alignment. */
- alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum);
+ alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum,
+ bprm->file);
/**
* DOC: PIE handling
--
2.52.0