The Linux Kernel Mailing List
 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 1/9] mm/migrate: do not clear device private entry in set_pmd_migration_entry() Zi Yan
                   ` (8 more replies)
  0 siblings, 9 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, 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] 17+ messages in thread

end of thread, other threads:[~2026-12-04 13:23 UTC | newest]

Thread overview: 17+ 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 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 ` [PATCH v2 3/9] riscv: mm: don't let GUP-fast follow entries without _PAGE_READ Zi Yan
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 ` [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-04  4:17 ` [PATCH v2 6/9] sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate() Zi Yan
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)
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
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)

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