From nobody Thu Apr 9 09:03:25 2026 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCA412517AF for ; Tue, 10 Mar 2026 01:40:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773106836; cv=none; b=hJfjiMCq9wIrTvhQDf5RjkZDCL6UCCPHYRKk4fixgl/q7DOrjeTjNdPYdBfpVGo0QcvNXdXa7sdziuk0/QQT/ZBlTS6QWLBbnLKQIjUHa2Gg9uTJQbcoZgh11Mm1SeEkyMCH+MBa3RrwvgFZ4S82jszZKKrjMv26uO5KOY0l+MQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773106836; c=relaxed/simple; bh=LGOHe3Q+N2DXozexMFe9YNC2S+lxzgYRIp9kDlu0v4M=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=dJgOIByd1FW4GJyJupi+wQKsSmRWyaH2f//Z8CdA7cf3f+JfTYJAFiymAaS1nH9AYEajZv/bYGDMVfZN4Swdih31UdCsZjYErJp4XpARhWlY+OjxhYEs5G1gmprZX5CokjHWL5H6okgljdvzYwWVlbVfAVdl58VLO3nSGpnI7WQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hev.cc; spf=pass smtp.mailfrom=hev.cc; dkim=pass (2048-bit key) header.d=hev-cc.20230601.gappssmtp.com header.i=@hev-cc.20230601.gappssmtp.com header.b=ICzd4zPg; arc=none smtp.client-ip=209.85.215.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hev.cc Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hev.cc Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=hev-cc.20230601.gappssmtp.com header.i=@hev-cc.20230601.gappssmtp.com header.b="ICzd4zPg" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-c70bfef17a4so7532974a12.2 for ; Mon, 09 Mar 2026 18:40:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hev-cc.20230601.gappssmtp.com; s=20230601; t=1773106834; x=1773711634; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=a7694XZeCof+FdxT7qTthMncnpG5NN0vfJefAJSmmkg=; b=ICzd4zPgWKgEQ/T/amfiFSzEb0GsSb0DhDWwKJ4j7gAuN1ab+43OPdH1DxidFZJ/vb SciPe5NxY1Cji/RcL6YjLn/FBPkPRa8H2pmvBoyYb/ilfMHFPf3nze5hZ0IgX8vSvd1A koEFzTa/Cuyp5HW/55ScCd1JXvKcwIJ0GVkOE1VJbuSqvCS2rAse6xIAUR6+AuU+Y90K zIrTtlGkzziIzVIpAp5xwoFteMRn+1LZbL+5JFk+wyRD9OWEReh8WYbCNaFTfSyCRkym kRyZz9YMJGz00Ed1phPtU/+w7ZerACwDctRIv6FU/oTDCX35n+iZEANYnbypRl0eTSrv yIwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773106834; x=1773711634; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=a7694XZeCof+FdxT7qTthMncnpG5NN0vfJefAJSmmkg=; b=rxhV9Dxub3GZ2LPpR+qJaC+yC+cA5QDgIpLIAMFdDpPi6k5VJOGkABMDIWTYF6ijYj mecZjnLs+bl0rQxYRWP1uLPgNnGmrawSmo2Ic+4B9kJbBi+TQuwJ1uICSxGO6pzWZbFp TtwxpB4QZMj9cY/KoGFyYK3VYeDU2gymB5CbK/Q1hN7s+0ZjfBWXJ5lYkEHMz3B6dKjK yR2LMXzLhZ1Yk49fn7TiXdZD08/0suIDwfSKG0JfjW8p0NMjSQoFHO0+MWuoW2BjBVZh elVinzloBO1N0/BV+dzEVqcEPINhhIXhcGVB3vEEEoj7JiLJFJ/kIEGMTuZXr6SPBRrO o4Vg== X-Forwarded-Encrypted: i=1; AJvYcCVt1BxFfPyOUmDM1ruPY20rs21GYl4wNAkX3anHgmNJeVh+K2dd67Fzwt1zuEhgTNHQfI6dLaFMwuN5OMo=@vger.kernel.org X-Gm-Message-State: AOJu0YynI+PDVTGpBNizJInqHELVl3AyPtyg0Q/H1PuMtWnGyoNYaHIA 5oWl8ThNkgG6HpVUaeeKh3sFvBsYNHLwGk+uUSERrF/i71apG46/Qqyr0107zL6jfdU= X-Gm-Gg: ATEYQzyLU2nT0axBNZaN8kxFdAVF0hKQe6Kno/r6N0z5IEl4rd1dkj7vYappaEdlwkS SMJBFcgGyiDcvMUZPDfyH0YXs+vwIuQl5r5C87W9xOnedPCuIOXlz0DgRad0ncOEQMC+0gOWkuF IVrr0RB8gL0Orid3y+d89taB3nECGimmdwCaUdNKgDfsrX2HQWA0kOMzJ3JtN8wgZR+rMIO1yvP r6QQpgH3p2HWZ+9CLDjVrO41Lyu7FeBQQkJksA+hrhnxVBsG0RqCeoVeGr6VxAlmJmM9A94KB+a PAavr+o+RItv+oR2/E7uqGMD8lQhWK2EgzIF0QWxnN7pB6ALANqbVVcLqVCOsmrgmmHtNwvLSmr dGiu/2IXVHJ3kxHirI1yOZMQv50wb9sweNaWJbEjsdlApv9KotVgixZC8PrDa3K2NETQqrJUFIQ 6zTEpkUK6L X-Received: by 2002:a17:903:1968:b0:2ae:8062:8362 with SMTP id d9443c01a7336-2ae82266d9fmr126325985ad.0.1773106834233; Mon, 09 Mar 2026 18:40:34 -0700 (PDT) Received: from localhost ([2400:8902:e002:de08:5754:7dac:85df:935a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2aea3d26565sm6966515ad.2.2026.03.09.18.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2026 18:40:33 -0700 (PDT) From: WANG Rui To: Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matthew Wilcox , "David Hildenbrand (Arm)" Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, WANG Rui Subject: [PATCH v3] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP Date: Tue, 10 Mar 2026 09:39:58 +0800 Message-ID: <20260310013958.103636-1-r@hev.cc> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 <=3D 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. Benchmark Machine: AMD Ryzen 9 7950X (x86_64) Binutils: 2.46 GCC: 15.2.1 (built with -z,noseparate-code + --enable-host-pie) Workload: building Linux v7.0-rc1 vmlinux with x86_64_defconfig. Without patch With patch instructions 8,246,133,611,932 8,246,025,137,750 cpu-cycles 8,001,028,142,928 7,565,925,107,502 itlb-misses 3,672,158,331 26,821,242 time elapsed 64.66 s 61.97 s Instructions are basically unchanged. iTLB misses drop from ~3.67B to ~26M (~99.27% reduction), which results in about a ~5.44% reduction in cycles and ~4.18% shorter wall time for this workload. Signed-off-by: WANG Rui --- Changes since [v2]: * Renamed align_to_pmd() to should_align_to_pmd(). * Added benchmark results to the commit message. 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. [v2]: https://lore.kernel.org/linux-fsdevel/20260304114727.384416-1-r@hev.cc [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..a0d679c31ede 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -489,6 +490,30 @@ static int elf_read(struct file *file, void *buf, size= _t len, loff_t pos) return 0; } =20 +static inline bool should_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 =3D 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 (should_align_to_pmd(&cmds[i]) && p_align < PMD_SIZE) + p_align =3D PMD_SIZE; + alignment =3D max(alignment, p_align); } } --=20 2.53.0