From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796390606; b=dPlix38e3mHZ7/pAFllbq4AkcsOtj2QCqEUGUWh9iwn3fh4LmlP0inVvYB4ZeXdhhsfk 5ZjR7Ggya9M0/EWJC4321Og97ge1n2wA6/ZEike47GClHA0K/31uLpnQ+P7lZg217qL6d Uhgq+h+1jWW8dv4QTjEZUoSfKTNwrxz/Nkk7rqDHNt0ed+Y7gW2Gl35bSKbWn6wslCbQn eS3pPdh6B3QrjrWO2eWl4V1QXywW+d4SrDpuoIyiyarGwrnW8KrdsiQzQ0+BoUpSUY9cW l1W0pCXCOSw1CzSlvgwbjXYwPT365Od8jedtgjp+rCK3UVRuhY2WW0s0nquw2by5xew== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796390606; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=NyYJ0ENXJ4jj+wpiShf1Jmxz1pcr03uNbJiR9CEKy9o=; b=V6sLYntb3Ri7SK8sBnuDtwSIqfP90osyI8fpT5QGUObEmKkQZO2WuUDtTXs7Q28e/+YB EyL+ozuRAnSed+wXNSYREL5hwGFVFSc942fuD+uVye7TVPyJ+FDCUO+VSy9gDg8knfDKF nl9TYvZ01dksi1rlC3fUMlH8MSCTNCLIWdeZd5M0xIbuR+AhSE85PX4hS0YUdwLFN1F9W O4TaOseahS2rJcAW6eS/M2x9f0fEerXaqzkd9/QN9E+i89aTkCUMiyKJt1VVe3j26XAFC Yn840T72UslENeMnsRVsQuIn+/8S/9SkcwsP4LSC/PEpgCeAmBQo9qnPyVJJIKcvn6w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=T2yKs31q; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5A5C606A339 for ; Fri, 4 Dec 2026 13:23:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=T2yKs31q Received: by smtp.kernel.org (Postfix) with ESMTPSA id 539DE1F00A11; Fri, 4 Dec 2026 13:23:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796390606; bh=NyYJ0ENXJ4jj+wpiShf1Jmxz1pcr03uNbJiR9CEKy9o=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=T2yKs31qdH7MNdRKCTpuOwp+wPGswv23merL+UtAex9KjSY7+cyawVV7ciywOejV8 NGSJSrv2VTXOHeJCsshb7qU4rGlTu+LB2ii1u074VhjogwihIEQIBzdHDf7wQt1tXq T3256xvCiR0x11Chp+zrJe1lgo4EZxUbX0YZAb4f7qTsTgUZUrQSivomZ8Nxq2q4mO YUyIHUsBiX17DBUGESy5BaJEoGY6j/wvhZo4hZfEMu6o7lyKcUI4caOBQyezLu+HUq HFYe011ce959cLOTxwMtBrGtkp21yYECZrRqJdnobbHXdyfNG1m/DeQrpMb6PWqtcX B3hKXOE3akP3A== Date: Fri, 4 Dec 2026 13:23:18 +0000 From: "Lorenzo Stoakes (ARM)" To: Zi Yan Cc: David Hildenbrand , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Kyle Zeng , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, John Hubbard , Peter Xu , Jason Gunthorpe Subject: Re: [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Message-ID: References: <20261203-anon_vma_uaf_fix-v2-0-3d4c746e8839@nvidia.com> <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=us-ascii Content-Disposition: inline In-Reply-To: <20261203-anon_vma_uaf_fix-v2-7-3d4c746e8839@nvidia.com> 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(). As usual, have to say that MREMAP_DONTUNMAP was probably a mistake :)) > > 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 LGTM so: Reviewed-by: Lorenzo Stoakes (ARM) > 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 > */ Similar to comment on other patch, could we have a 'paired with' and ref. to the GUP-fast function to keep with memory barrier convention? I think in try_grab_folio_fast(). > 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 > -- Cheers, Lorenzo