* [PATCH v2 1/9] mm/migrate: do not clear device private entry in set_pmd_migration_entry()
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 4:17 ` [PATCH v2 2/9] powerpc/book3s64/mm: don't count invalidated PMDs in page table check Zi Yan
` (7 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, stable, Balbir Singh,
Alistair Popple
A device private entry is never followed by GUP-fast, so there is no need
to clear the entry to avoid it. Furthermore, clearing the entry creates a
none PMD and a concurrent zap skips it. set_pmd_migration_entry() might not
hold a VMA lock nor a mmap lock. As a result:
1. When racing with munmap(), a "bad pmd" can appear, deposited page table
can be lost, and RSS stats can be off.
2. When racing with mremap(), the PMD is not moved, so the new address
faults and sees zeros instead of the original data.
Fix it by not clearing device private entry.
Fixes: 65edfda6f3f2e ("mm/rmap: extend rmap and migration support device-private entries")
Cc: stable@vger.kernel.org
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: David Hildenbrand <david@kernel.org>
To: Zi Yan <ziy@nvidia.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: "Liam R. Howlett" <liam@infradead.org>
Cc: Nico Pache <nico.pache@linux.dev>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Usama Arif <usama.arif@linux.dev>
Cc: Kiryl Shutsemau <kas@kernel.org>
Cc: Balbir Singh <balbirs@nvidia.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
---
mm/huge_memory.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 6305b4e2a6b68..91f3c7e7a044a 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -5223,7 +5223,12 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
} else {
softleaf_t old_entry;
- pmdval = pmdp_huge_get_and_clear(vma->vm_mm, address, pvmw->pmd);
+ /*
+ * The device private entry stays as is since GUP-fast
+ * never follows it and folio_try_share_anon_rmap_pmd()
+ * below never fails for it.
+ */
+ pmdval = pmdp_get(pvmw->pmd);
old_entry = softleaf_from_pmd(pmdval);
writable = softleaf_is_device_private_write(old_entry);
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* [PATCH v2 2/9] powerpc/book3s64/mm: don't count invalidated PMDs in page table check
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
2026-12-04 4:17 ` [PATCH v2 1/9] mm/migrate: do not clear device private entry in set_pmd_migration_entry() Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 4:17 ` [PATCH v2 3/9] riscv: mm: don't let GUP-fast follow entries without _PAGE_READ Zi Yan
` (6 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, stable, Madhavan Srinivasan,
Andrew Donnellan, Rohan McLure, Pasha Tatashin, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, linuxppc-dev
pmdp_invalidate() replaces a huge PMD with an invalidated one
(_PAGE_PRESENT cleared and _PAGE_INVALID set) and calls
page_table_check_pmd_clear() on the old entry. This decreases the page
table check map count. When the invalidated entry is later replaced with
set_pmd_at(), page_table_check_pmds_set() in set_pmd_at() decreases the map
count again because pmd_user_accessible_page() still counts it as a live
user mapping. The same happens when it is cleared with
pmdp_huge_get_and_clear(). Then the map count underflows and hits BUG_ON()
in page_table_check_clear().
Found by code inspection and confirmed in QEMU (pseries, POWER9, 64K
pages, CONFIG_PAGE_TABLE_CHECK_ENFORCED=y): MADV_COLD (before v7.4) or
mprotect() on an anonymous THP hits the BUG_ON() with both the radix and
the hash MMU.
Fix it by only counting entries that are hardware valid, so an
invalidated entry is skipped, as commit bdda2887cbf39 ("riscv: mm: exclude
invalid THP PMDs from page table check") did for riscv.
While at it, remove the migrate_vma_collect_huge_pmd() item from the
comment in pmdp_huge_get_and_clear(): set_pmd_migration_entry() no longer
clears device private PMDs.
Fixes: 641d47d4c963 ("powerpc/mm: support page table check")
Cc: stable@vger.kernel.org
To: Madhavan Srinivasan <maddy@linux.ibm.com>
To: Andrew Donnellan <andrew+kernel@donnellan.id.au>
To: Rohan McLure <rmclure@linux.ibm.com>
To: Pasha Tatashin <pasha.tatashin@soleen.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>
Cc: "Ritesh Harjani (IBM)" <ritesh.list@gmail.com>
Cc: Shrikanth Hegde <sshegde@linux.ibm.com>
Cc: linuxppc-dev@lists.ozlabs.org
Cc: linux-kernel@vger.kernel.org
Acked-by: Usama Arif <usama.arif@linux.dev>
Signed-off-by: Zi Yan <ziy@nvidia.com>
---
arch/powerpc/include/asm/book3s/64/pgtable.h | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h b/arch/powerpc/include/asm/book3s/64/pgtable.h
index dff8790a047db..d3808c3560703 100644
--- a/arch/powerpc/include/asm/book3s/64/pgtable.h
+++ b/arch/powerpc/include/asm/book3s/64/pgtable.h
@@ -1091,7 +1091,8 @@ static inline bool pmd_access_permitted(pmd_t pmd, bool write)
#define pmd_user_accessible_page pmd_user_accessible_page
static inline bool pmd_user_accessible_page(struct mm_struct *mm, unsigned long addr, pmd_t pmd)
{
- return pmd_leaf(pmd) && pte_user_accessible_page(mm, addr, pmd_pte(pmd));
+ return pmd_leaf(pmd) && pte_hw_valid(pmd_pte(pmd)) &&
+ pte_user_accessible_page(mm, addr, pmd_pte(pmd));
}
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -1331,11 +1332,9 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
/*
* Non-present PMDs can be migration entries or device-private THP
- * entries. This can happen at 2 places:
+ * entries. This can happen at:
* - When the address space is being unmapped zap_huge_pmd(), and we
* encounter non-present pmds.
- * - migrate_vma_collect_huge_pmd() could calls this during migration
- * of device-private pmd entries.
*/
if (!pmd_present(*pmdp)) {
old_pmd = READ_ONCE(*pmdp);
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* [PATCH v2 3/9] riscv: mm: don't let GUP-fast follow entries without _PAGE_READ
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
2026-12-04 4:17 ` [PATCH v2 1/9] mm/migrate: do not clear device private entry in set_pmd_migration_entry() Zi Yan
2026-12-04 4:17 ` [PATCH v2 2/9] powerpc/book3s64/mm: don't count invalidated PMDs in page table check Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 4:17 ` [PATCH v2 4/9] LoongArch: mm: don't let GUP-fast follow invalidated THP PMDs Zi Yan
` (5 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, stable, Sashiko, Jisheng Zhang,
Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
Zong Li, Deepak Gupta, Palmer Dabbelt, John Hubbard, Peter Xu,
linux-riscv, Jason Gunthorpe
riscv maps PROT_EXEC alone as PAGE_EXEC, which has
_PAGE_EXEC but not _PAGE_READ, and a writable shadow stack entry is
XWR = 010, which has _PAGE_WRITE but not _PAGE_READ. riscv uses the
generic pte_access_permitted(), pmd_access_permitted() and
pud_access_permitted(), which only check that the entry is present and,
for a write, writable. So GUP-fast follows both kinds of entries:
- a read, e.g. from vmsplice() or an O_DIRECT write, can read an
execute-only page, which userspace cannot load from;
- a write, e.g. from an O_DIRECT read, can modify a shadow stack page,
as pte_write() is true for it.
Slow GUP rejects both: check_vma_flags() fails a read from a VMA without
VM_READ and a write to a VM_SHADOW_STACK VMA, unless FOLL_FORCE is set.
Fix it by requiring both _PAGE_PRESENT and _PAGE_READ in
pte_access_permitted(), and using it for pmd_access_permitted() and
pud_access_permitted() too.
Requiring _PAGE_PRESENT also stops GUP-fast from following a THP PMD
invalidated by pmdp_invalidate(), whose pmd_present() is still true.
Code that invalidates a PMD and then decides, from the folio refcount,
whether it may drop the mapping relies on that. x86, arm64, and powerpc
do the same thing.
Fixes: 3f910b7a522e ("riscv: enable HAVE_FAST_GUP if MMU")
Fixes: c68c2ef9d641 ("riscv/mm: teach pte_mkwrite to manufacture shadow stack PTEs")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=2
Reviewed-by: Jisheng Zhang <jszhang@kernel.org>
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: Paul Walmsley <pjw@kernel.org>
To: Palmer Dabbelt <palmer@dabbelt.com>
To: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Jisheng Zhang <jszhang@kernel.org>
Cc: Zong Li <zong.li@sifive.com>
Cc: Deepak Gupta <debug@rivosinc.com>
Cc: Palmer Dabbelt <palmer@rivosinc.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: linux-riscv@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
---
arch/riscv/include/asm/pgtable.h | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 07ccacd51ab54..dcbcfa38a27c2 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -373,6 +373,21 @@ static inline int pte_write(pte_t pte)
return pte_val(pte) & _PAGE_WRITE;
}
+/*
+ * GUP-fast only follows entries that are present and have _PAGE_READ,
+ * and leaves the rest to slow GUP, which checks the VMA: an execute-only
+ * entry and a writable shadow stack entry (XWR = 010) lack _PAGE_READ, a
+ * PROT_NONE or invalidated entry lacks _PAGE_PRESENT. Any other writable
+ * entry is readable too.
+ */
+#define pte_access_permitted pte_access_permitted
+static inline bool pte_access_permitted(pte_t pte, bool write)
+{
+ const unsigned long need = _PAGE_PRESENT | _PAGE_READ;
+
+ return (pte_val(pte) & need) == need && (!write || pte_write(pte));
+}
+
static inline int pte_exec(pte_t pte)
{
return pte_val(pte) & _PAGE_EXEC;
@@ -817,12 +832,24 @@ static inline int pmd_write(pmd_t pmd)
return pte_write(pmd_pte(pmd));
}
+#define pmd_access_permitted pmd_access_permitted
+static inline bool pmd_access_permitted(pmd_t pmd, bool write)
+{
+ return pte_access_permitted(pmd_pte(pmd), write);
+}
+
#define pud_write pud_write
static inline int pud_write(pud_t pud)
{
return pte_write(pud_pte(pud));
}
+#define pud_access_permitted pud_access_permitted
+static inline bool pud_access_permitted(pud_t pud, bool write)
+{
+ return pte_access_permitted(pud_pte(pud), write);
+}
+
#define pmd_dirty pmd_dirty
static inline int pmd_dirty(pmd_t pmd)
{
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* [PATCH v2 4/9] LoongArch: mm: don't let GUP-fast follow invalidated THP PMDs
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
` (2 preceding siblings ...)
2026-12-04 4:17 ` [PATCH v2 3/9] riscv: mm: don't let GUP-fast follow entries without _PAGE_READ Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 4:17 ` [PATCH v2 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate() Zi Yan
` (4 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, Huacai Chen, WANG Xuerui,
John Hubbard, Peter Xu, loongarch, Jason Gunthorpe
pmdp_invalidate() does not change a PMD to make pmd_present() returns
false. So the generic pmd_access_permitted() returns true for an
invalidated PMD. This allows GUP-fast to grab a reference on the folio.
Code that invalidates a PMD and then decides whether it may drop the
mapping relies on GUP-fast backing off from an invalidated PMD. x86, arm64
and powerpc already reject invalidated PMDs in pmd_access_permitted().
Provide a LoongArch pmd_access_permitted() that requires _PAGE_PRESENT,
so invalidated (and PROT_NONE) PMDs are not followed by GUP-fast.
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: Huacai Chen <chenhuacai@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: loongarch@lists.linux.dev
Cc: linux-kernel@vger.kernel.org
---
arch/loongarch/include/asm/pgtable.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index 9a62b87c246de..ac2178fad0952 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -522,6 +522,12 @@ static inline int pmd_write(pmd_t pmd)
return !!(pmd_val(pmd) & _PAGE_WRITE);
}
+#define pmd_access_permitted pmd_access_permitted
+static inline bool pmd_access_permitted(pmd_t pmd, bool write)
+{
+ return (pmd_val(pmd) & _PAGE_PRESENT) && (!write || pmd_write(pmd));
+}
+
static inline pmd_t pmd_mkwrite_novma(pmd_t pmd)
{
pmd_val(pmd) |= _PAGE_WRITE;
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* [PATCH v2 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate()
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
` (3 preceding siblings ...)
2026-12-04 4:17 ` [PATCH v2 4/9] LoongArch: mm: don't let GUP-fast follow invalidated THP PMDs Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 12:31 ` Alexander Gordeev
2026-12-04 4:17 ` [PATCH v2 6/9] sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate() Zi Yan
` (3 subsequent siblings)
8 siblings, 1 reply; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, Alexander Gordeev,
Gerald Schaefer, Heiko Carstens, Vasily Gorbik,
Christian Borntraeger, Sven Schnelle, John Hubbard, Peter Xu,
linux-s390, Jason Gunthorpe
pmdp_invalidate() sets _SEGMENT_ENTRY_INVALID, but that bit alone does
not make the entry pmd_protnone(). Also clear the _SEGMENT_ENTRY_READ
bit to achieve it, so that GUP-fast can no longer grab a reference on the
folio mapped by the invalidated pmd.
Code can invalidate a PMD and then based on folio refcount decides
whether it may drop the mapping. That relies on GUP-fast backing off
from an invalidated PMD, as it does on x86, arm64, and powerpc.
For example, lazyfree THP discard is about to use pmdp_invalidate() to
prevent GUP-fast before dropping a lazyfree folio.
With CONFIG_PAGE_TABLE_CHECK, pmdp_invalidate() already clears
_SEGMENT_ENTRY_READ, which makes the entry pmd_protnone(), so GUP-fast
does not follow it. Do that unconditionally. As noted in commit
7b4dde5e40ad6 ("s390/pgtable: Add s390 support for page table check"),
an invalidated PMD that is pmd_protnone() is fine for the remaining
pmd_protnone() users, which run under the page table lock or are
lockless walkers that should not follow it anyway.
To: Alexander Gordeev <agordeev@linux.ibm.com>
To: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
To: Heiko Carstens <hca@linux.ibm.com>
To: Vasily Gorbik <gor@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: linux-s390@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Reviewed-by: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Signed-off-by: Zi Yan <ziy@nvidia.com>
---
arch/s390/include/asm/pgtable.h | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/arch/s390/include/asm/pgtable.h b/arch/s390/include/asm/pgtable.h
index 2d5c2ab06de98..fe1ba746203ab 100644
--- a/arch/s390/include/asm/pgtable.h
+++ b/arch/s390/include/asm/pgtable.h
@@ -1781,10 +1781,14 @@ static inline pmd_t pmdp_invalidate(struct vm_area_struct *vma,
pmd_t pmd = *pmdp;
VM_WARN_ON_ONCE(!pmd_present(pmd));
+ /*
+ * _SEGMENT_ENTRY_INVALID alone also marks an old (not young) entry,
+ * which lockless walkers like GUP-fast may follow. Clear
+ * _SEGMENT_ENTRY_READ too, so the invalidated entry is pmd_protnone()
+ * and is not followed, and page table check does not count it.
+ */
pmd = set_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_INVALID));
-#ifdef CONFIG_PAGE_TABLE_CHECK
pmd = clear_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_READ));
-#endif
page_table_check_pmd_set(vma->vm_mm, addr, pmdp, pmd);
pmd = pmdp_xchg_direct(vma->vm_mm, addr, pmdp, pmd);
return pmd;
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH v2 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate()
2026-12-04 4:17 ` [PATCH v2 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate() Zi Yan
@ 2026-12-04 12:31 ` Alexander Gordeev
0 siblings, 0 replies; 17+ messages in thread
From: Alexander Gordeev @ 2026-12-04 12:31 UTC (permalink / raw)
To: Zi Yan
Cc: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton, linux-mm,
linux-kernel, Gerald Schaefer, Heiko Carstens, Vasily Gorbik,
Christian Borntraeger, Sven Schnelle, John Hubbard, Peter Xu,
linux-s390, Jason Gunthorpe
On Thu, Dec 03, 2026 at 11:17:53PM -0500, Zi Yan wrote:
> pmdp_invalidate() sets _SEGMENT_ENTRY_INVALID, but that bit alone does
> not make the entry pmd_protnone(). Also clear the _SEGMENT_ENTRY_READ
> bit to achieve it, so that GUP-fast can no longer grab a reference on the
> folio mapped by the invalidated pmd.
>
> Code can invalidate a PMD and then based on folio refcount decides
> whether it may drop the mapping. That relies on GUP-fast backing off
> from an invalidated PMD, as it does on x86, arm64, and powerpc.
> For example, lazyfree THP discard is about to use pmdp_invalidate() to
> prevent GUP-fast before dropping a lazyfree folio.
>
> With CONFIG_PAGE_TABLE_CHECK, pmdp_invalidate() already clears
> _SEGMENT_ENTRY_READ, which makes the entry pmd_protnone(), so GUP-fast
> does not follow it. Do that unconditionally. As noted in commit
> 7b4dde5e40ad6 ("s390/pgtable: Add s390 support for page table check"),
> an invalidated PMD that is pmd_protnone() is fine for the remaining
> pmd_protnone() users, which run under the page table lock or are
> lockless walkers that should not follow it anyway.
>
> To: Alexander Gordeev <agordeev@linux.ibm.com>
> To: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
> To: Heiko Carstens <hca@linux.ibm.com>
> To: Vasily Gorbik <gor@linux.ibm.com>
> Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
> Cc: Sven Schnelle <svens@linux.ibm.com>
> Cc: Jason Gunthorpe <jgg@nvidia.com>
> Cc: John Hubbard <jhubbard@nvidia.com>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: linux-s390@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Reviewed-by: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
> Signed-off-by: Zi Yan <ziy@nvidia.com>
> ---
> arch/s390/include/asm/pgtable.h | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/arch/s390/include/asm/pgtable.h b/arch/s390/include/asm/pgtable.h
> index 2d5c2ab06de98..fe1ba746203ab 100644
> --- a/arch/s390/include/asm/pgtable.h
> +++ b/arch/s390/include/asm/pgtable.h
> @@ -1781,10 +1781,14 @@ static inline pmd_t pmdp_invalidate(struct vm_area_struct *vma,
> pmd_t pmd = *pmdp;
>
> VM_WARN_ON_ONCE(!pmd_present(pmd));
> + /*
> + * _SEGMENT_ENTRY_INVALID alone also marks an old (not young) entry,
> + * which lockless walkers like GUP-fast may follow. Clear
> + * _SEGMENT_ENTRY_READ too, so the invalidated entry is pmd_protnone()
> + * and is not followed, and page table check does not count it.
> + */
> pmd = set_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_INVALID));
> -#ifdef CONFIG_PAGE_TABLE_CHECK
> pmd = clear_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_READ));
> -#endif
> page_table_check_pmd_set(vma->vm_mm, addr, pmdp, pmd);
> pmd = pmdp_xchg_direct(vma->vm_mm, addr, pmdp, pmd);
> return pmd;
Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 6/9] sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate()
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
` (4 preceding siblings ...)
2026-12-04 4:17 ` [PATCH v2 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate() Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 4:17 ` [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Zi Yan
` (2 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, David S. Miller, Andreas Larsson,
sparclinux
thp_pte_count is maintained in __set_pmd_acct() whenever _PAGE_PMD_HUGE
flips. pmdp_invalidate() only clears _PAGE_VALID, so it does not flip
that bit. But it decrements thp_pte_count on its own, because THP
split replaces the invalidated PMD with a page table via pmd_populate(),
which does not change thp_pte_count.
This only accounts for the PMD split case, where a PMD is invalidated and
then replaced by a page table, but other cases exist:
1. Several callers invalidate a huge PMD and then install a valid one again
with set_pmd_at(): MADV_FREE, page_mkclean_one(), and change_huge_pmd().
The valid PMD is not counted, as _PAGE_PMD_HUGE did not flip, so each
such call loses one count.
2. When an invalidated PMD is cleared, _PAGE_PMD_HUGE flips and the count
is decreased again.
Eventually, thp_pte_count can wrap around and cause wrong TSB sizing.
Fix it by moving the decrement from pmdp_invalidate() to pmd_set(), which
backs pmd_populate(): THP split replaces the invalidated huge PMD with a
page table through it, so pmd_set() decrements the count when the entry
it overwrites is a huge PMD. Other pmd_populate() callers overwrite a none
PMD.
THP split also calls pmd_populate() on a local pmd_t to fill in the PTEs
before installing the page table. Initialize it to none, so pmd_set() does
not read an uninitialized value.
Fixes: 1e953d846ac0 ("sparc64 mm: Fix more TSB sizing issues")
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: "David S. Miller" <davem@davemloft.net>
To: Andreas Larsson <andreas@gaisler.com>
Cc: sparclinux@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
arch/sparc/include/asm/pgtable_64.h | 7 +++++++
arch/sparc/mm/tlb.c | 27 +++++++++++++++------------
mm/huge_memory.c | 4 ++--
3 files changed, 24 insertions(+), 14 deletions(-)
diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/pgtable_64.h
index 44d1333065a6a..141c204975219 100644
--- a/arch/sparc/include/asm/pgtable_64.h
+++ b/arch/sparc/include/asm/pgtable_64.h
@@ -803,19 +803,26 @@ static inline int pmd_present(pmd_t pmd)
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
void set_pmd_at(struct mm_struct *mm, unsigned long addr,
pmd_t *pmdp, pmd_t pmd);
+void pmd_set_acct(struct mm_struct *mm, pmd_t old);
#else
static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
pmd_t *pmdp, pmd_t pmd)
{
*pmdp = pmd;
}
+
+static inline void pmd_set_acct(struct mm_struct *mm, pmd_t old)
+{
+}
#endif
static inline void pmd_set(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
{
unsigned long val = __pa((unsigned long) (ptep));
+ pmd_t old = *pmdp;
pmd_val(*pmdp) = val;
+ pmd_set_acct(mm, old);
}
#define pud_set(pudp, pmdp) \
diff --git a/arch/sparc/mm/tlb.c b/arch/sparc/mm/tlb.c
index 6d9dd5eb13287..909db4f7b6389 100644
--- a/arch/sparc/mm/tlb.c
+++ b/arch/sparc/mm/tlb.c
@@ -228,6 +228,21 @@ static void __set_pmd_acct(struct mm_struct *mm, unsigned long addr,
}
}
+/*
+ * Called by pmd_set() and thus pmd_populate(). THP split uses pmd_populate()
+ * on an invalidated huge PMD and needs the accounting.
+ */
+void pmd_set_acct(struct mm_struct *mm, pmd_t old)
+{
+ if (mm == &init_mm || !(pmd_val(old) & _PAGE_PMD_HUGE))
+ return;
+
+ if (is_huge_zero_pmd(old))
+ mm->context.hugetlb_pte_count--;
+ else
+ mm->context.thp_pte_count--;
+}
+
void set_pmd_at(struct mm_struct *mm, unsigned long addr,
pmd_t *pmdp, pmd_t pmd)
{
@@ -250,9 +265,6 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
return old;
}
-/*
- * This routine is only called when splitting a THP
- */
pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
pmd_t *pmdp)
{
@@ -263,15 +275,6 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
old = pmdp_establish(vma, address, pmdp, entry);
flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
- /*
- * set_pmd_at() will not be called in a way to decrement
- * thp_pte_count when splitting a THP, so do it now.
- * Sanity check pmd before doing the actual decrement.
- */
- if ((pmd_val(entry) & _PAGE_PMD_HUGE) &&
- !is_huge_zero_pmd(entry))
- (vma->vm_mm)->context.thp_pte_count--;
-
return old;
}
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 91f3c7e7a044a..716bcf6873656 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3170,7 +3170,7 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
{
struct mm_struct *mm = vma->vm_mm;
pgtable_t pgtable;
- pmd_t _pmd, old_pmd;
+ pmd_t _pmd = __pmd(0), old_pmd;
unsigned long addr;
pte_t *pte;
int i;
@@ -3218,7 +3218,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
struct folio *folio;
struct page *page;
pgtable_t pgtable;
- pmd_t old_pmd, _pmd;
+ pmd_t old_pmd, _pmd = __pmd(0);
bool soft_dirty, uffd_wp = false, young = false, write = false;
bool anon_exclusive = false, dirty = false;
unsigned long addr;
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
` (5 preceding siblings ...)
2026-12-04 4:17 ` [PATCH v2 6/9] sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate() Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 4:35 ` Liam R. Howlett
` (2 more replies)
2026-12-04 4:17 ` [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level Zi Yan
2026-12-04 4:17 ` [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE Zi Yan
8 siblings, 3 replies; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, John Hubbard, Peter Xu,
Jason Gunthorpe
From: Kyle Zeng <kylebot@openai.com>
__discard_anon_folio_pmd_locked() clears and flushes a huge PMD before
checking whether the folio can be discarded. If the folio was redirtied
or has unexpected references, it restores the original PMD. Reclaim holds
the PMD lock and the anon_vma read lock, but not the owning mmap_lock.
Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD
without taking its lock. A concurrent munmap() or whole-VMA
MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source
VMA from its anon_vma, leaving a restored mapping behind. The anon_vma
write lock taken by unlink_anon_vmas() waits for reclaim to finish, but
does not repeat the skipped page-table walk. Once the remaining VMA
links are removed, the folio's positive mapcount no longer guarantees a
live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an
unprivileged user reproduces a KASAN use-after-free in
folio_lock_anon_vma_read().
Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while
the discard can still fail. Concurrent unmap and move operations then
have to synchronize on the PMD lock instead of skipping the mapping.
Clear the invalidated PMD only after the dirty and reference checks have
succeeded, before removing the rmap and withdrawing the deposited page
table. The full invalidation retains the TLB flush required for the
dirty and GUP-fast checks.
Signed-off-by: Kyle Zeng <kylebot@openai.com>
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: Andrew Morton <akpm@linux-foundation.org>
To: David Hildenbrand <david@kernel.org>
To: Lance Yang <lance.yang@linux.dev>
Cc: Zi Yan <ziy@nvidia.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: "Liam R. Howlett" <liam@infradead.org>
Cc: Nico Pache <nico.pache@linux.dev>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Usama Arif <usama.arif@linux.dev>
Cc: Kiryl Shutsemau <kas@kernel.org>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
---
mm/huge_memory.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 716bcf6873656..72322bb86ff4f 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3660,11 +3660,11 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
return false;
}
- orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
+ orig_pmd = pmdp_invalidate(vma, addr, pmdp);
/*
* Syncing against concurrent GUP-fast:
- * - clear PMD; barrier; read refcount
+ * - invalidate PMD; barrier; read refcount
* - inc refcount; barrier; read PMD
*/
smp_mb();
@@ -3699,6 +3699,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
return false;
}
+ pmdp_huge_get_and_clear(mm, addr, pmdp);
folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
zap_deposited_table(mm, pmdp);
add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim
2026-12-04 4:17 ` [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Zi Yan
@ 2026-12-04 4:35 ` Liam R. Howlett
2026-12-04 9:38 ` Baolin Wang
2026-12-04 13:23 ` Lorenzo Stoakes (ARM)
2 siblings, 0 replies; 17+ messages in thread
From: Liam R. Howlett @ 2026-12-04 4:35 UTC (permalink / raw)
To: Zi Yan
Cc: David Hildenbrand, Baolin Wang, Nico Pache, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
Lorenzo Stoakes, Rik van Riel, Vlastimil Babka, Harry Yoo,
Jann Horn, Kyle Zeng, Andrew Morton, linux-mm, linux-kernel,
John Hubbard, Peter Xu, Jason Gunthorpe
On 26/12/03 11:17PM, Zi Yan wrote:
> From: Kyle Zeng <kylebot@openai.com>
>
> __discard_anon_folio_pmd_locked() clears and flushes a huge PMD before
> checking whether the folio can be discarded. If the folio was redirtied
> or has unexpected references, it restores the original PMD. Reclaim holds
> the PMD lock and the anon_vma read lock, but not the owning mmap_lock.
>
> Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD
> without taking its lock. A concurrent munmap() or whole-VMA
> MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source
> VMA from its anon_vma, leaving a restored mapping behind. The anon_vma
> write lock taken by unlink_anon_vmas() waits for reclaim to finish, but
> does not repeat the skipped page-table walk. Once the remaining VMA
> links are removed, the folio's positive mapcount no longer guarantees a
> live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an
> unprivileged user reproduces a KASAN use-after-free in
> folio_lock_anon_vma_read().
>
> Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while
> the discard can still fail. Concurrent unmap and move operations then
> have to synchronize on the PMD lock instead of skipping the mapping.
> Clear the invalidated PMD only after the dirty and reference checks have
> succeeded, before removing the rmap and withdrawing the deposited page
> table. The full invalidation retains the TLB flush required for the
> dirty and GUP-fast checks.
>
> Signed-off-by: Kyle Zeng <kylebot@openai.com>
> Signed-off-by: Zi Yan <ziy@nvidia.com>
> To: Andrew Morton <akpm@linux-foundation.org>
> To: David Hildenbrand <david@kernel.org>
> To: Lance Yang <lance.yang@linux.dev>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: "Liam R. Howlett" <liam@infradead.org>
> Cc: Nico Pache <nico.pache@linux.dev>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Dev Jain <dev.jain@arm.com>
> Cc: Barry Song <baohua@kernel.org>
> Cc: Usama Arif <usama.arif@linux.dev>
> Cc: Kiryl Shutsemau <kas@kernel.org>
> Cc: Jason Gunthorpe <jgg@nvidia.com>
> Cc: John Hubbard <jhubbard@nvidia.com>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
Acked-by: Liam R. Howlett (Google) <liam@infradead.org>
> ---
> mm/huge_memory.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 716bcf6873656..72322bb86ff4f 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3660,11 +3660,11 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
> return false;
> }
>
> - orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
> + orig_pmd = pmdp_invalidate(vma, addr, pmdp);
>
> /*
> * Syncing against concurrent GUP-fast:
> - * - clear PMD; barrier; read refcount
> + * - invalidate PMD; barrier; read refcount
> * - inc refcount; barrier; read PMD
> */
> smp_mb();
> @@ -3699,6 +3699,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
> return false;
> }
>
> + pmdp_huge_get_and_clear(mm, addr, pmdp);
> folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
> zap_deposited_table(mm, pmdp);
> add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
>
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim
2026-12-04 4:17 ` [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Zi Yan
2026-12-04 4:35 ` Liam R. Howlett
@ 2026-12-04 9:38 ` Baolin Wang
2026-12-04 13:23 ` Lorenzo Stoakes (ARM)
2 siblings, 0 replies; 17+ messages in thread
From: Baolin Wang @ 2026-12-04 9:38 UTC (permalink / raw)
To: Zi Yan, David Hildenbrand, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, John Hubbard, Peter Xu, Jason Gunthorpe
On 12/4/26 12:17 PM, Zi Yan wrote:
> From: Kyle Zeng <kylebot@openai.com>
>
> __discard_anon_folio_pmd_locked() clears and flushes a huge PMD before
> checking whether the folio can be discarded. If the folio was redirtied
> or has unexpected references, it restores the original PMD. Reclaim holds
> the PMD lock and the anon_vma read lock, but not the owning mmap_lock.
>
> Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD
> without taking its lock. A concurrent munmap() or whole-VMA
> MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source
> VMA from its anon_vma, leaving a restored mapping behind. The anon_vma
> write lock taken by unlink_anon_vmas() waits for reclaim to finish, but
> does not repeat the skipped page-table walk. Once the remaining VMA
> links are removed, the folio's positive mapcount no longer guarantees a
> live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an
> unprivileged user reproduces a KASAN use-after-free in
> folio_lock_anon_vma_read().
>
> Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while
> the discard can still fail. Concurrent unmap and move operations then
> have to synchronize on the PMD lock instead of skipping the mapping.
> Clear the invalidated PMD only after the dirty and reference checks have
> succeeded, before removing the rmap and withdrawing the deposited page
> table. The full invalidation retains the TLB flush required for the
> dirty and GUP-fast checks.
>
> Signed-off-by: Kyle Zeng <kylebot@openai.com>
> Signed-off-by: Zi Yan <ziy@nvidia.com>
> To: Andrew Morton <akpm@linux-foundation.org>
> To: David Hildenbrand <david@kernel.org>
> To: Lance Yang <lance.yang@linux.dev>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: "Liam R. Howlett" <liam@infradead.org>
> Cc: Nico Pache <nico.pache@linux.dev>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Dev Jain <dev.jain@arm.com>
> Cc: Barry Song <baohua@kernel.org>
> Cc: Usama Arif <usama.arif@linux.dev>
> Cc: Kiryl Shutsemau <kas@kernel.org>
> Cc: Jason Gunthorpe <jgg@nvidia.com>
> Cc: John Hubbard <jhubbard@nvidia.com>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
> ---
Make sense to me.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim
2026-12-04 4:17 ` [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Zi Yan
2026-12-04 4:35 ` Liam R. Howlett
2026-12-04 9:38 ` Baolin Wang
@ 2026-12-04 13:23 ` Lorenzo Stoakes (ARM)
2 siblings, 0 replies; 17+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-04 13:23 UTC (permalink / raw)
To: Zi Yan
Cc: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Rik van Riel, Vlastimil Babka, Harry Yoo,
Jann Horn, Kyle Zeng, Andrew Morton, linux-mm, linux-kernel,
John Hubbard, Peter Xu, Jason Gunthorpe
On Thu, Dec 03, 2026 at 11:17:55PM -0500, Zi Yan wrote:
> From: Kyle Zeng <kylebot@openai.com>
>
> __discard_anon_folio_pmd_locked() clears and flushes a huge PMD before
> checking whether the folio can be discarded. If the folio was redirtied
> or has unexpected references, it restores the original PMD. Reclaim holds
> the PMD lock and the anon_vma read lock, but not the owning mmap_lock.
>
> Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD
> without taking its lock. A concurrent munmap() or whole-VMA
> MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source
> VMA from its anon_vma, leaving a restored mapping behind. The anon_vma
> write lock taken by unlink_anon_vmas() waits for reclaim to finish, but
> does not repeat the skipped page-table walk. Once the remaining VMA
> links are removed, the folio's positive mapcount no longer guarantees a
> live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an
> unprivileged user reproduces a KASAN use-after-free in
> folio_lock_anon_vma_read().
As usual, have to say that MREMAP_DONTUNMAP was probably a mistake :))
>
> Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while
> the discard can still fail. Concurrent unmap and move operations then
> have to synchronize on the PMD lock instead of skipping the mapping.
> Clear the invalidated PMD only after the dirty and reference checks have
> succeeded, before removing the rmap and withdrawing the deposited page
> table. The full invalidation retains the TLB flush required for the
> dirty and GUP-fast checks.
>
> Signed-off-by: Kyle Zeng <kylebot@openai.com>
> Signed-off-by: Zi Yan <ziy@nvidia.com>
LGTM so:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> To: Andrew Morton <akpm@linux-foundation.org>
> To: David Hildenbrand <david@kernel.org>
> To: Lance Yang <lance.yang@linux.dev>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: "Liam R. Howlett" <liam@infradead.org>
> Cc: Nico Pache <nico.pache@linux.dev>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Dev Jain <dev.jain@arm.com>
> Cc: Barry Song <baohua@kernel.org>
> Cc: Usama Arif <usama.arif@linux.dev>
> Cc: Kiryl Shutsemau <kas@kernel.org>
> Cc: Jason Gunthorpe <jgg@nvidia.com>
> Cc: John Hubbard <jhubbard@nvidia.com>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
> ---
> mm/huge_memory.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 716bcf6873656..72322bb86ff4f 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3660,11 +3660,11 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
> return false;
> }
>
> - orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
> + orig_pmd = pmdp_invalidate(vma, addr, pmdp);
>
> /*
> * Syncing against concurrent GUP-fast:
> - * - clear PMD; barrier; read refcount
> + * - invalidate PMD; barrier; read refcount
> * - inc refcount; barrier; read PMD
> */
Similar to comment on other patch, could we have a 'paired with' and
ref. to the GUP-fast function to keep with memory barrier convention? I
think in try_grab_folio_fast().
> smp_mb();
> @@ -3699,6 +3699,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
> return false;
> }
>
> + pmdp_huge_get_and_clear(mm, addr, pmdp);
> folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
> zap_deposited_table(mm, pmdp);
> add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
>
> --
> 2.53.0
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
` (6 preceding siblings ...)
2026-12-04 4:17 ` [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 4:37 ` Liam R. Howlett
2026-12-04 9:49 ` Baolin Wang
2026-12-04 4:17 ` [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE Zi Yan
8 siblings, 2 replies; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, Sashiko
The patch "mm/rmap: don't discard lazyfree THPs at PMD level" disabled
the PMD-level discard, because it cleared the PMD before checking whether
the folio could be discarded, and munmap() or mremap() could skip that
transiently none PMD.
__discard_anon_folio_pmd_locked() now keeps the PMD invalidated, not
cleared, until the discard is committed, GUP-fast no longer follows an
invalidated PMD on any architecture, powerpc page table check no longer
counts an invalidated PMD as mapped, and sparc64 keeps thp_pte_count
balanced when an invalidated PMD is set again or cleared. Discard lazyfree
THPs at PMD level again.
Also call update_hiwater_rss() before unmap_huge_pmd_locked(), as the
PTE path does before lowering RSS. The PMD path has skipped it since the
PMD-level discard was added in commit 735ecdfaf4e8 ("mm/vmscan: avoid
split lazyfree THP during shrink_folio_list()"), so a discard could lower
RSS without recording the high watermark first, and VmHWM could be
under-reported.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=7
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: Andrew Morton <akpm@linux-foundation.org>
To: David Hildenbrand <david@kernel.org>
To: Lorenzo Stoakes <ljs@kernel.org>
Cc: Rik van Riel <riel@surriel.com>
Cc: "Liam R. Howlett" <liam@infradead.org>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Harry Yoo <harry@kernel.org>
Cc: Jann Horn <jannh@google.com>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Kyle Zeng <kylebot@openai.com>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
---
mm/rmap.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/mm/rmap.c b/mm/rmap.c
index f0c17d2e04847..32acae9efc313 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2285,6 +2285,19 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
}
if (!pvmw.pte) {
+ if (folio_test_lazyfree(folio)) {
+ /* Update high watermark before we lower rss */
+ update_hiwater_rss(mm);
+ if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
+ goto walk_done;
+ /*
+ * unmap_huge_pmd_locked has either already marked
+ * the folio as swap-backed or decided to retain it
+ * due to GUP or speculative references.
+ */
+ goto walk_abort;
+ }
+
if (flags & TTU_SPLIT_HUGE_PMD) {
/*
* We temporarily have to drop the PTL and
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level
2026-12-04 4:17 ` [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level Zi Yan
@ 2026-12-04 4:37 ` Liam R. Howlett
2026-12-04 9:49 ` Baolin Wang
1 sibling, 0 replies; 17+ messages in thread
From: Liam R. Howlett @ 2026-12-04 4:37 UTC (permalink / raw)
To: Zi Yan
Cc: David Hildenbrand, Baolin Wang, Nico Pache, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
Lorenzo Stoakes, Rik van Riel, Vlastimil Babka, Harry Yoo,
Jann Horn, Kyle Zeng, Andrew Morton, linux-mm, linux-kernel,
Sashiko
On 26/12/03 11:17PM, Zi Yan wrote:
> The patch "mm/rmap: don't discard lazyfree THPs at PMD level" disabled
Nit, the hash for the disabling patch would be useful here.
> the PMD-level discard, because it cleared the PMD before checking whether
> the folio could be discarded, and munmap() or mremap() could skip that
> transiently none PMD.
>
> __discard_anon_folio_pmd_locked() now keeps the PMD invalidated, not
> cleared, until the discard is committed, GUP-fast no longer follows an
> invalidated PMD on any architecture, powerpc page table check no longer
> counts an invalidated PMD as mapped, and sparc64 keeps thp_pte_count
> balanced when an invalidated PMD is set again or cleared. Discard lazyfree
> THPs at PMD level again.
>
> Also call update_hiwater_rss() before unmap_huge_pmd_locked(), as the
> PTE path does before lowering RSS. The PMD path has skipped it since the
> PMD-level discard was added in commit 735ecdfaf4e8 ("mm/vmscan: avoid
> split lazyfree THP during shrink_folio_list()"), so a discard could lower
> RSS without recording the high watermark first, and VmHWM could be
> under-reported.
>
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=7
> Signed-off-by: Zi Yan <ziy@nvidia.com>
> To: Andrew Morton <akpm@linux-foundation.org>
> To: David Hildenbrand <david@kernel.org>
> To: Lorenzo Stoakes <ljs@kernel.org>
> Cc: Rik van Riel <riel@surriel.com>
> Cc: "Liam R. Howlett" <liam@infradead.org>
> Cc: Vlastimil Babka <vbabka@kernel.org>
> Cc: Harry Yoo <harry@kernel.org>
> Cc: Jann Horn <jannh@google.com>
> Cc: Lance Yang <lance.yang@linux.dev>
> Cc: Kyle Zeng <kylebot@openai.com>
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
Acked-by: Liam R. Howlett (Google) <liam@infradead.org>
> ---
> mm/rmap.c | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/mm/rmap.c b/mm/rmap.c
> index f0c17d2e04847..32acae9efc313 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -2285,6 +2285,19 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
> }
>
> if (!pvmw.pte) {
> + if (folio_test_lazyfree(folio)) {
> + /* Update high watermark before we lower rss */
> + update_hiwater_rss(mm);
> + if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
> + goto walk_done;
> + /*
> + * unmap_huge_pmd_locked has either already marked
> + * the folio as swap-backed or decided to retain it
> + * due to GUP or speculative references.
> + */
> + goto walk_abort;
> + }
> +
> if (flags & TTU_SPLIT_HUGE_PMD) {
> /*
> * We temporarily have to drop the PTL and
>
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level
2026-12-04 4:17 ` [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level Zi Yan
2026-12-04 4:37 ` Liam R. Howlett
@ 2026-12-04 9:49 ` Baolin Wang
1 sibling, 0 replies; 17+ messages in thread
From: Baolin Wang @ 2026-12-04 9:49 UTC (permalink / raw)
To: Zi Yan, David Hildenbrand, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Sashiko
On 12/4/26 12:17 PM, Zi Yan wrote:
> The patch "mm/rmap: don't discard lazyfree THPs at PMD level" disabled
> the PMD-level discard, because it cleared the PMD before checking whether
> the folio could be discarded, and munmap() or mremap() could skip that
> transiently none PMD.
>
> __discard_anon_folio_pmd_locked() now keeps the PMD invalidated, not
> cleared, until the discard is committed, GUP-fast no longer follows an
> invalidated PMD on any architecture, powerpc page table check no longer
> counts an invalidated PMD as mapped, and sparc64 keeps thp_pte_count
> balanced when an invalidated PMD is set again or cleared. Discard lazyfree
> THPs at PMD level again.
>
> Also call update_hiwater_rss() before unmap_huge_pmd_locked(), as the
> PTE path does before lowering RSS. The PMD path has skipped it since the
> PMD-level discard was added in commit 735ecdfaf4e8 ("mm/vmscan: avoid
> split lazyfree THP during shrink_folio_list()"), so a discard could lower
> RSS without recording the high watermark first, and VmHWM could be
> under-reported.
>
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=7
> Signed-off-by: Zi Yan <ziy@nvidia.com>
> To: Andrew Morton <akpm@linux-foundation.org>
> To: David Hildenbrand <david@kernel.org>
> To: Lorenzo Stoakes <ljs@kernel.org>
> Cc: Rik van Riel <riel@surriel.com>
> Cc: "Liam R. Howlett" <liam@infradead.org>
> Cc: Vlastimil Babka <vbabka@kernel.org>
> Cc: Harry Yoo <harry@kernel.org>
> Cc: Jann Horn <jannh@google.com>
> Cc: Lance Yang <lance.yang@linux.dev>
> Cc: Kyle Zeng <kylebot@openai.com>
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
> ---
Make sense to me.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE
2026-12-04 4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
` (7 preceding siblings ...)
2026-12-04 4:17 ` [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level Zi Yan
@ 2026-12-04 4:17 ` Zi Yan
2026-12-04 12:52 ` Lorenzo Stoakes (ARM)
8 siblings, 1 reply; 17+ messages in thread
From: Zi Yan @ 2026-12-04 4:17 UTC (permalink / raw)
To: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Lorenzo Stoakes, Rik van Riel, Vlastimil Babka,
Harry Yoo, Jann Horn, Kyle Zeng, Andrew Morton
Cc: linux-mm, linux-kernel, Zi Yan, Andrea Arcangeli,
Suren Baghdasaryan, Peter Xu
UFFDIO_MOVE under the page table lock clears a PMD, checks if the folio is
DMA pinned, and restores it if that is the case. A concurrent zap can skip
a none PMD without taking the page table lock. Since UFFDIO_MOVE holds the
VMA or mmap read lock, those taking the read lock can race with it, e.g.,
MADV_DONTNEED. In the MADV_DONTNEED case, the affected memory can be
neither discarded nor zeroed.
Fix it by invalidating the PMD with pmdp_invalidate() instead. Also add
smp_mb() between pmdp_invalidate() and reading folio pincount to pair with
GUP-fast.
Fixes: adef440691bab ("userfaultfd: UFFDIO_MOVE uABI")
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: David Hildenbrand <david@kernel.org>
To: Zi Yan <ziy@nvidia.com>
To: Andrew Morton <akpm@linux-foundation.org>
To: Andrea Arcangeli <aarcange@redhat.com>
To: Suren Baghdasaryan <surenb@google.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: "Liam R. Howlett" <liam@infradead.org>
Cc: Nico Pache <nico.pache@linux.dev>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Usama Arif <usama.arif@linux.dev>
Cc: Kiryl Shutsemau <kas@kernel.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
---
mm/huge_memory.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 72322bb86ff4f..6a9635c7e0b48 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2993,7 +2993,17 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
goto unlock_ptls;
}
- src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
+ /*
+ * Invalidate the PMD so that a concurrent zap does not skip it
+ * in case a restore is needed afterwards.
+ */
+ src_pmdval = pmdp_invalidate(src_vma, src_addr, src_pmd);
+ /*
+ * Syncing against concurrent GUP-fast:
+ * - invalidate PMD; barrier; read pincount
+ * - inc pincount; barrier; read PMD
+ */
+ smp_mb();
/* Folio got pinned from under us. Put it back and fail the move. */
if (folio_maybe_dma_pinned(src_folio)) {
set_pmd_at(mm, src_addr, src_pmd, src_pmdval);
@@ -3004,6 +3014,7 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
folio_move_anon_rmap(src_folio, dst_vma);
src_folio->index = linear_anon_page_index(dst_vma, dst_addr);
+ pmdp_huge_get_and_clear(mm, src_addr, src_pmd);
_dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot);
/* Follow mremap() behavior and treat the entry dirty after the move */
_dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);
--
2.53.0
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE
2026-12-04 4:17 ` [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE Zi Yan
@ 2026-12-04 12:52 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 17+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-04 12:52 UTC (permalink / raw)
To: Zi Yan
Cc: David Hildenbrand, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Rik van Riel, Vlastimil Babka, Harry Yoo,
Jann Horn, Kyle Zeng, Andrew Morton, linux-mm, linux-kernel,
Andrea Arcangeli, Suren Baghdasaryan, Peter Xu
On Thu, Dec 03, 2026 at 11:17:57PM -0500, Zi Yan wrote:
> UFFDIO_MOVE under the page table lock clears a PMD, checks if the folio is
> DMA pinned, and restores it if that is the case. A concurrent zap can skip
> a none PMD without taking the page table lock. Since UFFDIO_MOVE holds the
> VMA or mmap read lock, those taking the read lock can race with it, e.g.,
> MADV_DONTNEED. In the MADV_DONTNEED case, the affected memory can be
> neither discarded nor zeroed.
>
> Fix it by invalidating the PMD with pmdp_invalidate() instead. Also add
> smp_mb() between pmdp_invalidate() and reading folio pincount to pair with
> GUP-fast.
>
> Fixes: adef440691bab ("userfaultfd: UFFDIO_MOVE uABI")
No Cc: stable, on purpose?
> Signed-off-by: Zi Yan <ziy@nvidia.com>
With nits addressed, LGTM, so:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> To: David Hildenbrand <david@kernel.org>
> To: Zi Yan <ziy@nvidia.com>
> To: Andrew Morton <akpm@linux-foundation.org>
> To: Andrea Arcangeli <aarcange@redhat.com>
> To: Suren Baghdasaryan <surenb@google.com>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: "Liam R. Howlett" <liam@infradead.org>
> Cc: Nico Pache <nico.pache@linux.dev>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Dev Jain <dev.jain@arm.com>
> Cc: Barry Song <baohua@kernel.org>
> Cc: Lance Yang <lance.yang@linux.dev>
> Cc: Usama Arif <usama.arif@linux.dev>
> Cc: Kiryl Shutsemau <kas@kernel.org>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
> ---
> mm/huge_memory.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 72322bb86ff4f..6a9635c7e0b48 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -2993,7 +2993,17 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
> goto unlock_ptls;
> }
>
> - src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
> + /*
> + * Invalidate the PMD so that a concurrent zap does not skip it
> + * in case a restore is needed afterwards.
> + */
OK had to read it a few times to get it - it being cleared would result in zap
skipping it (+ thus not blocking on PMD PTL) + potentially a restore would then
leave an unzapped entry.
Maybe slightly rephrase like:
/*
* A concurrent zap skips none entries without taking the PTL, so
* invalidate rather than clear. The zap then blocks on the PTL until
* the move has either completed or been aborted and the entry restored.
*/
> + src_pmdval = pmdp_invalidate(src_vma, src_addr, src_pmd);
> + /*
> + * Syncing against concurrent GUP-fast:
> + * - invalidate PMD; barrier; read pincount
> + * - inc pincount; barrier; read PMD
Worth saying specifically 'pairs with' and the relevant function by convention.
> + */
> + smp_mb();
Real nitty but maybe newline here to highlight that you have a fence here :)
> /* Folio got pinned from under us. Put it back and fail the move. */
> if (folio_maybe_dma_pinned(src_folio)) {
> set_pmd_at(mm, src_addr, src_pmd, src_pmdval);
> @@ -3004,6 +3014,7 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
> folio_move_anon_rmap(src_folio, dst_vma);
> src_folio->index = linear_anon_page_index(dst_vma, dst_addr);
>
> + pmdp_huge_get_and_clear(mm, src_addr, src_pmd);
> _dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot);
> /* Follow mremap() behavior and treat the entry dirty after the move */
> _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);
>
> --
> 2.53.0
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 17+ messages in thread