From: Yeoreum Yun <yeoreum.yun@arm.com>
To: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, loongarch@lists.linux.dev,
linux-mips@vger.kernel.org, linux-arch@vger.kernel.org,
kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org,
x86@kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com,
linux-csky@vger.kernel.org, linux-m68k@lists.linux-m68k.org,
linux-openrisc@vger.kernel.org
Cc: david@kernel.org, linux@armlinux.org.uk,
akpm@linux-foundation.org, ankur.a.arora@oracle.com,
rppt@kernel.org, linmag7@gmail.com, chleroy@kernel.org,
klarasmodin@gmail.com, chenhuacai@kernel.org, kernel@xen0n.name,
kas@kernel.org, zhangtianyang@loongson.cn, wangyuli@aosc.io,
tsbogend@alpha.franken.de, ljs@kernel.org, jgg@ziepe.ca,
catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de,
ryan.roberts@arm.com, pasha.tatashin@soleen.com,
rmclure@linux.ibm.com, baolin.wang@linux.alibaba.com,
tj@kernel.org, kevin.brodsky@arm.com, anup@brainfault.org,
atish.patra@linux.dev, pjw@kernel.org, palmer@dabbelt.com,
aou@eecs.berkeley.edu, alex@ghiti.fr,
dave.hansen@linux.intel.com, luto@kernel.org,
peterz@infradead.org, tglx@kernel.org, mingo@redhat.com,
bp@alien8.de, hpa@zytor.com, hannes@cmpxchg.org,
mhocko@kernel.org, qi.zheng@linux.dev, shakeel.butt@linux.dev,
kasong@tencent.com, baohua@kernel.org, axelrasmussen@google.com,
yuanchu@google.com, weixugc@google.com, ryabinin.a.a@gmail.com,
glider@google.com, andreyknvl@gmail.com, dvyukov@google.com,
vincenzo.frascino@arm.com, anshuman.khandual@arm.com,
yang@os.amperecomputing.com, chaitanyas.prakash@arm.com,
ardb@kernel.org, guoren@kernel.org, yang.li85200@gmail.com,
viro@zeniv.linux.org.uk, dinguyen@kernel.org,
schuster.simon@siemens-energy.com, wangruikang@iscas.ac.cn,
junhui.liu@pigmoral.tech, muchun.song@linux.dev,
vishal.moola@gmail.com, namcao@linutronix.de, pavel@kernel.org,
djbw@kernel.org, yu-cheng.yu@intel.com, baolu.lu@linux.intel.com,
Jonathan.Cameron@huawei.com, coxu@redhat.com,
andreas@gaisler.com, liam@infradead.org, vbabka@kernel.org,
surenb@google.com, mhocko@suse.com, geert@linux-m68k.org,
shorne@gmail.com, jonas@southpole.se,
stefan.kristiansson@saunalahti.fi,
Yeoreum Yun <yeoreum.yun@arm.com>
Subject: [RFC PATCH 31/34] mm/pgtable: optimize pmdp_get() and friends for folded pagetable levels
Date: Mon, 13 Jul 2026 14:56:10 +0100 [thread overview]
Message-ID: <20260713135614.1618183-32-yeoreum.yun@arm.com> (raw)
In-Reply-To: <20260713135614.1618183-1-yeoreum.yun@arm.com>
From: "David Hildenbrand (Arm)" <david@kernel.org>
Using pmdp_get() and friends in common code on a kernel config with
folded page tables is suboptimal: they default to a READ_ONCE(), forcing
the compiler to actually read that value even though it will not actually
be used afterwards.
This was recently reported by Christophe Leroy [1] and block conversion
of more common code to pmdp_get() and friends.
(using pgdp_get() as one example)
Most of the code ignores the result from pgdp_get() on configs with
folded page tables entirely, as we hardcode:
pgd_present()==1 && pgd_leaf()==false
Common code will just treat it as a "this is a page table" and call
p4d_offset() or p4d_offset_lockless() for the next lower level, where
we just ignore the obtained pgdp_get() result entirely.
So we can just return a dummy value and avoid any memory reads.
There is a catch, though:
1) If code calls pgd_val() and somehow relies on the data, it would now
see dummy values. The code really must be aware of folded page table
levels. Fortunately, code usually ignores pgd_val() completely for
page tables (with ptdump being one exception when calculating
effective permissions). We checked + fixed the x86 ptdump mechanism.
2) If code passes the pgd_t to a function that would work on the result,
it would now see dummy values. The only concern is really passing
the pgd_t on the stack as a pointer to p4d_offset(). Most code that
would do that, should actually use p4d_offset_lockless(), which
handles this properly. We checked + fixed problematic instances.
As an example, this is the generated code for perf_get_page_size() with
PGTABLE_LEVELS=3 on arm64:
Before:
00000000000052a0 <perf_get_page_size>:
...
52dc: d53b4234 mrs x20, DAIF
52e0: d50343df msr DAIFSet, #0x3
...
52fc: d35e9a69 ubfx x9, x19, #30, #9 /* pud_offset_lockless() */
5300: f9403508 ldr x8, [x8, #0x68]
5304: f869790a ldr x10, [x8, x9, lsl #3] /* pudp_get() */
5308: f90007ea str x10, [sp, #0x8]
530c: f8697908 ldr x8, [x8, x9, lsl #3] /* pudp_get() */
...
5360: 90000009 adrp x9, 0x5000 <perf_prepare_sample+0x548>
5364: 92746908 and x8, x8, #0x7ffffff000
5368: d3557675 ubfx x21, x19, #21, #9 /* pmd_offset_lockless() */
...
5394: f8757ac8 ldr x8, [x22, x21, lsl #3] /* pmdp_get() */
After:
0000000000052a0 <perf_get_page_size>:
...
52dc: d53b4234 mrs x20, DAIF
52e0: d50343df msr DAIFSet, #0x3
... /* no pud_offset_lockless() and pudp_get() */
5318: 90000009 adrp x9, 0x5000 <perf_prepare_sample+0x548>
531c: 92746908 and x8, x8, #0x7ffffff000
5320: d3557675 ubfx x21, x19, #21, #9 /* pmd_offset_lockless() */
...
5334: f8757ac8 ldr x8, [x22, x21, lsl #3] /* pmdp_get() */
[1] https://lore.kernel.org/all/0019d675-ce3d-4a5c-89ed-f126c45145c9@kernel.org/
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
---
include/asm-generic/pgtable-nop4d.h | 8 ++++++++
include/asm-generic/pgtable-nopmd.h | 8 ++++++++
include/asm-generic/pgtable-nopud.h | 8 ++++++++
3 files changed, 24 insertions(+)
diff --git a/include/asm-generic/pgtable-nop4d.h b/include/asm-generic/pgtable-nop4d.h
index 25c26a587b512..84a529df27ee3 100644
--- a/include/asm-generic/pgtable-nop4d.h
+++ b/include/asm-generic/pgtable-nop4d.h
@@ -31,6 +31,14 @@ static inline bool pgd_leaf(pgd_t pgd) { return false; }
#define set_pgd(pgdptr, pgdval) BUILD_BUG()
+static inline pgd_t pgdp_get(pgd_t *p4dp)
+{
+ pgd_t dummy = { 0 };
+
+ return dummy;
+}
+#define pgdp_get pgdp_get
+
static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
{
return (p4d_t *)pgd;
diff --git a/include/asm-generic/pgtable-nopmd.h b/include/asm-generic/pgtable-nopmd.h
index 75650fa9111cd..96f3fccd22fda 100644
--- a/include/asm-generic/pgtable-nopmd.h
+++ b/include/asm-generic/pgtable-nopmd.h
@@ -40,6 +40,14 @@ static inline void pud_clear(pud_t *pud) { }
#define set_pud(pudptr, pudval) BUILD_BUG()
+static inline pud_t pudp_get(pud_t *pudp)
+{
+ pud_t dummy = { 0 };
+
+ return dummy;
+}
+#define pudp_get pudp_get
+
static inline pmd_t * pmd_offset(pud_t * pud, unsigned long address)
{
return (pmd_t *)pud;
diff --git a/include/asm-generic/pgtable-nopud.h b/include/asm-generic/pgtable-nopud.h
index b592fff18887a..faa0233bcde80 100644
--- a/include/asm-generic/pgtable-nopud.h
+++ b/include/asm-generic/pgtable-nopud.h
@@ -38,6 +38,14 @@ static inline bool p4d_leaf(p4d_t p4d) { return false; }
#define set_p4d(p4dptr, p4dval) BUILD_BUG()
+static inline p4d_t p4dp_get(p4d_t *p4dp)
+{
+ p4d_t dummy = { 0 };
+
+ return dummy;
+}
+#define p4dp_get p4dp_get
+
static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
{
return (pud_t *)p4d;
--
LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7}
next prev parent reply other threads:[~2026-07-13 14:02 UTC|newest]
Thread overview: 84+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-13 13:55 [RFC PATCH 00/34] mm: optimize unnecessary loads due to ptep_get() and friends out Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 01/34] ARM: mm: make nommu pgd_t a scalar Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 02/34] ARM: mm: make 2-level " Yeoreum Yun
2026-07-13 15:38 ` Arnd Bergmann
2026-07-13 16:04 ` David Hildenbrand (Arm)
2026-07-13 16:26 ` David Hildenbrand (Arm)
2026-07-14 10:26 ` Pedro Falcato
2026-07-14 10:54 ` David Hildenbrand (Arm)
2026-07-14 11:55 ` Arnd Bergmann
2026-07-14 13:34 ` David Laight
2026-07-14 15:06 ` Arnd Bergmann
2026-07-14 21:45 ` David Laight
2026-07-15 6:11 ` Arnd Bergmann
2026-07-16 19:04 ` Yeoreum Yun
2026-07-17 6:16 ` Arnd Bergmann
2026-07-17 7:55 ` Yeoreum Yun
2026-07-14 12:34 ` David Laight
2026-07-14 13:03 ` David Hildenbrand (Arm)
2026-07-15 6:15 ` Arnd Bergmann
2026-07-15 9:41 ` David Hildenbrand (Arm)
2026-07-16 10:56 ` Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 03/34] ARM: mm: remove custom pgdp_get() Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 04/34] LoongArch: mm: define pud_leaf() only when PUD exists Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 05/34] MIPS: " Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 06/34] mm/pgtable: define (pgd|p4d|pud)_leaf() for folded page tables Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 07/34] mm/pgtable: define (pgd|p4d|pud)_offset_lockless() " Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 08/34] loongarch: kvm: remove stack copy address of pXd in pXd_offset() Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 09/34] riscv: " Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 10/34] x86: mm: carve out the generic compile-time folded pgtable case in effective_prot() Yeoreum Yun
2026-07-13 14:19 ` Dave Hansen
2026-07-13 14:41 ` David Hildenbrand (Arm)
2026-07-13 14:55 ` Dave Hansen
2026-07-13 15:02 ` David Hildenbrand (Arm)
2026-07-13 17:57 ` Dave Hansen
2026-07-13 19:49 ` David Hildenbrand (Arm)
2026-07-13 20:27 ` Dave Hansen
2026-07-13 21:17 ` David Hildenbrand (Arm)
2026-07-13 21:40 ` Dave Hansen
2026-07-14 10:50 ` David Hildenbrand (Arm)
2026-07-14 11:40 ` Yeoreum Yun
2026-07-14 14:35 ` Dave Hansen
2026-07-14 16:08 ` Yeoreum Yun
2026-07-14 16:31 ` David Hildenbrand (Arm)
2026-07-14 18:28 ` Yeoreum Yun
2026-07-15 9:43 ` David Hildenbrand (Arm)
2026-07-14 16:51 ` Yeoreum Yun
2026-07-14 17:29 ` David Hildenbrand (Arm)
2026-07-14 17:58 ` Yeoreum Yun
2026-07-15 9:44 ` David Hildenbrand (Arm)
2026-07-16 8:47 ` Yeoreum Yun
2026-07-15 7:45 ` Geert Uytterhoeven
2026-07-15 9:41 ` Yeoreum Yun
2026-07-15 10:34 ` Geert Uytterhoeven
2026-07-16 7:49 ` Yeoreum Yun
2026-07-13 14:48 ` Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 11/34] mm: vmscan: remove stack copy address of pud pass in wallk_pud_range() Yeoreum Yun
2026-07-15 13:44 ` Usama Anjum
2026-07-13 13:55 ` [RFC PATCH 12/34] arm64: mm: use proper set_pXd() for generic compile-time folded patable in kasan_early_init() Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 13/34] arm64: mm: define pud_set_huge() when __PGTABLE_PMD_FOLDED not defined Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 14/34] csky: mm: use proper set_pXd() for generic compile-time folded patable in vmalloc_fault() Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 15/34] mips: mm: use proper set_pXd() for generic compile-time folded patable in vmalloc_fault path Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 16/34] nios2: " Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 17/34] riscv: mm: use proper set_pXd() for generic compile-time folded patable in vmalloc_fault() Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 18/34] riscv: mm: use proper set_pXd() for generic compile-time folded patable in setup_vm_final() Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 19/34] x86: power: use proper set_pXd() for generic compile-time folded patable in resume_one_md_table_init() Yeoreum Yun
2026-07-13 14:38 ` Dave Hansen
2026-07-13 14:59 ` David Hildenbrand (Arm)
2026-07-13 15:01 ` Yeoreum Yun
2026-07-13 13:55 ` [RFC PATCH 20/34] x86: kexec: use proper set_pXd() for generic compile-time folded patable in machine_kexec_page_table_set_one() Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 21/34] x86: platform: use proper set_pXd() for generic compile-time folded patable in setup_olpc_ofw_pgd() Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 22/34] x86: mm: use proper set_pXd() for generic compile-time folded patable in one_md_table_init() Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 23/34] x86: mm: skip pud setup when using generic compile-time folded pagetable Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 24/34] x86: mm: call try_to_free_pmd_page() when CONFIG_PGTABLE_LEVELS > 2 Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 25/34] x86: mm: remove usage of pgd_page_vaddr() for CONFIG_x86_PAE Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 26/34] x86: mm: define pudp_set_access_flags() when CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD is enabled only Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 27/34] m68k: mm: remove usage of pgd_page_vaddr() for CONFIG_PGTABLE_LEVELS=3 Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 28/34] arm: mm: use proper pgtable APIs for generic compile-time folded patable in kasan_init() Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 29/34] mm/pgtable: disallow calling folded set_pgd/set_p4d/set_pud Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 30/34] mm/pgtable: disallow calling folded (pgd|p4d|pud)_page, pgd_page_vaddr() and (p4d|pud)_pgtable Yeoreum Yun
2026-07-13 13:56 ` Yeoreum Yun [this message]
2026-07-13 13:56 ` [RFC PATCH 32/34] openrisc/pgtable: drop __pmd_offset() Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 33/34] mm/pgtable: catch abuse of folded dummy pgd_t/p4d_t/pud_t Yeoreum Yun
2026-07-13 13:56 ` [RFC PATCH 34/34] arm64: pgtable: convert pte_present() from macro to static inline Yeoreum Yun
2026-07-13 14:08 ` [RFC PATCH 00/34] mm: optimize unnecessary loads due to ptep_get() and friends out David Hildenbrand (Arm)
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=20260713135614.1618183-32-yeoreum.yun@arm.com \
--to=yeoreum.yun@arm.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=alex@ghiti.fr \
--cc=andreas@gaisler.com \
--cc=andreyknvl@gmail.com \
--cc=ankur.a.arora@oracle.com \
--cc=anshuman.khandual@arm.com \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=arnd@arndb.de \
--cc=atish.patra@linux.dev \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baolu.lu@linux.intel.com \
--cc=bp@alien8.de \
--cc=catalin.marinas@arm.com \
--cc=chaitanyas.prakash@arm.com \
--cc=chenhuacai@kernel.org \
--cc=chleroy@kernel.org \
--cc=coxu@redhat.com \
--cc=dave.hansen@linux.intel.com \
--cc=david@kernel.org \
--cc=dinguyen@kernel.org \
--cc=djbw@kernel.org \
--cc=dvyukov@google.com \
--cc=geert@linux-m68k.org \
--cc=glider@google.com \
--cc=guoren@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hpa@zytor.com \
--cc=jgg@ziepe.ca \
--cc=jonas@southpole.se \
--cc=junhui.liu@pigmoral.tech \
--cc=kas@kernel.org \
--cc=kasan-dev@googlegroups.com \
--cc=kasong@tencent.com \
--cc=kernel@xen0n.name \
--cc=kevin.brodsky@arm.com \
--cc=klarasmodin@gmail.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=liam@infradead.org \
--cc=linmag7@gmail.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-csky@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-m68k@lists.linux-m68k.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-openrisc@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux@armlinux.org.uk \
--cc=ljs@kernel.org \
--cc=loongarch@lists.linux.dev \
--cc=luto@kernel.org \
--cc=mhocko@kernel.org \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=muchun.song@linux.dev \
--cc=namcao@linutronix.de \
--cc=palmer@dabbelt.com \
--cc=pasha.tatashin@soleen.com \
--cc=pavel@kernel.org \
--cc=peterz@infradead.org \
--cc=pjw@kernel.org \
--cc=qi.zheng@linux.dev \
--cc=rmclure@linux.ibm.com \
--cc=rppt@kernel.org \
--cc=ryabinin.a.a@gmail.com \
--cc=ryan.roberts@arm.com \
--cc=schuster.simon@siemens-energy.com \
--cc=shakeel.butt@linux.dev \
--cc=shorne@gmail.com \
--cc=stefan.kristiansson@saunalahti.fi \
--cc=surenb@google.com \
--cc=tglx@kernel.org \
--cc=tj@kernel.org \
--cc=tsbogend@alpha.franken.de \
--cc=vbabka@kernel.org \
--cc=vincenzo.frascino@arm.com \
--cc=viro@zeniv.linux.org.uk \
--cc=vishal.moola@gmail.com \
--cc=wangruikang@iscas.ac.cn \
--cc=wangyuli@aosc.io \
--cc=weixugc@google.com \
--cc=will@kernel.org \
--cc=x86@kernel.org \
--cc=yang.li85200@gmail.com \
--cc=yang@os.amperecomputing.com \
--cc=yu-cheng.yu@intel.com \
--cc=yuanchu@google.com \
--cc=zhangtianyang@loongson.cn \
/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