From nobody Mon Apr 13 21:02:14 2026 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 271D2391839 for ; Wed, 4 Mar 2026 11:47:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772624869; cv=none; b=VNmXZttv3Cfp1bl/z3rQc34Q6HsOsbdc6OH0Qtius3gOUzuLoEKd+Mn/iYX7bqgFhHmYuKS9OdMjR4+a/DR/6zdx+nh9mra5vcHhauFlHEl0leQQ/URR1GBnbZloGD0drenHjAkGxMpU/PKTkdZi8wZcaN6VbYmAm6nfAiy9ivo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772624869; c=relaxed/simple; bh=Dcjy/fJCF/rN4gn0l0pE+hqWHG8jNAgHXv1CiahgN6E=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=gay+XW9bs5qaSHmysYgPOhv3LtsgoQIAgSamA5s+LGbXISZ7FutCkti1h+ENZQ0Ic7usUqfFldrioe5osJaTzH0eQjQnyOL8QQ7KRo6JlyjrSX9KZDiznbivR8Z/1BqBkQmYX1CDnSRcgGJCYY7eeYnaoxLBAGX0X4bQIweA490= 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=h0dqcF6L; arc=none smtp.client-ip=209.85.210.178 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="h0dqcF6L" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-8297310ce0aso277916b3a.2 for ; Wed, 04 Mar 2026 03:47:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hev-cc.20230601.gappssmtp.com; s=20230601; t=1772624867; x=1773229667; 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=OSmAArgo0j03YT7FseLxFhyf5NHLf28jrPvMl7zXPPo=; b=h0dqcF6LxBfOlEj7COrT2M3GPgiA7+PKqwwJgxHk8OlEDq0219Fn07wfrDidnKIIyl UtskNIIoCKeqVp85Yja0Ad3qjs3O9IO/CHmd0SNzr12r/OCeg9wlW2wvyfbQExeW9cky QVTBP2nzM58OXrAtMDT4ePobmxV2DnNrWlgP1BfoGBF8ANCfKDnzjNzx2t2UMKU3FGNm JlWF6H+6q4CdPGF8bTuZQGi60kkgFJG7dL2G6mU6mpsnag8FX7qBmHQzpCx9xR+IaoY1 FA3bcbCoQCeswpJunMftYZWrW3wgbYcEQ0PRyehPtzYR1P+l89ErvO86AhkDrmP7wom/ cX4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772624867; x=1773229667; 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=OSmAArgo0j03YT7FseLxFhyf5NHLf28jrPvMl7zXPPo=; b=r3uzBGzUO5Q2E9pgcWPEiBZYSdSKxS68KdemasOc53oCce38QfeKl4Iqo33bx/JXzA fn69rAncbrdCLihYouNOTsnODwYw8yKC8wklME5L33Ck6k+uqj5VkzLrnNXBW30iZ84V c6xvO5QYasExdvsyUarSZdJcwM5JMh+PhWfplMgtuLwIJrHDTmzgW0kQDaAT6X+s7qgy 8yUO85HFf9G0yysd9ieKsOeqtkv7zV32WQrVDOp8VfSlfzerpTgiwTvxk7pLiK/yI8Cr tZ7U53HrC91Kkok87wsPdJPEApj9bwogj6nKg+NoQFoibekMdMSdY72xRlobsZgVC+CI tddw== X-Forwarded-Encrypted: i=1; AJvYcCVA85C8NtFXOg3k3/3Bk89NGXAjqUN23DV0spLyon87VKdYocNWRfqV1IYDzQZd4PW8aU+PGKihUIivDSI=@vger.kernel.org X-Gm-Message-State: AOJu0YwCQPKYF3686mZV8Hd/7Xb9cF6F9XL8iNbTmjgcApaKNy9O4N9Q swt4IsWx7zZ1mhSGvmM7FuQfm/oLKqKgTfGzrtmsAKbiLEqWFnBW+vO87IMYr7A6vQo= X-Gm-Gg: ATEYQzxuSjGdp16O/WcJNFskgEqtUdyqGPDzX0LOV1ERkJuXpvL3jjqKJnOCfdYEatw FwpxikG9ls7PV04zc3mWOFuCjWzMvThYOtFebiUmMOTeHmexe8CCSH4+f2KaZSa9IjDCIvItREZ J6TxsbImR1PiB+VEhXQ5a3Skk0OY00yttPP7DkGxwfW4QEtm2ju1daOU6fgof+UwT7wIQPcD7lp kakrpFwJFHRt8lJXMh2K/M+NgjeAkit03RxhZGI3lrsmg2xqxl+CUtYyYpuRN1xZzjcAAlud5sh XeJtIXdniE2XnebJB6K8hQaQZyv/14OBNh1z68fpeuhFCBExAeggt9crd6EI1f/hXEuAKGl44Ri V51TbzujUQHb+60SLHlILICd/iv5R6MT/lCo6JWPktpz1piAn+Y0OrQFLp9+cEfaOkN07wxycKS e36fdxFXQR X-Received: by 2002:a05:6a20:d527:b0:395:732:2c87 with SMTP id adf61e73a8af0-3982e2475fbmr1405418637.56.1772624867385; Wed, 04 Mar 2026 03:47:47 -0800 (PST) Received: from localhost ([2400:8902:e002:de08:5754:7dac:85df:935a]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7387271048sm182776a12.27.2026.03.04.03.47.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2026 03:47:47 -0800 (PST) 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 v2] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP Date: Wed, 4 Mar 2026 19:47:27 +0800 Message-ID: <20260304114727.384416-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. Signed-off-by: WANG Rui --- 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 #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 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 (align_to_pmd(&cmds[i]) && p_align < PMD_SIZE) + p_align =3D PMD_SIZE; + alignment =3D max(alignment, p_align); } } --=20 2.53.0