The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Lance Yang <lance.yang@linux.dev>
To: ziy@nvidia.com, kylebot@openai.com
Cc: david@kernel.org, baolin.wang@linux.alibaba.com,
	liam@infradead.org, nico.pache@linux.dev, ryan.roberts@arm.com,
	dev.jain@arm.com, baohua@kernel.org, usama.arif@linux.dev,
	kas@kernel.org, ljs@kernel.org, riel@surriel.com,
	vbabka@kernel.org, harry@kernel.org, jannh@google.com,
	akpm@linux-foundation.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, jhubbard@nvidia.com,
	peterx@redhat.com, jgg@ziepe.ca
Subject: Re: [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim
Date: Sat,  5 Dec 2026 13:20:29 +0800	[thread overview]
Message-ID: <20261205052029.86370-1-lance.yang@linux.dev> (raw)
In-Reply-To: <20261205043547.70919-1-lance.yang@linux.dev>


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@openai.com>
>>
>>__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@openai.com>
>>Signed-off-by: Zi Yan <ziy@nvidia.com>
>>To: Andrew Morton <akpm@linux-foundation.org>
>>To: David Hildenbrand <david@kernel.org>
>>To: Lance Yang <lance.yang@linux.dev>
>>Cc: Zi Yan <ziy@nvidia.com>
>>Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
>>Cc: "Liam R. Howlett" <liam@infradead.org>
>>Cc: Nico Pache <nico.pache@linux.dev>
>>Cc: Ryan Roberts <ryan.roberts@arm.com>
>>Cc: Dev Jain <dev.jain@arm.com>
>>Cc: Barry Song <baohua@kernel.org>
>>Cc: Usama Arif <usama.arif@linux.dev>
>>Cc: Kiryl Shutsemau <kas@kernel.org>
>>Cc: Jason Gunthorpe <jgg@nvidia.com>
>>Cc: John Hubbard <jhubbard@nvidia.com>
>>Cc: Peter Xu <peterx@redhat.com>
>>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);

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@kernel.org/

>> 
>> 	/*
>> 	 * 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@kernel.org/
>
>Reviewed-by: Lance Yang <lance.yang@linux.dev>
>

  reply	other threads:[~2026-12-05  5:20 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-05  4:06   ` Lance Yang
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-05  4:35   ` Lance Yang
2026-12-05  5:20     ` Lance Yang [this message]
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-05  5:30   ` Lance Yang
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)
2026-12-05  5:45   ` Lance Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205052029.86370-1-lance.yang@linux.dev \
    --to=lance.yang@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=harry@kernel.org \
    --cc=jannh@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=kas@kernel.org \
    --cc=kylebot@openai.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=nico.pache@linux.dev \
    --cc=peterx@redhat.com \
    --cc=riel@surriel.com \
    --cc=ryan.roberts@arm.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox