* [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1
@ 2026-11-30 8:34 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
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Hirotaka Yamagata @ 2026-11-30 8:34 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-api, 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-kernel, linux-mm, linux-fsdevel,
linux-trace-kernel, linux-arch, cgroups, John Stultz,
Minchan Kim, Alexandre Marciano Gimenez, Tatsuyuki Ishi
This RFC series revives and modernizes the volatile ranges patchset
originally proposed by John Stultz and Minchan Kim [1].
Volatile ranges allow userspace caches holding regenerable data (such as
decoded image bitmaps or other private caches) to mark private anonymous
memory as safe for the kernel to discard under memory pressure,
bypassing swap. Such ranges are marked via madvise(MADV_VOLATILE).
Before reusing the buffer, userspace calls madvise(MADV_NONVOLATILE),
which unmarks the range and returns 1 if any pages in the range were
purged while volatile (or 0 if all pages remained intact).
Unlike MADV_FREE, which silently zero-fills dropped pages on re-access,
calling MADV_NONVOLATILE before reuse gives userspace deterministic
notification of whether cached data was lost without having to touch
every page.
If userspace instead accesses a purged page while the range is still
volatile, the fault raises SIGBUS; after MADV_NONVOLATILE, the next
access to it maps a zero page. Zero-filling on fault was considered,
but this series chooses SIGBUS to catch missing MADV_NONVOLATILE calls
and partially purged reads instead of silently seeing zeros, and to
keep the optimistic read model from earlier series possible so readers
can skip per-access MADV_NONVOLATILE/MADV_VOLATILE calls.
Volatile pages share the regular anon LRU, so currently they are not
purged when swap is disabled or full. Two alternatives were considered,
but neither was satisfactory:
- Keeping volatile pages on a separate LRU: changes eviction order and
creates balancing issues against page cache and swap.
- Keeping a linked list of volatile pages only, in addition to the anon
LRU: allows scanning for volatile pages cheaply when swap is full, but
making pages volatile then requires scanning, which is expensive.
As a future extension, finer-grained purge reporting could avoid
regenerating an entire large buffer when only part of it was purged.
Within madvise(2), up to 31 bits of a positive int return value could
encode a coarse mask over N equal sub-ranges; configurable granularity
or an arbitrary-length bitmap would instead call for a dedicated syscall
taking an output bitmap pointer (similar to mincore(2)).
Key changes from the earlier v14 series [1]:
- Use PTE markers (PTE_MARKER_PURGED) instead of consuming a swap type.
- Defer purging in shrink_folio_list() until after checking references
(immediately before folio_alloc_swap()), allowing frequently accessed
volatile pages to age normally via LRU/MGLRU.
- Port VMA handling to Maple Tree and modern folio APIs.
- Add a PGPURGE vmstat/memcg counter and a tracepoint.
This series was evaluated with Chromium patched to back discardable
memory with MADV_VOLATILE/MADV_NONVOLATILE, running the
multiwindow-memory-stress benchmark from chromium/web-tests (30 tabs
across 5 windows alongside 4 background Chromium instances in an 8 GiB
VM with 8 GiB swap). Backing decoded image caches with volatile ranges
reduced the software image decode count per run by 18.0% and total
software image decode time per run by 24.9%
(Renderer4.ImageDecodeTaskDurationUs.Software). Under slab_nomerge,
VMA-related slab overhead (vm_area_struct, anon_vma_chain, anon_vma,
maple_node) was +228 KiB mean (+1.55%, ~0.003% of ~7.2 GiB total
Chromium PSS) and +76 KiB peak (+0.42%).
[1] https://lore.kernel.org/lkml/1398806483-19122-1-git-send-email-john.stultz@linaro.org/
("[PATCH 0/4] Volatile Ranges (v14 - madvise reborn edition!)")
Hirotaka Yamagata (4):
mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA
handling
mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages
mm/volatile: purge volatile anonymous folios during reclaim
mm/volatile: add PGPURGE vmstat counter and purge tracepoint
MAINTAINERS | 2 +
fs/proc/task_mmu.c | 1 +
include/linux/leafops.h | 14 ++
include/linux/mm.h | 12 +
include/linux/mm_inline.h | 5 +-
include/linux/mvolatile.h | 48 ++++
include/linux/rmap.h | 1 +
include/linux/swapops.h | 18 +-
include/linux/vm_event_item.h | 1 +
include/trace/events/mmflags.h | 7 +
include/trace/events/vmscan.h | 24 ++
include/uapi/asm-generic/mman-common.h | 3 +
mm/Makefile | 2 +-
mm/khugepaged.c | 17 ++
mm/madvise.c | 29 +++
mm/memcontrol.c | 1 +
mm/memory.c | 5 +
mm/mprotect.c | 6 +-
mm/mseal.c | 20 ++
mm/mvolatile.c | 311 +++++++++++++++++++++++++
mm/rmap.c | 14 ++
mm/vmscan.c | 14 ++
mm/vmstat.c | 1 +
23 files changed, 550 insertions(+), 6 deletions(-)
create mode 100644 include/linux/mvolatile.h
create mode 100644 mm/mvolatile.c
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
--
2.56.0.611.g1030f0b940-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH v1 1/4] mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA handling
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 ` 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
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Hirotaka Yamagata @ 2026-11-30 8:34 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-api, 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-kernel, linux-mm, linux-fsdevel,
linux-trace-kernel, linux-arch, cgroups, John Stultz,
Minchan Kim, Alexandre Marciano Gimenez, Tatsuyuki Ishi
Userspace caches that hold regenerable data (such as decoded images or
compiled assets) need a way to let the kernel discard cached pages under
memory pressure without swap I/O, while being notified whether any pages
were discarded when the range is marked active again.
Introduce the MADV_VOLATILE (26) and MADV_NONVOLATILE (27) madvise()
behaviors and the corresponding VM_VOLATILE (VMA_VOLATILE_BIT) VMA flag
on 64-bit architectures.
Check the whole range in check_volatile_range() before changing any VMA.
Unmapped holes fail with -ENOMEM. File-backed, hugetlb, mixedmap and
special VMAs fail with -EINVAL. MADV_VOLATILE additionally fails on
mlocked VMAs (-EINVAL), because mlocked memory is not purged, and on
mseal()ed VMAs (-EPERM), because sealed memory must not be discarded.
MADV_NONVOLATILE stays allowed on mlocked and sealed VMAs. Conversely,
mseal() fails with -EBUSY and seals nothing if its range contains a
volatile page: MADV_NONVOLATILE first, then retry.
The check also fails with -ENOMEM up front if the boundary splits would
exceed max_map_count, so the usual failures change nothing. Only an
allocation failure inside a split or merge can leave a prefix of the
range changed. The start address is untagged, as for the other
behaviors.
Disallow MADV_VOLATILE and MADV_NONVOLATILE in process_madvise()
(vector_madvise()), as its byte-count return value cannot unambiguously
represent the positive purge status (1) returned by MADV_NONVOLATILE.
Also expose the "vo" mnemonic in /proc/<pid>/smaps and mmflags
tracepoints.
Based on the earlier volatile ranges series by John Stultz and Minchan
Kim.
Link: https://lore.kernel.org/1398806483-19122-1-git-send-email-john.stultz@linaro.org
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>
---
MAINTAINERS | 2 +
fs/proc/task_mmu.c | 1 +
include/linux/mm.h | 12 +++
include/linux/mvolatile.h | 18 ++++
include/trace/events/mmflags.h | 7 ++
include/uapi/asm-generic/mman-common.h | 3 +
mm/Makefile | 2 +-
mm/madvise.c | 29 +++++
mm/mseal.c | 20 ++++
mm/mvolatile.c | 143 +++++++++++++++++++++++++
10 files changed, 236 insertions(+), 1 deletion(-)
create mode 100644 include/linux/mvolatile.h
create mode 100644 mm/mvolatile.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 3a19da74d00c..390c2197b3d8 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17528,8 +17528,10 @@ L: linux-mm@kvack.org
S: Maintained
W: http://www.linux-mm.org
T: git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
+F: include/linux/mvolatile.h
F: include/uapi/asm-generic/mman-common.h
F: mm/madvise.c
+F: mm/mvolatile.c
MEMORY TECHNOLOGY DEVICES (MTD)
M: Miquel Raynal <miquel.raynal@bootlin.com>
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 5c54aebe2118..4bd0085f008a 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1248,6 +1248,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
#endif
#ifdef CONFIG_64BIT
[ilog2(VM_SEALED)] = "sl",
+ [ilog2(VM_VOLATILE)] = "vo",
#endif
};
size_t i;
diff --git a/include/linux/mm.h b/include/linux/mm.h
index dd09c438fa23..0f9373c40274 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -355,6 +355,9 @@ enum {
DECLARE_VMA_BIT(UFFD_MINOR, 41),
DECLARE_VMA_BIT(SEALED, 42),
DECLARE_VMA_BIT(UFFD_RWP, 43),
+#ifdef CONFIG_64BIT
+ DECLARE_VMA_BIT(VOLATILE, 44),
+#endif
/* Flags that reuse flags above. */
DECLARE_VMA_BIT_ALIAS(PKEY_BIT0, HIGH_ARCH_0),
DECLARE_VMA_BIT_ALIAS(PKEY_BIT1, HIGH_ARCH_1),
@@ -532,9 +535,13 @@ enum {
#ifdef CONFIG_64BIT
#define VM_ALLOW_ANY_UNCACHED INIT_VM_FLAG(ALLOW_ANY_UNCACHED)
#define VM_SEALED INIT_VM_FLAG(SEALED)
+#define VM_VOLATILE INIT_VM_FLAG(VOLATILE)
+#define VMA_VOLATILE mk_vma_flags(VMA_VOLATILE_BIT)
#else
#define VM_ALLOW_ANY_UNCACHED VM_NONE
#define VM_SEALED VM_NONE
+#define VM_VOLATILE VM_NONE
+#define VMA_VOLATILE EMPTY_VMA_FLAGS
#endif
#if defined(CONFIG_64BIT) || defined(CONFIG_PPC32)
#define VM_DROPPABLE INIT_VM_FLAG(DROPPABLE)
@@ -1561,6 +1568,11 @@ static inline bool vma_is_anonymous(const struct vm_area_struct *vma)
return !vma->vm_ops;
}
+static inline bool vma_is_volatile(const struct vm_area_struct *vma)
+{
+ return vma_test_single_mask(vma, VMA_VOLATILE);
+}
+
/*
* Indicate if the VMA is a heap for the given task; for
* /proc/PID/maps that is the heap of the main task.
diff --git a/include/linux/mvolatile.h b/include/linux/mvolatile.h
new file mode 100644
index 000000000000..bc8828c8e6c4
--- /dev/null
+++ b/include/linux/mvolatile.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_MVOLATILE_H
+#define _LINUX_MVOLATILE_H
+
+#include <linux/mm_types.h>
+
+#ifdef CONFIG_64BIT
+int madvise_volatile(struct mm_struct *mm, unsigned long start,
+ unsigned long end, int behavior);
+#else
+static inline int madvise_volatile(struct mm_struct *mm, unsigned long start,
+ unsigned long end, int behavior)
+{
+ return -EINVAL;
+}
+#endif
+
+#endif /* _LINUX_MVOLATILE_H */
diff --git a/include/trace/events/mmflags.h b/include/trace/events/mmflags.h
index 935893e5ea53..cda8b8ec7b53 100644
--- a/include/trace/events/mmflags.h
+++ b/include/trace/events/mmflags.h
@@ -198,6 +198,12 @@ IF_HAVE_PG_ARCH_3(arch_3)
# define IF_HAVE_VM_DROPPABLE(flag, name)
#endif
+#ifdef CONFIG_64BIT
+# define IF_HAVE_VM_VOLATILE(flag, name) {flag, name},
+#else
+# define IF_HAVE_VM_VOLATILE(flag, name)
+#endif
+
#define __def_vmaflag_names \
{VM_READ, "read" }, \
{VM_WRITE, "write" }, \
@@ -233,6 +239,7 @@ IF_HAVE_VM_SOFTDIRTY(VM_SOFTDIRTY, "softdirty" ) \
{VM_HUGEPAGE, "hugepage" }, \
{VM_NOHUGEPAGE, "nohugepage" }, \
IF_HAVE_VM_DROPPABLE(VM_DROPPABLE, "droppable" ) \
+IF_HAVE_VM_VOLATILE(VM_VOLATILE, "volatile") \
{VM_MERGEABLE, "mergeable" } \
#define show_vma_flags(flags) \
diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
index ef1c27fa3c57..cfaa94aea62a 100644
--- a/include/uapi/asm-generic/mman-common.h
+++ b/include/uapi/asm-generic/mman-common.h
@@ -79,6 +79,9 @@
#define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */
+#define MADV_VOLATILE 26 /* Mark pages as volatile */
+#define MADV_NONVOLATILE 27 /* Mark pages non-volatile, report if purged */
+
#define MADV_GUARD_INSTALL 102 /* fatal signal on access to range */
#define MADV_GUARD_REMOVE 103 /* unguard range */
diff --git a/mm/Makefile b/mm/Makefile
index e7245cb88c66..b4811b6e7a15 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -45,7 +45,7 @@ mmu-$(CONFIG_MMU) += process_vm_access.o
endif
ifdef CONFIG_64BIT
-mmu-$(CONFIG_MMU) += mseal.o
+mmu-$(CONFIG_MMU) += mseal.o mvolatile.o
endif
obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
diff --git a/mm/madvise.c b/mm/madvise.c
index 96f2387b2f46..d9d9d5a7221f 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -33,6 +33,7 @@
#include <linux/shmem_fs.h>
#include <linux/mmu_notifier.h>
#include <linux/swap_ops.h>
+#include <linux/mvolatile.h>
#include <asm/tlb.h>
@@ -1548,6 +1549,8 @@ madvise_behavior_valid(int behavior)
case MADV_KEEPONFORK:
case MADV_GUARD_INSTALL:
case MADV_GUARD_REMOVE:
+ case MADV_VOLATILE:
+ case MADV_NONVOLATILE:
#ifdef CONFIG_MEMORY_FAILURE
case MADV_SOFT_OFFLINE:
case MADV_HWPOISON:
@@ -1967,9 +1970,15 @@ static int madvise_do_behavior(unsigned long start, size_t len_in,
* triggering read faults if required
* MADV_POPULATE_WRITE - populate (prefault) page tables writable by
* triggering write faults if required
+ * MADV_VOLATILE - mark pages in the given range as volatile, allowing
+ * the kernel to purge them under memory pressure.
+ * MADV_NONVOLATILE - mark pages in the given range as non-volatile and
+ * report if any pages in the range were purged.
*
* return values:
* zero - success
+ * 1 - success, and at least one page in the range was purged
+ * (MADV_NONVOLATILE only).
* -EINVAL - start + len < 0, start is not page-aligned,
* "behavior" is not a valid value, or application
* is attempting to release locked or shared pages,
@@ -1999,6 +2008,18 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh
if (error || !len_in)
return error;
+ /*
+ * The volatile behaviors validate the whole range before changing any
+ * VMA, and MADV_NONVOLATILE can return 1, so they do not use the
+ * per-VMA walk below.
+ */
+ if (behavior == MADV_VOLATILE || behavior == MADV_NONVOLATILE) {
+ if (!PAGE_ALIGNED(len_in))
+ return -EINVAL;
+ start = get_untagged_addr(mm, start);
+ return madvise_volatile(mm, start, start + len_in, behavior);
+ }
+
error = madvise_lock(&madv_behavior);
if (error)
return error;
@@ -2028,6 +2049,14 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
.tlb = &tlb,
};
+ /*
+ * process_madvise() returns a byte count, which cannot carry the purge
+ * report of MADV_NONVOLATILE. Reject MADV_VOLATILE as well, it is of no
+ * use without its counterpart.
+ */
+ if (behavior == MADV_VOLATILE || behavior == MADV_NONVOLATILE)
+ return -EINVAL;
+
total_len = iov_iter_count(iter);
ret = madvise_lock(&madv_behavior);
diff --git a/mm/mseal.c b/mm/mseal.c
index 7a8ac66dc215..058b2194fb1b 100644
--- a/mm/mseal.c
+++ b/mm/mseal.c
@@ -32,6 +32,24 @@ static bool range_contains_unmapped(unsigned long start, unsigned long end)
return prev_end < end;
}
+/*
+ * Sealed memory must not be discarded, so refuse to seal volatile memory that
+ * reclaim may purge: madvise(MADV_NONVOLATILE) first, then retry. Check every
+ * VMA before sealing any, so that a refused mseal() changes nothing.
+ */
+static bool range_contains_volatile(unsigned long start, unsigned long end)
+{
+ VMA_ITERATOR(vmi, current->mm, start);
+ struct vm_area_struct *vma;
+
+ for_each_vma_range(vmi, vma, end) {
+ if (vma_is_volatile(vma))
+ return true;
+ }
+
+ return false;
+}
+
static int __mseal_range(unsigned long start, unsigned long end)
{
VMA_ITERATOR(vmi, current->mm, start);
@@ -75,6 +93,8 @@ static int mseal_range(unsigned long start, unsigned long end)
return err;
if (range_contains_unmapped(start, end))
err = -ENOMEM;
+ else if (range_contains_volatile(start, end))
+ err = -EBUSY;
else
err = __mseal_range(start, end);
mmap_write_unlock(current->mm);
diff --git a/mm/mvolatile.c b/mm/mvolatile.c
new file mode 100644
index 000000000000..fbe1028aaf5c
--- /dev/null
+++ b/mm/mvolatile.c
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2014, LG Electronics, Minchan Kim <minchan@kernel.org>
+ * Copyright (C) 2014 Linaro Ltd., John Stultz <john.stultz@linaro.org>
+ */
+#include <linux/syscalls.h>
+#include <linux/mvolatile.h>
+#include <linux/mm_inline.h>
+#include <linux/pagemap.h>
+#include <linux/rmap.h>
+#include <linux/hugetlb.h>
+#include <linux/mmu_notifier.h>
+#include <linux/pgtable.h>
+#include <linux/mman.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
+#include <linux/pagewalk.h>
+#include "internal.h"
+#include "vma.h"
+
+static int check_volatile_range(struct mm_struct *mm, unsigned long start,
+ unsigned long end, int behavior)
+{
+ VMA_ITERATOR(vmi, mm, start);
+ unsigned long prev_end = start;
+ struct vm_area_struct *vma;
+ bool make_volatile = behavior == MADV_VOLATILE;
+ int splits = 0;
+
+ for_each_vma_range(vmi, vma, end) {
+ if (vma->vm_start > prev_end)
+ return -ENOMEM;
+
+ if (vma_test_any_mask(vma, VMA_SPECIAL_FLAGS) ||
+ vma_test_any(vma, VMA_MIXEDMAP_BIT, VMA_HUGETLB_BIT))
+ return -EINVAL;
+
+ if (vma->vm_file)
+ return -EINVAL;
+
+ /*
+ * Mlocked memory is not purged and sealed memory must not be
+ * discarded, so neither may become volatile. MADV_NONVOLATILE
+ * stays allowed.
+ */
+ if (make_volatile && vma_test(vma, VMA_LOCKED_BIT))
+ return -EINVAL;
+ if (make_volatile && vma_is_sealed(vma))
+ return -EPERM;
+
+ /* Only the first and last VMA can need a split. */
+ if (vma_is_volatile(vma) != make_volatile)
+ splits += (vma->vm_start < start) + (vma->vm_end > end);
+
+ prev_end = vma->vm_end;
+ }
+
+ if (prev_end < end)
+ return -ENOMEM;
+
+ /*
+ * Fail before changing any VMA if the splits would exceed
+ * max_map_count, rather than in split_vma() after earlier VMAs have
+ * been changed.
+ */
+ if (splits && mm->map_count + splits > get_sysctl_max_map_count())
+ return -ENOMEM;
+
+ return 0;
+}
+
+/**
+ * madvise_volatile() - Mark or unmark the VMAs in [start, end) volatile
+ * @mm: target mm_struct
+ * @start: start address, page aligned
+ * @end: end address (exclusive), page aligned
+ * @behavior: %MADV_VOLATILE or %MADV_NONVOLATILE
+ *
+ * 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.
+ *
+ * Return: 0 on success 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;
+ int ret = 0;
+
+ if (start >= TASK_SIZE || end > TASK_SIZE)
+ return -EINVAL;
+
+ if (mmap_write_lock_killable(mm))
+ return -EINTR;
+
+ ret = check_volatile_range(mm, start, end, behavior);
+ if (ret)
+ goto out;
+
+ vma = vma_iter_load(&vmi);
+ prev = vma_prev(&vmi);
+ if (start > vma->vm_start)
+ prev = vma;
+
+ for_each_vma_range(vmi, vma, end) {
+ const unsigned long curr_start = max(vma->vm_start, start);
+ const unsigned long curr_end = min(vma->vm_end, end);
+ vma_flags_t vma_flags = vma->flags;
+
+ if (behavior == MADV_VOLATILE) {
+ if (!vma_is_volatile(vma)) {
+ vma_flags_set(&vma_flags, VMA_VOLATILE_BIT);
+ vma = vma_modify_flags(&vmi, prev, vma, curr_start,
+ curr_end, &vma_flags);
+ if (IS_ERR(vma)) {
+ ret = PTR_ERR(vma);
+ goto out;
+ }
+ vma_start_write(vma);
+ vma_set_flags(vma, VMA_VOLATILE_BIT);
+ }
+ } else { /* MADV_NONVOLATILE */
+ if (vma_is_volatile(vma)) {
+ vma_flags_clear(&vma_flags, VMA_VOLATILE_BIT);
+ vma = vma_modify_flags(&vmi, prev, vma, curr_start,
+ curr_end, &vma_flags);
+ if (IS_ERR(vma)) {
+ ret = PTR_ERR(vma);
+ goto out;
+ }
+ vma_start_write(vma);
+ vma_clear_flags(vma, VMA_VOLATILE_BIT);
+ }
+ }
+ prev = vma;
+ }
+
+out:
+ mmap_write_unlock(mm);
+ return ret;
+}
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH v1 2/4] mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages
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 ` Hirotaka Yamagata
2026-11-30 8:34 ` [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Hirotaka Yamagata
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Hirotaka Yamagata @ 2026-11-30 8:34 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-api, 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-kernel, linux-mm, linux-fsdevel,
linux-trace-kernel, linux-arch, cgroups, John Stultz,
Minchan Kim, Alexandre Marciano Gimenez, Tatsuyuki Ishi
When a volatile anonymous page is discarded under memory pressure, the
page table must record that the page was purged so that:
1. Accessing the purged address while still volatile raises SIGBUS.
2. A subsequent MADV_NONVOLATILE call can detect that data in the range
was discarded, clear the marker, and notify userspace.
Introduce PTE_MARKER_PURGED (BIT(3)) using the PTE marker infrastructure
and wire up its lifecycle across core mm paths:
- Deliver VM_FAULT_SIGBUS in handle_pte_marker() when faulting on a
PTE_MARKER_PURGED entry.
- Preserve PTE_MARKER_PURGED across fork() in copy_pte_marker() and
ignore it during mprotect() in change_softleaf_pte().
- Drop PTE_MARKER_PURGED unconditionally in zap_nonpresent_ptes() on
munmap() or MADV_DONTNEED.
- Add TTU_PURGE to enum ttu_flags so try_to_unmap_one() installs a
PTE_MARKER_PURGED entry when unmapping a purged volatile page; like
swap-out, it backs off when an anon-exclusive page may be pinned.
- Make khugepaged and MADV_COLLAPSE skip PTE markers (other than
uffd-wp) at scan time, so that collapse does not try to swap in purged
markers.
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/leafops.h | 14 ++++++++++++++
include/linux/mm_inline.h | 5 +++--
include/linux/rmap.h | 1 +
include/linux/swapops.h | 18 +++++++++++++++++-
mm/khugepaged.c | 17 +++++++++++++++++
mm/memory.c | 5 +++++
mm/mprotect.c | 6 ++++--
mm/rmap.c | 14 ++++++++++++++
8 files changed, 75 insertions(+), 5 deletions(-)
diff --git a/include/linux/leafops.h b/include/linux/leafops.h
index 4c1476ae3234..1408c1086c71 100644
--- a/include/linux/leafops.h
+++ b/include/linux/leafops.h
@@ -464,6 +464,20 @@ static inline bool softleaf_is_guard_marker(softleaf_t entry)
return softleaf_to_marker(entry) & PTE_MARKER_GUARD;
}
+/**
+ * softleaf_is_purged_marker() - Is this leaf entry a purged marker?
+ * @entry: Leaf entry.
+ *
+ * Returns: true if the leaf entry is a purged marker, otherwise false.
+ */
+static inline bool softleaf_is_purged_marker(softleaf_t entry)
+{
+ if (!softleaf_is_marker(entry))
+ return false;
+
+ return softleaf_to_marker(entry) & PTE_MARKER_PURGED;
+}
+
/**
* softleaf_is_uffd_wp_marker() - Is this leaf entry a userfautlfd write protect
* marker?
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 621c8653d8f7..f9ec2defc7b2 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -556,8 +556,9 @@ static inline pte_marker copy_pte_marker(
softleaf_t entry, struct vm_area_struct *dst_vma)
{
const pte_marker srcm = softleaf_to_marker(entry);
- /* Always copy error entries. */
- pte_marker dstm = srcm & (PTE_MARKER_POISONED | PTE_MARKER_GUARD);
+ /* Always copy error and purged entries. */
+ pte_marker dstm = srcm & (PTE_MARKER_POISONED | PTE_MARKER_GUARD |
+ PTE_MARKER_PURGED);
/* Only copy PTE markers if UFFD register matches. */
if ((srcm & PTE_MARKER_UFFD_WP) && userfaultfd_wp(dst_vma))
diff --git a/include/linux/rmap.h b/include/linux/rmap.h
index 0b332770abee..88b69a2dce96 100644
--- a/include/linux/rmap.h
+++ b/include/linux/rmap.h
@@ -102,6 +102,7 @@ enum ttu_flags {
* do a final flush if necessary */
TTU_RMAP_LOCKED = 0x80, /* do not grab rmap lock:
* caller holds it */
+ TTU_PURGE = 0x100, /* convert pte to purged marker entry */
};
#ifdef CONFIG_MMU
diff --git a/include/linux/swapops.h b/include/linux/swapops.h
index e7d0d529f3e0..1aa2baaed49e 100644
--- a/include/linux/swapops.h
+++ b/include/linux/swapops.h
@@ -298,7 +298,13 @@ typedef unsigned long pte_marker;
* PROT_NONE, rather than if they were a memory hole or equivalent.
*/
#define PTE_MARKER_GUARD BIT(2)
-#define PTE_MARKER_MASK (BIT(3) - 1)
+/*
+ * Indicates that a volatile anonymous page mapped by this PTE was purged
+ * under memory pressure. MADV_NONVOLATILE clears it and reports the purge;
+ * see handle_pte_marker() for faults before MADV_NONVOLATILE.
+ */
+#define PTE_MARKER_PURGED BIT(3)
+#define PTE_MARKER_MASK (BIT(4) - 1)
static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
{
@@ -320,6 +326,16 @@ static inline swp_entry_t make_guard_swp_entry(void)
return make_pte_marker_entry(PTE_MARKER_GUARD);
}
+static inline swp_entry_t make_purged_swp_entry(void)
+{
+ return make_pte_marker_entry(PTE_MARKER_PURGED);
+}
+
+static inline pte_t make_purged_entry(void)
+{
+ return make_pte_marker(PTE_MARKER_PURGED);
+}
+
struct page_vma_mapped_walk;
#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 11ff98d55c76..24cb60230746 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1200,6 +1200,14 @@ static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm,
pte_present(vmf.orig_pte))
continue;
+ /* Installed since the scan, see collapse_scan_pmd(). */
+ if (pte_is_marker(vmf.orig_pte)) {
+ pte_unmap(pte);
+ mmap_read_unlock(mm);
+ result = SCAN_PTE_NON_PRESENT;
+ goto out;
+ }
+
/*
* TODO: Support swapin without leading to further mTHP
* collapses. Currently bringing in new pages via swapin may
@@ -1667,6 +1675,15 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
continue;
}
if (!pte_present(pteval)) {
+ /*
+ * Markers such as purged or guard markers do not map a
+ * page that could be swapped in; faulting on them only
+ * raises a signal. uffd-wp markers are handled below.
+ */
+ if (pte_is_marker(pteval) && !pte_is_uffd_wp_marker(pteval)) {
+ result = SCAN_PTE_NON_PRESENT;
+ goto out_unmap;
+ }
if (++unmapped > max_ptes_swap) {
result = SCAN_EXCEED_SWAP_PTE;
count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SWAP_PTE,
diff --git a/mm/memory.c b/mm/memory.c
index 8b0c2c735d3d..eecb84bf76a6 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1919,6 +1919,8 @@ static inline int zap_nonpresent_ptes(struct mmu_gather *tlb,
*/
if (!zap_drop_markers(details))
return 1;
+ } else if (softleaf_is_purged_marker(entry)) {
+ /* Unlike guard markers, purged markers are always dropped. */
} else if (softleaf_is_hwpoison(entry) ||
softleaf_is_poison_marker(entry)) {
if (!should_zap_cows(details))
@@ -4736,6 +4738,9 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf)
if (marker & PTE_MARKER_GUARD)
return VM_FAULT_SIGSEGV;
+ if (marker & PTE_MARKER_PURGED)
+ return VM_FAULT_SIGBUS;
+
if (softleaf_is_uffd_wp_marker(entry))
return pte_marker_handle_uffd_wp(vmf);
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 2888ee638d87..7e725c9ce215 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -247,10 +247,12 @@ static long change_softleaf_pte(struct vm_area_struct *vma,
/*
* Ignore error swap entries unconditionally,
* because any access should sigbus/sigsegv
- * anyway.
+ * anyway. Purged markers carry no protection
+ * information.
*/
if (softleaf_is_poison_marker(entry) ||
- softleaf_is_guard_marker(entry))
+ softleaf_is_guard_marker(entry) ||
+ softleaf_is_purged_marker(entry))
return 0;
/*
* If this is uffd-wp pte marker and we'd like
diff --git a/mm/rmap.c b/mm/rmap.c
index d1819fd69938..a3273a9a7d22 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2359,6 +2359,20 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
pteval = swp_entry_to_pte(make_hwpoison_entry(page));
dec_mm_counter(mm, mm_counter(folio));
set_pte_at(mm, address, pvmw.pte, pteval);
+ } else if (flags & TTU_PURGE) {
+ /*
+ * Like swap-out, back off if the page may be pinned.
+ * See folio_try_share_anon_rmap(): clear PTE first.
+ */
+ if (PageAnonExclusive(page) &&
+ folio_try_share_anon_rmap_pte(folio, page)) {
+ set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
+ goto walk_abort;
+ }
+ pteval = make_purged_entry();
+ dec_mm_counter(mm, mm_counter(folio));
+ set_pte_at(mm, address, pvmw.pte, pteval);
+
} else if (likely(pte_present(pteval)) && pte_unused(pteval) &&
!userfaultfd_armed(vma)) {
/*
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim
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
2026-11-30 8:34 ` [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint Hirotaka Yamagata
2026-11-30 10:39 ` [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 David Hildenbrand (Arm)
4 siblings, 0 replies; 7+ messages in thread
From: Hirotaka Yamagata @ 2026-11-30 8:34 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-api, 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-kernel, linux-mm, linux-fsdevel,
linux-trace-kernel, linux-arch, cgroups, John Stultz,
Minchan Kim, Alexandre Marciano Gimenez, Tatsuyuki Ishi
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint
2026-11-30 8:34 [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 Hirotaka Yamagata
` (2 preceding siblings ...)
2026-11-30 8:34 ` [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Hirotaka Yamagata
@ 2026-11-30 8:34 ` Hirotaka Yamagata
2026-11-30 10:39 ` [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 David Hildenbrand (Arm)
4 siblings, 0 replies; 7+ messages in thread
From: Hirotaka Yamagata @ 2026-11-30 8:34 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-api, 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-kernel, linux-mm, linux-fsdevel,
linux-trace-kernel, linux-arch, cgroups, John Stultz,
Minchan Kim, Alexandre Marciano Gimenez, Tatsuyuki Ishi
Add accounting and a tracepoint for volatile folio purging:
- Add the PGPURGE ("pgpurge") vmstat and memcg event item counting the
pages discarded by purge_volatile_folio(). Pages are counted once
their mappings hold PTE_MARKER_PURGED markers and the swap cache is
gone, and are charged to the folio's memcg as well, so memory.stat
shows purges per cgroup.
- Add the mm_purge_volatile_folio tracepoint in
include/trace/events/vmscan.h recording the folio PFN, page count, and
whether the folio was purged or, being a large folio, left to swap.
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/vm_event_item.h | 1 +
include/trace/events/vmscan.h | 24 ++++++++++++++++++++++++
mm/memcontrol.c | 1 +
mm/mvolatile.c | 20 ++++++++++++++++++--
mm/vmstat.c | 1 +
5 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
index 2628ccda076a..9832711eafa7 100644
--- a/include/linux/vm_event_item.h
+++ b/include/linux/vm_event_item.h
@@ -38,6 +38,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
PGFREE, PGACTIVATE, PGDEACTIVATE, PGLAZYFREE,
PGFAULT, PGMAJFAULT,
PGLAZYFREED,
+ PGPURGE,
PGREUSE,
PGSCAN_DIRECT_THROTTLE,
#ifdef CONFIG_NUMA
diff --git a/include/trace/events/vmscan.h b/include/trace/events/vmscan.h
index b4bf7b8def1f..15e22b119914 100644
--- a/include/trace/events/vmscan.h
+++ b/include/trace/events/vmscan.h
@@ -429,6 +429,30 @@ TRACE_EVENT(mm_vmscan_write_folio,
show_reclaim_flags(__entry->reclaim_flags))
);
+TRACE_EVENT(mm_purge_volatile_folio,
+
+ TP_PROTO(struct folio *folio, bool purged),
+
+ TP_ARGS(folio, purged),
+
+ TP_STRUCT__entry(
+ __field(unsigned long, pfn)
+ __field(unsigned long, nr_pages)
+ __field(bool, purged)
+ ),
+
+ TP_fast_assign(
+ __entry->pfn = folio_pfn(folio);
+ __entry->nr_pages = folio_nr_pages(folio);
+ __entry->purged = purged;
+ ),
+
+ TP_printk("pfn=0x%lx nr_pages=%lu purged=%d",
+ __entry->pfn,
+ __entry->nr_pages,
+ __entry->purged)
+);
+
TRACE_EVENT(mm_vmscan_reclaim_pages,
TP_PROTO(int nid,
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1271d390b617..d075920cdd2b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -608,6 +608,7 @@ static const unsigned int memcg_vm_event_stat[] = {
PGDEACTIVATE,
PGLAZYFREE,
PGLAZYFREED,
+ PGPURGE,
#ifdef CONFIG_SWAP
SWPIN_ZERO,
SWPOUT_ZERO,
diff --git a/mm/mvolatile.c b/mm/mvolatile.c
index c111bc4f111d..aa532d72d81c 100644
--- a/mm/mvolatile.c
+++ b/mm/mvolatile.c
@@ -15,6 +15,9 @@
#include <linux/swap.h>
#include <linux/swapops.h>
#include <linux/pagewalk.h>
+#include <linux/memcontrol.h>
+#include <linux/vmstat.h>
+#include <trace/events/vmscan.h>
#include "internal.h"
#include "vma.h"
@@ -261,9 +264,13 @@ enum purge_result __purge_volatile_folio(struct folio *folio)
if (!mapped)
return PURGE_NONE;
- /* Only order-0 folios are supported for now. */
+ /*
+ * Only order-0 folios are supported for now. Checked after the
+ * volatile test so that the tracepoint shows large volatile folios
+ * falling back to swap.
+ */
if (folio_test_large(folio))
- return PURGE_NONE;
+ goto fail;
try_to_unmap(folio, TTU_PURGE | TTU_SYNC);
@@ -280,6 +287,11 @@ enum purge_result __purge_volatile_folio(struct folio *folio)
if (folio_test_swapcache(folio) && !folio_free_swap(folio))
return PURGE_NONE;
+ /* Count before the freeze, so that a retry is not counted again. */
+ count_vm_events(PGPURGE, folio_nr_pages(folio));
+ count_memcg_folio_events(folio, PGPURGE, folio_nr_pages(folio));
+ trace_mm_purge_volatile_folio(folio, true);
+
freeze:
/*
* Like __remove_mapping(), back off on a transient reference. No new
@@ -292,4 +304,8 @@ enum purge_result __purge_volatile_folio(struct folio *folio)
folio_unlock(folio);
return PURGE_DONE;
+
+fail:
+ trace_mm_purge_volatile_folio(folio, false);
+ return PURGE_NONE;
}
diff --git a/mm/vmstat.c b/mm/vmstat.c
index cb57714539fb..c2d4be97e0b0 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1347,6 +1347,7 @@ const char * const vmstat_text[] = {
[I(PGFAULT)] = "pgfault",
[I(PGMAJFAULT)] = "pgmajfault",
[I(PGLAZYFREED)] = "pglazyfreed",
+ [I(PGPURGE)] = "pgpurge",
[I(PGREUSE)] = "pgreuse",
[I(PGSCAN_DIRECT_THROTTLE)] = "pgscan_direct_throttle",
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1
2026-11-30 8:34 [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 Hirotaka Yamagata
` (3 preceding siblings ...)
2026-11-30 8:34 ` [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint Hirotaka Yamagata
@ 2026-11-30 10:39 ` David Hildenbrand (Arm)
2026-11-30 18:18 ` Lorenzo Stoakes (ARM)
4 siblings, 1 reply; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-30 10:39 UTC (permalink / raw)
To: Hirotaka Yamagata, Andrew Morton
Cc: linux-api, Liam R. Howlett, Lorenzo Stoakes, Chris Li,
Kairui Song, Steven Rostedt, Masami Hiramatsu, Arnd Bergmann,
Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
linux-kernel, linux-mm, linux-fsdevel, linux-trace-kernel,
linux-arch, cgroups, John Stultz, Minchan Kim,
Alexandre Marciano Gimenez, Tatsuyuki Ishi
On 11/30/26 09:34, Hirotaka Yamagata wrote:
Hi,
> This RFC series revives and modernizes the volatile ranges patchset
> originally proposed by John Stultz and Minchan Kim [1].
>
> Volatile ranges allow userspace caches holding regenerable data (such as
> decoded image bitmaps or other private caches) to mark private anonymous
> memory as safe for the kernel to discard under memory pressure,
> bypassing swap. Such ranges are marked via madvise(MADV_VOLATILE).
> Before reusing the buffer, userspace calls madvise(MADV_NONVOLATILE),
> which unmarks the range and returns 1 if any pages in the range were
> purged while volatile (or 0 if all pages remained intact).
We have MAP_DROPPABLE nowadays, I don't think we want more of that.
>
> Unlike MADV_FREE, which silently zero-fills dropped pages on re-access,
> calling MADV_NONVOLATILE before reuse gives userspace deterministic
> notification of whether cached data was lost without having to touch
> every page.
We do have userfaultfd missing faults + rwp (read-write-protection). Can't we
build something based on that instead?
We currently disable uffd with MAP_DROPPABLE, but maybe there could be a use
case for it. Of course, you can also build it based on MADV_FREE instead.
--
Cheers,
David
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1
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)
0 siblings, 0 replies; 7+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-30 18:18 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Hirotaka Yamagata, Andrew Morton, linux-api, Liam R. Howlett,
Chris Li, Kairui Song, Steven Rostedt, Masami Hiramatsu,
Arnd Bergmann, Johannes Weiner, Michal Hocko, Roman Gushchin,
Shakeel Butt, linux-kernel, linux-mm, linux-fsdevel,
linux-trace-kernel, linux-arch, cgroups, John Stultz,
Minchan Kim, Alexandre Marciano Gimenez, Tatsuyuki Ishi
On Mon, Nov 30, 2026 at 11:39:45AM +0100, David Hildenbrand (Arm) wrote:
> On 11/30/26 09:34, Hirotaka Yamagata wrote:
>
> Hi,
>
> > This RFC series revives and modernizes the volatile ranges patchset
> > originally proposed by John Stultz and Minchan Kim [1].
> >
> > Volatile ranges allow userspace caches holding regenerable data (such as
> > decoded image bitmaps or other private caches) to mark private anonymous
> > memory as safe for the kernel to discard under memory pressure,
> > bypassing swap. Such ranges are marked via madvise(MADV_VOLATILE).
> > Before reusing the buffer, userspace calls madvise(MADV_NONVOLATILE),
> > which unmarks the range and returns 1 if any pages in the range were
> > purged while volatile (or 0 if all pages remained intact).
>
> We have MAP_DROPPABLE nowadays, I don't think we want more of that.
Yes exactly.
And also absolutely no to this pattern of:
1. Create an unnecessary new file for something
2. Redirect an MADV_xxx to that file, maybe some hooks
3. ???
4. the control of what the MADV_xxx does sits outside of madvise now.
>
> >
> > Unlike MADV_FREE, which silently zero-fills dropped pages on re-access,
> > calling MADV_NONVOLATILE before reuse gives userspace deterministic
> > notification of whether cached data was lost without having to touch
> > every page.
>
> We do have userfaultfd missing faults + rwp (read-write-protection). Can't we
> build something based on that instead?
>
> We currently disable uffd with MAP_DROPPABLE, but maybe there could be a use
> case for it. Of course, you can also build it based on MADV_FREE instead.
Well we'd need to have a compelling reason to combine the two I think :P
But in general agree with - check to see what tooling there is already before
making a big, invasive change to mm.
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-30 18:18 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Hirotaka Yamagata
2026-11-30 8:34 ` [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint Hirotaka Yamagata
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)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®