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 To: David Hildenbrand To: Zi Yan To: Andrew Morton Cc: Baolin Wang Cc: "Liam R. Howlett" Cc: Nico Pache Cc: Ryan Roberts Cc: Dev Jain Cc: Barry Song Cc: Lance Yang Cc: Usama Arif Cc: Kiryl Shutsemau Cc: Balbir Singh Cc: Alistair Popple 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 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 To: Andrew Donnellan To: Rohan McLure To: Pasha Tatashin To: Andrew Morton Cc: Michael Ellerman Cc: Nicholas Piggin Cc: "Christophe Leroy (CS GROUP)" Cc: "Ritesh Harjani (IBM)" Cc: Shrikanth Hegde Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-kernel@vger.kernel.org Acked-by: Usama Arif Signed-off-by: Zi Yan --- 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 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 Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=2 Reviewed-by: Jisheng Zhang Signed-off-by: Zi Yan To: Paul Walmsley To: Palmer Dabbelt To: Albert Ou Cc: Alexandre Ghiti Cc: Jisheng Zhang Cc: Zong Li Cc: Deepak Gupta Cc: Palmer Dabbelt Cc: Jason Gunthorpe Cc: John Hubbard Cc: Peter Xu 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 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 To: Huacai Chen Cc: WANG Xuerui Cc: Jason Gunthorpe Cc: John Hubbard Cc: Peter Xu 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 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 To: Gerald Schaefer To: Heiko Carstens To: Vasily Gorbik Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Jason Gunthorpe Cc: John Hubbard Cc: Peter Xu Cc: linux-s390@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Gerald Schaefer Signed-off-by: Zi Yan --- 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 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 To: "David S. Miller" To: Andreas Larsson 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 From: Kyle Zeng __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 Signed-off-by: Zi Yan To: Andrew Morton To: David Hildenbrand To: Lance Yang Cc: Zi Yan Cc: Baolin Wang Cc: "Liam R. Howlett" Cc: Nico Pache Cc: Ryan Roberts Cc: Dev Jain Cc: Barry Song Cc: Usama Arif Cc: Kiryl Shutsemau Cc: Jason Gunthorpe Cc: John Hubbard Cc: Peter Xu 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 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 Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=7 Signed-off-by: Zi Yan To: Andrew Morton To: David Hildenbrand To: Lorenzo Stoakes Cc: Rik van Riel Cc: "Liam R. Howlett" Cc: Vlastimil Babka Cc: Harry Yoo Cc: Jann Horn Cc: Lance Yang Cc: Kyle Zeng 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 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 To: David Hildenbrand To: Zi Yan To: Andrew Morton To: Andrea Arcangeli To: Suren Baghdasaryan Cc: Baolin Wang Cc: "Liam R. Howlett" Cc: Nico Pache Cc: Ryan Roberts Cc: Dev Jain Cc: Barry Song Cc: Lance Yang Cc: Usama Arif Cc: Kiryl Shutsemau Cc: Peter Xu 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