From nobody Thu Apr 9 08:10:19 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 6E9403B9606 for ; Tue, 10 Mar 2026 03:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773112358; cv=none; b=tkH1Gp0yBsAemBTdNH9f2oxo6k2ynvbivji6jHUXQxcTSn7vyOVA4wNpXM31LRf6mFGlPe9pXMYCXZrOf2/KG009wNIMcb6xmq9Q/OLP3HvElPy0w2K0dx1GtdXQHGjNDc0iE9J3ZsipOmTl6mr9o07/1eZbegLucce0DTLF77A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773112358; c=relaxed/simple; bh=YPiZVfutGHLcCD+o5iVeksaNVWkh+aaGWV9f2eUOkaI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Drb3j85MqMSnS87x9gEObj/yniqfT6xDa9u+zU7uLNOKJwrARqMiTTK/MXBOz4qju31mU99UtECZCYQkuwtB7x9vPLtYKfCuPyWu9eZyBZ59eIm6ZaUiq/p/XI1Cn2vQG5XnTWIjJxHFeu56CdmesZKtL+8LFHBKk6c5o/kKLtg= 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=mVNW5eGK; arc=none smtp.client-ip=209.85.210.175 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="mVNW5eGK" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-829afe24fb5so2146597b3a.0 for ; Mon, 09 Mar 2026 20:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hev-cc.20230601.gappssmtp.com; s=20230601; t=1773112357; x=1773717157; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8Gnfc04EYdLBOgTA22D3ZA5OANJFWm2SEibGG/QoA0M=; b=mVNW5eGK2CgiGYnSDn3EwcAMpafyN/YXcSQxZZAqlWLPVxJPHaHbIGyQ20TIjySzW/ cpcak719IVrt+zqy6dx6RwXuqxVsrk04AGu0tUiGm/bfG8xzM/4sYJf74bx5P1yNoCYp 50nfPaDqAIpoFv13C7/nT3UqwFO7SZQXgXEyBs0tJxSagb9CPEfwSm/G+R5VX4jcOe4H QsTYfJRSvS9IMvq+y3z8KIpbNXNtFEugZn5yBzJSJ4TUDzLfn6DKBC2V8nRyMd/gubFA w4OdLl50/Ztdjbm8NsuVwUYKYKlf0HDH+ECv86soKV+UrNaegWroP7d4QhFiFFYuSSlZ S4fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773112357; x=1773717157; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=8Gnfc04EYdLBOgTA22D3ZA5OANJFWm2SEibGG/QoA0M=; b=tM07dWBZ098uSW2eMSog+arrEHuAanPeM1P5Lkm8PllIB0bk5sFLK2KByZSgYG5Jry B+didJNSA/s7Jli+Cax1OcwY+ZOgba26s+OyFzfUWXwpRQLksVD0vXLFQV8nYeKkZu9X kuOWEuCNFzEjZQFrHa6N1rDu7eUzhRI/m37FHAW+jyOKHy+Lihf7wTAQ9W8PK+1JH5fl ZdNMZ/GDzrP9z+4a/lbpCrVxDyU7GpMEfHDXE9wTg9YWPW58T1xM6LhxTiHOKyMp+SIp HnKJabZ5o0L2qzt5/65HioTKXTo8kpKOleZXYKlXl0SrcTcJsZBeJuXymZp13lmPnD5L +qmQ== X-Forwarded-Encrypted: i=1; AJvYcCXQsg6td2V/UOcVkXYJLR+NA+6tQz3HtpOCsDv7ejGXo159qtbDKkAd0qbH2s37i6KYVr1Ok3BcGPZWiCI=@vger.kernel.org X-Gm-Message-State: AOJu0Ywwn9qxSDKBY2YH7+mKPYfOxENlQhneiIYgkOVcXnQw4ycxElKU sO5QzHcEmrgkwXn1i36dV5necBVCwutIiupdpCWfK+yHd3OAQWU1hgPZ063zoUv4FvOpZtenKN5 4NZI33vA= X-Gm-Gg: ATEYQzzH0lI6u3S1bvrOCidSM5ULSNBDfkRLy1FaFhfAzve22K9wRr2Q1yhdnV1yFvt kMemOgX6drTkG2MulM/c7KJmQFE2r0whwiC9rtMGXSpFZyU0KTLepCyb0Hs8US/diHfVnn85WxC hMlU+6jSnTnvHlBAcSkzJUN7UIDOC9HYWHUawhzh2F4UMiTfwpkdKGOOukNDTRnAc2peDCVxtkr uJeVNOYz1YvpbGXuqVIpUawDsGZuctBkn0ej56ywuRBOFraFfyhrq/18dwv76rMjM2IK53ABB14 NiclHAOq7RoZdqY/I+iDJ5CTWwUKHtkhido6XVwqFQoZY8jIAg6oe4Mo0uA79Ph4BswzXkrzg7l NytsFEsoxmt52GxClUjHtCFRaKlEofYYZ1ZTV7sKpZO9FAsF2Z5SQP3YNfdmRX3ZcgckYzsPOmc OJsAhJhCLA X-Received: by 2002:a05:6a00:1a8c:b0:829:810e:8af8 with SMTP id d2e1a72fcca58-829a2dc4f99mr12713671b3a.22.1773112356834; Mon, 09 Mar 2026 20:12:36 -0700 (PDT) Received: from localhost ([2400:8902:e002:de08:5754:7dac:85df:935a]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-829a465c872sm14213691b3a.19.2026.03.09.20.12.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2026 20:12:36 -0700 (PDT) From: WANG Rui To: Alexander Viro , Andrew Morton , Baolin Wang , Barry Song , Christian Brauner , David Hildenbrand , Dev Jain , Jan Kara , Kees Cook , Lance Yang , "Liam R. Howlett" , Lorenzo Stoakes , Matthew Wilcox , Nico Pache , Ryan Roberts , Zi Yan Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, WANG Rui Subject: [PATCH v4 1/2] huge_mm: add stubs for THP-disabled configs Date: Tue, 10 Mar 2026 11:11:37 +0800 Message-ID: <20260310031138.509730-2-r@hev.cc> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260310031138.509730-1-r@hev.cc> References: <20260310031138.509730-1-r@hev.cc> 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" hugepage_global_enabled() and hugepage_global_always() only exist when CONFIG_TRANSPARENT_HUGEPAGE is set. Add inline stubs that return false to let code compile when THP is disabled. Signed-off-by: WANG Rui --- include/linux/huge_mm.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index a4d9f964dfde..badeebd4ea98 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -570,6 +570,16 @@ void map_anon_folio_pmd_nopf(struct folio *folio, pmd_= t *pmd, =20 #else /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 +static inline bool hugepage_global_enabled(void) +{ + return false; +} + +static inline bool hugepage_global_always(void) +{ + return false; +} + static inline bool folio_test_pmd_mappable(struct folio *folio) { return false; --=20 2.53.0 From nobody Thu Apr 9 08:10:19 2026 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 5837238B7C0 for ; Tue, 10 Mar 2026 03:12:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773112363; cv=none; b=XnVNu0QzTFho1Elt9w0QYa6K2DFOmgvodL6lxdLg7q4o80NZRteDk2qELmNc3Simw0HAsRdv+rKlfHAYsBq4E0qTS4gNdGNJlEDJiihYUl5gHF7hJc0536njdDls7+fOy7bpvreCPYM65YMa2ezmmP7AMY+LQLdoy3nPj1nhYeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773112363; c=relaxed/simple; bh=857ZAe0mduS9VFMFqaKfW/expzuU5bBWUgJR+Mpgl8E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CNyLviLTT2urzd+/QEAESP3oOhV6RbcWBfTHnJjeD9C2nyZ5OYvmijShyTbgzD82muOVH2XhkgC7eCBsx7mkBb7nWAae+YziaV+7BEQud7Q5dqqjTEkiZ3Pv4SXU6ROBu2K0DGz0LX1AoWJoPt4O0Vw0W4yY0wPxoDkt98z1FW8= 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=o+x3e9IS; arc=none smtp.client-ip=209.85.210.179 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="o+x3e9IS" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-8299c75f730so2222050b3a.0 for ; Mon, 09 Mar 2026 20:12:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hev-cc.20230601.gappssmtp.com; s=20230601; t=1773112361; x=1773717161; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PGbxRmtnq4j2JJLS66I6xJ9tyvKo4E5pO4kbP3Ck6zI=; b=o+x3e9ISrmHQWlWO9mgn9Q8LZeBm0lbBDNsZUSLR9UUaMnobKat8UL/CprUMrHDWfd xja6OPC+joHrBPdErZjDjD+CI+BB4qZv390jVOI7elo4WeTT2/imc55rLLLdCMYaAXd6 /w0zxLSGJyOG0tmDi5W5DVYVL9eZCcAV5wpes1NzGgWn8xkN1I0UNvl94EonMQ/icXBe CjiuzaX/Gf76CLNbm9+vyKKSBYwYZlY66V4wduv5psyoi+RWbifZ0gagA0X/kuCTPfjO WYOJOytYyhxqLmEeLsLcaItK/BRnawRqVwisK14WZdIIOvSgnubZZKUbU7govCgJiJ+G 5QPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773112361; x=1773717161; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=PGbxRmtnq4j2JJLS66I6xJ9tyvKo4E5pO4kbP3Ck6zI=; b=gyQCI3sJpWD5qeiU+Aco3LA/dqP+3NXNHf8bkkxRRHjYQKsJ5HFnWFIINjf271sigc k2wSYrYpNGHQ5EjTaG0S8I5YqNM322ddUVnNg49Mt5OZ3Vkhajn5+Ewnm0rEotrd/ImQ y5oD2a+sdq3Z3tLBzlYzPfeA/rRnkxIkkacB4dir7KLFfIojXqJommeCWMgrXr1hW8eO QFP+LNGgG9gzDLD761rX4+pTGb5uR6LZo19gc4mPR6q/1BGdTc5LGAwVInOBzNffYkUP 8EDSO/8PNOV3RlLMvbPUsBmiwbKcNSR2guIT7norQjBz9VEmQCmeqjSqHB6DhZq21R54 3xZQ== X-Forwarded-Encrypted: i=1; AJvYcCXUD+4bUmjdf3RtCJBHScJCj6FsNYE/MpDeZ3bXvTgxSxCaygCzK1VL9tvfZiDUkf2ufeiSYbSAuPjGY9w=@vger.kernel.org X-Gm-Message-State: AOJu0YxlJ5xrnKf9FaRTf6GROq2ZCPLMC3tNPQQAohRQ4LRsjSG+6VTE uHpFV8MFyiTLp9EdtX+XXJERENaddOe9QN/sH818a0V05vuDATh0ctfhi+BV3IFyALQ= X-Gm-Gg: ATEYQzy6FpiP8xT+fQZLOwQkgtUDWQXVvETCpN7INPZjpHhPWKAvAfwOuMdFe4/abQ9 oEbvlUQ4JDkUTqsRHEGypAQjgsKG/xmL2vyeva4imEqjTtmbIgRJ78yEsHi/+siYsFRhFZxPPxP uQbh0vaoYGiC81Q+4qy6Ktjgwp5X5kuXYGa7dGNfcsJbkY3bWWayvfm9vReHFlcbStUeZYrOKO/ xmMqHKddZAd1T6SpKz3zlIX9p5g0gNwjZJmwBuXWQaov4vDjztXUp0RkuBTsTBnA6UfWeek5pKt xMX5H9Ujy7Tg7bjptD9k9PMSVcTn7Uwn4eSqTRuwys20v4ZML2oi1Gpn9n+RWphuKclnCagNp87 OdpDwvw9PrLB32P3S9jBrqE9z19dTeG/sAmd6cInTWef9wrko/GgFDTkzMORiGE6hLygRjKbjmX RMflmWpcrL X-Received: by 2002:a05:6a00:9517:b0:829:cbc0:f0c1 with SMTP id d2e1a72fcca58-829e3fe0536mr1540396b3a.18.1773112360560; Mon, 09 Mar 2026 20:12:40 -0700 (PDT) Received: from localhost ([2400:8902:e002:de08:5754:7dac:85df:935a]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-829a465c872sm14213691b3a.19.2026.03.09.20.12.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2026 20:12:40 -0700 (PDT) From: WANG Rui To: Alexander Viro , Andrew Morton , Baolin Wang , Barry Song , Christian Brauner , David Hildenbrand , Dev Jain , Jan Kara , Kees Cook , Lance Yang , "Liam R. Howlett" , Lorenzo Stoakes , Matthew Wilcox , Nico Pache , Ryan Roberts , Zi Yan Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, WANG Rui Subject: [PATCH v4 2/2] binfmt_elf: Align eligible read-only PT_LOAD segments to PMD_SIZE for THP Date: Tue, 10 Mar 2026 11:11:38 +0800 Message-ID: <20260310031138.509730-3-r@hev.cc> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260310031138.509730-1-r@hev.cc> References: <20260310031138.509730-1-r@hev.cc> 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 --- 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