From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.58 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796448055; b=UFQ8H8unARJk6mo968GbH/Fv1dXx6Ng/dp0mmY22EAp1sREFYcmJCOpQweKv5+CuAltQ lLcaRThlb26DtUNEOlDwMEiTw6x4f8gdADBA3lDwbJ07KbXaj5jQODcmFtum3uxSZsNG/ O13PH/sxvgHJ4jHX4NVRHM9jdKX55hQQ+emGuFSIeW/efqlnot4MYqx3TspocdokvcQuL uR6X9Rb6vtsKYDo5ljAbQW7hC4+qkN25nuGMRNBHGi/oZoK+GCnTi/7i8Tgk8KQaI3+z3 rwFyXg7ANh4kMeuSMZohtgYEDISOamllOxbTMZWwm+7FYalvtUIJHCXTf6t2x6F/Q8g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796448055; 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=2lm5eAIK2m1tYYfc5eHecS5d5ZNEKOLRd1PIdcb3WfQ=; b=RsGdrtjDrmThYhzGUNXCMtF8CLKzNPMet78ghqCKUqs/cIqUCr/KnZntnQ1CmbMzEM4y WMyrQqwLlvjadaaituvDfUJYi23EyurSrjTJBNz3jSOOmNJwMuusy/fq++0QSdx7XARc4 dWbrXreosxjnIvv5CK2n2uqa7Op5A6lxmDnZK/1YhzIHwo4EFwCEyHFSQM6HjeXVW280L TLM41jBuU5adHiVMDOaqXASfcVScTlBGnL1x2nZLRi0PNFCq0Ra+ugxBaIaqWtTZB3OWc IdkSR0SPXg57mAsueCAElK8B/DUbaGxqkSlmdICymWzLjoLcrXxYsA9SQ40QRNh+i5g== 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=pBpFexMd; arc=none smtp.remote-ip=91.218.175.58 Received: from mta0.migadu.com (out-58.mta0.migadu.com [91.218.175.58]) (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 AB56B606A32D for ; Sat, 5 Dec 2026 05:20:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AB56B606A32D 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=pBpFexMd X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=Qbe/ZMF34tkshDN1Vky7+cMH/dHjb8egCPgbBqN7No0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796448051; v=1; x=1797052851; b=pBpFexMd4SWsHh5vlLrEoU7bLvGvxjIssxM9ToIr/ypyuzJQ0lksliOzR7de/bcW2T07Xqj0 Sis4/NqNBMdD4wlTnnhvFQFv6CQ0yLOe08ehECz+coX3uwp3N8/l22362cbJE5mZwQnHjrbMIfO MbwPbdBY0kA/ro3NufI0kO7g= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id edbc8357e93f9753; Sat, 05 Dec 2026 05:20:39 +0000 X-Mizu-Trace-ID: edbc8357e93f9753 X-Migadu-Flow: FLOW_OUT From: Lance Yang 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 Message-ID: <20261205052029.86370-1-lance.yang@linux.dev> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20261205043547.70919-1-lance.yang@linux.dev> References: <20261205043547.70919-1-lance.yang@linux.dev> 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 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 >> >>__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); 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 >