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/[email protected]/ [1]
Link:
https://lore.kernel.org/all/[email protected]/
[2]
Link:
https://lore.kernel.org/all/[email protected]/
[3]
Signed-off-by: Zi Yan <[email protected]>
---
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,
--
Yan, Zi