Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796445361; b=rh0kVOJ//8yL6COGdCaq/VV3o1d85aIPXx2LOqT8/EF+N1vcHHyinZ18akublsAnRIL3 zyhvcDYSohsqyGnZMqy7M9/xzmpKfFUROvsawltzVBDrVXFox5oFigjMpwz4MIPSejt8e 3Y9rHEcihDjtklMs0skOYpUbuMcw4ac1FxvSMN9lBE9RUzFEkslrbYRoUuOFxAo80BxnQ YrR9BPCd5v/M3D+1uCiKqMVyMTLbjszqctKb2fWDn80rUZR4oYrHQLRquaGqk4TsXQ6sS gxXJJy2SBSdzSLt/XaG6ye2dHjaKIcjujQ/ytIz/PGJCc7OwEZNElPScnVJ3QTIrp6w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796445361; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=dYV3h9HHsK9UGIcWg4QdyEEorogiCdmIzPrEl3ttXoc=; b=pQpQdtmZvVvAd74B4Vun4Csa7Rlv5+GF80y7HYFwwJ3Q3+rIEhZR9tbw0MaKV0T9jEce mHgcvrCnpOCC6ssBFiX/J9Tikc8Wh/fC2S4+QbrMv9nysRHSVtb29GM0zFnD/QREKIeS/ mJ+K15QEjOI7IiE60N9mAKep6SpnSiZTIsekZ0gn2PiXpi8SvAnGd/3kOVDSgYj+daQjz jhiDracvvD9DIuigV5LGSjVhoNTDO8erUclMuPV8fMoWkJ6jjQI6p6Ci6tlzGxyfNV88d oMFEue+ivTKJjvRmf2uLTIgo9fORFdui7ziUoyALRmkrlyotYsdXemb+F1wvqZb8GTQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=EtjGQxMq; arc=none smtp.remote-ip=91.218.175.69 Received: from mta0.migadu.com (out-69.mta0.migadu.com [91.218.175.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37FD9606A322 for ; Sat, 5 Dec 2026 04:35:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 37FD9606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=EtjGQxMq X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=ugB3km0OOltmPBH0IXOEPQvKKD6osu0Fh99kF445nPc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796445358; v=1; x=1797050158; b=EtjGQxMqLh3MLCTvIoK+J77QQHvKqvbbbZ0j7x2fS/3RzcBVzfCJvZdM4LQHJuO14oly5/WD fmIbZW1liykKu3c195svHQhL7qP7GB8WzGATsiIz/YAAxgwYHtCwzR6C6SrH3RkJrshSwmvncrX rVtL9MZx84FT5I3niPHaDERg= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 47a5f27fbc0792b6; Sat, 05 Dec 2026 04:35:56 +0000 X-Mizu-Trace-ID: 47a5f27fbc0792b6 X-Migadu-Flow: FLOW_OUT From: Lance Yang To: ziy@nvidia.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, lance.yang@linux.dev, usama.arif@linux.dev, kas@kernel.org, ljs@kernel.org, riel@surriel.com, vbabka@kernel.org, harry@kernel.org, jannh@google.com, kylebot@openai.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 12:35:47 +0800 Message-ID: <20261205043547.70919-1-lance.yang@linux.dev> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20261203-anon_vma_uaf_fix-v2-7-3d4c746e8839@nvidia.com> References: <20261203-anon_vma_uaf_fix-v2-7-3d4c746e8839@nvidia.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Thu, Dec 03, 2026 at 11:17:55PM -0500, Zi Yan wrote: >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); 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