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=1796114177; b=wpSEAxObw6MzrUEDRzUKvZYqhwvuiVN58/2WPchFdWc5lNMai6VB2B+N4r9cXVPuNjmi ea9ZAIgzyNhuAJ/YuYY6W56GL1ejaMv2fpeuDXLVjYcACVtXHlpbe27/NrEZcOQGXFEi+ 4TlAfErcqE1HYCnXRuhp04rrRUPCHgoYGexOpvYl9JYAOAm1Wckj5KwPnMP/n7ep9hX44 KwAmW8eVgYlQQLPtSeVxoJtDT0fldNWnKuQGYjRlUmPUSzJk8/uiieqBor/opdSJiZ5CR 2uY5TCkxeoqoTJoJ08y+c3rAAwOrye2jM4QhexsWRivFBM+WPS7Tfwsq7HHMrtmJs1Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796114177; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=AzQ7yhsBhl4Kjp+0KANbpE0g0CBQG+UsdHDvstrMsxs=; b=A/e68CsttNbm6g5up8k6H8LAM1LPfSTpI/gLvJG6/Yh6kr23PQXo/PXwUWiEZ9M+TDx6 bnYJzzq3BlCJ8o9zw/P/M5BnxqHHXZf40mlwS3JAdwt4DBt5PWbY+x5f9yIb9gR2w8qFp 6+p/NCe1euExeyIZkBRyJM9ExuT+lBIsNOGOzznORRFfLBqQ6ecfTP23TsVruF7lSY5j8 1f0rJES0Lt1e0vcxw4mt7wYv3h/8VU2Io7Bt4NqjYY7HMA9U1ezAQlVjLMsuCVRLfNBEY I5nkZ/o3Upa42BXDhwuZi4LSEZYD7xDf3BY4d4jlmSmepUtDdu0WIPBAK56ZCqCGjEA== 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=VZLRavXi; 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 EE0D8606A32F for ; Tue, 1 Dec 2026 08:36:16 +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=VZLRavXi Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2090E1F00925; Tue, 1 Dec 2026 08:36:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796114176; bh=AzQ7yhsBhl4Kjp+0KANbpE0g0CBQG+UsdHDvstrMsxs=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=VZLRavXi81TvW0MzziCksDlZ/nlXDzt8Bk8mvt0nsWyx8pKD5his6PcbBF1Q3eZuk jgmyAPh+rBWWWTRMQ3BPlGMK2vSqd4kzLNN0e56PAqbwObRYqfFspd0Y7ZT9UwABq2 4dUoznVSpEEre21A6lcsqTsllYm2P24dB5OLp8py4gmHfICApJO49m0YZ9RVCjW7jx i6fKZVSdxavBpM6ZqikkwzNzuheSJ1NFNfxM3J4bAU4fdKriDxT5bOo21luRK4/yLW moMsZD3+DCgfHUWiEjbwfk59zADiyVFA9iNvPNRGR+yOW7Jn2B/eu3bytw+ywtteEl taAeiBbJuZaKQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Reply-To: sashiko-reviews@lists.linux.dev To: "Hirotaka Yamagata" , "Andrew Morton" Cc: "Liam R. Howlett" , "Lorenzo Stoakes" , "David Hildenbrand" , "Chris Li" , "Kairui Song" , "Steven Rostedt" , "Masami Hiramatsu" , "Arnd Bergmann" , "Johannes Weiner" , "Michal Hocko" , "Roman Gushchin" , "Shakeel Butt" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, "John Stultz" , "Minchan Kim" , "Alexandre Marciano Gimenez" , "Tatsuyuki Ishi" , tj@kernel.org, mkoutny@suse.com In-Reply-To: <20261130083407.2669584-4-hiro@pisnp.net> References: <20261130083407.2669584-1-hiro@pisnp.net> <20261130083407.2669584-4-hiro@pisnp.net> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 08:36:13 +0000 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] mm/volatile: TOCTOU race between __purge_volatile_folio() and MADV_NONVOLATILE - [High] mm/volatile: permanent file-LRU folio leak on dirty !swapbacked PURGE_RETRY - [Medium] mm/volatile: THP split and missing VMA write lock in mvolatile_walk_ops - [Medium] mm/volatile: missing TTU_BATCH_FLUSH in __purge_volatile_folio() unmap - [Medium] mm/volatile: redundant anon_vma locking in __purge_volatile_folio() -- commit 5a06e394b8fc0014b7153297b7cd49e620e2f1c3 Author: Hirotaka Yamagata mm/volatile: purge volatile anonymous folios during reclaim This commit hooks volatile anonymous folios into page reclaim via purge_volatile_folio() and clears PTE_MARKER_PURGED entries during madvise(MADV_NONVOLATILE). > diff --git a/mm/mvolatile.c b/mm/mvolatile.c > index fbe1028aaf5c3..c111bc4f111d4 100644 > --- a/mm/mvolatile.c > +++ b/mm/mvolatile.c > @@ -18,6 +18,46 @@ > #include "internal.h" > #include "vma.h" > =20 > +/* Enabled by the first MADV_VOLATILE, never disabled. */ > +DEFINE_STATIC_KEY_FALSE(volatile_ranges_key); > + > +struct mvolatile_walker { > + int page_was_purged; > +}; > + > +static int mvolatile_check_purged_pte(pte_t *ptep, unsigned long addr, > + unsigned long next, struct mm_walk *walk) > +{ > + struct mvolatile_walker *vw =3D walk->private; > + pte_t pte =3D ptep_get(ptep); > + > + if (!pte_present(pte)) { > + softleaf_t entry =3D softleaf_from_pte(pte); > + > + if (softleaf_is_purged_marker(entry)) { > + vw->page_was_purged =3D 1; > + > + /* Clear the marker, the next access maps a zero page. */ > + pte_clear(walk->mm, addr, ptep); > + } > + } > + return 0; > +} > + > +static const struct mm_walk_ops mvolatile_walk_ops =3D { > + .pte_entry =3D mvolatile_check_purged_pte, > + .walk_lock =3D PGWALK_RDLOCK, > +}; [Severity: Medium] Does mvolatile_walk_ops cause walk_page_range() to split all huge PMDs and PUDs in the range, and can it clear PTE markers without a per-VMA write lock? When .pte_entry is set without .pmd_entry or .pud_entry callbacks that set walk->action =3D ACTION_CONTINUE on huge mappings, walk_pmd_range() and walk_pud_range() unconditionally call split_huge_pmd() and split_huge_pud() before walking the base PTEs. Since __purge_volatile_folio() skips large folios, present huge PMDs cannot hold PTE_MARKER_PURGED entries and do not need to be split. Also, in madvise_volatile(), vma_start_write(vma) is only called when vma_is_volatile(vma) is true. If an earlier MADV_NONVOLATILE failed mid- range after clearing VMA_VOLATILE_BIT on a prefix of VMAs, a retry (or a range spanning an already non-volatile VMA) walks those VMAs under PGWALK_RDLOCK without vma_start_write(vma), allowing concurrent per-VMA page faults to race with pte_clear() and observe PTE_MARKER_PURGED. [ ... ] > @@ -137,7 +181,115 @@ int madvise_volatile(struct mm_struct *mm, unsigned= long start, unsigned long en > prev =3D vma; > } > =20 > + if (behavior =3D=3D MADV_NONVOLATILE) > + ret =3D mvolatile_check_purged(mm, start, end); > + > out: > mmap_write_unlock(mm); > + /* Outside mmap_lock: static_branch_enable() takes cpus_read_lock(). */ > + if (validated && behavior =3D=3D MADV_VOLATILE && > + !static_key_enabled(&volatile_ranges_key)) > + static_branch_enable(&volatile_ranges_key); > return ret; > } > + > +/** > + * __purge_volatile_folio() - Discard a folio only mapped by volatile VM= As > + * @folio: locked folio isolated for reclaim > + * > + * If every VMA mapping the order-0 anonymous @folio is volatile, replac= e its > + * PTEs with PTE_MARKER_PURGED markers, drop its swap cache and freeze i= ts > + * refcount. > + * > + * Context: Caller holds the folio lock; @folio is not on the LRU. > + * Return: see &enum purge_result. > + */ > +enum purge_result __purge_volatile_folio(struct folio *folio) > +{ > + struct anon_vma *anon_vma; > + struct anon_vma_chain *avc; > + pgoff_t pgoff_start, pgoff_end; > + bool mapped =3D false; > + > + VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); > + VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio); > + > + if (!folio_test_anon(folio) || folio_test_ksm(folio)) > + return PURGE_NONE; > + > + /* Like swap-out, keep a folio the pin holder may still be using. */ > + if (folio_maybe_dma_pinned(folio)) > + return PURGE_NONE; > + > + /* > + * An earlier attempt may have installed the markers and dropped the > + * swap cache, and then failed to freeze the refcount. The folio can no > + * longer be found through its anon_vma, but no PTE or swap entry refers > + * to it either, so just retry the freeze. Lazyfree folios take their > + * own path in shrink_folio_list(). > + */ > + if (!folio_mapped(folio)) { > + if (!folio_test_swapbacked(folio) || folio_test_swapcache(folio) || > + folio_test_large(folio)) > + return PURGE_NONE; > + goto freeze; > + } [Severity: High] Can a mapped, dirty, non-swapbacked anonymous folio (such as an MADV_FREE folio rewritten after MADV_FREE) be permanently leaked on the file LRU if folio_ref_freeze() fails and returns PURGE_RETRY? folio_test_swapbacked(folio) is only checked when !folio_mapped(folio). When the folio is still mapped, __purge_volatile_folio() proceeds to try_to_unmap(folio, TTU_PURGE | TTU_SYNC). In try_to_unmap_one(), if the PTE is dirty, folio_mark_dirty(folio) sets PG_dirty before the TTU_PURGE branch installs the marker and unmaps the page without calling ttu_anon_lazyfree_folio(), leaving the folio unmapped with PG_dirty set and PG_swapbacked clear. If folio_ref_freeze() then fails on a transient reference and returns PURGE_RETRY, the folio is put back on the file LRU. On every later reclaim pass, __purge_volatile_folio() sees !folio_mapped(folio) && !folio_test_swapbacked(folio) and returns PURGE_NONE, after which shrink_folio_list() hits: mm/vmscan.c:shrink_folio_list() { ... if (folio_test_dirty(folio)) { if (folio_is_file_lru(folio)) { node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE, nr_pages); if (!folio_test_reclaim(folio)) folio_set_reclaim(folio); goto activate_locked; } ... } Because the unmapped anonymous folio has no file mapping and is not swapbacked, nothing ever clears PG_dirty, so reclaim reactivates it on the file LRU forever without reaching the folio_test_lazyfree() check. > + > + anon_vma =3D folio_lock_anon_vma_read(folio, NULL); > + if (!anon_vma) > + return PURGE_NONE; > + > + pgoff_start =3D folio_pgoff(folio); > + pgoff_end =3D pgoff_start + folio_nr_pages(folio) - 1; > + > + /* > + * Keep the contents if any VMA mapping the folio, e.g. in a child > + * after fork(), is not volatile. > + */ > + anon_rmap_tree_foreach(avc, anon_vma, pgoff_start, pgoff_end) { > + struct vm_area_struct *vma =3D avc->vma; > + > + if (!vma_is_volatile(vma)) { > + anon_vma_unlock_read(anon_vma); > + return PURGE_NONE; > + } > + mapped =3D true; > + } > + anon_vma_unlock_read(anon_vma); [Severity: High] Can __purge_volatile_folio() race with madvise(MADV_NONVOLATILE) and install PTE_MARKER_PURGED after MADV_NONVOLATILE has already returned 0? CPU0 (reclaim) CPU1 (madvise) __purge_volatile_folio(folio) folio_lock_anon_vma_read() vma_is_volatile(vma) -> true anon_vma_unlock_read() madvise_volatile(..., MADV_NONVOLATILE) vma_clear_flags(vma, VMA_VOLATILE_BIT) mvolatile_check_purged() /* PTE is still present */ return 0; try_to_unmap(folio, TTU_PURGE | ...) try_to_unmap_one() set_pte_at(..., make_purged_entry()) <-- purges non-volatile VMA Neither vma_clear_flags() nor mvolatile_check_purged() serializes against an in-flight __purge_volatile_folio() that has already checked vma_is_volatile(vma), and try_to_unmap_one() does not re-check vma_is_volatile(vma) under the page table lock before installing PTE_MARKER_PURGED. Userspace is told that the range was made non-volatile with no pages purged, and then gets SIGBUS on the next access. > + > + /* Not expected for a mapped folio, but never purge without a VMA. */ > + if (!mapped) > + return PURGE_NONE; > + > + /* Only order-0 folios are supported for now. */ > + if (folio_test_large(folio)) > + return PURGE_NONE; [Severity: Medium] Why check folio_test_large(folio) only after locking anon_vma and walking the rmap tree? Once volatile_ranges_key is enabled, every mapped large anonymous folio scanned by shrink_folio_list() takes anon_vma->root->rwsem and walks its anon_rmap_tree here just to return PURGE_NONE, and for order-0 folios that do get purged, dropping anon_vma_unlock_read() before try_to_unmap() makes rmap_walk_anon() immediately re-acquire the same lock. > + > + try_to_unmap(folio, TTU_PURGE | TTU_SYNC); [Severity: Medium] Should try_to_unmap() pass TTU_BATCH_FLUSH here? Without TTU_BATCH_FLUSH, should_defer_flush() returns false and try_to_unmap_one() issues a synchronous flush_tlb_range() and cross-CPU TLB shootdown on every purged 4KB folio, even though PURGE_DONE jumps to free_it in shrink_folio_list() which flushes batched TLB entries via try_to_unmap_flush() before freeing free_folios. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130083407.2669= 584-1-hiro@pisnp.net?part=3D3