From: Hirotaka Yamagata <hiro@pisnp.net>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-api@vger.kernel.org, "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-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-fsdevel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org, linux-arch@vger.kernel.org,
cgroups@vger.kernel.org, John Stultz <jstultz@google.com>,
Minchan Kim <minchan@kernel.org>,
Alexandre Marciano Gimenez <raging@google.com>,
Tatsuyuki Ishi <ishitatsuyuki@google.com>
Subject: [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim
Date: Mon, 30 Nov 2026 17:34:06 +0900 [thread overview]
Message-ID: <20261130083407.2669584-4-hiro@pisnp.net> (raw)
In-Reply-To: <20261130083407.2669584-1-hiro@pisnp.net>
Connect the VM_VOLATILE VMA state and PTE_MARKER_PURGED entries to page
reclaim and MADV_NONVOLATILE:
1. In shrink_folio_list(), once a folio has been selected for reclaim by
normal LRU/MGLRU aging, call purge_volatile_folio() before allocating
swap space. It returns PURGE_DONE (free the folio), PURGE_RETRY (keep
it for a later attempt) or PURGE_NONE (reclaim it normally).
2. In __purge_volatile_folio(), skip non-anonymous and KSM folios. For a
mapped folio, walk its anon_vma rmap under the anon_vma read lock and
check that every VMA mapping it has VM_VOLATILE set; if one does not
(e.g. after fork()), reclaim the folio normally. Otherwise unmap an
order-0 folio with TTU_PURGE to install PTE_MARKER_PURGED entries,
free its swap cache entry and freeze its refcount so that
shrink_folio_list() frees it.
folio_ref_freeze() can fail on a reference taken without pinning
(speculative or FOLL_GET references, PFN walkers); such a holder
keeps the old contents, as after MADV_DONTNEED. The markers are in
place by then, so return PURGE_RETRY and let shrink_folio_list() keep
the folio, as it does when __remove_mapping() cannot freeze the
refcount, rather than swap it out. On the next attempt the folio is
unmapped and out of the swap cache, so the rmap walk is skipped and
only the freeze is retried. This also frees any other unmapped,
swap-backed anonymous folio outside the swap cache that reclaim meets
once volatile ranges are in use; no PTE or swap entry refers to such
a folio, so nothing is lost.
A folio that may be DMA-pinned is not purged: __purge_volatile_folio()
returns PURGE_NONE for it, so the regular pin handling keeps or
activates it, and it is not reported by the mm_purge_volatile_folio
tracepoint. try_to_unmap(TTU_PURGE) also backs off on a racing GUP
pin, and the folio is kept for a later pass. Mlocked mappings are
respected: try_to_unmap() keeps a folio mapped by a VM_LOCKED VMA and
moves it to the unevictable LRU.
3. In madvise_volatile() for MADV_NONVOLATILE, walk the range with
walk_page_range() to find and clear PTE_MARKER_PURGED entries, and
return 1 if any page in the range was purged. If the call fails after
changing part of the range, it returns the error and leaves the
markers in place, so a retry reports them.
Guard purge_volatile_folio() with a static branch (volatile_ranges_key)
so that systems not using volatile ranges do not pay for it in the
reclaim path. The first MADV_VOLATILE that passes validation enables it
after dropping mmap_lock. The mvolatile.h declarations depend on
CONFIG_64BIT && CONFIG_MMU, matching mm/Makefile.
Cc: John Stultz <jstultz@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Alexandre Marciano Gimenez <raging@google.com>
Cc: Tatsuyuki Ishi <ishitatsuyuki@google.com>
Signed-off-by: Hirotaka Yamagata <hiro@pisnp.net>
---
include/linux/mvolatile.h | 32 +++++++-
mm/mvolatile.c | 156 +++++++++++++++++++++++++++++++++++++-
mm/vmscan.c | 14 ++++
3 files changed, 199 insertions(+), 3 deletions(-)
diff --git a/include/linux/mvolatile.h b/include/linux/mvolatile.h
index bc8828c8e6c4..bf2625aad07b 100644
--- a/include/linux/mvolatile.h
+++ b/include/linux/mvolatile.h
@@ -2,17 +2,47 @@
#ifndef _LINUX_MVOLATILE_H
#define _LINUX_MVOLATILE_H
+#include <linux/jump_label.h>
#include <linux/mm_types.h>
-#ifdef CONFIG_64BIT
+/**
+ * enum purge_result - outcome of purge_volatile_folio()
+ * @PURGE_NONE: not purgeable, reclaim the folio normally (still locked).
+ * @PURGE_RETRY: purgeable, but a transient reference prevented the purge;
+ * keep the folio for a later attempt (still locked).
+ * @PURGE_DONE: purged; the folio is unlocked, out of the swap cache and its
+ * refcount is frozen to zero.
+ */
+enum purge_result {
+ PURGE_NONE,
+ PURGE_RETRY,
+ PURGE_DONE,
+};
+
+/* mm/mvolatile.o is built only for 64BIT && MMU, see mm/Makefile. */
+#if defined(CONFIG_64BIT) && defined(CONFIG_MMU)
+DECLARE_STATIC_KEY_FALSE(volatile_ranges_key);
+
int madvise_volatile(struct mm_struct *mm, unsigned long start,
unsigned long end, int behavior);
+enum purge_result __purge_volatile_folio(struct folio *folio);
+
+static inline enum purge_result purge_volatile_folio(struct folio *folio)
+{
+ if (!static_branch_unlikely(&volatile_ranges_key))
+ return PURGE_NONE;
+ return __purge_volatile_folio(folio);
+}
#else
static inline int madvise_volatile(struct mm_struct *mm, unsigned long start,
unsigned long end, int behavior)
{
return -EINVAL;
}
+static inline enum purge_result purge_volatile_folio(struct folio *folio)
+{
+ return PURGE_NONE;
+}
#endif
#endif /* _LINUX_MVOLATILE_H */
diff --git a/mm/mvolatile.c b/mm/mvolatile.c
index fbe1028aaf5c..c111bc4f111d 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,
+};
+
+static int mvolatile_check_purged(struct mm_struct *mm, unsigned long start,
+ unsigned long end)
+{
+ struct mvolatile_walker vw = { .page_was_purged = 0 };
+
+ walk_page_range(mm, start, end, &mvolatile_walk_ops, &vw);
+ return vw.page_was_purged;
+}
+
static int check_volatile_range(struct mm_struct *mm, unsigned long start,
unsigned long end, int behavior)
{
@@ -79,14 +119,17 @@ static int check_volatile_range(struct mm_struct *mm, unsigned long start,
* The whole range is checked before any VMA is changed, so unmapped holes,
* unsupported VMAs and VMA splits beyond max_map_count fail with nothing
* changed. Only an allocation failure while splitting or merging a VMA can
- * leave a prefix of the range changed.
+ * leave a prefix of the range changed; %MADV_NONVOLATILE then returns the
+ * error without consuming purged pages, so that a retry reports them.
*
- * Return: 0 on success or a negative errno.
+ * Return: 0 on success, 1 if %MADV_NONVOLATILE found purged pages in the
+ * range, or a negative errno.
*/
int madvise_volatile(struct mm_struct *mm, unsigned long start, unsigned long end, int behavior)
{
VMA_ITERATOR(vmi, mm, start);
struct vm_area_struct *vma, *prev;
+ bool validated = false;
int ret = 0;
if (start >= TASK_SIZE || end > TASK_SIZE)
@@ -98,6 +141,7 @@ int madvise_volatile(struct mm_struct *mm, unsigned long start, unsigned long en
ret = check_volatile_range(mm, start, end, behavior);
if (ret)
goto out;
+ validated = true;
vma = vma_iter_load(&vmi);
prev = vma_prev(&vmi);
@@ -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;
+ }
+
+ 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);
+
+ /* 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;
+
+ try_to_unmap(folio, TTU_PURGE | TTU_SYNC);
+
+ /*
+ * try_to_unmap_one() backs off if the folio got pinned meanwhile or is
+ * mapped by a VM_LOCKED VMA; in the latter case it restores the mlock,
+ * so the folio goes to the unevictable LRU. Some mappings may already
+ * hold markers, so retry rather than swap the folio out.
+ */
+ if (folio_mapped(folio))
+ return PURGE_RETRY;
+
+ /* Other PTEs still refer to the swap entry, the data must stay. */
+ if (folio_test_swapcache(folio) && !folio_free_swap(folio))
+ return PURGE_NONE;
+
+freeze:
+ /*
+ * Like __remove_mapping(), back off on a transient reference. No new
+ * pin can be taken from here on; the holder of a reference taken
+ * without pinning (speculative or FOLL_GET) keeps the old contents,
+ * as after MADV_DONTNEED.
+ */
+ if (!folio_ref_freeze(folio, 1))
+ return PURGE_RETRY;
+
+ folio_unlock(folio);
+ return PURGE_DONE;
+}
diff --git a/mm/vmscan.c b/mm/vmscan.c
index f11491ee9ed5..b72078a37930 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -59,6 +59,7 @@
#include <linux/mmu_notifier.h>
#include <linux/parser.h>
#include <linux/swap_ops.h>
+#include <linux/mvolatile.h>
#include <asm/tlbflush.h>
#include <asm/div64.h>
@@ -1252,6 +1253,19 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
continue;
}
+ /*
+ * Discard folios only mapped by volatile VMAs instead of
+ * allocating swap for them.
+ */
+ switch (purge_volatile_folio(folio)) {
+ case PURGE_DONE:
+ goto free_it;
+ case PURGE_RETRY:
+ goto keep_locked;
+ case PURGE_NONE:
+ break;
+ }
+
/*
* Anonymous process memory has backing store?
* Try to allocate it some swap space here.
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-30 8:34 UTC|newest]
Thread overview: 5+ 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-11-30 8:34 ` [RFC PATCH v1 2/4] mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages Hirotaka Yamagata
2026-11-30 8:34 ` Hirotaka Yamagata [this message]
2026-11-30 8:34 ` [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint Hirotaka Yamagata
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=20261130083407.2669584-4-hiro@pisnp.net \
--to=hiro@pisnp.net \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=cgroups@vger.kernel.org \
--cc=chrisl@kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=ishitatsuyuki@google.com \
--cc=jstultz@google.com \
--cc=kasong@tencent.com \
--cc=liam@infradead.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.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=raging@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=shakeel.butt@linux.dev \
/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