From: sashiko-bot@kernel.org
To: "Hirotaka Yamagata" <hiro@pisnp.net>,
"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Liam R. Howlett" <liam@infradead.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"David Hildenbrand" <david@kernel.org>,
"Chris Li" <chrisl@kernel.org>,
"Kairui Song" <kasong@tencent.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Masami Hiramatsu" <mhiramat@kernel.org>,
"Arnd Bergmann" <arnd@arndb.de>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Hocko" <mhocko@kernel.org>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Shakeel Butt" <shakeel.butt@linux.dev>,
linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org,
"John Stultz" <jstultz@google.com>,
"Minchan Kim" <minchan@kernel.org>,
"Alexandre Marciano Gimenez" <raging@google.com>,
"Tatsuyuki Ishi" <ishitatsuyuki@google.com>,
tj@kernel.org, mkoutny@suse.com
Subject: Re: [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim
Date: Tue, 01 Dec 2026 08:36:13 +0000 [thread overview]
Message-ID: <sashiko-outbox-242855@kernel.org> (raw)
In-Reply-To: <20261130083407.2669584-4-hiro@pisnp.net>
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 <hiro@pisnp.net>
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"
>
> +/* 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 = walk->private;
> + pte_t pte = ptep_get(ptep);
> +
> + if (!pte_present(pte)) {
> + softleaf_t entry = softleaf_from_pte(pte);
> +
> + if (softleaf_is_purged_marker(entry)) {
> + vw->page_was_purged = 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 = {
> + .pte_entry = mvolatile_check_purged_pte,
> + .walk_lock = 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 = 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 = vma;
> }
>
> + if (behavior == MADV_NONVOLATILE)
> + ret = mvolatile_check_purged(mm, start, end);
> +
> out:
> mmap_write_unlock(mm);
> + /* Outside mmap_lock: static_branch_enable() takes cpus_read_lock(). */
> + if (validated && behavior == 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 VMAs
> + * @folio: locked folio isolated for reclaim
> + *
> + * If every VMA mapping the order-0 anonymous @folio is volatile, replace its
> + * PTEs with PTE_MARKER_PURGED markers, drop its swap cache and freeze its
> + * 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 = 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 = folio_lock_anon_vma_read(folio, NULL);
> + if (!anon_vma)
> + return PURGE_NONE;
> +
> + pgoff_start = folio_pgoff(folio);
> + pgoff_end = 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 = avc->vma;
> +
> + if (!vma_is_volatile(vma)) {
> + anon_vma_unlock_read(anon_vma);
> + return PURGE_NONE;
> + }
> + mapped = 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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130083407.2669584-1-hiro@pisnp.net?part=3
next prev parent reply other threads:[~2026-12-01 8:36 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 8:34 [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 Hirotaka Yamagata
2026-11-30 8:34 ` [RFC PATCH v1 1/4] mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA handling Hirotaka Yamagata
2026-12-01 8:36 ` sashiko-bot
2026-11-30 8:34 ` [RFC PATCH v1 2/4] mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages Hirotaka Yamagata
2026-12-01 8:36 ` sashiko-bot
2026-11-30 8:34 ` [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Hirotaka Yamagata
2026-12-01 8:36 ` sashiko-bot [this message]
2026-11-30 8:34 ` [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint Hirotaka Yamagata
2026-12-01 8:36 ` sashiko-bot
2026-11-30 10:39 ` [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 David Hildenbrand (Arm)
2026-11-30 18:18 ` Lorenzo Stoakes (ARM)
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=sashiko-outbox-242855@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=chrisl@kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hiro@pisnp.net \
--cc=ishitatsuyuki@google.com \
--cc=jstultz@google.com \
--cc=kasong@tencent.com \
--cc=liam@infradead.org \
--cc=linux-mm@kvack.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mhiramat@kernel.org \
--cc=mhocko@kernel.org \
--cc=minchan@kernel.org \
--cc=mkoutny@suse.com \
--cc=raging@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=tj@kernel.org \
/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