# Re: \[RFC PATCH v1 3/4\] mm/volatile: purge volatile anonymous folios during reclaim

- From: sashiko-bot@kernel.org
- Date: 2026-12-01 08:36:24 
- List: linux-trace-kernel
- Page: https://ratatoskr.run/linux-trace-kernel/2026/12/18058093
- Thread: https://ratatoskr.run/linux-mm/2026/11/18048701/t.md

```text
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 [ref]

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
```
