Skip to content

Re-enable discarding lazyfree PMD THPs and transient none PMD fixes

Zi Yan linux-mm linux-riscv linux-s390 linuxppc-dev lkml loongarch sparclinux stable 2 versions – In review

Get series with b4
b4 am [email protected] change-id 20261007-anon_vma_uaf_fix-e7accd1d2ee1

Versions newest first

  1. v2 Latest9 patches 7 repliesIn review
  2. v17 patches 12 repliesSuperseded

Changes in v2 from the cover letter · 17 lines

- 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/[email protected]

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9mm/migrate: do not clear device private entry in set_pmd_migration_entry()mm/huge_memory.c+6 -10 replies
2/9powerpc/book3s64/mm: don't count invalidated PMDs in page table checkarch/powerpc/include/asm/book3s/64/pgtable.h+3 -40 replies
3/9riscv: mm: don't let GUP-fast follow entries without _PAGE_READarch/riscv/include/asm/pgtable.h+27 -00 replies
4/9LoongArch: mm: don't let GUP-fast follow invalidated THP PMDsarch/loongarch/include/asm/pgtable.h+6 -00 replies
5/9s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate()arch/s390/include/asm/pgtable.h+6 -21 reply
6/9sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate()arch/sparc/include/asm/pgtable_64.h arch/sparc/mm/tlb.c mm/huge_memory.c+24 -140 replies
7/9mm/huge_memory: avoid transient none PMDs during lazyfree reclaimmm/huge_memory.c+3 -23 replies
8/9mm/rmap: re-enable discarding lazyfree THPs at PMD levelmm/rmap.c+13 -02 replies
9/9userfaultfd: avoid transient none PMDs during UFFDIO_MOVEmm/huge_memory.c+12 -11 reply

Reviews trailers per patch

6 of 9 patches have a Reviewed-by or Acked-by.

PatchUsama ArifJisheng ZhangGerald SchaeferLiam R. Howlett (Google)Baolin WangAlexander GordeevLorenzo Stoakes (ARM)
1/9
2/9A-b
3/9R-b
4/9
5/9R-b R-b
6/9
7/9A-b R-b R-b
8/9A-b R-b
9/9R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 7 reviewers.

Read the v2 thread

Cover letter [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes

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

---
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/[email protected]

---
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,

Signature · 1 line
Yan, Zi