Re: [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim

From: Lance Yang

Date: Sat Dec 05 2026 - 00:21:01 EST



On Sat, Dec 05, 2026 at 12:35:47PM +0800, Lance Yang wrote:
>
>On Thu, Dec 03, 2026 at 11:17:55PM -0500, Zi Yan wrote:
>>From: Kyle Zeng <kylebot@xxxxxxxxxx>
>>
>>__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 <kylebot@xxxxxxxxxx>
>>Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
>>To: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>>To: David Hildenbrand <david@xxxxxxxxxx>
>>To: Lance Yang <lance.yang@xxxxxxxxx>
>>Cc: Zi Yan <ziy@xxxxxxxxxx>
>>Cc: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
>>Cc: "Liam R. Howlett" <liam@xxxxxxxxxxxxx>
>>Cc: Nico Pache <nico.pache@xxxxxxxxx>
>>Cc: Ryan Roberts <ryan.roberts@xxxxxxx>
>>Cc: Dev Jain <dev.jain@xxxxxxx>
>>Cc: Barry Song <baohua@xxxxxxxxxx>
>>Cc: Usama Arif <usama.arif@xxxxxxxxx>
>>Cc: Kiryl Shutsemau <kas@xxxxxxxxxx>
>>Cc: Jason Gunthorpe <jgg@xxxxxxxxxx>
>>Cc: John Hubbard <jhubbard@xxxxxxxxxx>
>>Cc: Peter Xu <peterx@xxxxxxxxxx>
>>Cc: linux-mm@xxxxxxxxx
>>Cc: linux-kernel@xxxxxxxxxxxxxxx
>>---
>> 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);

Ouch ... Sashiko has a point on flush_cache_range() in patch #08 [1].

Just like set_pmd_migration_entry(), we should call flush_cache_range()
before pmdp_invalidate() in __discard_anon_folio_pmd_locked() so caches
get flushed while the mapping is still valid (on archs where it isn't a
no-op) ...

Something like:

- orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
+ flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE);
+ orig_pmd = pmdp_invalidate(vma, addr, pmdp);

[1] https://lore.kernel.org/all/sashiko-outbox-251076@xxxxxxxxxx/

>>
>> /*
>> * 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);
>
>Right. Clearing the PMD while we could still bail out was kinda asking for
>trouble without mmap_lock (that one's on me ...) ;) Thanks!
>
>I would assume the arch issues Sashiko flagged [1] aren't hard to fix :P
>
>Invalidate first, then clear once we're sure. That's how it should work
>anyway.
>
>[1] https://lore.kernel.org/all/sashiko-outbox-251076@xxxxxxxxxx/
>
>Reviewed-by: Lance Yang <lance.yang@xxxxxxxxx>
>