public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: WANG Rui <r@hev.cc>, Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	Kees Cook <kees@kernel.org>, Matthew Wilcox <willy@infradead.org>
Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP
Date: Mon, 9 Mar 2026 15:22:28 +0100	[thread overview]
Message-ID: <fbfea9cd-0bd5-459f-a656-22af2b9f53f3@kernel.org> (raw)
In-Reply-To: <20260304114727.384416-1-r@hev.cc>

On 3/4/26 12:47, WANG Rui wrote:
> 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.
> 
> 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,
> since requiring larger alignments would be unreasonable, especially on
> 32-bit systems with a much more limited virtual address space.
> 
> 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 linker) are not affected.
> 
> Signed-off-by: WANG Rui <r@hev.cc>
> ---
> Changes since [v1]:
> * Dropped the Kconfig option CONFIG_ELF_RO_LOAD_THP_ALIGNMENT.
> * Moved the alignment logic into a helper align_to_pmd() for clarity.
> * Improved the comment explaining why we skip the optimization
>   when PMD_SIZE > 32MB.
> 
> [v1]: https://lore.kernel.org/linux-fsdevel/20260302155046.286650-1-r@hev.cc
> ---
>  fs/binfmt_elf.c | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
> index fb857faaf0d6..39bad27d8490 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>
> @@ -489,6 +490,30 @@ static int elf_read(struct file *file, void *buf, size_t len, loff_t pos)
>  	return 0;
>  }
>  
> +static inline bool align_to_pmd(const struct elf_phdr *cmd)
> +{
> +	/*
> +	 * Avoid excessive virtual address space padding when PMD_SIZE is very
> +	 * large (e.g. some 64K base-page configurations).
> +	 */
> +	if (PMD_SIZE > SZ_32M)
> +		return false;
> +
> +	if (!hugepage_global_always())
> +		return false;
> +
> +	if (!IS_ALIGNED(cmd->p_vaddr | cmd->p_offset, PMD_SIZE))
> +		return false;
> +
> +	if (cmd->p_filesz < PMD_SIZE)
> +		return false;
> +
> +	if (cmd->p_flags & PF_W)
> +		return false;
> +
> +	return true;
> +}
> +
>  static unsigned long maximum_alignment(struct elf_phdr *cmds, int nr)
>  {
>  	unsigned long alignment = 0;
> @@ -501,6 +526,10 @@ 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 (align_to_pmd(&cmds[i]) && p_align < PMD_SIZE)
> +				p_align = PMD_SIZE;

The function name does not make any sense when reading this chunk.

"should_align_XXX_to_pmd" ?

-- 
Cheers,

David

      parent reply	other threads:[~2026-03-09 14:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-04 11:47 [PATCH v2] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP WANG Rui
2026-03-08  9:57 ` hev
2026-03-09 14:22 ` David Hildenbrand (Arm) [this message]

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=fbfea9cd-0bd5-459f-a656-22af2b9f53f3@kernel.org \
    --to=david@kernel.org \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=r@hev.cc \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox