Linux s390 Architecture development
 help / color / mirror / Atom feed
* [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes
@ 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
  2026-12-05  4:19 ` [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes sashiko-bot
  0 siblings, 2 replies; 4+ 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, Madhavan Srinivasan, Andrew Donnellan,
	Rohan McLure, Pasha Tatashin, Michael Ellerman, Nicholas Piggin,
	Christophe Leroy (CS GROUP), Ritesh Harjani (IBM),
	Shrikanth Hegde, linuxppc-dev, Sashiko, Jisheng Zhang,
	Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Zong Li, Deepak Gupta, Palmer Dabbelt, John Hubbard, Peter Xu,
	linux-riscv, Huacai Chen, WANG Xuerui, loongarch,
	Alexander Gordeev, Gerald Schaefer, Heiko Carstens, Vasily Gorbik,
	Christian Borntraeger, Sven Schnelle, linux-s390, David S. Miller,
	Andreas Larsson, sparclinux, Andrea Arcangeli, Suren Baghdasaryan,
	Jason Gunthorpe

Patch 1 can be picked up separately if more work is needed for the rest of
the patches.

For maintainers, Patch 7 and 9 use pmdp_invalidate() in place of PMD
clearing, so they need the arch fixes from Patch 2 to 6. It might be
better to get ack from arch maintainers for Patch 2 to 6 and merge the
series via mm tree.

Motivation
===

A bug was found in the PMD-level discard of lazyfree THPs [1]. The
proposed fix keeps the PMD invalidated with pmdp_invalidate() instead of
clearing it, but that does not work on every architecture:

1. riscv, LoongArch and s390 let GUP-fast follow an invalidated PMD;
2. powerpc/book3s64 page table check counts an invalidated PMD as mapped;
3. sparc64's thp_pte_count becomes unbalanced around pmdp_invalidate().

So the PMD-level discard was disabled instead [2]. This series fixes the
arch code and re-enables it.

The discard decides from the folio refcount, so GUP-fast must not take a
reference through an invalidated PMD, and arch accounting must stay
balanced when an invalidated PMD is set again or cleared.

Details
===
Patch 1 fixes set_pmd_migration_entry() when migrating a device private
folio. set_pmd_migration_entry() clears the device private PMD
unnecessarily and the transient none PMD can be skipped by a concurrent
zap. It can cause lost deposited page table, RSS stats mismatch, or data
loss.

Patches 2-6 fix the arch code, patch 7 from Kyle keeps the PMD invalidated
instead of cleared until the discard is committed, and patch 8 enables the
PMD-level discard again:

2. powerpc/book3s64: page table check counts an invalidated PMD as a live
   mapping, so setting it again or clearing it decrements the map count a
   second time and hits BUG_ON(). This is already reachable via mprotect(),
   and via MADV_COLD before v7.4, with CONFIG_PAGE_TABLE_CHECK.

3. riscv: an invalidated PMD still passes pmd_present() and the generic
   pmd_access_permitted(), so GUP-fast follows it. The generic helpers
   also let GUP-fast read execute-only entries and write shadow stack
   entries. Require _PAGE_PRESENT and _PAGE_READ in
   pte_access_permitted() and use it for PMDs and PUDs too. This patch
   was posted on its own [3] and has Cc: stable; it is carried here
   because the series depends on it.

4. LoongArch: an invalidated PMD passes the generic pmd_access_permitted()
   too. Add a pmd_access_permitted() that requires _PAGE_PRESENT.

5. s390: _SEGMENT_ENTRY_INVALID also marks an old PMD, so GUP-fast follows
   an invalidated PMD. Always clear _SEGMENT_ENTRY_READ in
   pmdp_invalidate(), which makes it pmd_protnone(), so GUP-fast rejects
   it.

6. sparc64: pmdp_invalidate() decrements thp_pte_count assuming a THP
   split follows, so setting the PMD again loses a count. Move the
   decrement to pmd_set(), which THP split uses to replace the huge PMD
   with a page table, and initialize the split's on-stack _pmd to none.

7. mm/huge_memory: use pmdp_invalidate() in the discard, so munmap() and
   mremap() wait on the PMD lock instead of skipping a none PMD.

8. mm/rmap: discard lazyfree THPs at PMD level again.

Patch 9 fixes a similar transient none PMD issue using pmdp_invalidate() in
UFFDIO_MOVE.

Note
===

1. x86, arm64 and powerpc already make GUP-fast reject an invalidated PMD,
   and sparc64 has no GUP-fast. 32-bit MIPS would not reject it, but it
   cannot have THP: _PAGE_HUGE makes PFN_PTE_SHIFT exceed PAGE_SHIFT, so
   the BUILD_BUG_ON() in arch_mm_preinit() fires, which is why Ingenic
   dropped huge pages in commit 69b686ddd579 ("MIPS: ingenic: Remove
   CPU_SUPPORTS_HUGEPAGES").

2. A follow-up patch to remove on-stack _pmd in PMD split code can get rid
   of the for-sparc64 _pmd initialization code

Link: https://lore.kernel.org/all/20261009165214.40212-2-kylebot@openai.com/ [1]
Link: https://lore.kernel.org/all/20261012-lazyfree-stable-fix-v2-1-6387d6e30063@nvidia.com/ [2]
Link: https://lore.kernel.org/all/20261015-riscv-gup-read-fix-v1-1-8cf7f2504029@nvidia.com/ [3]

Signed-off-by: Zi Yan <ziy@nvidia.com>
---
Changes in v2:
- Patch 1: new. Do not clear a device private PMD in
  set_pmd_migration_entry(), since a concurrent zap can skip the
  transient none PMD.
- Patch 2: cite riscv commit bdda2887cbf39 instead of its subject; added
  Usama's Acked-by.
- Patch 3: replaced "riscv: mm: don't let GUP-fast follow invalidated THP
  PMDs" with the standalone "riscv: mm: don't let GUP-fast follow entries
  without _PAGE_READ" [3], which also rejects execute-only and shadow
  stack entries (Sashiko); added Jisheng's Reviewed-by.
- Patch 4: revised the commit message.
- Patch 5: revised the commit message.
- Patch 6: move the decrement to pmd_set() instead of counting only valid
  huge PMDs: _PAGE_VALID is also the referenced bit (_PAGE_R), so v1
  dropped old THPs from the count.
- Patch 7: dropped the dependency note.
- Patch 8: call update_hiwater_rss() before the PMD-level discard, as the
  PTE path does (Sashiko).
- Patch 9: new. Use pmdp_invalidate() instead of clearing the PMD in
  UFFDIO_MOVE.
- Dropped the Assisted-by tags; Claude and Codex were used to analyze the
  code and review the patches.
- Link to v1: https://patch.msgid.link/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b@nvidia.com

---
Kyle Zeng (1):
      mm/huge_memory: avoid transient none PMDs during lazyfree reclaim

Zi Yan (8):
      mm/migrate: do not clear device private entry in set_pmd_migration_entry()
      powerpc/book3s64/mm: don't count invalidated PMDs in page table check
      riscv: mm: don't let GUP-fast follow entries without _PAGE_READ
      LoongArch: mm: don't let GUP-fast follow invalidated THP PMDs
      s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate()
      sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate()
      mm/rmap: re-enable discarding lazyfree THPs at PMD level
      userfaultfd: avoid transient none PMDs during UFFDIO_MOVE

 arch/loongarch/include/asm/pgtable.h         |  6 ++++++
 arch/powerpc/include/asm/book3s/64/pgtable.h |  7 +++----
 arch/riscv/include/asm/pgtable.h             | 27 ++++++++++++++++++++++++++
 arch/s390/include/asm/pgtable.h              |  8 ++++++--
 arch/sparc/include/asm/pgtable_64.h          |  7 +++++++
 arch/sparc/mm/tlb.c                          | 27 ++++++++++++++------------
 mm/huge_memory.c                             | 29 ++++++++++++++++++++++------
 mm/rmap.c                                    | 13 +++++++++++++
 8 files changed, 100 insertions(+), 24 deletions(-)
---
base-commit: c6952260203fadee8e9f96c3f999f03b301d6f6d
change-id: 20261007-anon_vma_uaf_fix-e7accd1d2ee1

Best regards,
--  
Yan, Zi


^ permalink raw reply	[flat|nested] 4+ 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
@ 2026-12-04  4:17 ` Zi Yan
  2026-12-04 12:31   ` Alexander Gordeev
  2026-12-05  4:19 ` [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes sashiko-bot
  1 sibling, 1 reply; 4+ 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] 4+ 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; 4+ 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] 4+ messages in thread

* Re: [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes
  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 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate() Zi Yan
@ 2026-12-05  4:19 ` sashiko-bot
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-05  4:19 UTC (permalink / raw)
  To: Zi Yan
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

Sashiko has finished reviewing this 9-patch series:

- Patches 1-3, 5, and 6: no issues found.
- Patch 4 ("LoongArch: mm: don't let GUP-fast follow invalidated..."): 1
  potential issue (1 Low)
- Patch 7 ("mm/huge_memory: avoid transient none PMDs during laz..."): 4
  potential issues (1 High, 2 Medium, 1 Low)
- Patch 8 ("mm/rmap: re-enable discarding lazyfree THPs at PMD l..."): 2
  potential issues (2 High)
- Patch 9 ("userfaultfd: avoid transient none PMDs during UFFDIO..."): 3
  potential issues (1 High, 2 Medium)

Detailed reviews have been sent as separate replies to patches 4 and
7-9.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-anon_vma_uaf_fix-v2-0-3d4c746e8839@nvidia.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-05  4:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate() Zi Yan
2026-12-04 12:31   ` Alexander Gordeev
2026-12-05  4:19 ` [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox